batterymonitor.qml 11.1 KB
Newer Older
1
2
3
/*
 *   Copyright 2011 Sebastian Kügler <sebas@kde.org>
 *   Copyright 2011 Viranch Mehta <viranch.mehta@gmail.com>
4
 *   Copyright 2013-2015 Kai Uwe Broulik <kde@privat.broulik.de>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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
26
import org.kde.kcoreaddons 1.0 as KCoreAddons
Lukáš Tinkl's avatar
Lukáš Tinkl committed
27
import org.kde.kquickcontrolsaddons 2.0
28
import "logic.js" as Logic
29
30
31
32
33
34
35
36
37

Item {
    id: batterymonitor
    Plasmoid.switchWidth: units.gridUnit * 10
    Plasmoid.switchHeight: units.gridUnit * 10

    LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
    LayoutMirroring.childrenInherit: true

38
39
40
41
42
    Plasmoid.status: {
        if (powermanagementDisabled) {
            return PlasmaCore.Types.ActiveStatus
        }

43
44
45
        if (pmSource.data.Battery["Has Cumulative"]
            && pmSource.data["Battery"]["State"] !== "FullyCharged") {
            return PlasmaCore.Types.ActiveStatus
46
47
48
49
50
51
        }

        return PlasmaCore.Types.PassiveStatus
    }

    Plasmoid.toolTipMainText: {
52
53
        if (batteries.count === 0 || !pmSource.data["Battery"]["Has Cumulative"]) {
            return plasmoid.title
54
55
        } else if (pmSource.data["Battery"]["State"] === "FullyCharged") {
            return i18n("Fully Charged");
56
        }
57
58

        const percent = pmSource.data.Battery.Percent;
59
        if (pmSource.data["AC Adapter"] && pmSource.data["AC Adapter"]["Plugged in"]) {
60
61
62
            const state = pmSource.data.Battery.State;
            if (state === "NoCharge") {
                return i18n("Battery at %1%, not Charging", percent);
63
64
            } else if (state === "Discharging") {
                return i18n("Battery at %1%, plugged in but still discharging", percent);
65
66
            } else if (state === "Charging") {
                return i18n("Battery at %1%, Charging", percent);
67
            }
68
        }
69
        return i18n("Battery at %1%", percent);
70
71
72
73
    }

    Plasmoid.toolTipSubText: {
        var parts = [];
74
75
76
77
78
79

        // Add special text for the "plugged in but still discharging" case
        if (pmSource.data["AC Adapter"] && pmSource.data["AC Adapter"]["Plugged in"] && pmSource.data.Battery.State === "Discharging") {
            parts.push(i18n("The power supply is not powerful enough to charge the battery"))
        }

80
81
82
83
84
85
86
87
        if (batteries.count === 0) {
            parts.push("No Batteries Available");
        } else if (remainingTime > 0) {
            var remainingTimeString = KCoreAddons.Format.formatDuration(remainingTime, KCoreAddons.FormatTypes.HideSeconds);
            if (pmSource.data["Battery"]["State"] === "FullyCharged") {
                // Don't add anything
            } else if (pmSource.data["AC Adapter"] && pmSource.data["AC Adapter"]["Plugged in"]) {
                parts.push(i18nc("time until fully charged - HH:MM","%1 until fully charged", remainingTimeString));
88
            } else {
89
                parts.push(i18nc("remaining time left of battery usage - HH:MM","%1 remaining", remainingTimeString));
90
            }
91
92
93
94
95
        } else if (pmSource.data.Battery.State === "NoCharge") {
            parts.push(i18n("Not charging"))
        } // otherwise, don't add anything

        if (powermanagementDisabled) {
96
            parts.push(i18n("Automatic sleep and screen locking are disabled"));
97
        }
98
        return parts.join("\n");
99
    }
100

101
    Plasmoid.icon: "battery"
102

103
    property bool disableBrightnessUpdate: true
104
105

    property int screenBrightness
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
106
    readonly property int maximumScreenBrightness: pmSource.data["PowerDevil"] ? pmSource.data["PowerDevil"]["Maximum Screen Brightness"] || 0 : 0
107

108
    property int keyboardBrightness
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
109
    readonly property int maximumKeyboardBrightness: pmSource.data["PowerDevil"] ? pmSource.data["PowerDevil"]["Maximum Keyboard Brightness"] || 0 : 0
110

111
    readonly property int remainingTime: Number(pmSource.data["Battery"]["Remaining msec"])
112

113
114
    property bool powermanagementDisabled: false

115
116
    property var inhibitions: []

117
118
119
120
121
    readonly property var kcms: ["powerdevilprofilesconfig.desktop",
                                 "powerdevilactivitiesconfig.desktop",
                                 "powerdevilglobalconfig.desktop"]
    readonly property bool kcmsAuthorized: KCMShell.authorize(batterymonitor.kcms).length > 0

122
    readonly property bool kcmEnergyInformationAuthorized: KCMShell.authorize("kcm_energyinfo.desktop").length > 0
123

124
    property QtObject updateScreenBrightnessJob
125
126
127
128
129
130
131
    onScreenBrightnessChanged: {
        if (disableBrightnessUpdate) {
            return;
        }
        var service = pmSource.serviceForSource("PowerDevil");
        var operation = service.operationDescription("setBrightness");
        operation.brightness = screenBrightness;
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
132
133
        // show OSD only when the plasmoid isn't expanded since the moving slider is feedback enough
        operation.silent = plasmoid.expanded
134
135
136
137
        updateScreenBrightnessJob = service.startOperationCall(operation);
        updateScreenBrightnessJob.finished.connect(function(job) {
            Logic.updateBrightness(batterymonitor, pmSource);
        });
138
    }
139
140

    property QtObject updateKeyboardBrightnessJob
141
142
143
144
145
146
147
    onKeyboardBrightnessChanged: {
        if (disableBrightnessUpdate) {
            return;
        }
        var service = pmSource.serviceForSource("PowerDevil");
        var operation = service.operationDescription("setKeyboardBrightness");
        operation.brightness = keyboardBrightness;
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
148
        operation.silent = plasmoid.expanded
149
150
151
152
        updateKeyboardBrightnessJob = service.startOperationCall(operation);
        updateKeyboardBrightnessJob.finished.connect(function(job) {
            Logic.updateBrightness(batterymonitor, pmSource);
        });
153
154
    }

155
    function action_powerdevilkcm() {
156
        KCMShell.open(batterymonitor.kcms);
157
158
    }

159
    function action_energyinformationkcm() {
160
        KCMShell.openInfoCenter("kcm_energyinfo");
161
162
    }

163
    Component.onCompleted: {
164
        Logic.updateBrightness(batterymonitor, pmSource);
165
        Logic.updateInhibitions(batterymonitor, pmSource)
166

167
168
169
        if (batterymonitor.kcmEnergyInformationAuthorized) {
            plasmoid.setAction("energyinformationkcm", i18n("&Show Energy Information..."), "battery");
        }
170
171
172
        if (batterymonitor.kcmsAuthorized) {
            plasmoid.setAction("powerdevilkcm", i18n("&Configure Power Saving..."), "preferences-system-power-management");
        }
173
174
    }

175
    Plasmoid.compactRepresentation: CompactRepresentation {
176
177
        property int wheelDelta: 0

178
179
        onEntered: wheelDelta = 0
        onExited: wheelDelta = 0
180
181
        onWheel: {
            var delta = wheel.angleDelta.y || wheel.angleDelta.x
182
183
184
185
186
187
188
189

            var maximumBrightness = batterymonitor.maximumScreenBrightness
            // Don't allow the UI to turn off the screen
            // Please see https://git.reviewboard.kde.org/r/122505/ for more information
            var minimumBrightness = (maximumBrightness > 100 ? 1 : 0)
            var steps = Math.max(1, Math.round(maximumBrightness / 20))
            var deltaSteps = delta / 120;
            batterymonitor.screenBrightness = Math.max(minimumBrightness, Math.min(maximumBrightness, batterymonitor.screenBrightness + deltaSteps * steps));
190
191
        }
    }
192
193
194
195
196
197

    property QtObject pmSource: PlasmaCore.DataSource {
        id: pmSource
        engine: "powermanagement"
        connectedSources: sources
        onSourceAdded: {
198
199
            disconnectSource(source);
            connectSource(source);
200
201
        }
        onSourceRemoved: {
202
            disconnectSource(source);
203
        }
204
205
        onDataChanged: {
            Logic.updateBrightness(batterymonitor, pmSource)
206
            Logic.updateInhibitions(batterymonitor, pmSource)
207
        }
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    }

    property QtObject batteries: PlasmaCore.SortFilterModel {
        id: batteries
        filterRole: "Is Power Supply"
        sortOrder: Qt.DescendingOrder
        sourceModel: PlasmaCore.SortFilterModel {
            sortRole: "Pretty Name"
            sortOrder: Qt.AscendingOrder
            sortCaseSensitivity: Qt.CaseInsensitive
            sourceModel: PlasmaCore.DataModel {
                dataSource: pmSource
                sourceFilter: "Battery[0-9]+"
            }
        }
    }

    Plasmoid.fullRepresentation: PopupDialog {
        id: dialogItem
        Layout.minimumWidth: units.iconSizes.medium * 9
228
        Layout.minimumHeight: units.gridUnit * 15
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
229
        // TODO Probably needs a sensible preferredHeight too
230

231
        model: plasmoid.expanded ? batteries : null
232
233
234
        anchors.fill: parent
        focus: true

235
236
        isBrightnessAvailable: pmSource.data["PowerDevil"] && pmSource.data["PowerDevil"]["Screen Brightness Available"] ? true : false
        isKeyboardBrightnessAvailable: pmSource.data["PowerDevil"] && pmSource.data["PowerDevil"]["Keyboard Brightness Available"] ? true : false
237

Laurent Montel's avatar
Laurent Montel committed
238
        pluggedIn: pmSource.data["AC Adapter"] !== undefined && pmSource.data["AC Adapter"]["Plugged in"]
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

        property int cookie1: -1
        property int cookie2: -1
        onPowermanagementChanged: {
            var service = pmSource.serviceForSource("PowerDevil");
            if (checked) {
                var op1 = service.operationDescription("stopSuppressingSleep");
                op1.cookie = cookie1;
                var op2 = service.operationDescription("stopSuppressingScreenPowerManagement");
                op2.cookie = cookie2;

                var job1 = service.startOperationCall(op1);
                job1.finished.connect(function(job) {
                    cookie1 = -1;
                });

                var job2 = service.startOperationCall(op2);
                job2.finished.connect(function(job) {
                    cookie2 = -1;
                });
            } else {
                var reason = i18n("The battery applet has enabled system-wide inhibition");
                var op1 = service.operationDescription("beginSuppressingSleep");
                op1.reason = reason;
                var op2 = service.operationDescription("beginSuppressingScreenPowerManagement");
                op2.reason = reason;

                var job1 = service.startOperationCall(op1);
                job1.finished.connect(function(job) {
                    cookie1 = job.result;
                });

                var job2 = service.startOperationCall(op2);
                job2.finished.connect(function(job) {
                    cookie2 = job.result;
                });
            }
276
            batterymonitor.powermanagementDisabled = !checked
277
278
279
        }
    }
}