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

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/settings_writer.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) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
3
 * Copyright (C) 2009 Michael Jansen <kde@michael-jansen.biz>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License version 2 as published by the Free Software Foundation.
 
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 "settings_writer.h"
 
22
 
 
23
#include "action_data/action_data_group.h"
 
24
#include "action_data/action_data.h"
 
25
#include "action_data/generic_action_data.h"
 
26
#include "action_data/menuentry_shortcut_action_data.h"
 
27
#include "action_data/simple_action_data.h"
 
28
 
 
29
#include "conditions/conditions_list.h"
 
30
 
 
31
#include "settings.h"
 
32
#include "windows_helper/window_selection_list.h"
 
33
 
 
34
 
 
35
#include <KDE/KConfig>
 
36
#include <KDE/KConfigGroup>
 
37
#include <KDE/KDebug>
 
38
 
 
39
namespace KHotKeys {
 
40
 
 
41
const int CurrentFileVersion = 2;
 
42
 
 
43
 
 
44
SettingsWriter::SettingsWriter(
 
45
            const Settings *settings,
 
46
            ActionState state,
 
47
            const QString &id,
 
48
            bool allowMerging)
 
49
    :   _settings(settings)
 
50
        ,_state(state)
 
51
        ,_importId(id)
 
52
        ,_allowMerging(allowMerging)
 
53
        ,_export(false)
 
54
    {
 
55
    }
 
56
 
 
57
 
 
58
void SettingsWriter::exportTo(
 
59
        const ActionDataBase *element,
 
60
        KConfigBase &config)
 
61
    {
 
62
    _export = true;
 
63
 
 
64
    if (!element)
 
65
        {
 
66
        Q_ASSERT(element);
 
67
        return;
 
68
        }
 
69
 
 
70
    // Clean the file
 
71
    QStringList groups = config.groupList();
 
72
    Q_FOREACH (const QString &name, config.groupList())
 
73
        {
 
74
        config.deleteGroup(name);
 
75
        }
 
76
 
 
77
    KConfigGroup mainGroup(&config, "Main");
 
78
    mainGroup.writeEntry("Version", CurrentFileVersion);
 
79
    mainGroup.writeEntry("AllowMerge", _allowMerging);
 
80
 
 
81
    if (!_importId.isEmpty()) mainGroup.writeEntry("ImportId", _importId);
 
82
 
 
83
    // The root group contains nothing but the datacount!
 
84
    KConfigGroup dataGroup(&config,  "Data");
 
85
    dataGroup.writeEntry("DataCount", 1);
 
86
 
 
87
    // The group for the element to export
 
88
    KConfigGroup data1Group(&config,  "Data_1");
 
89
    _stack.push(&data1Group);
 
90
    element->accept(this);
 
91
    _stack.pop();
 
92
 
 
93
    _export = false;
 
94
    }
 
95
 
 
96
 
 
97
void SettingsWriter::visitActionData(const ActionData *data)
 
98
    {
 
99
    visitActionDataBase(data);
 
100
 
 
101
    KConfigGroup *config = _stack.top();
 
102
 
 
103
    // Write triggers if available
 
104
    if (data->triggers())
 
105
        {
 
106
        KConfigGroup triggersGroup( config->config(), config->name() + "Triggers" );
 
107
        data->triggers()->cfg_write( triggersGroup );
 
108
        }
 
109
 
 
110
    // Write actions if available
 
111
    if (data->actions())
 
112
        {
 
113
        KConfigGroup actionsGroup( config->config(), config->name() + "Actions" );
 
114
        data->actions()->cfg_write( actionsGroup );
 
115
        }
 
116
    }
 
117
 
 
118
 
 
119
void SettingsWriter::visitActionDataBase(const ActionDataBase *base)
 
120
    {
 
121
    KConfigGroup *config = _stack.top();
 
122
 
 
123
    config->writeEntry( "Type",    "ERROR" ); // derived classes should call with their type
 
124
    config->writeEntry( "Name",    base->name());
 
125
    config->writeEntry( "Comment", base->comment());
 
126
 
 
127
    switch (_state)
 
128
        {
 
129
        case KHotKeys::Retain:
 
130
            config->writeEntry( "Enabled", base->isEnabled(ActionDataBase::Ignore));
 
131
            break;
 
132
 
 
133
        case KHotKeys::Enabled:
 
134
            config->writeEntry("Enabled", true);
 
135
            break;
 
136
 
 
137
        case KHotKeys::Disabled:
 
138
            config->writeEntry("Enabled", false);
 
139
            break;
 
140
 
 
141
        default:
 
142
            Q_ASSERT(false);
 
143
            config->writeEntry("Enabled", false);
 
144
        }
 
145
 
 
146
    if (base->conditions())
 
147
        {
 
148
        KConfigGroup conditionsConfig( config->config(), config->name() + "Conditions" );
 
149
        base->conditions()->cfg_write( conditionsConfig );
 
150
        }
 
151
    else
 
152
        {
 
153
        kDebug() << "No conditions";
 
154
        }
 
155
    }
 
156
 
 
157
void SettingsWriter::visitActionDataGroup(const ActionDataGroup *group)
 
158
    {
 
159
    visitActionDataBase(group);
 
160
 
 
161
    KConfigGroup *config = _stack.top();
 
162
 
 
163
    config->writeEntry( "SystemGroup", int(group->system_group()));
 
164
    config->writeEntry( "Type", "ACTION_DATA_GROUP" );
 
165
 
 
166
    int cnt = 0;
 
167
    Q_FOREACH(ActionDataBase *child, group->children())
 
168
        {
 
169
        ++cnt;
 
170
        KConfigGroup childConfig(
 
171
                config->config(),
 
172
                config->name() + QString("_") + QString::number(cnt));
 
173
        _stack.push(&childConfig);
 
174
        child->accept(this);
 
175
        _stack.pop();
 
176
        }
 
177
    config->writeEntry( "DataCount", cnt );
 
178
 
 
179
    // We only write those two back if we do not export the settings
 
180
    if (!_export)
 
181
        {
 
182
        // ImportId only if set
 
183
        if (!group->importId().isEmpty())
 
184
            config->writeEntry("ImportId", group->importId());
 
185
        if (group->allowMerging())
 
186
            config->writeEntry("AllowMerge", group->allowMerging());
 
187
        }
 
188
 
 
189
    }
 
190
 
 
191
 
 
192
void SettingsWriter::visitGenericActionData(const Generic_action_data *data)
 
193
    {
 
194
    visitActionData(data);
 
195
 
 
196
    KConfigGroup *config = _stack.top();
 
197
    config->writeEntry( "Type", "GENERIC_ACTION_DATA" );
 
198
    }
 
199
 
 
200
 
 
201
void SettingsWriter::visitMenuentryShortcutActionData(const MenuEntryShortcutActionData *data)
 
202
    {
 
203
    visitActionData(data);
 
204
 
 
205
    KConfigGroup *config = _stack.top();
 
206
    config->writeEntry( "Type", "MENUENTRY_SHORTCUT_ACTION_DATA" );
 
207
    }
 
208
 
 
209
 
 
210
void SettingsWriter::visitSimpleActionData(const SimpleActionData *data)
 
211
    {
 
212
    visitActionData(data);
 
213
 
 
214
    KConfigGroup *config = _stack.top();
 
215
    config->writeEntry( "Type", "SIMPLE_ACTION_DATA" );
 
216
    }
 
217
 
 
218
 
 
219
void SettingsWriter::writeTo(KConfigBase &config)
 
220
    {
 
221
    QStringList groups = config.groupList();
 
222
    for( QStringList::ConstIterator it = groups.constBegin();
 
223
         it != groups.constEnd();
 
224
         ++it )
 
225
        config.deleteGroup( *it );
 
226
 
 
227
    // Write the global settings
 
228
    KConfigGroup mainGroup(&config, "Main");
 
229
    mainGroup.writeEntry("Version", 2);
 
230
    mainGroup.writeEntry("AlreadyImported", _settings->already_imported);
 
231
    mainGroup.writeEntry("Disabled", _settings->isDaemonDisabled());
 
232
 
 
233
    // Write the actions
 
234
    KConfigGroup dataGroup( &config,  "Data" );
 
235
    _stack.push(&dataGroup);
 
236
 
 
237
    int cnt = 0;
 
238
    if (_settings->actions())
 
239
        {
 
240
        Q_FOREACH(ActionDataBase *child, _settings->actions()->children())
 
241
            {
 
242
            ++cnt;
 
243
            KConfigGroup childConfig(
 
244
                    dataGroup.config(),
 
245
                    QString("Data_") + QString::number(cnt));
 
246
            _stack.push(&childConfig);
 
247
            child->accept(this);
 
248
            _stack.pop();
 
249
            }
 
250
        }
 
251
    dataGroup.writeEntry( "DataCount", cnt );
 
252
    _stack.pop();
 
253
 
 
254
    // CHECKME Count still needed????
 
255
    // int cnt = write_actions( dataGroup, _settings->actions(), true );
 
256
    // mainGroup.writeEntry( "Autostart", cnt != 0 && !_settings->isDaemonDisabled() );
 
257
 
 
258
    // Write the gestures configuration
 
259
    KConfigGroup gesturesConfig( &config, "Gestures" );
 
260
    gesturesConfig.writeEntry( "Disabled", _settings->areGesturesDisabled() );
 
261
    gesturesConfig.writeEntry( "MouseButton", _settings->gestureMouseButton() );
 
262
    gesturesConfig.writeEntry( "Timeout", _settings->gestureTimeOut() );
 
263
    if( _settings->gesturesExclude() != NULL )
 
264
        {
 
265
        KConfigGroup gesturesExcludeConfig( &config, "GesturesExclude" );
 
266
        _settings->gesturesExclude()->cfg_write( gesturesExcludeConfig );
 
267
        }
 
268
    else
 
269
        config.deleteGroup( "GesturesExclude" );
 
270
    KConfigGroup voiceConfig( &config, "Voice" );
 
271
    voiceConfig.writeEntry("Shortcut" , _settings->voice_shortcut.toString() );
 
272
    }
 
273
 
 
274
 
 
275
} // namespace KHotKeys
 
276