main.qml 5.14 KB
Newer Older
1
/*
2
3
4
5
6
    SPDX-FileCopyrightText: 2013 Heena Mahour <heena393@gmail.com>
    SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
7
8
9
10
11
import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras
Lukáš Tinkl's avatar
Lukáš Tinkl committed
12
import org.kde.kquickcontrolsaddons 2.0
13
import org.kde.plasma.private.digitalclock 1.0
14
import org.kde.kquickcontrolsaddons 2.0
15
import org.kde.plasma.workspace.calendar 2.0 as PlasmaCalendar
16
17

Item {
18
    id: root
19

Nate Graham's avatar
Nate Graham committed
20
21
    width: PlasmaCore.Units.gridUnit * 10
    height: PlasmaCore.Units.gridUnit * 4
22
    property string dateFormatString: setDateFormatString()
23
    Plasmoid.backgroundHints: PlasmaCore.Types.ShadowBackground | PlasmaCore.Types.ConfigurableBackground
24
25
    property date tzDate: {
        // get the time for the given timezone from the dataengine
26
        var now = dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["DateTime"];
27
28
29
        // get current UTC time
        var msUTC = now.getTime() + (now.getTimezoneOffset() * 60000);
        // add the dataengine TZ offset to it
30
        return new Date(msUTC + (dataSource.data[Plasmoid.configuration.lastSelectedTimezone]["Offset"] * 1000));
31
    }
32

Marco Martin's avatar
Marco Martin committed
33
34
    function initTimezones() {
        var tz  = Array()
35
        if (Plasmoid.configuration.selectedTimeZones.indexOf("Local") === -1) {
Marco Martin's avatar
Marco Martin committed
36
37
            tz.push("Local");
        }
38
        root.allTimezones = tz.concat(Plasmoid.configuration.selectedTimeZones);
Marco Martin's avatar
Marco Martin committed
39
40
    }

41
    function timeForZone(zone) {
42
        var compactRepresentationItem = Plasmoid.compactRepresentationItem;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        if (!compactRepresentationItem) {
            return "";
        }

        // get the time for the given timezone from the dataengine
        var now = dataSource.data[zone]["DateTime"];
        // get current UTC time
        var msUTC = now.getTime() + (now.getTimezoneOffset() * 60000);
        // add the dataengine TZ offset to it
        var dateTime = new Date(msUTC + (dataSource.data[zone]["Offset"] * 1000));

        var formattedTime = Qt.formatTime(dateTime, compactRepresentationItem.timeFormat);

        if (dateTime.getDay() !== dataSource.data["Local"]["DateTime"].getDay()) {
            formattedTime += " (" + Qt.formatDate(dateTime, compactRepresentationItem.dateFormat) + ")";
        }

        return formattedTime;
    }

    function nameForZone(zone) {
        // add the timezone string to the clock
65
        var timezoneString = Plasmoid.configuration.displayTimezoneAsCode ? dataSource.data[zone]["Timezone Abbreviation"]
66
67
68
69
70
                                                                          : TimezonesI18n.i18nCity(dataSource.data[zone]["Timezone City"]);

        return timezoneString;
    }

71
72
73
74
    Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
    Plasmoid.compactRepresentation: DigitalClock { }
    Plasmoid.fullRepresentation: CalendarView { }

75
76
77
    Plasmoid.toolTipItem: Loader {
        id: tooltipLoader

78
79
80
81
        Layout.minimumWidth: item ? item.implicitWidth : 0
        Layout.maximumWidth: item ? item.implicitWidth : 0
        Layout.minimumHeight: item ? item.implicitHeight : 0
        Layout.maximumHeight: item ? item.implicitHeight : 0
82
83
84

        source: "Tooltip.qml"
    }
85

86
87
88
89
    //We need Local to be *always* present, even if not disaplayed as
    //it's used for formatting in ToolTip.dateTimeChanged()
    property var allTimezones
    Connections {
90
        target: Plasmoid.configuration
91
        function onSelectedTimeZonesChanged() { root.initTimezones(); }
92
93
    }

94
95
96
    PlasmaCore.DataSource {
        id: dataSource
        engine: "time"
97
        connectedSources: allTimezones
98
99
        interval: Plasmoid.configuration.showSeconds ? 1000 : 60000
        intervalAlignment: Plasmoid.configuration.showSeconds ? PlasmaCore.Types.NoAlignment : PlasmaCore.Types.AlignToMinute
100
101
102
103
104
105
106
107
108
    }

    function setDateFormatString() {
        // remove "dddd" from the locale format string
        // /all/ locales in LongFormat have "dddd" either
        // at the beginning or at the end. so we just
        // remove it + the delimiter and space
        var format = Qt.locale().dateFormat(Locale.LongFormat);
        format = format.replace(/(^dddd.?\s)|(,?\sdddd$)/, "");
109
        return format;
110
    }
111

112
    function action_clockkcm() {
113
        KCMShell.openSystemSettings("kcm_clock");
114
115
    }

116
    function action_formatskcm() {
117
        KCMShell.openSystemSettings("kcm_formats");
118
119
120
    }

    Component.onCompleted: {
121
122
        Plasmoid.setAction("clipboard", i18n("Copy to Clipboard"), "edit-copy");
        ClipboardMenu.setupMenu(Plasmoid.action("clipboard"));
123

Marco Martin's avatar
Marco Martin committed
124
        root.initTimezones();
125
        if (KCMShell.authorize("kcm_clock.desktop").length > 0) {
126
            Plasmoid.setAction("clockkcm", i18n("Adjust Date and Time…"), "clock");
127
        }
128
        if (KCMShell.authorize("kcm_formats.desktop").length > 0) {
129
            Plasmoid.setAction("formatskcm", i18n("Set Time Format…"), "gnumeric-format-thousand-separator");
130
        }
131
132
133

        // Set the list of enabled plugins from config
        // to the manager
134
        PlasmaCalendar.EventPluginsManager.enabledPlugins = Plasmoid.configuration.enabledCalendarPlugins;
135
    }
136
}