2
* Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program 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
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "keyboard_config.h"
21
#include <ksharedconfig.h>
22
#include <kconfiggroup.h>
26
static const char* SWITCHING_POLICIES[] = {"Global", "Desktop", "WinClass", "Window", NULL };
27
static const char* LIST_SEPARATOR = ",";
28
//static const char* DEFAULT_LAYOUT = "us";
29
static const char* DEFAULT_MODEL = "pc104";
31
static const QString CONFIG_FILENAME("kxkbrc");
32
static const QString CONFIG_GROUPNAME("Layout");
34
const int KeyboardConfig::NO_LOOPING = -1;
36
QString KeyboardConfig::getSwitchingPolicyString(SwitchingPolicy switchingPolicy) {
37
return SWITCHING_POLICIES[switchingPolicy];
40
static int findStringIndex(const char* strings[], const QString& toFind, int defaultIndex)
42
for(int i=0; strings[i] != NULL; i++) {
43
if( toFind == strings[i] ) {
50
void KeyboardConfig::setDefaults()
52
keyboardModel = DEFAULT_MODEL;
53
resetOldXkbOptions = false;
56
// init layouts options
57
configureLayouts = false;
59
// layouts.append(LayoutUnit(DEFAULT_LAYOUT));
60
layoutLoopCount = NO_LOOPING;
62
// switch cotrol options
63
switchingPolicy = SWITCH_POLICY_GLOBAL;
64
// stickySwitching = false;
65
// stickySwitchingDepth = 2;
74
void KeyboardConfig::load()
76
KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);
78
keyboardModel = config.readEntry("Model", "");
80
resetOldXkbOptions = config.readEntry("ResetOldOptions", false);
81
QString options = config.readEntry("Options", "");
82
xkbOptions = options.split(LIST_SEPARATOR, QString::SkipEmptyParts);
84
configureLayouts = config.readEntry("Use", false);
85
QString layoutsString = config.readEntry("LayoutList", "");
86
QStringList layoutStrings = layoutsString.split(LIST_SEPARATOR, QString::SkipEmptyParts);
87
// if( layoutStrings.isEmpty() ) {
88
// layoutStrings.append(DEFAULT_LAYOUT);
91
foreach(const QString& layoutString, layoutStrings) {
92
layouts.append(LayoutUnit(layoutString));
94
if( layouts.isEmpty() ) {
95
configureLayouts = false;
98
layoutLoopCount = config.readEntry("LayoutLoopCount", NO_LOOPING);
100
QString layoutMode = config.readEntry("SwitchMode", "Global");
101
switchingPolicy = static_cast<SwitchingPolicy>(findStringIndex(SWITCHING_POLICIES, layoutMode, SWITCH_POLICY_GLOBAL));
103
showIndicator = config.readEntry("ShowLayoutIndicator", true);
104
showFlag = config.readEntry("ShowFlag", false);
105
showSingle = config.readEntry("ShowSingle", false);
107
QString labelsStr = config.readEntry("DisplayNames", "");
108
QStringList labels = labelsStr.split(LIST_SEPARATOR, QString::KeepEmptyParts);
109
for(int i=0; i<labels.count() && i<layouts.count(); i++) {
110
if( !labels[i].isEmpty() && labels[i] != layouts[i].layout ) {
111
layouts[i].setDisplayName(labels[i]);
115
// QString shortcutsStr = config.readEntry("LayoutShortcuts", "");
116
// QStringList shortcutsList = shortcutsStr.split(LIST_SEPARATOR, QString::KeepEmptyParts);
117
// for(int i=0; i<shortcutsList.count() && i<layouts.count(); i++) {
118
// if( !shortcutsList[i].isEmpty() ) {
119
// layouts[i].setShortcut(QKeySequence(shortcutsList[i]));
123
kDebug() << "configuring layouts" << configureLayouts << "configuring options" << resetOldXkbOptions;
126
void KeyboardConfig::save()
128
KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);
130
config.writeEntry("Model", keyboardModel);
132
config.writeEntry("ResetOldOptions", resetOldXkbOptions);
133
if( resetOldXkbOptions ) {
134
config.writeEntry("Options", xkbOptions.join(LIST_SEPARATOR));
137
config.deleteEntry("Options");
140
config.writeEntry("Use", configureLayouts);
142
QStringList layoutStrings;
143
QStringList displayNames;
144
// QStringList shortcuts;
145
foreach(const LayoutUnit& layoutUnit, layouts) {
146
layoutStrings.append(layoutUnit.toString());
147
displayNames.append(layoutUnit.getRawDisplayName());
148
// shortcuts.append(layoutUnit.getShortcut().toString());
150
config.writeEntry("LayoutList", layoutStrings.join(LIST_SEPARATOR));
151
config.writeEntry("DisplayNames", displayNames.join(LIST_SEPARATOR));
152
// config.writeEntry("LayoutShortcuts", shortcuts.join(LIST_SEPARATOR));
154
config.writeEntry("LayoutLoopCount", layoutLoopCount);
156
config.writeEntry("SwitchMode", SWITCHING_POLICIES[switchingPolicy]);
158
config.writeEntry("ShowLayoutIndicator", showIndicator);
159
config.writeEntry("ShowFlag", showFlag);
160
config.writeEntry("ShowSingle", showSingle);
165
QList<LayoutUnit> KeyboardConfig::getDefaultLayouts() const
167
QList<LayoutUnit> defaultLayoutList;
169
foreach(const LayoutUnit& layoutUnit, layouts) {
170
defaultLayoutList.append(layoutUnit);
171
if( layoutLoopCount != KeyboardConfig::NO_LOOPING && i >= layoutLoopCount-1 )
175
return defaultLayoutList;
178
QList<LayoutUnit> KeyboardConfig::getExtraLayouts() const
180
if( layoutLoopCount == KeyboardConfig::NO_LOOPING )
181
return QList<LayoutUnit>();
183
return layouts.mid(layoutLoopCount, layouts.size());