~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to powerdevil/daemon/powerdevilprofilegenerator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "powerdevilprofilegenerator.h"
 
21
 
 
22
#include <PowerDevilSettings.h>
 
23
 
 
24
#include <QtCore/QFile>
 
25
 
 
26
#include <Solid/Device>
 
27
#include <Solid/Battery>
 
28
#include <Solid/PowerManagement>
 
29
 
 
30
#include <KConfigGroup>
 
31
#include <KSharedConfig>
 
32
#include <KLocalizedString>
 
33
#include <KNotification>
 
34
#include <KIcon>
 
35
#include <KStandardDirs>
 
36
 
 
37
namespace PowerDevil {
 
38
 
 
39
ProfileGenerator::GeneratorResult ProfileGenerator::generateProfiles(bool tryUpgrade)
 
40
{
 
41
    if (tryUpgrade) {
 
42
        KSharedConfigPtr oldProfilesConfig = KSharedConfig::openConfig("powerdevilprofilesrc", KConfig::SimpleConfig);
 
43
        if (!oldProfilesConfig->groupList().isEmpty()) {
 
44
            // We can upgrade, let's do that.
 
45
            upgradeProfiles();
 
46
            return ResultUpgraded;
 
47
        }
 
48
    }
 
49
    QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates();
 
50
 
 
51
    // Let's change some defaults
 
52
    if (!methods.contains(Solid::PowerManagement::SuspendState)) {
 
53
        if (!methods.contains(Solid::PowerManagement::HibernateState)) {
 
54
            PowerDevilSettings::setBatteryCriticalAction(0);
 
55
        } else {
 
56
            PowerDevilSettings::setBatteryCriticalAction(2);
 
57
        }
 
58
    }
 
59
 
 
60
    // Ok, let's get our config file.
 
61
    KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powerdevil2profilesrc", KConfig::SimpleConfig);
 
62
 
 
63
    // And clear it
 
64
    foreach (const QString &group, profilesConfig->groupList()) {
 
65
        profilesConfig->deleteGroup(group);
 
66
    }
 
67
 
 
68
    // Let's start: performance profile before anything else
 
69
    KConfigGroup performance(profilesConfig, "Performance");
 
70
    performance.writeEntry("icon", "preferences-system-performance");
 
71
 
 
72
    // We want to dim the screen after a while, definitely
 
73
    {
 
74
        KConfigGroup dimDisplay(&performance, "DimDisplay");
 
75
        dimDisplay.writeEntry< int >("idleTime", 600000);
 
76
    }
 
77
    // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
 
78
    {
 
79
        KConfigGroup handleButtonEvents(&performance, "HandleButtonEvents");
 
80
        handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
 
81
        if (methods.contains(Solid::PowerManagement::SuspendState)) {
 
82
            handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
 
83
        } else {
 
84
            handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
 
85
        }
 
86
    }
 
87
    // And we also want to turn off the screen after another while
 
88
    {
 
89
        KConfigGroup dpmsControl(&performance, "DPMSControl");
 
90
        dpmsControl.writeEntry< uint >("idleTime", 600);
 
91
    }
 
92
 
 
93
    // Assign the profile, of course!
 
94
    PowerDevilSettings::setACProfile(performance.name());
 
95
 
 
96
    // Easy part done. Now, any batteries?
 
97
    int batteryCount = 0;
 
98
 
 
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) {
 
103
            continue;
 
104
        }
 
105
        ++batteryCount;
 
106
    }
 
107
 
 
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.
 
111
        {
 
112
            KConfigGroup brightnessControl(&performance, "BrightnessControl");
 
113
            brightnessControl.writeEntry< int >("value", 100);
 
114
        }
 
115
 
 
116
        // Powersave
 
117
        KConfigGroup powersave(profilesConfig, "Powersave");
 
118
        powersave.writeEntry("icon", "preferences-system-power-management");
 
119
        // Less brightness.
 
120
        {
 
121
            KConfigGroup brightnessControl(&powersave, "BrightnessControl");
 
122
            brightnessControl.writeEntry< int >("value", 60);
 
123
        }
 
124
        // We want to dim the screen after a while, definitely
 
125
        {
 
126
            KConfigGroup dimDisplay(&powersave, "DimDisplay");
 
127
            dimDisplay.writeEntry< int >("idleTime", 300000);
 
128
        }
 
129
        // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
 
130
        {
 
131
            KConfigGroup handleButtonEvents(&powersave, "HandleButtonEvents");
 
132
            handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
 
133
            if (methods.contains(Solid::PowerManagement::SuspendState)) {
 
134
                handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
 
135
            } else {
 
136
                handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
 
137
            }
 
138
        }
 
139
        // We want to turn off the screen after another while
 
140
        {
 
141
            KConfigGroup dpmsControl(&powersave, "DPMSControl");
 
142
            dpmsControl.writeEntry< uint >("idleTime", 300);
 
143
        }
 
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);
 
149
        }
 
150
 
 
151
 
 
152
        // Ok, now for aggressive powersave
 
153
        KConfigGroup aggrPowersave(profilesConfig, "Aggressive powersave");
 
154
        aggrPowersave.writeEntry("icon", "battery-low");
 
155
        // Less brightness.
 
156
        {
 
157
            KConfigGroup brightnessControl(&aggrPowersave, "BrightnessControl");
 
158
            brightnessControl.writeEntry< int >("value", 30);
 
159
        }
 
160
        // We want to dim the screen after a while, definitely
 
161
        {
 
162
            KConfigGroup dimDisplay(&aggrPowersave, "DimDisplay");
 
163
            dimDisplay.writeEntry< int >("idleTime", 120000);
 
164
        }
 
165
        // Show the dialog when power button is pressed and suspend on suspend button pressed and lid closed (if supported)
 
166
        {
 
167
            KConfigGroup handleButtonEvents(&aggrPowersave, "HandleButtonEvents");
 
168
            handleButtonEvents.writeEntry< uint >("powerButtonAction", LogoutDialogMode);
 
169
            if (methods.contains(Solid::PowerManagement::SuspendState)) {
 
170
                handleButtonEvents.writeEntry< uint >("lidAction", ToRamMode);
 
171
            } else {
 
172
                handleButtonEvents.writeEntry< uint >("lidAction", TurnOffScreenMode);
 
173
            }
 
174
        }
 
175
        // We want to turn off the screen after another while
 
176
        {
 
177
            KConfigGroup dpmsControl(&aggrPowersave, "DPMSControl");
 
178
            dpmsControl.writeEntry< uint >("idleTime", 120);
 
179
        }
 
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);
 
185
        }
 
186
 
 
187
        // Assign profiles
 
188
        PowerDevilSettings::setACProfile(performance.name());
 
189
        PowerDevilSettings::setBatteryProfile(powersave.name());
 
190
        PowerDevilSettings::setLowProfile(aggrPowersave.name());
 
191
        PowerDevilSettings::setWarningProfile(aggrPowersave.name());
 
192
    }
 
193
 
 
194
    // Save and be happy
 
195
    PowerDevilSettings::self()->writeConfig();
 
196
    profilesConfig->sync();
 
197
 
 
198
    return ResultGenerated;
 
199
}
 
200
 
 
201
void ProfileGenerator::upgradeProfiles()
 
202
{
 
203
    QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates();
 
204
 
 
205
    // Let's change some defaults
 
206
    if (!methods.contains(Solid::PowerManagement::SuspendState)) {
 
207
        if (!methods.contains(Solid::PowerManagement::HibernateState)) {
 
208
            PowerDevilSettings::setBatteryCriticalAction(None);
 
209
        } else {
 
210
            PowerDevilSettings::setBatteryCriticalAction(ToDiskMode);
 
211
        }
 
212
    } else {
 
213
        PowerDevilSettings::setBatteryCriticalAction(ToRamMode);
 
214
    }
 
215
 
 
216
    // Ok, let's get our config file.
 
217
    KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powerdevil2profilesrc", KConfig::SimpleConfig);
 
218
    KSharedConfigPtr oldProfilesConfig = KSharedConfig::openConfig("powerdevilprofilesrc", KConfig::SimpleConfig);
 
219
 
 
220
    // And clear it
 
221
    foreach (const QString &group, profilesConfig->groupList()) {
 
222
        profilesConfig->deleteGroup(group);
 
223
    }
 
224
 
 
225
    foreach (const QString &group, oldProfilesConfig->groupList()) {
 
226
        KConfigGroup oldGroup = oldProfilesConfig->group(group);
 
227
        KConfigGroup newGroup(profilesConfig, oldGroup.readEntry< QString >("name", QString()));
 
228
 
 
229
        // Read stuff
 
230
        // Brightness.
 
231
        {
 
232
            KConfigGroup brightnessControl(&newGroup, "BrightnessControl");
 
233
            brightnessControl.writeEntry< int >("value", oldGroup.readEntry< int >("brightness", 100));
 
234
        }
 
235
        // Dim screen
 
236
        if (oldGroup.readEntry< bool >("dimOnIdle", false)) {
 
237
            KConfigGroup dimDisplay(&newGroup, "DimDisplay");
 
238
            dimDisplay.writeEntry< int >("idleTime", oldGroup.readEntry< int >("dimOnIdleTime", 30) * 60 * 1000);
 
239
        }
 
240
        // DPMS
 
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);
 
244
        }
 
245
        // Script
 
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);
 
250
        }
 
251
        // SuspendSession
 
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)));
 
256
        }
 
257
        // Buttons
 
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)));
 
262
        }
 
263
    }
 
264
 
 
265
    // Save and be happy
 
266
    profilesConfig->sync();
 
267
 
 
268
    // We also want to backup and erase the old profiles.
 
269
    QString oldProfilesFile = KGlobal::dirs()->findResource("config", "powerdevilprofilesrc");
 
270
    if (!oldProfilesFile.isEmpty()) {
 
271
        // Backup
 
272
        QString bkProfilesFile = oldProfilesFile;
 
273
        bkProfilesFile.append(".old");
 
274
        KConfig *bkConfig = oldProfilesConfig->copyTo(bkProfilesFile);
 
275
        if (bkConfig != 0) {
 
276
            bkConfig->sync();
 
277
            delete bkConfig;
 
278
 
 
279
            // Delete the old profiles now.
 
280
            QFile::remove(oldProfilesFile);
 
281
        }
 
282
    }
 
283
}
 
284
 
 
285
uint ProfileGenerator::upgradeOldAction(uint oldAction)
 
286
{
 
287
    switch ((OldIdleAction)oldAction) {
 
288
        case Standby:
 
289
        case S2Ram:
 
290
            return ToRamMode;
 
291
        case S2Disk:
 
292
            return ToDiskMode;
 
293
        case Shutdown:
 
294
            return ShutdownMode;
 
295
        case Lock:
 
296
            return LockScreenMode;
 
297
        case ShutdownDialog:
 
298
            return LogoutDialogMode;
 
299
        case TurnOffScreen:
 
300
            return TurnOffScreenMode;
 
301
        default:
 
302
            return 0;
 
303
    }
 
304
}
 
305
 
 
306
}