~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to khotkeys/shared/settings.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 
 
3
 KHotKeys
 
4
 
 
5
 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
6
 
 
7
 Distributed under the terms of the GNU General Public License version 2.
 
8
 
 
9
****************************************************************************/
 
10
 
 
11
#define _SETTINGS_CPP_
 
12
 
 
13
#include "settings.h"
 
14
 
 
15
#include <kconfig.h>
 
16
#include <kdebug.h>
 
17
#include <klocale.h>
 
18
#include <kglobal.h>
 
19
#include <kmessagebox.h>
 
20
 
 
21
#include "triggers.h"
 
22
#include "conditions.h"
 
23
#include "action_data.h"
 
24
 
 
25
namespace KHotKeys
 
26
{
 
27
 
 
28
// Settings
 
29
 
 
30
Settings::Settings()
 
31
    : actions( NULL ), gestures_exclude( NULL )
 
32
    {
 
33
    }
 
34
 
 
35
bool Settings::read_settings( bool include_disabled_P )
 
36
    {
 
37
    KConfig cfg( KHOTKEYS_CONFIG_FILE );
 
38
    return read_settings( cfg, include_disabled_P, ImportNone );
 
39
    }
 
40
 
 
41
bool Settings::import( KConfig& cfg_P, bool ask_P )
 
42
    {
 
43
    return read_settings( cfg_P, true, ask_P ? ImportAsk : ImportSilent );
 
44
    }
 
45
 
 
46
bool Settings::read_settings( KConfig& cfg_P, bool include_disabled_P, ImportType import_P )
 
47
    {
 
48
    if( actions == NULL )
 
49
        actions = new Action_data_group( NULL, "should never see", "should never see",
 
50
            NULL, Action_data_group::SYSTEM_ROOT, true );
 
51
    if( cfg_P.groupList().count() == 0 ) // empty
 
52
        return false;
 
53
    KConfigGroup mainGroup( &cfg_P, "Main" ); // main group
 
54
    if( import_P == ImportNone ) // reading main cfg file
 
55
        already_imported = mainGroup.readEntry( "AlreadyImported",QStringList() );
 
56
    else
 
57
        {
 
58
        QString import_id = mainGroup.readEntry( "ImportId" );
 
59
        if( !import_id.isEmpty())
 
60
            {
 
61
            if( already_imported.contains( import_id ))
 
62
                {
 
63
                if( import_P == ImportSilent
 
64
                    || KMessageBox::warningContinueCancel( NULL,
 
65
                        i18n( "This \"actions\" file has already been imported before. "
 
66
                              "Are you sure you want to import it again?" )) != KMessageBox::Continue )
 
67
                    return true; // import "successful"
 
68
                }
 
69
            else
 
70
                already_imported.append( import_id );
 
71
            }
 
72
        else
 
73
            {
 
74
            if( import_P != ImportSilent
 
75
                && KMessageBox::warningContinueCancel( NULL,
 
76
                    i18n( "This \"actions\" file has no ImportId field and therefore it cannot be determined "
 
77
                          "whether or not it has been imported already. Are you sure you want to import it?" ))
 
78
                    == KMessageBox::Cancel )
 
79
                return true;
 
80
            }
 
81
        }
 
82
    int version = mainGroup.readEntry( "Version", -1234576 );
 
83
    switch( version )
 
84
        {
 
85
        case 1:
 
86
            read_settings_v1( cfg_P );
 
87
          break;
 
88
        case 2:
 
89
            read_settings_v2( cfg_P, include_disabled_P );
 
90
          break;
 
91
        default:
 
92
            kWarning( 1217 ) << "Unknown cfg. file version\n";
 
93
          return false;
 
94
        case -1234576: // no config file
 
95
            if( import_P ) // if importing, this is an error
 
96
                return false;
 
97
          break;
 
98
        }
 
99
    if( import_P != ImportNone )
 
100
        return true; // don't read global settings
 
101
    daemon_disabled = mainGroup.readEntry( "Disabled", false);
 
102
    KConfigGroup gesturesConfig( &cfg_P, "Gestures" );
 
103
    gestures_disabled_globally = gesturesConfig.readEntry( "Disabled", true);
 
104
    gesture_mouse_button = gesturesConfig.readEntry( "MouseButton", 2 );
 
105
    gesture_mouse_button = qBound( 2, gesture_mouse_button, 9 );
 
106
    gesture_timeout = gesturesConfig.readEntry( "Timeout", 300 );
 
107
    KConfigGroup gesturesExcludeConfig( &cfg_P, "GesturesExclude" );
 
108
    delete gestures_exclude;
 
109
    gestures_exclude = new Windowdef_list( gesturesExcludeConfig );
 
110
    KConfigGroup voiceConfig( &cfg_P, "Voice" );
 
111
    voice_shortcut=KShortcut( voiceConfig.readEntry("Shortcut" , "")  );
 
112
    return true;
 
113
    }
 
114
 
 
115
void Settings::write_settings()
 
116
    {
 
117
    KConfig cfg( KHOTKEYS_CONFIG_FILE );
 
118
// CHECKME    smazat stare sekce ?
 
119
    QStringList groups = cfg.groupList();
 
120
    for( QStringList::ConstIterator it = groups.begin();
 
121
         it != groups.end();
 
122
         ++it )
 
123
        cfg.deleteGroup( *it );
 
124
    KConfigGroup mainGroup( &cfg, "Main" ); // main group
 
125
    mainGroup.writeEntry( "Version", 2 ); // now it's version 2 cfg. file
 
126
    mainGroup.writeEntry( "AlreadyImported", already_imported );
 
127
    KConfigGroup dataGroup( &cfg,  "Data" );
 
128
    int cnt = write_actions_recursively_v2( dataGroup, actions, true );
 
129
    mainGroup.writeEntry( "Autostart", cnt != 0 && !daemon_disabled );
 
130
    mainGroup.writeEntry( "Disabled", daemon_disabled );
 
131
    KConfigGroup gesturesConfig( &cfg, "Gestures" );
 
132
    gesturesConfig.writeEntry( "Disabled", gestures_disabled_globally );
 
133
    gesturesConfig.writeEntry( "MouseButton", gesture_mouse_button );
 
134
    gesturesConfig.writeEntry( "Timeout", gesture_timeout );
 
135
    if( gestures_exclude != NULL )
 
136
        {
 
137
        KConfigGroup gesturesExcludeConfig( &cfg, "GesturesExclude" );
 
138
        gestures_exclude->cfg_write( gesturesExcludeConfig );
 
139
        }
 
140
    else
 
141
        cfg.deleteGroup( "GesturesExclude" );
 
142
    KConfigGroup voiceConfig( &cfg, "Voice" );
 
143
    voiceConfig.writeEntry("Shortcut" , voice_shortcut.toString() );
 
144
 
 
145
    }
 
146
 
 
147
 
 
148
// return value means the number of enabled actions written in the cfg file
 
149
// i.e. 'Autostart' for value > 0 should be on
 
150
int Settings::write_actions_recursively_v2( KConfigGroup& cfg_P, Action_data_group* parent_P, bool enabled_P )
 
151
    {
 
152
    int enabled_cnt = 0;
 
153
    QString save_cfg_group = cfg_P.group();
 
154
    int cnt = 0;
 
155
    for( Action_data_group::Iterator it = parent_P->first_child();
 
156
         it;
 
157
         ++it )
 
158
        {
 
159
        ++cnt;
 
160
        if( enabled_P && (*it)->enabled( true ))
 
161
            ++enabled_cnt;
 
162
        KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( cnt ));
 
163
        ( *it )->cfg_write( itConfig );
 
164
        Action_data_group* grp = dynamic_cast< Action_data_group* >( *it );
 
165
        if( grp != NULL )
 
166
            enabled_cnt += write_actions_recursively_v2( cfg_P, grp, enabled_P && (*it)->enabled( true ));
 
167
        }
 
168
    cfg_P.writeEntry( "DataCount", cnt );
 
169
    return enabled_cnt;
 
170
    }
 
171
 
 
172
void Settings::read_settings_v2( KConfig& cfg_P, bool include_disabled_P  )
 
173
    {
 
174
    KConfigGroup dataGroup( &cfg_P, "Data" );
 
175
    read_actions_recursively_v2( dataGroup, actions, include_disabled_P );
 
176
    }
 
177
 
 
178
void Settings::read_actions_recursively_v2( KConfigGroup& cfg_P, Action_data_group* parent_P,
 
179
    bool include_disabled_P )
 
180
    {
 
181
    QString save_cfg_group = cfg_P.group();
 
182
    int cnt = cfg_P.readEntry( "DataCount",0 );
 
183
    for( int i = 1;
 
184
         i <= cnt;
 
185
         ++i )
 
186
        {
 
187
        KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( i ));
 
188
        if( include_disabled_P || Action_data_base::cfg_is_enabled( itConfig ))
 
189
            {
 
190
            Action_data_base* new_action = Action_data_base::create_cfg_read( itConfig, parent_P );
 
191
            Action_data_group* grp = dynamic_cast< Action_data_group* >( new_action );
 
192
            if( grp != NULL )
 
193
                read_actions_recursively_v2( itConfig, grp, include_disabled_P );
 
194
            }
 
195
        }
 
196
    }
 
197
 
 
198
// backward compatibility
 
199
void Settings::read_settings_v1( KConfig& cfg_P )
 
200
    {
 
201
    KConfigGroup mainGroup( &cfg_P, "Main" );
 
202
    int sections = mainGroup.readEntry( "Num_Sections", 0 );
 
203
    Action_data_group* menuentries = NULL;
 
204
    for( Action_data_group::Iterator it( actions->first_child());
 
205
         *it;
 
206
         ++it )
 
207
        {
 
208
        Action_data_group* tmp = dynamic_cast< Action_data_group* >( *it );
 
209
        if( tmp == NULL )
 
210
            continue;
 
211
        if( tmp->system_group() == Action_data_group::SYSTEM_MENUENTRIES )
 
212
            {
 
213
            menuentries = tmp;
 
214
            break;
 
215
            }
 
216
        }
 
217
    for( int sect = 1;
 
218
         sect <= sections;
 
219
         ++sect )
 
220
        {
 
221
        QString group = QString( "Section%1" ).arg( sect );
 
222
        if( !cfg_P.hasGroup( group ))
 
223
            continue;
 
224
        KConfigGroup sectionConfig( &cfg_P, group );
 
225
        QString name = sectionConfig.readEntry( "Name" );
 
226
        if( name.isNull() )
 
227
            continue;
 
228
        QString shortcut = sectionConfig.readEntry( "Shortcut" );
 
229
        if( shortcut.isNull() )
 
230
            continue;
 
231
        QString run = sectionConfig.readEntry( "Run" );
 
232
        if( run.isNull() )
 
233
            continue;
 
234
        bool menuentry = sectionConfig.readEntry( "MenuEntry", false);
 
235
        // CHECKME tohle pridavani az pak je trosku HACK
 
236
        if( menuentry )
 
237
            {
 
238
            if( menuentries == NULL )
 
239
                {
 
240
                menuentries = new Action_data_group( actions,
 
241
                    i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ),
 
242
                    i18n( "These entries were created using Menu Editor." ), NULL,
 
243
                    Action_data_group::SYSTEM_MENUENTRIES, true );
 
244
                menuentries->set_conditions( new Condition_list( "", menuentries ));
 
245
                }
 
246
            ( void ) new Menuentry_shortcut_action_data( menuentries, name, "",
 
247
                KShortcut( shortcut ), run );
 
248
            }
 
249
        else
 
250
            {
 
251
            ( void ) new Command_url_shortcut_action_data( actions, name, "",
 
252
                KShortcut( shortcut ), run );
 
253
            }
 
254
        }
 
255
    }
 
256
 
 
257
} // namespace KHotKeys