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_
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"
20 #include <kconfiggroup.h>
22 #include <khotkeysglobal.h>
25 #include <kmessagebox.h>
27 #include "triggers/triggers.h"
28 #include "conditions/conditions.h"
37 gestures_disabled(false),
38 gesture_mouse_button(0),
40 gestures_exclude(NULL
),
41 daemon_disabled(false)
48 delete m_actions
; m_actions
= 0;
52 ActionDataGroup
*Settings::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
)
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
;
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
)
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
;
201 // Check if we found the group
202 if (system_group
==NULL
)
206 case ActionDataGroup::SYSTEM_MENUENTRIES
:
207 system_group
= new ActionDataGroup(
210 "KMenuEdit Global Shortcuts",
212 ActionDataGroup::SYSTEM_MENUENTRIES
,
222 Q_ASSERT(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
232 // Initialize m_actions
233 m_actions
= new ActionDataGroup(
238 ActionDataGroup::SYSTEM_ROOT
,
241 // If the config group we should read from is empty, return.
242 if( cfg_P
.groupList().count() == 0 ) {
246 // If we read the main settings and there is no main. Initialize the file
248 KConfigGroup
mainGroup( &cfg_P
, "Main" ); // main group
249 if (import_P
== ImportNone
)
251 if (!cfg_P
.groupList().contains("Main"))
257 // List of already imported configuration files
258 already_imported
= mainGroup
.readEntry( "AlreadyImported",QStringList() );
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"
274 already_imported
.append( import_id
);
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
)
286 int version
= mainGroup
.readEntry( "Version", -1234576 );
290 read_settings_v1( cfg_P
);
293 read_settings_v2( cfg_P
, include_disabled_P
);
296 kWarning() << "Unknown cfg. file version\n";
298 case -1234576: // no config file
299 if( import_P
) // if importing, this is an error
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
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();
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
);
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
)
361 QString save_cfg_group
= cfg_P
.name();
365 Q_FOREACH(ActionDataBase
*child
,parent_P
->children())
368 if( enabled_P
&& child
->enabled( true ))
370 KConfigGroup
itConfig( cfg_P
.config(), save_cfg_group
+ '_' + QString::number( cnt
));
371 child
->cfg_write( itConfig
);
372 ActionDataGroup
* grp
= dynamic_cast< ActionDataGroup
* >(child
);
374 enabled_cnt
+= write_actions_recursively_v2( itConfig
, grp
, enabled_P
&& child
->enabled( true ));
377 cfg_P
.writeEntry( "DataCount", 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 );
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
);
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
);
418 if( tmp
->system_group() == ActionDataGroup::SYSTEM_MENUENTRIES
)
428 QString group
= QString( "Section%1" ).arg( sect
);
429 if( !cfg_P
.hasGroup( group
))
431 KConfigGroup
sectionConfig( &cfg_P
, group
);
432 QString name
= sectionConfig
.readEntry( "Name" );
435 QString shortcut
= sectionConfig
.readEntry( "Shortcut" );
436 if( shortcut
.isNull() )
438 QString run
= sectionConfig
.readEntry( "Run" );
441 bool menuentry
= sectionConfig
.readEntry( "MenuEntry", false);
442 // CHECKME tohle pridavani az pak je trosku HACK
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
);
458 ( void ) new CommandUrlShortcutActionData( m_actions
, name
, "",
459 KShortcut( shortcut
), run
);
464 } // namespace KHotKeys