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

« back to all changes in this revision

Viewing changes to khotkeys/kcm_hotkeys/kcm_hotkeys.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 Michael Jansen <kde@michael-jansen.biz>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library 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 GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
 
 
21
#include "kcm_hotkeys.h"
 
22
#include "hotkeys_context_menu.h"
 
23
#include "ui_kcm_hotkeys.h"
 
24
#include "kcm_module_factory.h"
 
25
 
 
26
#include <typeinfo>
 
27
 
 
28
 
 
29
// ACTION_DATAS
 
30
#include "action_data/action_data_group.h"
 
31
// OUR ACTION WIDGETS
 
32
#include "action_group_widget.h"
 
33
#include "simple_action_data_widget.h"
 
34
#include "global_settings_widget.h"
 
35
// REST
 
36
#include "daemon/daemon.h"
 
37
#include "khotkeys_interface.h"
 
38
#include "hotkeys_model.h"
 
39
#include "hotkeys_proxy_model.h"
 
40
#include "hotkeys_tree_view.h"
 
41
#include "khotkeysglobal.h"
 
42
 
 
43
#include <QtDBus/QtDBus>
 
44
 
 
45
#include <KDE/KAboutData>
 
46
#include <KDE/KDebug>
 
47
#include <KDE/KLocale>
 
48
#include <KDE/KMessageBox>
 
49
#include <KDE/KPluginLoader>
 
50
 
 
51
 
 
52
 
 
53
class KCMHotkeysPrivate : public Ui::KCMHotkeysWidget
 
54
    {
 
55
    public:
 
56
 
 
57
        KCMHotkeysPrivate( KCMHotkeys *host );
 
58
 
 
59
        /** The model holding the shortcut settings. Beware! There is a proxy
 
60
         * model between us and that model */
 
61
        KHotkeysModel *model;
 
62
 
 
63
        //! Our host
 
64
        KCMHotkeys *q;
 
65
 
 
66
        //! The currently shown dialog
 
67
        HotkeysWidgetIFace *current;
 
68
 
 
69
        //! The currently shown item
 
70
        QModelIndex currentIndex;
 
71
 
 
72
        /**
 
73
         * Show the widget. If the current widget has changes allow
 
74
         * cancelation ! of this action
 
75
         */
 
76
        bool maybeShowWidget(const QModelIndex &next);
 
77
 
 
78
        /**
 
79
         * Applies the changes from the current item
 
80
         */
 
81
        void applyCurrentItem();
 
82
 
 
83
        /**
 
84
         * A Hotkey was changed. Update the treeview.
 
85
         */
 
86
        void _k_hotkeyChanged(KHotKeys::ActionDataBase*);
 
87
 
 
88
        /**
 
89
         * Activate the current item. The method is needed because of qt bug
 
90
         */
 
91
        void _k_activateCurrentItem();
 
92
 
 
93
        void load();
 
94
        void save();
 
95
    };
 
96
 
 
97
 
 
98
KCMHotkeys::KCMHotkeys( QWidget *parent, const QVariantList & /* args */ )
 
99
    : KCModule( KCMModuleFactory::componentData(), parent )
 
100
     ,d( new KCMHotkeysPrivate(this) )
 
101
    {
 
102
    // Inform KCModule of the buttons we support
 
103
    KCModule::setButtons(KCModule::Buttons(KCModule::Default | KCModule::Apply));
 
104
 
 
105
    // Add the about data
 
106
    KAboutData *about = new KAboutData(
 
107
        "khotkeys",
 
108
        0,
 
109
        ki18n("KDE Hotkeys Configuration Module"),
 
110
        KDE_VERSION_STRING,
 
111
        KLocalizedString(),
 
112
        KAboutData::License_GPL,
 
113
        ki18n("Copyright 2008 (c) Michael Jansen")
 
114
        );
 
115
    about->addAuthor(
 
116
        ki18n("Michael Jansen"),
 
117
        ki18n("Maintainer"),
 
118
        "kde@michael-jansen.biz" );
 
119
    setAboutData(about);
 
120
 
 
121
    // Tell KCModule we were changed.
 
122
    connect(
 
123
        d->action_group, SIGNAL(changed(bool)),
 
124
        this, SIGNAL(changed(bool)) );
 
125
    connect(
 
126
        d->simple_action, SIGNAL(changed(bool)),
 
127
        this, SIGNAL(changed(bool)) );
 
128
    connect(
 
129
        d->global_settings, SIGNAL(changed(bool)),
 
130
        this, SIGNAL(changed(bool)) );
 
131
    // Update TreeView if hotkeys was changed
 
132
    connect(
 
133
        d->simple_action, SIGNAL(changed(KHotKeys::ActionDataBase*)),
 
134
        this, SLOT(_k_hotkeyChanged(KHotKeys::ActionDataBase*)));
 
135
    connect(
 
136
        d->action_group, SIGNAL(changed(KHotKeys::ActionDataBase*)),
 
137
        this, SLOT(_k_hotkeyChanged(KHotKeys::ActionDataBase*)));
 
138
 
 
139
    // Show the context menu
 
140
    d->menu_button->setMenu(new HotkeysTreeViewContextMenu(d->tree_view));
 
141
 
 
142
    // Switch to the global settings dialog
 
143
    connect(d->settings_button, SIGNAL(clicked(bool)),
 
144
            SLOT(showGlobalSettings()));
 
145
    }
 
146
 
 
147
 
 
148
void KCMHotkeys::currentChanged( const QModelIndex &pCurrent, const QModelIndex &pPrevious )
 
149
    {
 
150
    // We're not interested in changes of columns. Just compare the rows
 
151
    QModelIndex current =
 
152
        pCurrent.isValid()
 
153
        ? pCurrent.sibling( pCurrent.row(), 0 )
 
154
        : QModelIndex();
 
155
    QModelIndex previous =
 
156
        pPrevious.isValid()
 
157
        ? pPrevious.sibling( pPrevious.row(), 0 )
 
158
        : QModelIndex();
 
159
 
 
160
    // Now it's possible for previous and current to be the same
 
161
    if (current==previous || current==d->currentIndex)
 
162
        {
 
163
        return;
 
164
        }
 
165
 
 
166
    // Current and previous differ. Ask user if there are unsaved changes
 
167
    if ( !d->maybeShowWidget(current) )
 
168
        {
 
169
        // Bring focus back to the current item
 
170
        d->tree_view->selectionModel()->setCurrentIndex(
 
171
                d->currentIndex,
 
172
                QItemSelectionModel::SelectCurrent);
 
173
 
 
174
        // See http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id=132516
 
175
        QTimer::singleShot(0, this, SLOT(_k_activateCurrentItem()));
 
176
        return;
 
177
        }
 
178
 
 
179
    if (!current.isValid())
 
180
        {
 
181
        return showGlobalSettings();
 
182
        }
 
183
 
 
184
    // Now go on and activate the new item;
 
185
    KHotKeys::ActionDataBase *item = d->model->indexToActionDataBase( current );
 
186
    QModelIndex typeOfIndex = d->model->index( current.row(), KHotkeysModel::TypeColumn, current.parent() );
 
187
 
 
188
    switch (d->model->data( typeOfIndex ).toInt())
 
189
        {
 
190
 
 
191
        case KHotkeysModel::SimpleActionData:
 
192
            {
 
193
            KHotKeys::SimpleActionData *data = dynamic_cast<KHotKeys::SimpleActionData*>(item);
 
194
            if (data)
 
195
                {
 
196
                d->simple_action->setActionData( data );
 
197
                d->current = d->simple_action;
 
198
                }
 
199
            }
 
200
            break;
 
201
 
 
202
        case KHotkeysModel::ActionDataGroup:
 
203
            {
 
204
            KHotKeys::ActionDataGroup *group = dynamic_cast<KHotKeys::ActionDataGroup*>(item);
 
205
            if (group)
 
206
                {
 
207
                d->action_group->setActionData( group );
 
208
                d->current = d->action_group;
 
209
                }
 
210
            }
 
211
            break;
 
212
 
 
213
        default:
 
214
            {
 
215
            const std::type_info &ti = typeid(*item);
 
216
            kDebug() << "##### Unknown ActionDataType " << ti.name();
 
217
            }
 
218
 
 
219
        } // switch
 
220
 
 
221
    d->currentIndex = current;
 
222
    d->stack->setCurrentWidget( d->current );
 
223
    }
 
224
 
 
225
 
 
226
KCMHotkeys::~KCMHotkeys()
 
227
    {
 
228
    delete d; d=0;
 
229
    }
 
230
 
 
231
 
 
232
void KCMHotkeys::defaults()
 
233
    {
 
234
    kWarning() << "not yet implemented!";
 
235
    }
 
236
 
 
237
 
 
238
void KCMHotkeys::load()
 
239
    {
 
240
    showGlobalSettings();
 
241
    d->load();
 
242
    }
 
243
 
 
244
 
 
245
void KCMHotkeys::showGlobalSettings()
 
246
    {
 
247
    d->current = d->global_settings;
 
248
    d->currentIndex = QModelIndex();
 
249
 
 
250
    d->tree_view->setCurrentIndex(d->currentIndex);
 
251
    d->global_settings->copyFromObject();
 
252
    d->stack->setCurrentWidget( d->global_settings );
 
253
    }
 
254
 
 
255
 
 
256
void KCMHotkeys::slotChanged()
 
257
    {
 
258
    emit changed(true);
 
259
    }
 
260
 
 
261
 
 
262
void KCMHotkeys::slotReset()
 
263
    {
 
264
    showGlobalSettings();
 
265
    }
 
266
 
 
267
 
 
268
void KCMHotkeys::save()
 
269
    {
 
270
    d->save();
 
271
    emit changed(false);
 
272
    }
 
273
 
 
274
 
 
275
// ==========================================================================
 
276
// KCMHotkeysPrivate
 
277
 
 
278
 
 
279
KCMHotkeysPrivate::KCMHotkeysPrivate( KCMHotkeys *host )
 
280
    : Ui::KCMHotkeysWidget()
 
281
     ,model(NULL)
 
282
     ,q(host)
 
283
     ,current(NULL)
 
284
    {
 
285
    setupUi(q);
 
286
 
 
287
    // Initialize the global part of the khotkeys lib ( handler ... )
 
288
    KHotKeys::init_global_data(false, q);
 
289
    }
 
290
 
 
291
 
 
292
void KCMHotkeysPrivate::load()
 
293
    {
 
294
    // Start khotkeys
 
295
    KHotKeys::Daemon::start();
 
296
 
 
297
    // disconnect the signals
 
298
    if (tree_view->selectionModel())
 
299
        {
 
300
        QObject::disconnect(
 
301
            tree_view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
 
302
            q, SLOT(currentChanged(QModelIndex,QModelIndex)) );
 
303
        }
 
304
 
 
305
    // Create a new model;
 
306
    tree_view->setModel(new KHotkeysModel);
 
307
    // Delete the old
 
308
    delete model;
 
309
    // Now use the old
 
310
    model = tree_view->model();
 
311
 
 
312
    model->load();
 
313
    global_settings->setModel(model);
 
314
 
 
315
    QObject::connect(
 
316
        model, SIGNAL( rowsRemoved( QModelIndex, int, int )),
 
317
        q,  SLOT( slotChanged() ));
 
318
    QObject::connect(
 
319
        model, SIGNAL( rowsInserted( QModelIndex, int, int )),
 
320
        q,  SLOT( slotChanged() ));
 
321
    QObject::connect(
 
322
        model, SIGNAL( dataChanged( QModelIndex, QModelIndex )),
 
323
        q,  SLOT( slotChanged() ));
 
324
 
 
325
    QObject::connect(
 
326
            model, SIGNAL( modelAboutToBeReset()),
 
327
            q,  SLOT( slotReset() ));
 
328
 
 
329
    // reconnect the signals
 
330
    QObject::connect(
 
331
        tree_view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
 
332
        q, SLOT(currentChanged(QModelIndex,QModelIndex)) );
 
333
    }
 
334
 
 
335
 
 
336
bool KCMHotkeysPrivate::maybeShowWidget(const QModelIndex &nextIndex)
 
337
    {
 
338
    kDebug();
 
339
 
 
340
    // If the current widget is changed, ask user if switch is ok
 
341
    if (current && (currentIndex != nextIndex) && current->isChanged())
 
342
        {
 
343
        int choice = KMessageBox::warningContinueCancel(
 
344
             q,
 
345
             i18n("The current action has unsaved changes. If you continue these changes will be lost."),
 
346
             i18n("Save changes") );
 
347
        if (choice != KMessageBox::Continue)
 
348
            {
 
349
            return false;
 
350
            }
 
351
        // Apply the changes from the current item
 
352
        //applyCurrentItem();
 
353
        //save();
 
354
        }
 
355
    return true;
 
356
    }
 
357
 
 
358
 
 
359
void KCMHotkeysPrivate::save()
 
360
    {
 
361
    if (current)
 
362
        applyCurrentItem();
 
363
 
 
364
    // Write the settings
 
365
    model->save();
 
366
 
 
367
    if (!KHotKeys::Daemon::isRunning())
 
368
        {
 
369
        if (!KHotKeys::Daemon::start())
 
370
            {
 
371
            // On startup the demon does the updating stuff, therefore reload
 
372
            // the actions.
 
373
            model->load();
 
374
            }
 
375
        else
 
376
            {
 
377
            KMessageBox::error(
 
378
                q,
 
379
                "<qt>" + i18n("Unable to contact khotkeys. Your changes are saved, but they could not be activated.") + "</qt>" );
 
380
            }
 
381
 
 
382
        return;
 
383
        }
 
384
 
 
385
    // Inform kdedkhotkeys demon to reload settings
 
386
    QDBusConnection bus = QDBusConnection::sessionBus();
 
387
    QPointer<OrgKdeKhotkeysInterface> iface = new OrgKdeKhotkeysInterface(
 
388
        "org.kde.kded",
 
389
        "/modules/khotkeys",
 
390
        bus,
 
391
        q);
 
392
 
 
393
    QDBusError err;
 
394
    if(!iface->isValid())
 
395
        {
 
396
        err = iface->lastError();
 
397
        if (err.isValid())
 
398
            {
 
399
            kError() << err.name() << ":" << err.message();
 
400
            }
 
401
        KMessageBox::error(
 
402
            q,
 
403
            "<qt>" + i18n("Unable to contact khotkeys. Your changes are saved, but they could not be activated.") + "</qt>" );
 
404
        return;
 
405
        }
 
406
 
 
407
    // Reread the configuration. We have no possibility to check if it worked.
 
408
    iface->reread_configuration();
 
409
    }
 
410
 
 
411
 
 
412
void KCMHotkeysPrivate::applyCurrentItem()
 
413
    {
 
414
    Q_ASSERT( current );
 
415
    // Only save when really changed
 
416
    if (current->isChanged())
 
417
        {
 
418
        current->apply();
 
419
        }
 
420
    }
 
421
 
 
422
 
 
423
void KCMHotkeysPrivate::_k_hotkeyChanged(KHotKeys::ActionDataBase* hotkey)
 
424
    {
 
425
    model->emitChanged(hotkey);
 
426
    }
 
427
 
 
428
 
 
429
void KCMHotkeysPrivate::_k_activateCurrentItem()
 
430
    {
 
431
    tree_view->selectionModel()->setCurrentIndex(
 
432
            tree_view->selectionModel()->currentIndex(),
 
433
            QItemSelectionModel::SelectCurrent);
 
434
    }
 
435
 
 
436
#include "moc_kcm_hotkeys.cpp"