1
/***************************************************************************
2
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
20
#include "powerdevilprofilegenerator.h"
22
#include <PowerDevilSettings.h>
24
#include <QtCore/QFile>
26
#include <Solid/Device>
27
#include <Solid/Battery>
28
#include <Solid/PowerManagement>
30
#include <KConfigGroup>
31
#include <KSharedConfig>
32
#include <KLocalizedString>
33
#include <KNotification>
35
#include <KStandardDirs>
37
namespace PowerDevil {
39
ProfileGenerator::GeneratorResult ProfileGenerator::generateProfiles(bool tryUpgrade)
42
KSharedConfigPtr oldProfilesConfig = KSharedConfig::openConfig("powerdevilprofilesrc", KConfig::SimpleConfig);
43
if (!oldProfilesConfig->groupList().isEmpty()) {
44
// We can upgrade, let's do that.
46
return ResultUpgraded;
49
QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates();
51
// Let's change some defaults
52
if (!methods.contains(Solid::PowerManagement::SuspendState)) {
53
if (!methods.contains(Solid::PowerManagement::HibernateState)) {
54
PowerDevilSettings::setBatteryCriticalAction(0);
56
PowerDevilSettings::setBatteryCriticalAction(2);
60
// Ok, let's get our config file.
61
KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powerdevil2profilesrc", KConfig::SimpleConfig);
64
foreach (const QString &group, profilesConfig->groupList()) {
65
profilesConfig->deleteGroup(group);
68
// Let's start: performance profile before anything else
69
KConfigGroup performance(profilesConfig, "Performance");
70
performance.writeEntry("icon", "preferences-system-performance");
72
// We want to dim the screen after a while, definitely
74
KConfigGroup dimDisplay(&performance, "DimDisplay");
75
dimDisplay.writeEntry< int >("idleTime", 600000);
77
// Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
79
KConfigGroup handleButtonEvents(&performance, "HandleButtonEvents");
80
handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
81
if (methods.contains(Solid::PowerManagement::SuspendState)) {
82
handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
84
handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
87
// And we also want to turn off the screen after another while
89
KConfigGroup dpmsControl(&performance, "DPMSControl");
90
dpmsControl.writeEntry< uint >("idleTime", 600);
93
// Assign the profile, of course!
94
PowerDevilSettings::setACProfile(performance.name());
96
// Easy part done. Now, any batteries?
99
foreach(const Solid::Device &device, Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString())) {
100
Solid::Device dev = device;
101
Solid::Battery *b = qobject_cast<Solid::Battery*> (dev.asDeviceInterface(Solid::DeviceInterface::Battery));
102
if (b->type() != Solid::Battery::PrimaryBattery && b->type() != Solid::Battery::UpsBattery) {
108
if (batteryCount > 0) {
109
// Ok, we need a pair more profiles: powersave and aggressive powersave.
110
// Also, now we want to handle brightness in performance.
112
KConfigGroup brightnessControl(&performance, "BrightnessControl");
113
brightnessControl.writeEntry< int >("value", 100);
117
KConfigGroup powersave(profilesConfig, "Powersave");
118
powersave.writeEntry("icon", "preferences-system-power-management");
121
KConfigGroup brightnessControl(&powersave, "BrightnessControl");
122
brightnessControl.writeEntry< int >("value", 60);
124
// We want to dim the screen after a while, definitely
126
KConfigGroup dimDisplay(&powersave, "DimDisplay");
127
dimDisplay.writeEntry< int >("idleTime", 300000);
129
// Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
131
KConfigGroup handleButtonEvents(&powersave, "HandleButtonEvents");
132
handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
133
if (methods.contains(Solid::PowerManagement::SuspendState)) {
134
handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
136
handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
139
// We want to turn off the screen after another while
141
KConfigGroup dpmsControl(&powersave, "DPMSControl");
142
dpmsControl.writeEntry< uint >("idleTime", 300);
144
// Last but not least, we want to suspend after a rather long period of inactivity
145
if (methods.contains(Solid::PowerManagement::SuspendState)) {
146
KConfigGroup suspendSession(&powersave, "SuspendSession");
147
suspendSession.writeEntry< uint >("idleTime", 600000);
148
suspendSession.writeEntry< uint >("suspendType", ToRamMode);
152
// Ok, now for aggressive powersave
153
KConfigGroup aggrPowersave(profilesConfig, "Aggressive powersave");
154
aggrPowersave.writeEntry("icon", "battery-low");
157
KConfigGroup brightnessControl(&aggrPowersave, "BrightnessControl");
158
brightnessControl.writeEntry< int >("value", 30);
160
// We want to dim the screen after a while, definitely
162
KConfigGroup dimDisplay(&aggrPowersave, "DimDisplay");
163
dimDisplay.writeEntry< int >("idleTime", 120000);
165
// Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
167
KConfigGroup handleButtonEvents(&aggrPowersave, "HandleButtonEvents");
168
handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
169
if (methods.contains(Solid::PowerManagement::SuspendState)) {
170
handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
172
handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
175
// We want to turn off the screen after another while
177
KConfigGroup dpmsControl(&aggrPowersave, "DPMSControl");
178
dpmsControl.writeEntry< uint >("idleTime", 120);
180
// Last but not least, we want to suspend after a rather long period of inactivity
181
if (methods.contains(Solid::PowerManagement::SuspendState)) {
182
KConfigGroup suspendSession(&aggrPowersave, "SuspendSession");
183
suspendSession.writeEntry< uint >("idleTime", 300000);
184
suspendSession.writeEntry< uint >("suspendType", ToRamMode);
188
PowerDevilSettings::setACProfile(performance.name());
189
PowerDevilSettings::setBatteryProfile(powersave.name());
190
PowerDevilSettings::setLowProfile(aggrPowersave.name());
191
PowerDevilSettings::setWarningProfile(aggrPowersave.name());
195
PowerDevilSettings::self()->writeConfig();
196
profilesConfig->sync();
198
return ResultGenerated;
201
void ProfileGenerator::upgradeProfiles()
203
QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates();
205
// Let's change some defaults
206
if (!methods.contains(Solid::PowerManagement::SuspendState)) {
207
if (!methods.contains(Solid::PowerManagement::HibernateState)) {
208
PowerDevilSettings::setBatteryCriticalAction(None);
210
PowerDevilSettings::setBatteryCriticalAction(ToDiskMode);
213
PowerDevilSettings::setBatteryCriticalAction(ToRamMode);
216
// Ok, let's get our config file.
217
KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powerdevil2profilesrc", KConfig::SimpleConfig);
218
KSharedConfigPtr oldProfilesConfig = KSharedConfig::openConfig("powerdevilprofilesrc", KConfig::SimpleConfig);
221
foreach (const QString &group, profilesConfig->groupList()) {
222
profilesConfig->deleteGroup(group);
225
foreach (const QString &group, oldProfilesConfig->groupList()) {
226
KConfigGroup oldGroup = oldProfilesConfig->group(group);
227
KConfigGroup newGroup(profilesConfig, oldGroup.readEntry< QString >("name", QString()));
232
KConfigGroup brightnessControl(&newGroup, "BrightnessControl");
233
brightnessControl.writeEntry< int >("value", oldGroup.readEntry< int >("brightness", 100));
236
if (oldGroup.readEntry< bool >("dimOnIdle", false)) {
237
KConfigGroup dimDisplay(&newGroup, "DimDisplay");
238
dimDisplay.writeEntry< int >("idleTime", oldGroup.readEntry< int >("dimOnIdleTime", 30) * 60 * 1000);
241
if (oldGroup.readEntry< bool >("DPMSEnabled", false) && oldGroup.readEntry< int >("DPMSPowerOff", 0) > 0) {
242
KConfigGroup dpmsControl(&newGroup, "DPMSControl");
243
dpmsControl.writeEntry< uint >("idleTime", oldGroup.readEntry< int >("DPMSPowerOff", 30) * 60);
246
if (!oldGroup.readEntry< QString >("scriptpath", QString()).isEmpty()) {
247
KConfigGroup runScript(&newGroup, "RunScript");
248
runScript.writeEntry< QString >("scriptCommand", oldGroup.readEntry< QString >("scriptpath", QString()));
249
runScript.writeEntry< uint >("scriptPhase", 0);
252
if (oldGroup.readEntry< uint >("idleAction", 0) > 0) {
253
KConfigGroup suspendSession(&newGroup, "SuspendSession");
254
suspendSession.writeEntry< uint >("idleTime", oldGroup.readEntry< int >("idleTime", 30) * 60 * 1000);
255
suspendSession.writeEntry< uint >("suspendType", upgradeOldAction(oldGroup.readEntry< uint >("idleAction", 0)));
258
if (oldGroup.readEntry< uint >("powerButtonAction", 0) > 0 || oldGroup.readEntry< uint >("lidAction", 0) > 0) {
259
KConfigGroup handleButtons(&newGroup, "HandleButtonEvents");
260
handleButtons.writeEntry< uint >("powerButtonAction", upgradeOldAction(oldGroup.readEntry< uint >("powerButtonAction", 0)));
261
handleButtons.writeEntry< uint >("lidAction", upgradeOldAction(oldGroup.readEntry< uint >("lidAction", 0)));
266
profilesConfig->sync();
268
// We also want to backup and erase the old profiles.
269
QString oldProfilesFile = KGlobal::dirs()->findResource("config", "powerdevilprofilesrc");
270
if (!oldProfilesFile.isEmpty()) {
272
QString bkProfilesFile = oldProfilesFile;
273
bkProfilesFile.append(".old");
274
KConfig *bkConfig = oldProfilesConfig->copyTo(bkProfilesFile);
279
// Delete the old profiles now.
280
QFile::remove(oldProfilesFile);
285
uint ProfileGenerator::upgradeOldAction(uint oldAction)
287
switch ((OldIdleAction)oldAction) {
296
return LockScreenMode;
298
return LogoutDialogMode;
300
return TurnOffScreenMode;