1
/****************************************************************************
5
Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
7
Distributed under the terms of the GNU General Public License version 2.
9
****************************************************************************/
11
#define _SETTINGS_CPP_
19
#include <kmessagebox.h>
22
#include "conditions.h"
23
#include "action_data.h"
31
: actions( NULL ), gestures_exclude( NULL )
35
bool Settings::read_settings( bool include_disabled_P )
37
KConfig cfg( KHOTKEYS_CONFIG_FILE );
38
return read_settings( cfg, include_disabled_P, ImportNone );
41
bool Settings::import( KConfig& cfg_P, bool ask_P )
43
return read_settings( cfg_P, true, ask_P ? ImportAsk : ImportSilent );
46
bool Settings::read_settings( KConfig& cfg_P, bool include_disabled_P, ImportType import_P )
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
53
KConfigGroup mainGroup( &cfg_P, "Main" ); // main group
54
if( import_P == ImportNone ) // reading main cfg file
55
already_imported = mainGroup.readEntry( "AlreadyImported",QStringList() );
58
QString import_id = mainGroup.readEntry( "ImportId" );
59
if( !import_id.isEmpty())
61
if( already_imported.contains( import_id ))
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"
70
already_imported.append( import_id );
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 )
82
int version = mainGroup.readEntry( "Version", -1234576 );
86
read_settings_v1( cfg_P );
89
read_settings_v2( cfg_P, include_disabled_P );
92
kWarning( 1217 ) << "Unknown cfg. file version\n";
94
case -1234576: // no config file
95
if( import_P ) // if importing, this is an error
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" , "") );
115
void Settings::write_settings()
117
KConfig cfg( KHOTKEYS_CONFIG_FILE );
118
// CHECKME smazat stare sekce ?
119
QStringList groups = cfg.groupList();
120
for( QStringList::ConstIterator it = groups.begin();
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 )
137
KConfigGroup gesturesExcludeConfig( &cfg, "GesturesExclude" );
138
gestures_exclude->cfg_write( gesturesExcludeConfig );
141
cfg.deleteGroup( "GesturesExclude" );
142
KConfigGroup voiceConfig( &cfg, "Voice" );
143
voiceConfig.writeEntry("Shortcut" , voice_shortcut.toString() );
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 )
153
QString save_cfg_group = cfg_P.group();
155
for( Action_data_group::Iterator it = parent_P->first_child();
160
if( enabled_P && (*it)->enabled( true ))
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 );
166
enabled_cnt += write_actions_recursively_v2( cfg_P, grp, enabled_P && (*it)->enabled( true ));
168
cfg_P.writeEntry( "DataCount", cnt );
172
void Settings::read_settings_v2( KConfig& cfg_P, bool include_disabled_P )
174
KConfigGroup dataGroup( &cfg_P, "Data" );
175
read_actions_recursively_v2( dataGroup, actions, include_disabled_P );
178
void Settings::read_actions_recursively_v2( KConfigGroup& cfg_P, Action_data_group* parent_P,
179
bool include_disabled_P )
181
QString save_cfg_group = cfg_P.group();
182
int cnt = cfg_P.readEntry( "DataCount",0 );
187
KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( i ));
188
if( include_disabled_P || Action_data_base::cfg_is_enabled( itConfig ))
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 );
193
read_actions_recursively_v2( itConfig, grp, include_disabled_P );
198
// backward compatibility
199
void Settings::read_settings_v1( KConfig& cfg_P )
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());
208
Action_data_group* tmp = dynamic_cast< Action_data_group* >( *it );
211
if( tmp->system_group() == Action_data_group::SYSTEM_MENUENTRIES )
221
QString group = QString( "Section%1" ).arg( sect );
222
if( !cfg_P.hasGroup( group ))
224
KConfigGroup sectionConfig( &cfg_P, group );
225
QString name = sectionConfig.readEntry( "Name" );
228
QString shortcut = sectionConfig.readEntry( "Shortcut" );
229
if( shortcut.isNull() )
231
QString run = sectionConfig.readEntry( "Run" );
234
bool menuentry = sectionConfig.readEntry( "MenuEntry", false);
235
// CHECKME tohle pridavani az pak je trosku HACK
238
if( menuentries == NULL )
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 ));
246
( void ) new Menuentry_shortcut_action_data( menuentries, name, "",
247
KShortcut( shortcut ), run );
251
( void ) new Command_url_shortcut_action_data( actions, name, "",
252
KShortcut( shortcut ), run );
257
} // namespace KHotKeys