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_
16 #include <kconfiggroup.h>
20 #include <kmessagebox.h>
23 #include "conditions.h"
24 #include "action_data.h"
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
)
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
54 KConfigGroup
mainGroup( &cfg_P
, "Main" ); // main group
55 if( import_P
== ImportNone
) // reading main cfg file
56 already_imported
= mainGroup
.readEntry( "AlreadyImported",QStringList() );
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"
71 already_imported
.append( import_id
);
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
)
83 int version
= mainGroup
.readEntry( "Version", -1234576 );
87 read_settings_v1( cfg_P
);
90 read_settings_v2( cfg_P
, include_disabled_P
);
93 kWarning( 1217 ) << "Unknown cfg. file version\n";
95 case -1234576: // no config file
96 if( import_P
) // if importing, this is an error
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" , "") );
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();
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
);
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
)
154 QString save_cfg_group
= cfg_P
.name();
158 for( Action_data_group::ConstIterator it
= parent_P
->first_child();
159 it
!= parent_P
->after_last_child();
163 if( enabled_P
&& (*it
)->enabled( true ))
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
);
169 enabled_cnt
+= write_actions_recursively_v2( itConfig
, grp
, enabled_P
&& (*it
)->enabled( true ));
172 cfg_P
.writeEntry( "DataCount", 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 );
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
);
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();
212 Action_data_group
* tmp
= dynamic_cast< Action_data_group
* >( *it
);
215 if( tmp
->system_group() == Action_data_group::SYSTEM_MENUENTRIES
)
225 QString group
= QString( "Section%1" ).arg( sect
);
226 if( !cfg_P
.hasGroup( group
))
228 KConfigGroup
sectionConfig( &cfg_P
, group
);
229 QString name
= sectionConfig
.readEntry( "Name" );
232 QString shortcut
= sectionConfig
.readEntry( "Shortcut" );
233 if( shortcut
.isNull() )
235 QString run
= sectionConfig
.readEntry( "Run" );
238 bool menuentry
= sectionConfig
.readEntry( "MenuEntry", false);
239 // CHECKME tohle pridavani az pak je trosku HACK
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
);
255 ( void ) new Command_url_shortcut_action_data( actions
, name
, "",
256 KShortcut( shortcut
), run
);
261 } // namespace KHotKeys