add more spacing
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / settings.cpp
blobe3544af0c0ea3d3e6847bf41408efe9dbf6d6d17
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 "action_data/action_data.h"
16 #include "action_data/menuentry_shortcut_action_data.h"
17 #include "action_data/command_url_shortcut_action_data.h"
19 #include <kconfig.h>
20 #include <kconfiggroup.h>
21 #include <kdebug.h>
22 #include <khotkeysglobal.h>
23 #include <klocale.h>
24 #include <kglobal.h>
25 #include <kmessagebox.h>
27 #include "triggers/triggers.h"
28 #include "conditions/conditions.h"
30 namespace KHotKeys
33 // Settings
35 Settings::Settings()
36 : m_actions( NULL ),
37 gestures_disabled(false),
38 gesture_mouse_button(0),
39 gesture_timeout(0),
40 gestures_exclude(NULL),
41 daemon_disabled(false)
46 Settings::~Settings()
48 delete m_actions; m_actions = 0;
52 ActionDataGroup *Settings::actions()
54 return m_actions;
58 bool Settings::areGesturesDisabled() const
60 return gestures_disabled;
64 void Settings::disableDaemon()
66 daemon_disabled = true;
70 void Settings::disableGestures()
72 gestures_disabled = true;
76 void Settings::enableDaemon()
78 daemon_disabled = false;
82 void Settings::enableGestures()
84 gestures_disabled = false;
88 int Settings::gestureMouseButton() const
90 return gesture_mouse_button;
94 Windowdef_list *Settings::gesturesExclude()
96 return gestures_exclude;
100 const Windowdef_list *Settings::gesturesExclude() const
102 return gestures_exclude;
106 int Settings::gestureTimeOut() const
108 return gesture_timeout;
112 bool Settings::isDaemonDisabled() const
114 return daemon_disabled;
118 void Settings::setActions( ActionDataGroup *actions )
120 delete m_actions;
121 m_actions = actions;
125 void Settings::setGesturesExclude( Windowdef_list *gestures )
127 delete gestures_exclude;
128 gestures_exclude = gestures;
132 void Settings::setGestureMouseButton( int mouse_button )
134 gesture_mouse_button = mouse_button;
138 void Settings::setGestureTimeOut(int timeout)
140 gesture_timeout = timeout;
144 void Settings::setVoiceShortcut( const KShortcut &shortcut )
146 voice_shortcut = shortcut;
150 ActionDataGroup *Settings::takeActions()
152 ActionDataGroup *res = m_actions;
153 m_actions = 0;
154 return res;
158 KShortcut Settings::voiceShortcut() const
160 return voice_shortcut;
164 bool Settings::read_settings( bool include_disabled_P )
166 KConfig cfg( KHOTKEYS_CONFIG_FILE );
167 return read_settings( cfg, include_disabled_P, ImportNone );
170 bool Settings::import( KConfig& cfg_P, bool ask_P )
172 return read_settings( cfg_P, true, ask_P ? ImportAsk : ImportSilent );
176 void Settings::initialize()
178 // Create the KMenuEdit group
179 get_system_group(ActionDataGroup::SYSTEM_MENUENTRIES);
183 ActionDataGroup *Settings::get_system_group(ActionDataGroup::system_group_t group_id)
185 Q_ASSERT(m_actions);
187 // Search for the menuentries system group.
188 ActionDataGroup *system_group = NULL;
190 Q_FOREACH(KHotKeys::ActionDataBase* element, m_actions->children())
192 ActionDataGroup *group = dynamic_cast<ActionDataGroup*>(element);
194 if (group && (group->system_group() == group_id))
196 system_group = group;
197 break;
201 // Check if we found the group
202 if (system_group==NULL)
204 switch (group_id)
206 case ActionDataGroup::SYSTEM_MENUENTRIES:
207 system_group = new ActionDataGroup(
208 m_actions,
209 "KMenuEdit",
210 "KMenuEdit Global Shortcuts",
211 NULL,
212 ActionDataGroup::SYSTEM_MENUENTRIES,
213 true);
214 break;
216 default:
217 Q_ASSERT(false);
218 return NULL;
222 Q_ASSERT(system_group);
223 return system_group;
227 bool Settings::read_settings( KConfig& cfg_P, bool include_disabled_P, ImportType import_P )
229 // Reread settings. First delete what we have
230 setActions(NULL);
232 // Initialize m_actions
233 m_actions = new ActionDataGroup(
234 NULL,
235 "should never see",
236 "should never see",
237 NULL,
238 ActionDataGroup::SYSTEM_ROOT,
239 true );
241 // If the config group we should read from is empty, return.
242 if( cfg_P.groupList().count() == 0 ) {
243 return false;
246 // If we read the main settings and there is no main. Initialize the file
247 // and return
248 KConfigGroup mainGroup( &cfg_P, "Main" ); // main group
249 if (import_P == ImportNone)
251 if (!cfg_P.groupList().contains("Main"))
253 initialize();
254 return false;
257 // List of already imported configuration files
258 already_imported = mainGroup.readEntry( "AlreadyImported",QStringList() );
260 else
262 QString import_id = mainGroup.readEntry( "ImportId" );
263 if( !import_id.isEmpty())
265 if( already_imported.contains( import_id ))
267 if( import_P == ImportSilent
268 || KMessageBox::warningContinueCancel( NULL,
269 i18n( "This \"actions\" file has already been imported before. "
270 "Are you sure you want to import it again?" )) != KMessageBox::Continue )
271 return true; // import "successful"
273 else
274 already_imported.append( import_id );
276 else
278 if( import_P != ImportSilent
279 && KMessageBox::warningContinueCancel( NULL,
280 i18n( "This \"actions\" file has no ImportId field and therefore it cannot be determined "
281 "whether or not it has been imported already. Are you sure you want to import it?" ))
282 == KMessageBox::Cancel )
283 return true;
286 int version = mainGroup.readEntry( "Version", -1234576 );
287 switch( version )
289 case 1:
290 read_settings_v1( cfg_P );
291 break;
292 case 2:
293 read_settings_v2( cfg_P, include_disabled_P );
294 break;
295 default:
296 kWarning() << "Unknown cfg. file version\n";
297 return false;
298 case -1234576: // no config file
299 if( import_P ) // if importing, this is an error
300 return false;
301 break;
303 if( import_P != ImportNone )
304 return true; // don't read global settings
305 daemon_disabled = mainGroup.readEntry( "Disabled", false);
306 KConfigGroup gesturesConfig( &cfg_P, "Gestures" );
307 gestures_disabled = gesturesConfig.readEntry( "Disabled", true);
308 gesture_mouse_button = gesturesConfig.readEntry( "MouseButton", 2 );
309 gesture_mouse_button = qBound( 2, gesture_mouse_button, 9 );
310 gesture_timeout = gesturesConfig.readEntry( "Timeout", 300 );
311 KConfigGroup gesturesExcludeConfig( &cfg_P, "GesturesExclude" );
312 delete gestures_exclude;
313 gestures_exclude = new Windowdef_list( gesturesExcludeConfig );
314 KConfigGroup voiceConfig( &cfg_P, "Voice" );
315 voice_shortcut=KShortcut( voiceConfig.readEntry("Shortcut" , "") );
317 // Ensure the system groups exist
318 initialize();
319 return true;
322 void Settings::write_settings()
324 KConfig cfg( KHOTKEYS_CONFIG_FILE );
326 // CHECKME smazat stare sekce ?
327 QStringList groups = cfg.groupList();
328 for( QStringList::ConstIterator it = groups.constBegin();
329 it != groups.constEnd();
330 ++it )
331 cfg.deleteGroup( *it );
332 KConfigGroup mainGroup( &cfg, "Main" ); // main group
333 mainGroup.writeEntry( "Version", 2 ); // now it's version 2 cfg. file
334 mainGroup.writeEntry( "AlreadyImported", already_imported );
335 KConfigGroup dataGroup( &cfg, "Data" );
336 int cnt = write_actions_recursively_v2( dataGroup, m_actions, true );
337 mainGroup.writeEntry( "Autostart", cnt != 0 && !daemon_disabled );
338 mainGroup.writeEntry( "Disabled", daemon_disabled );
339 KConfigGroup gesturesConfig( &cfg, "Gestures" );
340 gesturesConfig.writeEntry( "Disabled", gestures_disabled );
341 gesturesConfig.writeEntry( "MouseButton", gesture_mouse_button );
342 gesturesConfig.writeEntry( "Timeout", gesture_timeout );
343 if( gestures_exclude != NULL )
345 KConfigGroup gesturesExcludeConfig( &cfg, "GesturesExclude" );
346 gestures_exclude->cfg_write( gesturesExcludeConfig );
348 else
349 cfg.deleteGroup( "GesturesExclude" );
350 KConfigGroup voiceConfig( &cfg, "Voice" );
351 voiceConfig.writeEntry("Shortcut" , voice_shortcut.toString() );
356 // return value means the number of enabled actions written in the cfg file
357 // i.e. 'Autostart' for value > 0 should be on
358 int Settings::write_actions_recursively_v2( KConfigGroup& cfg_P, ActionDataGroup* parent_P, bool enabled_P )
360 int enabled_cnt = 0;
361 QString save_cfg_group = cfg_P.name();
362 int cnt = 0;
363 if( parent_P )
365 Q_FOREACH(ActionDataBase *child,parent_P->children())
367 ++cnt;
368 if( enabled_P && child->enabled( true ))
369 ++enabled_cnt;
370 KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( cnt ));
371 child->cfg_write( itConfig );
372 ActionDataGroup* grp = dynamic_cast< ActionDataGroup* >(child);
373 if( grp != NULL )
374 enabled_cnt += write_actions_recursively_v2( itConfig, grp, enabled_P && child->enabled( true ));
377 cfg_P.writeEntry( "DataCount", cnt );
378 return enabled_cnt;
381 void Settings::read_settings_v2( KConfig& cfg_P, bool include_disabled_P )
383 KConfigGroup dataGroup( &cfg_P, "Data" );
384 read_actions_recursively_v2( dataGroup, m_actions, include_disabled_P );
387 void Settings::read_actions_recursively_v2( KConfigGroup& cfg_P, ActionDataGroup* parent_P,
388 bool include_disabled_P )
390 QString save_cfg_group = cfg_P.name();
391 int cnt = cfg_P.readEntry( "DataCount",0 );
392 for( int i = 1;
393 i <= cnt;
394 ++i )
396 KConfigGroup itConfig( cfg_P.config(), save_cfg_group + '_' + QString::number( i ));
397 if( include_disabled_P || ActionDataBase::cfg_is_enabled( itConfig ))
399 ActionDataBase* new_action = ActionDataBase::create_cfg_read( itConfig, parent_P );
400 ActionDataGroup* grp = dynamic_cast< ActionDataGroup* >( new_action );
401 if( grp != NULL )
402 read_actions_recursively_v2( itConfig, grp, include_disabled_P );
407 // backward compatibility
408 void Settings::read_settings_v1( KConfig& cfg_P )
410 KConfigGroup mainGroup( &cfg_P, "Main" );
411 int sections = mainGroup.readEntry( "Num_Sections", 0 );
412 ActionDataGroup* menuentries = NULL;
413 Q_FOREACH(ActionDataBase *child, m_actions->children())
415 ActionDataGroup* tmp = dynamic_cast< ActionDataGroup* >(child);
416 if( tmp == NULL )
417 continue;
418 if( tmp->system_group() == ActionDataGroup::SYSTEM_MENUENTRIES )
420 menuentries = tmp;
421 break;
424 for( int sect = 1;
425 sect <= sections;
426 ++sect )
428 QString group = QString( "Section%1" ).arg( sect );
429 if( !cfg_P.hasGroup( group ))
430 continue;
431 KConfigGroup sectionConfig( &cfg_P, group );
432 QString name = sectionConfig.readEntry( "Name" );
433 if( name.isNull() )
434 continue;
435 QString shortcut = sectionConfig.readEntry( "Shortcut" );
436 if( shortcut.isNull() )
437 continue;
438 QString run = sectionConfig.readEntry( "Run" );
439 if( run.isNull() )
440 continue;
441 bool menuentry = sectionConfig.readEntry( "MenuEntry", false);
442 // CHECKME tohle pridavani az pak je trosku HACK
443 if( menuentry )
445 if( menuentries == NULL )
447 menuentries = new ActionDataGroup( m_actions,
448 i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ),
449 i18n( "These entries were created using Menu Editor." ), NULL,
450 ActionDataGroup::SYSTEM_MENUENTRIES, true );
451 menuentries->set_conditions( new Condition_list( "", menuentries ));
453 ( void ) new MenuEntryShortcutActionData( menuentries, name, "",
454 KShortcut( shortcut ), run );
456 else
458 ( void ) new CommandUrlShortcutActionData( m_actions, name, "",
459 KShortcut( shortcut ), run );
464 } // namespace KHotKeys