1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2  * (c)LGPL2+
3  *
4  * LXQt - a lightweight, Qt based, desktop toolset
5  *
6  * Authors:
7  *   Christian Surlykke <christian@surlykke.dk>
8  *
9  * This program or library is free software; you can redistribute it
10  * and/or modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18 
19  * You should have received a copy of the GNU Lesser General
20  * Public License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301 USA
23  *
24  * END_COMMON_COPYRIGHT_HEADER */
25 
26 #include <QDebug>
27 
28 #include <LXQt/Power>
29 
30 #include "powermanagementsettings.h"
31 
32 // the pause state is only for the tray icon and is not saved
33 bool PowerManagementSettings::mIdlenessWatcherPaused = false;
34 
35 namespace PowerManagementSettingsConstants
36 {
37     const QString RUN_CHECK_LEVEL { QSL("runCheckLevel") };
38     const QString ENABLE_BATTERY_WATCHER_KEY { QSL("enableBatteryWatcher") };
39     const QString ENABLE_LID_WATCHER_KEY { QSL("enableLidWatcher") };
40     const QString ENABLE_IDLENESS_WATCHER_KEY { QL1S("enableIdlenessWatcher") };
41     const QString ENABLE_IDLENESS_BACKLIGHT_WATCHER_KEY { QL1S("enableIdlenessBacklightWatcher") };
42     const QString LID_CLOSED_ACTION_KEY { QL1S("lidClosedAction") };
43     const QString LID_CLOSED_AC_ACTION_KEY { QL1S("lidClosedAcAction") };
44     const QString LID_CLOSED_EXT_MON_ACTION_KEY { QL1S("lidClosedExtMonAction") };
45     const QString LID_CLOSED_EXT_MON_AC_ACTION_KEY { QL1S("lidClosedExtMonAcAction") };
46     const QString ENABLE_EXT_MON_LIDCLOSED_ACTIONS_KEY { QL1S("enableExtMonLidClosedActions") };
47     const QString POWER_LOW_ACTION_KEY { QL1S("powerLowAction") };
48     const QString POWER_LOW_WARNING_KEY { QL1S("powerLowWarning") };
49     const QString POWER_LOW_LEVEL_KEY { QL1S("powerLowLevel") };
50     const QString SHOW_ICON_KEY { QL1S("showIcon") };
51     const QString USE_THEME_ICONS_KEY { QL1S("useThemeIcons") };
52     const QString IDLENESS_AC_ACTION_KEY { QL1S("idlenessACAction") };
53     const QString IDLENESS_AC_TIME { QL1S("idlenessACTime") };
54     const QString IDLENESS_BATTERY_ACTION_KEY { QL1S("idlenessBatteryAction") };
55     const QString IDLENESS_BATTERY_TIME { QL1S("idlenessBatteryTime") };
56     const QString IDLENESS_BACKLIGHT_TIME { QL1S("idlenessTime") };
57     const QString IDLENESS_BACKLIGHT { QL1S("backlightIdleness") };
58     const QString IDLENESS_BACKLIGHT_ON_BATTERY_DISCHARGING { QL1S("backlightIdlenessOnBatteryDischarging") };
59     const QString DISABLE_IDLENESS_WHEN_FULLSCREEN { QL1S("disableIdlenessWhenFullscreen") };
60     const QString POWER_KEY_ACTION { QL1S("powerKeyAction") };
61     const QString SUSPEND_KEY_ACTION { QL1S("suspendKeyAction") };
62     const QString HIBERNATE_KEY_ACTION { QL1S("hibernateKeyAction") };
63 }
64 
65 using namespace PowerManagementSettingsConstants;
66 
PowerManagementSettings(QObject * parent)67 PowerManagementSettings::PowerManagementSettings(QObject* parent) : LXQt::Settings(QSL("lxqt-powermanagement"), parent)
68 {
69 }
70 
~PowerManagementSettings()71 PowerManagementSettings::~PowerManagementSettings()
72 {
73 }
74 
getRunCheckLevel()75 int PowerManagementSettings::getRunCheckLevel()
76 {
77     return value(RUN_CHECK_LEVEL, 0).toInt();
78 }
79 
setRunCheckLevel(int newLevel)80 void PowerManagementSettings::setRunCheckLevel(int newLevel)
81 {
82     setValue(RUN_CHECK_LEVEL, newLevel);
83 }
84 
isBatteryWatcherEnabled()85 bool PowerManagementSettings::isBatteryWatcherEnabled()
86 {
87     return value(ENABLE_BATTERY_WATCHER_KEY, true).toBool();
88 }
89 
setBatteryWatcherEnabled(bool batteryWatcherEnabled)90 void PowerManagementSettings::setBatteryWatcherEnabled(bool batteryWatcherEnabled)
91 {
92     setValue(ENABLE_BATTERY_WATCHER_KEY, batteryWatcherEnabled);
93 }
94 
getPowerLowAction()95 int PowerManagementSettings::getPowerLowAction()
96 {
97     return value(POWER_LOW_ACTION_KEY, -1).toInt();
98 }
99 
setPowerLowAction(int powerLowAction)100 void PowerManagementSettings::setPowerLowAction(int powerLowAction)
101 {
102     setValue(POWER_LOW_ACTION_KEY, powerLowAction);
103 }
104 
getPowerLowLevel()105 int PowerManagementSettings::getPowerLowLevel()
106 {
107     return value(POWER_LOW_LEVEL_KEY, 5).toInt();
108 }
109 
setPowerLowLevel(int powerLowLevel)110 void PowerManagementSettings::setPowerLowLevel(int powerLowLevel)
111 {
112     setValue(POWER_LOW_LEVEL_KEY, powerLowLevel);
113 }
114 
getPowerLowWarningTime()115 int PowerManagementSettings::getPowerLowWarningTime()
116 {
117     return value(POWER_LOW_WARNING_KEY, 30).toInt();
118 }
119 
setPowerLowWarningTime(int powerLowWarningTime)120 void PowerManagementSettings::setPowerLowWarningTime(int powerLowWarningTime)
121 {
122     setValue(POWER_LOW_WARNING_KEY, powerLowWarningTime);
123 }
124 
isShowIcon()125 bool PowerManagementSettings::isShowIcon()
126 {
127     return value(SHOW_ICON_KEY, true).toBool();
128 }
129 
setShowIcon(bool showIcon)130 void PowerManagementSettings::setShowIcon(bool showIcon)
131 {
132     setValue(SHOW_ICON_KEY, showIcon);
133 }
134 
isUseThemeIcons()135 bool PowerManagementSettings::isUseThemeIcons()
136 {
137     return value(USE_THEME_ICONS_KEY, false).toBool();
138 }
139 
setUseThemeIcons(bool useThemeIcons)140 void PowerManagementSettings::setUseThemeIcons(bool useThemeIcons)
141 {
142     setValue(USE_THEME_ICONS_KEY, useThemeIcons);
143 }
144 
isLidWatcherEnabled()145 bool PowerManagementSettings::isLidWatcherEnabled()
146 {
147     return value(ENABLE_LID_WATCHER_KEY, true).toBool();
148 }
149 
setLidWatcherEnabled(bool lidWatcherEnabled)150 void PowerManagementSettings::setLidWatcherEnabled(bool lidWatcherEnabled)
151 {
152     setValue(ENABLE_LID_WATCHER_KEY, lidWatcherEnabled);
153 }
154 
getLidClosedAcAction()155 int PowerManagementSettings::getLidClosedAcAction()
156 {
157     return value(LID_CLOSED_AC_ACTION_KEY, -1).toInt();
158 }
159 
setLidClosedAcAction(int lidClosedAcAction)160 void PowerManagementSettings::setLidClosedAcAction(int lidClosedAcAction)
161 {
162     setValue(LID_CLOSED_AC_ACTION_KEY, lidClosedAcAction);
163 }
164 
getLidClosedAction()165 int PowerManagementSettings::getLidClosedAction()
166 {
167     return value(LID_CLOSED_ACTION_KEY, -1).toInt();
168 }
169 
setLidClosedAction(int lidClosedAction)170 void PowerManagementSettings::setLidClosedAction(int lidClosedAction)
171 {
172     setValue(LID_CLOSED_ACTION_KEY, lidClosedAction);
173 }
174 
getLidClosedExtMonAcAction()175 int PowerManagementSettings::getLidClosedExtMonAcAction()
176 {
177     return value(LID_CLOSED_EXT_MON_AC_ACTION_KEY, -1).toInt();
178 }
179 
setLidClosedExtMonAcAction(int lidClosedExtMonAcAction)180 void PowerManagementSettings::setLidClosedExtMonAcAction(int lidClosedExtMonAcAction)
181 {
182     setValue(LID_CLOSED_EXT_MON_AC_ACTION_KEY, lidClosedExtMonAcAction);
183 }
184 
getLidClosedExtMonAction()185 int PowerManagementSettings::getLidClosedExtMonAction()
186 {
187     return value(LID_CLOSED_EXT_MON_ACTION_KEY, -1).toInt();
188 }
189 
setLidClosedExtMonAction(int lidClosedExtMonAction)190 void PowerManagementSettings::setLidClosedExtMonAction(int lidClosedExtMonAction)
191 {
192     setValue(LID_CLOSED_EXT_MON_ACTION_KEY, lidClosedExtMonAction);
193 }
194 
isEnableExtMonLidClosedActions()195 bool PowerManagementSettings::isEnableExtMonLidClosedActions()
196 {
197     return value(ENABLE_EXT_MON_LIDCLOSED_ACTIONS_KEY, 0).toBool();
198 }
199 
setEnableExtMonLidClosedActions(bool enableExtMonLidClosedActions)200 void PowerManagementSettings::setEnableExtMonLidClosedActions(bool enableExtMonLidClosedActions)
201 {
202     setValue(ENABLE_EXT_MON_LIDCLOSED_ACTIONS_KEY, enableExtMonLidClosedActions);
203 }
204 
205 
isIdlenessWatcherEnabled()206 bool PowerManagementSettings::isIdlenessWatcherEnabled()
207 {
208     return value(ENABLE_IDLENESS_WATCHER_KEY, false).toBool();
209 }
210 
setIdlenessWatcherEnabled(bool idlenessWatcherEnabled)211 void PowerManagementSettings::setIdlenessWatcherEnabled(bool idlenessWatcherEnabled)
212 {
213     setValue(ENABLE_IDLENESS_WATCHER_KEY, idlenessWatcherEnabled);
214 }
215 
getIdlenessACAction()216 int PowerManagementSettings::getIdlenessACAction()
217 {
218     // defaults to nothing (-1) and eventually load legacy value
219     int oldValue = value(QL1S("idlenessAction"), -1).toInt();
220     return value(IDLENESS_AC_ACTION_KEY, oldValue).toInt();
221 }
222 
setIdlenessACAction(int idlenessAction)223 void PowerManagementSettings::setIdlenessACAction(int idlenessAction)
224 {
225     setValue(IDLENESS_AC_ACTION_KEY, idlenessAction);
226 }
227 
getIdlenessACTime()228 QTime PowerManagementSettings::getIdlenessACTime()
229 {
230     // 1. The default value is 15 minutes (the legacy key is also read);
231     // 2. Intervals greater than or equal to one hour aren't supported; and
232     // 3. Intervals smaller than one minute are prevented.
233     int oldValue = qBound(60, value(QL1S("idlenessTimeSecs"), 900).toInt(), 3599);
234     int m = oldValue / 60;
235     int s = oldValue - m * 60;
236     return value(IDLENESS_AC_TIME, QTime(0, m, s)).toTime();
237 }
238 
setIdlenessACTime(QTime idlenessTime)239 void PowerManagementSettings::setIdlenessACTime(QTime idlenessTime)
240 {
241     setValue(IDLENESS_AC_TIME, idlenessTime);
242 }
243 
getIdlenessBatteryAction()244 int PowerManagementSettings::getIdlenessBatteryAction()
245 {
246     // defaults to nothing (-1) and eventually load legacy value
247     int oldValue = value(QL1S("idlenessAction"), -1).toInt();
248     return value(IDLENESS_BATTERY_ACTION_KEY, oldValue).toInt();
249 }
250 
setIdlenessBatteryAction(int idlenessAction)251 void PowerManagementSettings::setIdlenessBatteryAction(int idlenessAction)
252 {
253     setValue(IDLENESS_BATTERY_ACTION_KEY, idlenessAction);
254 }
255 
getIdlenessBatteryTime()256 QTime PowerManagementSettings::getIdlenessBatteryTime()
257 {
258     // 1. The default value is 15 minutes (the legacy key is also read);
259     // 2. Intervals greater than or equal to one hour aren't supported; and
260     // 3. Intervals smaller than one minute are prevented.
261     int oldValue = qBound(60, value(QL1S("idlenessTimeSecs"), 900).toInt(), 3599);
262     int m = oldValue / 60;
263     int s = oldValue - m * 60;
264     return value(IDLENESS_BATTERY_TIME, QTime(0, m, s)).toTime();
265 }
266 
setIdlenessBatteryTime(QTime idlenessTime)267 void PowerManagementSettings::setIdlenessBatteryTime(QTime idlenessTime)
268 {
269     setValue(IDLENESS_BATTERY_TIME, idlenessTime);
270 }
271 
isIdlenessBacklightWatcherEnabled()272 bool PowerManagementSettings::isIdlenessBacklightWatcherEnabled()
273 {
274     return value(ENABLE_IDLENESS_BACKLIGHT_WATCHER_KEY, false).toBool();
275 }
276 
setIdlenessBacklightWatcherEnabled(bool idlenessBacklightWatcherEnabled)277 void PowerManagementSettings::setIdlenessBacklightWatcherEnabled(bool idlenessBacklightWatcherEnabled)
278 {
279     setValue(ENABLE_IDLENESS_BACKLIGHT_WATCHER_KEY, idlenessBacklightWatcherEnabled);
280 }
281 
getIdlenessBacklightTime()282 QTime PowerManagementSettings::getIdlenessBacklightTime()
283 {
284     // default to 1 minute
285     return value(IDLENESS_BACKLIGHT_TIME, QTime(0, 1)).toTime();
286 }
287 
setIdlenessBacklightTime(QTime idlenessBacklightTime)288 void PowerManagementSettings::setIdlenessBacklightTime(QTime idlenessBacklightTime)
289 {
290     setValue(IDLENESS_BACKLIGHT_TIME, idlenessBacklightTime);
291 }
292 
getBacklight()293 int PowerManagementSettings::getBacklight()
294 {
295     return value(IDLENESS_BACKLIGHT, 50).toInt();
296 }
297 
setBacklight(int backlight)298 void PowerManagementSettings::setBacklight(int backlight)
299 {
300     setValue(IDLENESS_BACKLIGHT, backlight);
301 }
302 
isIdlenessBacklightOnBatteryDischargingEnabled()303 bool PowerManagementSettings::isIdlenessBacklightOnBatteryDischargingEnabled()
304 {
305     return value(IDLENESS_BACKLIGHT_ON_BATTERY_DISCHARGING, true).toBool();
306 }
307 
setIdlenessBacklightOnBatteryDischargingEnabled(bool enabled)308 void PowerManagementSettings::setIdlenessBacklightOnBatteryDischargingEnabled(bool enabled)
309 {
310     setValue(IDLENESS_BACKLIGHT_ON_BATTERY_DISCHARGING, enabled);
311 }
312 
isDisableIdlenessWhenFullscreenEnabled()313 bool PowerManagementSettings::isDisableIdlenessWhenFullscreenEnabled()
314 {
315     return value(DISABLE_IDLENESS_WHEN_FULLSCREEN, false).toBool();
316 }
317 
setDisableIdlenessWhenFullscreen(bool enabled)318 void PowerManagementSettings::setDisableIdlenessWhenFullscreen(bool enabled)
319 {
320     setValue(DISABLE_IDLENESS_WHEN_FULLSCREEN, enabled);
321 }
322 
getPowerKeyAction()323 int PowerManagementSettings::getPowerKeyAction()
324 {
325     return value(POWER_KEY_ACTION, LXQt::Power::Action::PowerShutdown).toInt();
326 }
327 
setPowerKeyAction(int action)328 void PowerManagementSettings::setPowerKeyAction(int action)
329 {
330     setValue(POWER_KEY_ACTION, action);
331 }
332 
getSuspendKeyAction()333 int PowerManagementSettings::getSuspendKeyAction()
334 {
335     return value(SUSPEND_KEY_ACTION, LXQt::Power::Action::PowerSuspend).toInt();
336 }
337 
setSuspendKeyAction(int action)338 void PowerManagementSettings::setSuspendKeyAction(int action)
339 {
340     setValue(SUSPEND_KEY_ACTION, action);
341 }
342 
getHibernateKeyAction()343 int PowerManagementSettings::getHibernateKeyAction()
344 {
345     return value(HIBERNATE_KEY_ACTION, LXQt::Power::Action::PowerHibernate).toInt();
346 }
347 
setHibernateKeyAction(int action)348 void PowerManagementSettings::setHibernateKeyAction(int action)
349 {
350     setValue(HIBERNATE_KEY_ACTION, action);
351 }
352 
isIdlenessWatcherPaused() const353 bool PowerManagementSettings::isIdlenessWatcherPaused() const
354 {
355     return mIdlenessWatcherPaused;
356 }
357 
setIdlenessWatcherPaused(bool idlenessWatcherPaused)358 void PowerManagementSettings::setIdlenessWatcherPaused(bool idlenessWatcherPaused) {
359     mIdlenessWatcherPaused = idlenessWatcherPaused;
360 }
361 
362