Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / khotkeys / shared / settings.cpp
blob795b7e98bb09309cc8a8b6c3b8dbad33fcbf1f82
1 /****************************************************************************
3 KHotKeys
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_
13 #include "settings.h"
15 #include <kconfig.h>
16 #include <kconfiggroup.h>
17 #include <kdebug.h>
18 #include <klocale.h>
19 #include <kglobal.h>
20 #include <kmessagebox.h>
22 #include "triggers.h"
23 #include "conditions.h"
24 #include "action_data.h"
26 namespace KHotKeys
29 // Settings
31 Settings::Settings()
32 : actions( NULL ), gestures_exclude( NULL )
36 bool Settings::read_settings( bool include_disabled_P )
38 KConfig cfg( KHOTKEYS_CONFIG_FILE );
39 return read_settings( cfg, include_disabled_P, ImportNone );
42 bool Settings::import( KConfig& cfg_P, bool ask_P )
44 return read_settings( cfg_P, true, ask_P ? ImportAsk : ImportSilent );
47 bool Settings::read_settings( KConfig& cfg_P, bool include_disabled_P, ImportType import_P )
49 if( actions == NULL )
50 actions = new Action_data_group( NULL, "should never see", "should never see",
51 NULL, Action_data_group::SYSTEM_ROOT, true );
52 if( cfg_P.groupList().count() == 0 ) // empty
53 return false;
54 KConfigGroup mainGroup( &cfg_P, "Main" ); // main group
55 if( import_P == ImportNone ) // reading main cfg file
56 already_imported = mainGroup.readEntry( "AlreadyImported",QStringList() );
57 else
59 QString import_id = mainGroup.readEntry( "ImportId" );
60 if( !import_id.isEmpty())
62 if( already_imported.contains( import_id ))
64 if( import_P == ImportSilent
65 || KMessageBox::warningContinueCancel( NULL,
66 i18n( "This \"actions\" file has already been imported before. "
67 "Are you sure you want to import it again?" )) != KMessageBox::Continue )
68 return true; // import "successful"
70 else
71 already_imported.append( import_id );
73 else
75 if( import_P != ImportSilent
76 && KMessageBox::warningContinueCancel( NULL,
77 i18n( "This \"actions\" file has no ImportId field and therefore it cannot be determined "
78 "whether or not it has been imported already. Are you sure you want to import it?" ))
79 == KMessageBox::Cancel )
80 return true;
83 int version = mainGroup.readEntry( "Version", -1234576 );
84 switch( version )
86 case 1:
87 read_settings_v1( cfg_P );
88 break;
89 case 2:
90 read_settings_v2( cfg_P, include_disabled_P );
91 break;
92 default:
93 kWarning( 1217 ) << "Unknown cfg. file version\n";
94 return false;
95 case -1234576: // no config file
96 if( import_P ) // if importing, this is an error
97 return false;
98 break;
100 if( import_P != ImportNone )
101 return true; // don't read global settings
102 daemon_disabled = mainGroup.readEntry( "Disabled", false);
103 KConfigGroup gesturesConfig( &cfg_P, "Gestures" );
104 gestures_disabled_globally = gesturesConfig.readEntry( "Disabled", true);
105 gesture_mouse_button = gesturesConfig.readEntry( "MouseButton", 2 );
106 gesture_mouse_button = qBound( 2, gesture_mouse_button, 9 );
107 gesture_timeout = gesturesConfig.readEntry( "Timeout", 300 );
108 KConfigGroup gesturesExcludeConfig( &cfg_P, "GesturesExclude" );
109 delete gestures_exclude;
110 gestures_exclude = new Windowdef_list( gesturesExcludeConfig );
111 KConfigGroup voiceConfig( &cfg_P, "Voice" );
112 voice_shortcut=KShortcut( voiceConfig.readEntry("Shortcut" , "") );
113 return true;
116 void Settings::write_settings()
118 KConfig cfg( KHOTKEYS_CONFIG_FILE );
119 // CHECKME smazat stare sekce ?
120 QStringList groups = cfg.groupList();
121 for( QStringList::ConstIterator it = groups.begin();
122 it != groups.end();
123 ++it )
124 cfg.deleteGroup( *it );
125 KConfigGroup mainGroup( &cfg, "Main" ); // main group
126 mainGroup.writeEntry( "Version", 2 ); // now it's version 2 cfg. file
127 mainGroup.writeEntry( "AlreadyImported", already_imported );
128 KConfigGroup dataGroup( &cfg, "Data" );
129 int cnt = write_actions_recursively_v2( dataGroup, actions, true );
130 mainGroup.writeEntry( "Autostart", cnt != 0 && !daemon_disabled );
131 mainGroup.writeEntry( "Disabled", daemon_disabled );
132 KConfigGroup gesturesConfig( &cfg, "Gestures" );
133 gesturesConfig.writeEntry( "Disabled", gestures_disabled_globally );
134 gesturesConfig.writeEntry( "MouseButton", gesture_mouse_button );
135 gesturesConfig.writeEntry( "Timeout", gesture_timeout );
136 if( gestures_exclude != NULL )
138 KConfigGroup gesturesExcludeConfig( &cfg, "GesturesExclude" );
139 gestures_exclude->cfg_write( gesturesExcludeConfig );
141 else
142 cfg.deleteGroup( "GesturesExclude" );
143 KConfigGroup voiceConfig( &cfg, "Voice" );
144 voiceConfig.writeEntry("Shortcut" , voice_shortcut.toString() );
149 // return value means the number of enabled actions written in the cfg file
150 // i.e. 'Autostart' for value > 0 should be on
151 int Settings::write_actions_recursively_v2( KConfigGroup& cfg_P, Action_data_group* parent_P, bool enabled_P )
153 int enabled_cnt = 0;
154 QString save_cfg_group = cfg_P.name();
155 int cnt = 0;
156 if( parent_P )
158 for( Action_data_group::ConstIterator it = parent_P->first_child();
159 it != parent_P->after_last_child();
160 ++it )
162 ++cnt;
163 if( enabled_P && (*it)->enabled( true ))
164 ++enabled_cnt;
165 KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( cnt ));
166 ( *it )->cfg_write( itConfig );
167 Action_data_group* grp = dynamic_cast< Action_data_group* >( *it );
168 if( grp != NULL )
169 enabled_cnt += write_actions_recursively_v2( itConfig, grp, enabled_P && (*it)->enabled( true ));
172 cfg_P.writeEntry( "DataCount", cnt );
173 return enabled_cnt;
176 void Settings::read_settings_v2( KConfig& cfg_P, bool include_disabled_P )
178 KConfigGroup dataGroup( &cfg_P, "Data" );
179 read_actions_recursively_v2( dataGroup, actions, include_disabled_P );
182 void Settings::read_actions_recursively_v2( KConfigGroup& cfg_P, Action_data_group* parent_P,
183 bool include_disabled_P )
185 QString save_cfg_group = cfg_P.name();
186 int cnt = cfg_P.readEntry( "DataCount",0 );
187 for( int i = 1;
188 i <= cnt;
189 ++i )
191 KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( i ));
192 if( include_disabled_P || Action_data_base::cfg_is_enabled( itConfig ))
194 Action_data_base* new_action = Action_data_base::create_cfg_read( itConfig, parent_P );
195 Action_data_group* grp = dynamic_cast< Action_data_group* >( new_action );
196 if( grp != NULL )
197 read_actions_recursively_v2( itConfig, grp, include_disabled_P );
202 // backward compatibility
203 void Settings::read_settings_v1( KConfig& cfg_P )
205 KConfigGroup mainGroup( &cfg_P, "Main" );
206 int sections = mainGroup.readEntry( "Num_Sections", 0 );
207 Action_data_group* menuentries = NULL;
208 for( Action_data_group::ConstIterator it = actions->first_child();
209 it != actions->after_last_child();
210 ++it )
212 Action_data_group* tmp = dynamic_cast< Action_data_group* >( *it );
213 if( tmp == NULL )
214 continue;
215 if( tmp->system_group() == Action_data_group::SYSTEM_MENUENTRIES )
217 menuentries = tmp;
218 break;
221 for( int sect = 1;
222 sect <= sections;
223 ++sect )
225 QString group = QString( "Section%1" ).arg( sect );
226 if( !cfg_P.hasGroup( group ))
227 continue;
228 KConfigGroup sectionConfig( &cfg_P, group );
229 QString name = sectionConfig.readEntry( "Name" );
230 if( name.isNull() )
231 continue;
232 QString shortcut = sectionConfig.readEntry( "Shortcut" );
233 if( shortcut.isNull() )
234 continue;
235 QString run = sectionConfig.readEntry( "Run" );
236 if( run.isNull() )
237 continue;
238 bool menuentry = sectionConfig.readEntry( "MenuEntry", false);
239 // CHECKME tohle pridavani az pak je trosku HACK
240 if( menuentry )
242 if( menuentries == NULL )
244 menuentries = new Action_data_group( actions,
245 i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ),
246 i18n( "These entries were created using Menu Editor." ), NULL,
247 Action_data_group::SYSTEM_MENUENTRIES, true );
248 menuentries->set_conditions( new Condition_list( "", menuentries ));
250 ( void ) new Menuentry_shortcut_action_data( menuentries, name, "",
251 KShortcut( shortcut ), run );
253 else
255 ( void ) new Command_url_shortcut_action_data( actions, name, "",
256 KShortcut( shortcut ), run );
261 } // namespace KHotKeys