2
* Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
3
* Copyright (C) 2009 Michael Jansen <kde@michael-jansen.biz>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License version 2 as published by the Free Software Foundation.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
22
#include "action_data/action_data.h"
23
#include "settings_reader_v2.h"
24
#include "settings_writer.h"
25
#include "windows_helper/window_selection_list.h"
27
#include <KDE/KConfig>
29
#include <KDE/KMessageBox>
30
#include <KDE/KStandardDirs>
32
#include <QtGui/QApplication>
41
gestures_exclude(NULL)
49
delete m_actions; m_actions = 0;
53
ActionDataGroup *Settings::actions()
59
const ActionDataGroup *Settings::actions() const
65
bool Settings::areGesturesDisabled() const
67
return gestures_disabled;
71
void Settings::disableDaemon()
73
daemon_disabled = true;
77
void Settings::disableGestures()
79
gestures_disabled = true;
83
void Settings::enableDaemon()
85
daemon_disabled = false;
89
void Settings::enableGestures()
91
gestures_disabled = false;
95
void Settings::exportTo(
99
KHotKeys::ActionState state,
102
SettingsWriter writer(this, state, id, allowMerging);
103
writer.exportTo(what, config);
107
int Settings::gestureMouseButton() const
109
return gesture_mouse_button;
113
Windowdef_list *Settings::gesturesExclude()
115
return gestures_exclude;
119
const Windowdef_list *Settings::gesturesExclude() const
121
return gestures_exclude;
125
int Settings::gestureTimeOut() const
127
return gesture_timeout;
131
bool Settings::isDaemonDisabled() const
133
return daemon_disabled;
137
bool Settings::loadDefaults()
141
// Load the default set.
142
QString installPath = KGlobal::dirs()->installPath("data");
144
KConfig file(installPath + "khotkeys/defaults.khotkeys");
145
if (read_settings(m_actions, file, true, Enabled))
147
kDebug() << "Loaded defaults from" << file.name();
148
already_imported.append("defaults");
153
kDebug() << "Failed to load defaults from" << file.name();
160
void Settings::reinitialize()
162
// Rereading settings. First delete what we have
165
gestures_disabled = true;
166
gesture_mouse_button = 2;
167
gesture_timeout = 300;
168
gestures_exclude = NULL,
170
daemon_disabled = false;
173
voice_shortcut = KShortcut();
175
already_imported = QStringList();
179
void Settings::setActions( ActionDataGroup *actions )
185
: new ActionDataGroup(
190
ActionDataGroup::SYSTEM_ROOT);
195
void Settings::setGesturesExclude( Windowdef_list *gestures )
197
delete gestures_exclude;
198
gestures_exclude = gestures;
202
void Settings::setGestureMouseButton( int mouse_button )
204
gesture_mouse_button = mouse_button;
208
void Settings::setGestureTimeOut(int timeout)
210
gesture_timeout = timeout;
214
void Settings::setVoiceShortcut( const KShortcut &shortcut )
216
voice_shortcut = shortcut;
220
ActionDataGroup *Settings::takeActions()
222
ActionDataGroup *res = m_actions;
228
KShortcut Settings::voiceShortcut() const
230
return voice_shortcut;
234
bool Settings::import(KConfig& config, ImportType ask, ActionState state)
236
return importFrom(m_actions, config, ask, state);
240
bool Settings::isConfigFileValid(KConfigBase const &config, ImportType ask)
244
// The file is only valid if it has a main group
245
KConfigGroup mainGroup( &config, "Main" );
246
if (mainGroup.isValid())
248
// Now check the version
249
int version = mainGroup.readEntry( "Version", -1234576 );
256
case 1: // Version 1 files no longer supported
257
kDebug() << "Version 1 file encountered.";
260
case -1234576: // No Version entry -> invalid file
261
kDebug() << "No version specified in file:";
266
kDebug() << "Invalid Version found:" << version;
272
// if it's valid we are finished.
273
if (valid) return valid;
275
// See if we should inform the user.
280
KMessageBox::information(
281
QApplication::activeWindow(),
282
"The specified file is empty or not a configuration file",
298
bool Settings::importFrom(ActionDataGroup *element, KConfigBase const &config, ImportType ask, ActionState state)
300
// Make sure the given file is valid
301
if (!isConfigFileValid(config, ask)) return false;
303
KConfigGroup mainGroup(&config, "Main");
304
// A file can have a import id.
305
QString import_id = mainGroup.readEntry( "ImportId" );
306
if (!import_id.isEmpty())
308
// File has a id. Check for a previous import.
309
if (already_imported.contains( import_id ))
315
if( ask == ImportSilent
316
|| ( ask == ImportAsk && KMessageBox::warningContinueCancel(
318
i18n( "This \"actions\" file has already been imported before. "
319
"Are you sure you want to import it again?" )) != KMessageBox::Continue ) )
321
return true; // import "successful"
329
// Unknown ImportType. Most likely None.
336
already_imported.append(import_id);
344
if (KMessageBox::warningContinueCancel(
346
i18n( "This \"actions\" file has no ImportId field and therefore it cannot be determined "
347
"whether or not it has been imported already. Are you sure you want to import it?" ))
348
== KMessageBox::Cancel )
358
// Unknown ImportType. Most likely None.
364
// Include Disabled, Disable the imported actions
365
return read_settings(element, config, true, state);
369
void Settings::validate()
371
// Create the KMenuEdit group if it does not yet exist
372
get_system_group(ActionDataGroup::SYSTEM_MENUENTRIES);
376
ActionDataGroup *Settings::get_system_group(ActionDataGroup::system_group_t group_id)
380
// Search for the menuentries system group.
381
ActionDataGroup *system_group = NULL;
383
Q_FOREACH(KHotKeys::ActionDataBase* element, m_actions->children())
385
ActionDataGroup *group = dynamic_cast<ActionDataGroup*>(element);
387
if (group && (group->system_group() == group_id))
389
system_group = group;
394
// Check if we found the group
395
if (system_group==NULL)
399
case ActionDataGroup::SYSTEM_MENUENTRIES:
400
system_group = new ActionDataGroup(
403
"KMenuEdit Global Shortcuts",
405
ActionDataGroup::SYSTEM_MENUENTRIES);
406
system_group->enable();
415
Q_ASSERT(system_group);
420
bool Settings::reread_settings(bool include_disabled)
422
KConfig config( KHOTKEYS_CONFIG_FILE );
424
// If we read the main settings and there is no main. Initialize the file
426
KConfigGroup mainGroup( &config, "Main" ); // main group
427
if (!mainGroup.exists())
434
// First delete what we have
437
// ### Read the global configurations. Reinitialize sets the default
438
daemon_disabled = mainGroup.readEntry( "Disabled", daemon_disabled);
440
// ### List of already imported configuration files
441
already_imported = mainGroup.readEntry(
446
KConfigGroup gesturesConfig( &config, "Gestures" );
447
// ### Read the gesture configurations. Reinitialize sets the default.
449
gestures_disabled = gesturesConfig.readEntry( "Disabled", gestures_disabled);
450
gesture_mouse_button = gesturesConfig.readEntry( "MouseButton", gesture_mouse_button );
451
gesture_mouse_button = qBound( 2, gesture_mouse_button, 9 );
452
gesture_timeout = gesturesConfig.readEntry( "Timeout", gesture_timeout );
454
// Somhow gesture_timeout found it's way into my config file. Fix it for
455
// everyone else too.
456
if (gesture_timeout < 100) gesture_timeout = 300;
458
KConfigGroup gesturesExcludeConfig( &config, "GesturesExclude" );
459
delete gestures_exclude;
460
gestures_exclude = new Windowdef_list( gesturesExcludeConfig );
463
KConfigGroup voiceConfig( &config, "Voice" );
464
voice_shortcut=KShortcut( voiceConfig.readEntry("Shortcut" , "") );
466
bool rc = read_settings(m_actions, config, include_disabled, Retain);
467
// Ensure the system groups exist
473
bool Settings::read_settings(ActionDataGroup *root, KConfigBase const &config, bool include_disabled, ActionState stateStrategy)
475
// Make sure the given file is valid
476
if (!isConfigFileValid(config, ImportSilent)) return false;
478
KConfigGroup mainGroup( &config, "Main" ); // main group
479
int version = mainGroup.readEntry( "Version", -1234576 );
480
QString import_id = mainGroup.readEntry( "ImportId" );
485
kDebug() << "Version 2 File!";
486
SettingsReaderV2 reader(this, include_disabled, stateStrategy, import_id);
487
reader.read(config, root);
492
// All other values are impossible because of the
493
// isConfigFileValid() call above.
502
bool Settings::update()
504
QStringList updates(KGlobal::dirs()->findAllResources("data", "khotkeys/*.khotkeys"));
505
bool imported(false);
507
Q_FOREACH (const QString &path, updates)
509
// Import checks if the file was already imported.
511
if (import(file, ImportSilent, Retain))
513
kDebug() << "Imported file" << path;
526
void Settings::write()
528
KConfig cfg( KHOTKEYS_CONFIG_FILE );
529
SettingsWriter writer(this, Retain);
533
} // namespace KHotKeys