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

« back to all changes in this revision

Viewing changes to powerdevil/kcmodule/profiles/EditPage.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) 2008-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 "EditPage.h"
 
21
 
 
22
#include "actionconfigwidget.h"
 
23
 
 
24
#include <daemon/powerdevilactionconfig.h>
 
25
#include <daemon/powerdevilprofilegenerator.h>
 
26
 
 
27
#include <config-workspace.h>
 
28
 
 
29
#include <QtGui/QCheckBox>
 
30
#include <QtGui/QFormLayout>
 
31
#include <QtGui/QLabel>
 
32
 
 
33
#include <QtDBus/QDBusMessage>
 
34
#include <QtDBus/QDBusReply>
 
35
#include <QtDBus/QDBusConnection>
 
36
#include <QtDBus/QDBusMetaType>
 
37
 
 
38
#include <KConfigGroup>
 
39
#include <KLineEdit>
 
40
#include <KDebug>
 
41
#include <KDialog>
 
42
#include <KFileDialog>
 
43
#include <KMessageBox>
 
44
#include <KIconButton>
 
45
#include <KToolBar>
 
46
#include <KAboutData>
 
47
#include <KPluginFactory>
 
48
#include <KServiceTypeTrader>
 
49
#include <KStandardDirs>
 
50
#include <KRun>
 
51
 
 
52
K_PLUGIN_FACTORY(PowerDevilProfilesKCMFactory,
 
53
                 registerPlugin<EditPage>();
 
54
                )
 
55
K_EXPORT_PLUGIN(PowerDevilProfilesKCMFactory("powerdevilprofilesconfig","powerdevil"))
 
56
 
 
57
typedef QMap< QString, QString > StringStringMap;
 
58
Q_DECLARE_METATYPE(StringStringMap)
 
59
 
 
60
EditPage::EditPage(QWidget *parent, const QVariantList &args)
 
61
        : KCModule(PowerDevilProfilesKCMFactory::componentData(), parent, args)
 
62
        , m_profileEdited(false)
 
63
{
 
64
    qDBusRegisterMetaType< StringStringMap >();
 
65
 
 
66
    setButtons(Apply | Help | Default);
 
67
 
 
68
    KAboutData *about =
 
69
        new KAboutData("powerdevilprofilesconfig", "powerdevilprofilesconfig", ki18n("Power Profiles Configuration"),
 
70
                       "", ki18n("A profile configurator for KDE Power Management System"),
 
71
                       KAboutData::License_GPL, ki18n("(c), 2010 Dario Freddi"),
 
72
                       ki18n("From this module, you can manage KDE Power Management System's power profiles, by tweaking "
 
73
                             "existing ones or creating new ones."));
 
74
 
 
75
    about->addAuthor(ki18n("Dario Freddi"), ki18n("Maintainer") , "drf@kde.org",
 
76
                     "http://drfav.wordpress.com");
 
77
 
 
78
    setAboutData(about);
 
79
 
 
80
    setupUi(this);
 
81
 
 
82
    m_profilesConfig = KSharedConfig::openConfig("powerdevil2profilesrc", KConfig::SimpleConfig);
 
83
 
 
84
    if (m_profilesConfig->groupList().isEmpty()) {
 
85
        // Use the generator
 
86
        PowerDevil::ProfileGenerator::generateProfiles();
 
87
        m_profilesConfig->reparseConfiguration();
 
88
    }
 
89
 
 
90
    m_toolBar = new KToolBar(this);
 
91
    listLayout->addWidget(m_toolBar);
 
92
 
 
93
    m_toolBar->addAction(actionNewProfile);
 
94
    m_toolBar->addAction(actionEditProfile);
 
95
    m_toolBar->addAction(actionDeleteProfile);
 
96
    m_toolBar->addSeparator();
 
97
    m_toolBar->addAction(actionImportProfiles);
 
98
    m_toolBar->addAction(actionExportProfiles);
 
99
    m_toolBar->addAction(actionRestoreDefaultProfiles);
 
100
    m_toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
 
101
 
 
102
    actionNewProfile->setIcon(KIcon("document-new"));
 
103
    actionEditProfile->setIcon(KIcon("document-edit"));
 
104
    actionDeleteProfile->setIcon(KIcon("edit-delete-page"));
 
105
    actionImportProfiles->setIcon(KIcon("document-import"));
 
106
    actionExportProfiles->setIcon(KIcon("document-export"));
 
107
    actionRestoreDefaultProfiles->setIcon(KIcon("document-revert"));
 
108
 
 
109
    connect(profilesList, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
 
110
            SLOT(switchProfile(QListWidgetItem*, QListWidgetItem*)));
 
111
    connect(this, SIGNAL(changed(bool)),
 
112
            this, SLOT(onChanged(bool)));
 
113
 
 
114
    connect(actionDeleteProfile, SIGNAL(triggered()), SLOT(deleteCurrentProfile()));
 
115
    connect(actionEditProfile, SIGNAL(triggered(bool)), SLOT(editProfile()));
 
116
    connect(actionNewProfile, SIGNAL(triggered()), SLOT(createProfile()));
 
117
    connect(actionImportProfiles, SIGNAL(triggered()), SLOT(importProfiles()));
 
118
    connect(actionExportProfiles, SIGNAL(triggered()), SLOT(exportProfiles()));
 
119
    connect(actionRestoreDefaultProfiles, SIGNAL(triggered(bool)), SLOT(restoreDefaultProfiles()));
 
120
 
 
121
    reloadAvailableProfiles();
 
122
    ActionConfigWidget *actionConfigWidget = new ActionConfigWidget(0);
 
123
    QMap< int, QList<QPair<QString, QWidget*> > > widgets;
 
124
 
 
125
    // Load all the services
 
126
    KService::List offers = KServiceTypeTrader::self()->query("PowerDevil/Action", "(Type == 'Service')");
 
127
 
 
128
    foreach (const KService::Ptr &offer, offers) {
 
129
        //try to load the specified library
 
130
        KPluginFactory *factory = KPluginLoader(offer->property("X-KDE-PowerDevil-Action-UIComponentLibrary",
 
131
                                                                QVariant::String).toString()).factory();
 
132
 
 
133
        if (!factory) {
 
134
            kError() << "KPluginFactory could not load the plugin:" << offer->property("X-KDE-PowerDevil-Action-UIComponentLibrary",
 
135
                                                                       QVariant::String).toString();
 
136
            continue;
 
137
        }
 
138
 
 
139
        PowerDevil::ActionConfig *actionConfig = factory->create<PowerDevil::ActionConfig>();
 
140
        if (!actionConfig) {
 
141
            kError() << "KPluginFactory could not load the plugin:" << offer->property("X-KDE-PowerDevil-Action-UIComponentLibrary",
 
142
                                                                       QVariant::String).toString();
 
143
            continue;
 
144
        }
 
145
 
 
146
        connect(actionConfig, SIGNAL(changed()), this, SLOT(changed()));
 
147
 
 
148
        QCheckBox *checkbox = new QCheckBox(offer->name());
 
149
        if (!offer->icon().isEmpty()) {
 
150
            checkbox->setIcon(KIcon(offer->icon()));
 
151
        }
 
152
        connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(changed()));
 
153
        m_actionsHash.insert(offer->property("X-KDE-PowerDevil-Action-ID", QVariant::String).toString(), checkbox);
 
154
        m_actionsConfigHash.insert(offer->property("X-KDE-PowerDevil-Action-ID", QVariant::String).toString(), actionConfig);
 
155
 
 
156
        QList<QPair<QString, QWidget*> > offerWidgets = actionConfig->buildUi();
 
157
        offerWidgets.prepend(qMakePair<QString,QWidget*>(QString(), checkbox));
 
158
        widgets.insertMulti(100 - offer->property("X-KDE-PowerDevil-Action-ConfigPriority", QVariant::Int).toInt(),
 
159
                            offerWidgets);
 
160
    }
 
161
 
 
162
    for (QMap< int, QList<QPair<QString, QWidget*> > >::const_iterator i = widgets.constBegin(); i != widgets.constEnd(); ++i) {
 
163
        actionConfigWidget->addWidgets(i.value());
 
164
    }
 
165
 
 
166
    // Add a proxy widget to prevent vertical fuck ups
 
167
    QWidget *tw = new QWidget;
 
168
    QVBoxLayout *lay = new QVBoxLayout;
 
169
    lay->addWidget(actionConfigWidget);
 
170
    lay->addStretch();
 
171
    tw->setLayout(lay);
 
172
    scrollArea->setWidget(tw);
 
173
}
 
174
 
 
175
EditPage::~EditPage()
 
176
{
 
177
}
 
178
 
 
179
void EditPage::onChanged(bool changed)
 
180
{
 
181
    m_profileEdited = changed;
 
182
}
 
183
 
 
184
void EditPage::load()
 
185
{
 
186
    loadProfile();
 
187
}
 
188
 
 
189
void EditPage::save()
 
190
{
 
191
    if (!profilesList->currentItem()) {
 
192
        return;
 
193
    }
 
194
 
 
195
    QString profile = profilesList->currentItem()->data(Qt::UserRole).toString();
 
196
    saveProfile(profile);
 
197
    // Notify the daemon
 
198
    notifyDaemon(profile);
 
199
}
 
200
 
 
201
void EditPage::notifyDaemon(const QString &editedProfile)
 
202
{
 
203
    QDBusMessage call;
 
204
    if (!editedProfile.isNull()) {
 
205
        call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
206
                                              "org.kde.Solid.PowerManagement", "currentProfile");
 
207
        QDBusPendingReply< QString > reply = QDBusConnection::sessionBus().asyncCall(call);
 
208
        reply.waitForFinished();
 
209
 
 
210
        if (reply.isValid()) {
 
211
            if (reply.value() == editedProfile) {
 
212
                // Ask to reload the profile
 
213
                kDebug() << "Active profile edited, reloading profile";
 
214
                call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
215
                                                      "org.kde.Solid.PowerManagement", "reloadCurrentProfile");
 
216
            } else {
 
217
                // Ask to reparse config
 
218
                kDebug() << "Inactive profile edited, reparsing configuration";
 
219
                call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
220
                                                      "org.kde.Solid.PowerManagement", "reparseConfiguration");
 
221
            }
 
222
        } else {
 
223
            kWarning() << "Invalid reply from daemon when asking for current profile!";
 
224
            // To be sure, reload profile
 
225
            call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
226
                                                  "org.kde.Solid.PowerManagement", "reloadCurrentProfile");
 
227
        }
 
228
    } else {
 
229
        // Refresh status
 
230
        call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
231
                                              "org.kde.Solid.PowerManagement", "refreshStatus");
 
232
    }
 
233
 
 
234
    // Perform call
 
235
    QDBusConnection::sessionBus().asyncCall(call);
 
236
}
 
237
 
 
238
void EditPage::loadProfile()
 
239
{
 
240
    kDebug() << "Loading a profile";
 
241
 
 
242
    if (!profilesList->currentItem()) {
 
243
        return;
 
244
    }
 
245
 
 
246
    // Check if the profile is not reserved
 
247
    QString profileId = profilesList->currentItem()->data(Qt::UserRole).toString();
 
248
    if (profileId == "Performance" || profileId == "Powersave" || profileId == "Aggressive powersave") {
 
249
        actionDeleteProfile->setEnabled(false);
 
250
        actionEditProfile->setEnabled(false);
 
251
    } else {
 
252
        actionDeleteProfile->setEnabled(true);
 
253
        actionEditProfile->setEnabled(true);
 
254
    }
 
255
 
 
256
    kDebug() << profileId;
 
257
 
 
258
    KConfigGroup group(m_profilesConfig, profileId);
 
259
 
 
260
    if (!group.isValid()) {
 
261
        return;
 
262
    }
 
263
    kDebug() << "Ok, KConfigGroup ready";
 
264
 
 
265
    // Iterate over the possible actions
 
266
    for (QHash< QString, QCheckBox* >::const_iterator i = m_actionsHash.constBegin(); i != m_actionsHash.constEnd(); ++i) {
 
267
        i.value()->setChecked(group.groupList().contains(i.key()));
 
268
 
 
269
        KConfigGroup actionGroup = group.group(i.key());
 
270
        m_actionsConfigHash[i.key()]->setConfigGroup(actionGroup);
 
271
        m_actionsConfigHash[i.key()]->load();
 
272
    }
 
273
 
 
274
    emit changed(false);
 
275
}
 
276
 
 
277
void EditPage::saveProfile(const QString &p)
 
278
{
 
279
    if (!profilesList->currentItem() && p.isEmpty()) {
 
280
        kDebug() << "Could not perform a save operation";
 
281
        return;
 
282
    }
 
283
 
 
284
    QString profile;
 
285
 
 
286
    if (p.isEmpty()) {
 
287
        profile = profilesList->currentItem()->data(Qt::UserRole).toString();
 
288
    } else {
 
289
        profile = p;
 
290
    }
 
291
 
 
292
    KConfigGroup group(m_profilesConfig, profile);
 
293
 
 
294
    if (!group.isValid()) {
 
295
        kDebug() << "Could not perform a save operation, group is not valid!";
 
296
        return;
 
297
    }
 
298
 
 
299
    // Iterate over the possible actions
 
300
    for (QHash< QString, QCheckBox* >::const_iterator i = m_actionsHash.constBegin(); i != m_actionsHash.constEnd(); ++i) {
 
301
        if (i.value()->isChecked()) {
 
302
            // Perform the actual save
 
303
            m_actionsConfigHash[i.key()]->save();
 
304
        } else {
 
305
            // Erase the group
 
306
            group.deleteGroup(i.key());
 
307
        }
 
308
    }
 
309
 
 
310
    group.sync();
 
311
 
 
312
    // After saving, reload the config to make sure we'll pick up changes.
 
313
    m_profilesConfig.data()->reparseConfiguration();
 
314
 
 
315
    emit changed(false);
 
316
}
 
317
 
 
318
void EditPage::reloadAvailableProfiles()
 
319
{
 
320
    profilesList->clear();
 
321
 
 
322
    // Request profiles to the daemon
 
323
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
324
                                                       "org.kde.Solid.PowerManagement", "availableProfiles");
 
325
    QDBusPendingReply< StringStringMap > reply = QDBusConnection::sessionBus().asyncCall(call);
 
326
    reply.waitForFinished();
 
327
 
 
328
    if (!reply.isValid()) {
 
329
        kDebug() << "Error contacting the daemon!";
 
330
        return;
 
331
    }
 
332
 
 
333
    StringStringMap profiles = reply.value();
 
334
 
 
335
    if (profiles.isEmpty()) {
 
336
        kDebug() << "No available profiles!";
 
337
        return;
 
338
    }
 
339
 
 
340
    m_profilesConfig->reparseConfiguration();
 
341
 
 
342
    for (StringStringMap::const_iterator i = profiles.constBegin(); i != profiles.constEnd(); ++i) {
 
343
        KConfigGroup group(m_profilesConfig, i.key());
 
344
        QListWidgetItem *itm = new QListWidgetItem(KIcon(group.readEntry("icon")), i.value());
 
345
        itm->setData(Qt::UserRole, i.key());
 
346
        profilesList->addItem(itm);
 
347
    }
 
348
 
 
349
    profilesList->setCurrentRow(0);
 
350
}
 
351
 
 
352
void EditPage::deleteCurrentProfile()
 
353
{
 
354
    if (!profilesList->currentItem()) {
 
355
        return;
 
356
    }
 
357
 
 
358
    // We're deleting it, we don't care anymore
 
359
    emit changed(false);
 
360
 
 
361
    m_profilesConfig->deleteGroup(profilesList->currentItem()->data(Qt::UserRole).toString());
 
362
    m_profilesConfig->sync();
 
363
 
 
364
    // Notify the daemon
 
365
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
366
                                                       "org.kde.Solid.PowerManagement", "reparseConfiguration");
 
367
    QDBusConnection::sessionBus().asyncCall(call);
 
368
 
 
369
    reloadAvailableProfiles();
 
370
}
 
371
 
 
372
void EditPage::createProfile(const QString &name, const QString &icon)
 
373
{
 
374
    if (name.isEmpty()) {
 
375
        return;
 
376
    }
 
377
    KConfigGroup group(m_profilesConfig, name);
 
378
    group.writeEntry("icon", icon);
 
379
    group.writeEntry("name", name);
 
380
 
 
381
    group.sync();
 
382
 
 
383
    // Notify the daemon
 
384
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
385
                                                       "org.kde.Solid.PowerManagement", "reparseConfiguration");
 
386
    QDBusConnection::sessionBus().asyncCall(call);
 
387
 
 
388
    reloadAvailableProfiles();
 
389
}
 
390
 
 
391
void EditPage::createProfile()
 
392
{
 
393
    KDialog *dialog = new KDialog(this);
 
394
    QWidget *wg = new QWidget();
 
395
    KLineEdit *ed = new KLineEdit(wg);
 
396
    QLabel *lb = new QLabel(wg);
 
397
    QFormLayout *lay = new QFormLayout();
 
398
    KIconButton *ibt = new KIconButton(wg);
 
399
 
 
400
    ibt->setIconSize(KIconLoader::SizeSmall);
 
401
 
 
402
    lb->setText(i18n("Please enter a name for the new profile:"));
 
403
 
 
404
    lb->setToolTip(i18n("The name for the new profile"));
 
405
    lb->setWhatsThis(i18n("Enter here the name for the profile you are creating"));
 
406
 
 
407
    ed->setToolTip(i18n("The name for the new profile"));
 
408
    ed->setWhatsThis(i18n("Enter here the name for the profile you are creating"));
 
409
 
 
410
    lay->addRow(lb);
 
411
    lay->addRow(ibt, ed);
 
412
 
 
413
    wg->setLayout(lay);
 
414
 
 
415
    dialog->setMainWidget(wg);
 
416
    ed->setFocus();
 
417
 
 
418
    if (dialog->exec() == KDialog::Accepted) {
 
419
        createProfile(ed->text(), ibt->icon());
 
420
    }
 
421
    delete dialog;
 
422
}
 
423
 
 
424
void EditPage::editProfile(const QString &id, const QString &name, const QString &icon)
 
425
{
 
426
    if (id.isEmpty() || !m_profilesConfig->hasGroup(id)) {
 
427
        return;
 
428
    }
 
429
 
 
430
    KConfigGroup group(m_profilesConfig, id);
 
431
 
 
432
    group.writeEntry("icon", icon);
 
433
    group.writeEntry("name", name);
 
434
 
 
435
    group.sync();
 
436
 
 
437
    // Notify the daemon
 
438
    notifyDaemon(id);
 
439
 
 
440
    reloadAvailableProfiles();
 
441
}
 
442
 
 
443
void EditPage::editProfile()
 
444
{
 
445
    if (!profilesList->currentItem()) {
 
446
        return;
 
447
    }
 
448
 
 
449
    KDialog *dialog = new KDialog(this);
 
450
    QWidget *wg = new QWidget();
 
451
    KLineEdit *ed = new KLineEdit(wg);
 
452
    QLabel *lb = new QLabel(wg);
 
453
    QFormLayout *lay = new QFormLayout();
 
454
    KIconButton *ibt = new KIconButton(wg);
 
455
 
 
456
    ibt->setIconSize(KIconLoader::SizeSmall);
 
457
 
 
458
    lb->setText(i18n("Please enter a name for this profile:"));
 
459
 
 
460
    lb->setToolTip(i18n("The name for the new profile"));
 
461
    lb->setWhatsThis(i18n("Enter here the name for the profile you are creating"));
 
462
 
 
463
    ed->setToolTip(i18n("The name for the new profile"));
 
464
    ed->setWhatsThis(i18n("Enter here the name for the profile you are creating"));
 
465
 
 
466
    KConfigGroup group(m_profilesConfig, profilesList->currentItem()->data(Qt::UserRole).toString());
 
467
 
 
468
    ibt->setIcon(group.readEntry("icon"));
 
469
    ed->setText(group.readEntry("name"));
 
470
 
 
471
    lay->addRow(lb);
 
472
    lay->addRow(ibt, ed);
 
473
 
 
474
    wg->setLayout(lay);
 
475
 
 
476
    dialog->setMainWidget(wg);
 
477
    ed->setFocus();
 
478
 
 
479
    if (dialog->exec() == KDialog::Accepted) {
 
480
        editProfile(profilesList->currentItem()->data(Qt::UserRole).toString(), ed->text(), ibt->icon());
 
481
    }
 
482
 
 
483
    delete dialog;
 
484
}
 
485
 
 
486
void EditPage::importProfiles()
 
487
{
 
488
    QString fileName = KFileDialog::getOpenFileName(KUrl(), "*.kpmsprofiles|KDE Power Management System Profiles "
 
489
                       "(*.kpmsprofiles)", this, i18n("Import Power Management Profiles"));
 
490
 
 
491
    if (fileName.isEmpty()) {
 
492
        return;
 
493
    }
 
494
 
 
495
    KConfig toImport(fileName, KConfig::SimpleConfig);
 
496
 
 
497
    foreach(const QString &ent, toImport.groupList()) {
 
498
        KConfigGroup copyFrom(&toImport, ent);
 
499
        KConfigGroup copyTo(m_profilesConfig, ent);
 
500
 
 
501
        copyFrom.copyTo(&copyTo);
 
502
    }
 
503
 
 
504
    m_profilesConfig->sync();
 
505
 
 
506
    // Notify the daemon
 
507
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement",
 
508
                                                       "org.kde.Solid.PowerManagement", "reparseConfiguration");
 
509
    QDBusConnection::sessionBus().asyncCall(call);
 
510
 
 
511
    reloadAvailableProfiles();
 
512
}
 
513
 
 
514
void EditPage::exportProfiles()
 
515
{
 
516
    QString fileName = KFileDialog::getSaveFileName(KUrl(), "*.kpmsprofiles|KDE Power Management System Profiles "
 
517
                       "(*.kpmsprofiles)", this, i18n("Export Power Management Profiles"));
 
518
 
 
519
    if (fileName.isEmpty()) {
 
520
        return;
 
521
    }
 
522
 
 
523
    kDebug() << "Filename is" << fileName;
 
524
 
 
525
    KConfig *toExport = m_profilesConfig->copyTo(fileName);
 
526
 
 
527
    toExport->sync();
 
528
 
 
529
    delete toExport;
 
530
}
 
531
 
 
532
void EditPage::restoreDefaultProfiles()
 
533
{
 
534
    // Confirm
 
535
    int ret = KMessageBox::warningContinueCancel(this, i18n("The KDE Power Management System will now generate a set of default "
 
536
                                                            "profiles based on your computer's capabilities. This will also erase "
 
537
                                                            "all existing profiles. "
 
538
                                                            "Are you sure you want to continue?"), i18n("Restore Default Profiles"));
 
539
    if (ret == KMessageBox::Continue) {
 
540
        kDebug() << "Restoring defaults.";
 
541
        PowerDevil::ProfileGenerator::generateProfiles();
 
542
 
 
543
        // Notify the daemon
 
544
        notifyDaemon();
 
545
 
 
546
        reloadAvailableProfiles();
 
547
    }
 
548
}
 
549
 
 
550
void EditPage::switchProfile(QListWidgetItem *current, QListWidgetItem *previous)
 
551
{
 
552
    Q_UNUSED(current)
 
553
 
 
554
    if (!m_profileEdited) {
 
555
        kDebug() << "Profile has not been edited, switch";
 
556
        loadProfile();
 
557
    } else {
 
558
        if (!previous) {
 
559
            // Pass by, the profile has probably been deleted
 
560
            kDebug() << "No previous profile";
 
561
            loadProfile();
 
562
            return;
 
563
        } else if (!m_profilesConfig.data()->groupList().contains(previous->text())) {
 
564
            // Pass by, the profile has probably been deleted
 
565
            kDebug() << "No previous profile saved";
 
566
            loadProfile();
 
567
            return;
 
568
        }
 
569
 
 
570
        int result = KMessageBox::warningYesNoCancel(this, i18n("The current profile has not been saved.\n"
 
571
                     "Do you want to save it?"), i18n("Save Profile"));
 
572
 
 
573
        if (result == KMessageBox::Yes) {
 
574
            saveProfile(previous->data(Qt::UserRole).toString());
 
575
            loadProfile();
 
576
        } else if (result == KMessageBox::No) {
 
577
            loadProfile();
 
578
        } else if (result == KMessageBox::Cancel) {
 
579
            disconnect(profilesList, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
 
580
                       this, SLOT(switchProfile(QListWidgetItem*, QListWidgetItem*)));
 
581
            profilesList->setCurrentItem(previous);
 
582
            connect(profilesList, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
 
583
                    SLOT(switchProfile(QListWidgetItem*, QListWidgetItem*)));
 
584
        }
 
585
    }
 
586
}
 
587
 
 
588
void EditPage::openUrl(const QString &url)
 
589
{
 
590
    new KRun(KUrl(url), this);
 
591
}
 
592
 
 
593
void EditPage::defaults()
 
594
{
 
595
    restoreDefaultProfiles();
 
596
}
 
597
 
 
598
#include "EditPage.moc"