~ubuntu-branches/ubuntu/quantal/kpackagekit/quantal

« back to all changes in this revision

Viewing changes to KPackageKit/kcmultidialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-01-10 17:20:02 UTC
  • mfrom: (1.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110110172002-64acog2s0tk4iav9
Tags: 0.6.3.3-0ubuntu1
* New upstream release
  - Refresh kubuntu_06_no_automatic_updates.diff
  - Drop kubuntu_08_updates_info.diff, applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   Copyright (c) 2000 Matthias Elter <elter@kde.org>
3
 
   Copyright (c) 2003 Daniel Molkentin <molkentin@kde.org>
4
 
   Copyright (c) 2003,2006 Matthias Kretz <kretz@kde.org>
5
 
   Copyright (c) 2004 Frans Englich <frans.englich@telia.com>
6
 
   Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
7
 
 
8
 
   This library is free software; you can redistribute it and/or
9
 
   modify it under the terms of the GNU Library General Public
10
 
   License as published by the Free Software Foundation; either
11
 
   version 2 of the License, or (at your option) any later version.
12
 
 
13
 
   This library is distributed in the hope that it will be useful,
14
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
   Library General Public License for more details.
17
 
 
18
 
   You should have received a copy of the GNU Library General Public License
19
 
   along with this library; see the file COPYING.LIB.  If not, write to
20
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
 
   Boston, MA 02110-1301, USA.
22
 
 
23
 
*/
24
 
 
25
 
#include "kcmultidialog.h"
26
 
#include "kcmultidialog_p.h"
27
 
 
28
 
#include <QtCore/QStringList>
29
 
#include <QtCore/QProcess>
30
 
 
31
 
#include <kauthorized.h>
32
 
#include <kguiitem.h>
33
 
#include <khbox.h>
34
 
#include <kicon.h>
35
 
#include <klocale.h>
36
 
#include <kpagewidgetmodel.h>
37
 
#include <kpushbutton.h>
38
 
#include <ktoolinvocation.h>
39
 
#include <kdebug.h>
40
 
#include <kmessagebox.h>
41
 
 
42
 
#include "auth/kauthaction.h"
43
 
 
44
 
#include "kcolorscheme.h"
45
 
 
46
 
#include "kcmoduleloader.h"
47
 
#include "kcmoduleproxy.h"
48
 
 
49
 
bool KCMultiDialogPrivate::resolveChanges(KCModuleProxy *currentProxy)
50
 
{
51
 
    Q_Q(KCMultiDialog);
52
 
    if( !currentProxy || !currentProxy->changed() ) {
53
 
        return true;
54
 
    }
55
 
 
56
 
    // Let the user decide
57
 
    const int queryUser = KMessageBox::warningYesNoCancel(
58
 
        q,
59
 
        i18n("The settings of the current module have changed.\n"
60
 
             "Do you want to apply the changes or discard them?"),
61
 
        i18n("Apply Settings"),
62
 
        KStandardGuiItem::apply(),
63
 
        KStandardGuiItem::discard(),
64
 
        KStandardGuiItem::cancel());
65
 
 
66
 
    switch (queryUser) {
67
 
        case KMessageBox::Yes:
68
 
            return moduleSave(currentProxy);
69
 
 
70
 
        case KMessageBox::No:
71
 
            currentProxy->load();
72
 
            return true;
73
 
 
74
 
        case KMessageBox::Cancel:
75
 
            return false;
76
 
 
77
 
        default:
78
 
            Q_ASSERT(false);
79
 
            return false;
80
 
    }
81
 
}
82
 
 
83
 
void KCMultiDialogPrivate::_k_slotCurrentPageChanged( KPageWidgetItem *current, KPageWidgetItem *previous )
84
 
{
85
 
    Q_Q(KCMultiDialog);
86
 
    kDebug(710);
87
 
 
88
 
    q->blockSignals(true);
89
 
    q->setCurrentPage(previous);
90
 
 
91
 
    KCModuleProxy *previousModule = 0;
92
 
    for ( int i = 0; i < modules.count(); ++i ) {
93
 
        if ( modules[ i ].item == previous ) {
94
 
            previousModule = modules[ i ].kcm;
95
 
            break;
96
 
        }
97
 
    }
98
 
 
99
 
    KCModuleProxy *currentModule = 0;
100
 
    for ( int i = 0; i < modules.count(); ++i ) {
101
 
        if ( modules[ i ].item == current ) {
102
 
            currentModule = modules[ i ].kcm;
103
 
            break;
104
 
        }
105
 
    }
106
 
 
107
 
    if ( !previousModule || !currentModule ) {
108
 
        q->blockSignals(false);
109
 
        return;
110
 
    }
111
 
 
112
 
    if( resolveChanges(previousModule) ) {
113
 
        q->setCurrentPage(current);
114
 
    }
115
 
    q->blockSignals(false);
116
 
 
117
 
    // We need to get the state of the now active module
118
 
    _k_clientChanged();
119
 
}
120
 
 
121
 
void KCMultiDialogPrivate::_k_clientChanged()
122
 
{
123
 
    Q_Q(KCMultiDialog);
124
 
    kDebug(710);
125
 
    // Get the current module
126
 
    KCModuleProxy *activeModule = 0;
127
 
    for ( int i = 0; i < modules.count(); ++i ) {
128
 
        if ( modules[ i ].item == q->currentPage() ) {
129
 
            activeModule = modules[ i ].kcm;
130
 
            break;
131
 
        }
132
 
    }
133
 
 
134
 
    bool change = false;
135
 
    if (activeModule) {
136
 
        change = activeModule->changed();
137
 
 
138
 
        if (q->button(KDialog::Apply)) {
139
 
            q->disconnect(q, SIGNAL(applyClicked()), q, SLOT(slotApplyClicked()));
140
 
            q->disconnect(q->button(KDialog::Apply), SIGNAL(authorized(KAuth::Action*)), q, SLOT(slotApplyClicked()));
141
 
            q->button(KDialog::Apply)->setEnabled(change);
142
 
        }
143
 
 
144
 
        if (q->button(KDialog::Ok)) {
145
 
            q->disconnect(q, SIGNAL(okClicked()), q, SLOT(slotOkClicked()));
146
 
            q->disconnect(q->button(KDialog::Ok), SIGNAL(authorized(KAuth::Action*)), q, SLOT(slotOkClicked()));
147
 
        }
148
 
 
149
 
        if (activeModule->realModule()->needsAuthorization()) {
150
 
            if (q->button(KDialog::Apply)) {
151
 
                q->button(KDialog::Apply)->setAuthAction(currentModule->realModule()->authAction());
152
 
                q->connect(q->button(KDialog::Apply), SIGNAL(authorized(KAuth::Action*)), SLOT(slotApplyClicked()));
153
 
            }
154
 
 
155
 
            if (q->button(KDialog::Ok)) {
156
 
                q->button(KDialog::Ok)->setAuthAction(currentModule->realModule()->authAction());
157
 
                q->connect(q->button(KDialog::Ok), SIGNAL(authorized(KAuth::Action*)), SLOT(slotOkClicked()));
158
 
            }
159
 
        } else {
160
 
            if (q->button(KDialog::Apply)) {
161
 
                q->connect(q, SIGNAL(applyClicked()), SLOT(slotApplyClicked()));
162
 
                q->button(KDialog::Apply)->setAuthAction(activeModule->realModule()->authAction());
163
 
            }
164
 
 
165
 
            if (q->button(KDialog::Ok)) {
166
 
                q->connect(q, SIGNAL(okClicked()), SLOT(slotOkClicked()));
167
 
                q->button(KDialog::Ok)->setAuthAction(activeModule->realModule()->authAction());
168
 
            }
169
 
        }
170
 
    }
171
 
 
172
 
    if (q->button(KDialog::Reset)) {
173
 
        q->button(KDialog::Reset)->setEnabled(change);
174
 
    }
175
 
 
176
 
    if (q->button(KDialog::Apply)) {
177
 
        q->button(KDialog::Apply)->setEnabled(change);
178
 
    }
179
 
 
180
 
    q->enableButton(KDialog::Help,    activeModule->buttons() & KCModule::Help);
181
 
    q->enableButton(KDialog::Default, activeModule->buttons() & KCModule::Default);
182
 
}
183
 
 
184
 
void KCMultiDialogPrivate::_k_updateHeader(bool use, const QString &message)
185
 
{
186
 
    Q_Q(KCMultiDialog);
187
 
    KPageWidgetItem *item = q->currentPage();
188
 
    KCModuleProxy *kcm = qobject_cast<KCModuleProxy*>(item->widget());
189
 
 
190
 
    if (use) {
191
 
        item->setHeader( "<b>"+kcm->moduleInfo().comment() + "</b><br><i>" +
192
 
                         message + "</i>" );
193
 
        item->setIcon( KIcon( kcm->moduleInfo().icon(), 0, QStringList() << "dialog-warning" ) );
194
 
    } else {
195
 
        item->setHeader( kcm->moduleInfo().comment() );
196
 
        item->setIcon( KIcon( kcm->moduleInfo().icon() ) );
197
 
    }
198
 
}
199
 
 
200
 
void KCMultiDialogPrivate::_k_dialogClosed()
201
 
{
202
 
  kDebug(710) ;
203
 
 
204
 
  /**
205
 
   * If we don't delete them, the DBUS registration stays, and trying to load the KCMs
206
 
   * in other situations will lead to "module already loaded in Foo," while to the user
207
 
   * doesn't appear so(the dialog is hidden)
208
 
   */
209
 
  for ( int i = 0; i < modules.count(); ++i )
210
 
    modules[ i ].kcm->deleteClient();
211
 
}
212
 
 
213
 
void KCMultiDialogPrivate::init()
214
 
{
215
 
    Q_Q(KCMultiDialog);
216
 
    q->setFaceType(KPageDialog::Auto);
217
 
    q->setCaption(i18n("Configure"));
218
 
    q->setButtons(KDialog::Help | KDialog::Default |KDialog::Cancel | KDialog::Apply | KDialog::Ok | KDialog::Reset);
219
 
 
220
 
    q->setModal(false);
221
 
 
222
 
    q->connect(q, SIGNAL(finished()), SLOT(_k_dialogClosed()));
223
 
    q->connect(q, SIGNAL(applyClicked()), SLOT(slotApplyClicked()));
224
 
    q->connect(q, SIGNAL(okClicked()), SLOT(slotOkClicked()));
225
 
    q->connect(q, SIGNAL(defaultClicked()), SLOT(slotDefaultClicked()));
226
 
    q->connect(q, SIGNAL(helpClicked()), SLOT(slotHelpClicked()));
227
 
    q->connect(q, SIGNAL(user1Clicked()), SLOT(slotUser1Clicked()));
228
 
    q->connect(q, SIGNAL(resetClicked()), SLOT(slotUser1Clicked()));
229
 
 
230
 
    q->connect(q, SIGNAL(currentPageChanged(KPageWidgetItem*, KPageWidgetItem*)),
231
 
            SLOT(_k_slotCurrentPageChanged(KPageWidgetItem*, KPageWidgetItem*)));
232
 
 
233
 
    q->setInitialSize(QSize(800, 550));
234
 
}
235
 
 
236
 
KCMultiDialog::KCMultiDialog( QWidget *parent )
237
 
    : KPageDialog(*new KCMultiDialogPrivate, NULL, parent)
238
 
{
239
 
    d_func()->init();
240
 
}
241
 
 
242
 
KCMultiDialog::KCMultiDialog(KPageWidget *pageWidget, QWidget *parent, Qt::WFlags flags)
243
 
    : KPageDialog(*new KCMultiDialogPrivate, pageWidget, parent, flags)
244
 
{
245
 
    d_func()->init();
246
 
}
247
 
 
248
 
KCMultiDialog::KCMultiDialog(KCMultiDialogPrivate &dd, KPageWidget *pageWidget, QWidget *parent, Qt::WFlags flags)
249
 
    : KPageDialog(dd, pageWidget, parent, flags)
250
 
{
251
 
    d_func()->init();
252
 
}
253
 
 
254
 
KCMultiDialog::~KCMultiDialog()
255
 
{
256
 
}
257
 
 
258
 
void KCMultiDialog::slotDefaultClicked()
259
 
{
260
 
    Q_D(KCMultiDialog);
261
 
  const KPageWidgetItem *item = currentPage();
262
 
  if ( !item )
263
 
    return;
264
 
 
265
 
  for ( int i = 0; i < d->modules.count(); ++i ) {
266
 
    if ( d->modules[ i ].item == item ) {
267
 
      d->modules[ i ].kcm->defaults();
268
 
            d->_k_clientChanged();
269
 
      return;
270
 
    }
271
 
  }
272
 
}
273
 
 
274
 
void KCMultiDialog::slotUser1Clicked()
275
 
{
276
 
  const KPageWidgetItem *item = currentPage();
277
 
  if ( !item )
278
 
    return;
279
 
 
280
 
    Q_D(KCMultiDialog);
281
 
  for ( int i = 0; i < d->modules.count(); ++i ) {
282
 
    if ( d->modules[ i ].item == item ) {
283
 
      d->modules[ i ].kcm->load();
284
 
            d->_k_clientChanged();
285
 
      return;
286
 
    }
287
 
  }
288
 
}
289
 
 
290
 
bool KCMultiDialogPrivate::moduleSave(KCModuleProxy *module)
291
 
{
292
 
    if( !module ) {
293
 
        return false;
294
 
    }
295
 
 
296
 
    module->save();
297
 
    return true;
298
 
}
299
 
 
300
 
void KCMultiDialogPrivate::apply()
301
 
{
302
 
    Q_Q(KCMultiDialog);
303
 
    QStringList updatedComponents;
304
 
 
305
 
    foreach (const CreatedModule &module, modules) {
306
 
        KCModuleProxy *proxy = module.kcm;
307
 
 
308
 
        if (proxy->changed()) {
309
 
            proxy->save();
310
 
            /**
311
 
                * Add name of the components the kcm belongs to the list
312
 
                * of updated components.
313
 
                */
314
 
            const QStringList componentNames = module.componentNames;
315
 
            foreach (const QString &componentName, module.componentNames) {
316
 
                if (!updatedComponents.contains(componentName)) {
317
 
                    updatedComponents.append(componentName);
318
 
                }
319
 
            }
320
 
        }
321
 
    }
322
 
 
323
 
    // Send the configCommitted signal for every updated component.
324
 
    foreach (const QString &name, updatedComponents) {
325
 
        emit q->configCommitted(name.toLatin1());
326
 
    }
327
 
 
328
 
    emit q->configCommitted();
329
 
}
330
 
 
331
 
void KCMultiDialog::slotApplyClicked()
332
 
{
333
 
  setButtonFocus( Apply );
334
 
 
335
 
    d_func()->apply();
336
 
}
337
 
 
338
 
 
339
 
void KCMultiDialog::slotOkClicked()
340
 
{
341
 
  setButtonFocus( Ok );
342
 
 
343
 
    d_func()->apply();
344
 
  accept();
345
 
}
346
 
 
347
 
void KCMultiDialog::slotHelpClicked()
348
 
{
349
 
  const KPageWidgetItem *item = currentPage();
350
 
  if ( !item )
351
 
    return;
352
 
 
353
 
    Q_D(KCMultiDialog);
354
 
  QString docPath;
355
 
  for ( int i = 0; i < d->modules.count(); ++i ) {
356
 
    if ( d->modules[ i ].item == item ) {
357
 
      docPath = d->modules[ i ].kcm->moduleInfo().docPath();
358
 
      break;
359
 
    }
360
 
  }
361
 
 
362
 
  KUrl docUrl( KUrl( "help:/" ), docPath );
363
 
  if ( docUrl.protocol() == "help" || docUrl.protocol() == "man" || docUrl.protocol() == "info" ) {
364
 
    QProcess::startDetached("khelpcenter", QStringList() << docUrl.url());
365
 
  } else {
366
 
    KToolInvocation::invokeBrowser( docUrl.url() );
367
 
  }
368
 
}
369
 
 
370
 
 
371
 
KPageWidgetItem* KCMultiDialog::addModule( const QString& path, const QStringList& args )
372
 
{
373
 
  QString complete = path;
374
 
 
375
 
  if ( !path.endsWith( QLatin1String(".desktop") ) )
376
 
    complete += ".desktop";
377
 
 
378
 
  KService::Ptr service = KService::serviceByStorageId( complete );
379
 
 
380
 
  return addModule( KCModuleInfo( service ), 0, args );
381
 
}
382
 
 
383
 
KPageWidgetItem* KCMultiDialog::addModule( const KCModuleInfo& moduleInfo,
384
 
                                           KPageWidgetItem *parentItem, const QStringList& args )
385
 
{
386
 
  if ( !moduleInfo.service() )
387
 
    return 0;
388
 
 
389
 
  //KAuthorized::authorizeControlModule( moduleInfo.service()->menuId() ) is
390
 
  //checked in noDisplay already
391
 
  if ( moduleInfo.service()->noDisplay() )
392
 
    return 0;
393
 
 
394
 
    KCModuleProxy *kcm = new KCModuleProxy(moduleInfo, 0, args);
395
 
 
396
 
    kDebug(710) << moduleInfo.moduleName();
397
 
    KPageWidgetItem *item = new KPageWidgetItem(kcm, moduleInfo.moduleName());
398
 
 
399
 
    if (kcm->useRootOnlyMessage()) {
400
 
        item->setHeader( "<b>"+moduleInfo.comment() + "</b><br><i>" + kcm->rootOnlyMessage() + "</i>" );
401
 
        item->setIcon( KIcon( moduleInfo.icon(), 0, QStringList() << "dialog-warning" ) );
402
 
    } else {
403
 
        item->setHeader( moduleInfo.comment() );
404
 
        item->setIcon( KIcon( moduleInfo.icon() ) );
405
 
    }
406
 
    item->setProperty("_k_weight", moduleInfo.weight());
407
 
 
408
 
    bool updateCurrentPage = false;
409
 
    const KPageWidgetModel *model = qobject_cast<const KPageWidgetModel *>(pageWidget()->model());
410
 
    Q_ASSERT(model);
411
 
    if (parentItem) {
412
 
        const QModelIndex parentIndex = model->index(parentItem);
413
 
        const int siblingCount = model->rowCount(parentIndex);
414
 
        int row = 0;
415
 
        for (; row < siblingCount; ++row) {
416
 
            KPageWidgetItem *siblingItem = model->item(parentIndex.child(row, 0));
417
 
            if (siblingItem->property("_k_weight").toInt() > moduleInfo.weight()) {
418
 
                // the item we found is heavier than the new module
419
 
                kDebug(710) << "adding KCM " << item->name() << " before " << siblingItem->name();
420
 
                insertPage(siblingItem, item);
421
 
                break;
422
 
            }
423
 
        }
424
 
        if (row >= siblingCount) {
425
 
            // the new module is either the first or the heaviest item
426
 
            kDebug(710) << "adding KCM " << item->name() << " with parent " << parentItem->name();
427
 
            addSubPage(parentItem, item);
428
 
        }
429
 
    } else {
430
 
        const int siblingCount = model->rowCount();
431
 
        int row = 0;
432
 
        for (; row < siblingCount; ++row) {
433
 
            KPageWidgetItem *siblingItem = model->item(model->index(row, 0));
434
 
            if (siblingItem->property("_k_weight").toInt() > moduleInfo.weight()) {
435
 
                // the item we found is heavier than the new module
436
 
                kDebug(710) << "adding KCM " << item->name() << " before " << siblingItem->name();
437
 
                insertPage(siblingItem, item);
438
 
                if ( siblingItem == currentPage() )
439
 
                    updateCurrentPage = true;
440
 
 
441
 
                break;
442
 
            }
443
 
        }
444
 
        if (row == siblingCount) {
445
 
            // the new module is either the first or the heaviest item
446
 
            kDebug(710) << "adding KCM " << item->name() << " at the top level";
447
 
            addPage(item);
448
 
        }
449
 
    }
450
 
 
451
 
    connect(kcm, SIGNAL(changed(bool)), this, SLOT(_k_clientChanged()));
452
 
    connect(kcm->realModule(), SIGNAL(rootOnlyMessageChanged(bool,QString)), this, SLOT(_k_updateHeader(bool,QString)));
453
 
 
454
 
    Q_D(KCMultiDialog);
455
 
  KCMultiDialogPrivate::CreatedModule cm;
456
 
  cm.kcm = kcm;
457
 
  cm.item = item;
458
 
  cm.componentNames = moduleInfo.service()->property( "X-KDE-ParentComponents" ).toStringList();
459
 
  d->modules.append( cm );
460
 
 
461
 
  if ( d->modules.count() == 1 || updateCurrentPage )
462
 
  {
463
 
    setCurrentPage( item );
464
 
    d->_k_clientChanged();
465
 
  }
466
 
  return item;
467
 
}
468
 
 
469
 
void KCMultiDialog::clear()
470
 
{
471
 
    Q_D(KCMultiDialog);
472
 
  kDebug( 710 ) ;
473
 
 
474
 
  for ( int i = 0; i < d->modules.count(); ++i ) {
475
 
    removePage( d->modules[ i ].item );
476
 
    delete d->modules[ i ].kcm;
477
 
  }
478
 
 
479
 
  d->modules.clear();
480
 
 
481
 
    d->_k_clientChanged();
482
 
}
483
 
 
484
 
void KCMultiDialog::setButtons(ButtonCodes buttonMask)
485
 
{
486
 
    KPageDialog::setButtons(buttonMask);
487
 
 
488
 
    // Set Auto-Default mode ( KDE Bug #211187 )
489
 
    if (buttonMask & KDialog::Ok) {
490
 
        button(KDialog::Ok)->setAutoDefault(true);
491
 
    }
492
 
    if (buttonMask & KDialog::Apply) {
493
 
        button(KDialog::Apply)->setAutoDefault(true);
494
 
    }
495
 
    if (buttonMask & KDialog::Default) {
496
 
        button(KDialog::Default)->setAutoDefault(true);
497
 
    }
498
 
    if (buttonMask & KDialog::Reset) {
499
 
        button(KDialog::Reset)->setAutoDefault(true);
500
 
    }
501
 
    if (buttonMask & KDialog::Cancel) {
502
 
        button(KDialog::Cancel)->setAutoDefault(true);
503
 
    }
504
 
    if (buttonMask & KDialog::Help) {
505
 
        button(KDialog::Help)->setAutoDefault(true);
506
 
    }
507
 
 
508
 
    // Old Reset Button
509
 
    enableButton(KDialog::User1, false);
510
 
    enableButton(KDialog::Reset, false);
511
 
    enableButton(KDialog::Apply, false);
512
 
}
513
 
 
514
 
 
515
 
#include "kcmultidialog.moc"