~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/keyboard_config.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "keyboard_config.h"
 
20
 
 
21
#include <ksharedconfig.h>
 
22
#include <kconfiggroup.h>
 
23
#include <kdebug.h>
 
24
 
 
25
 
 
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";
 
30
 
 
31
static const QString CONFIG_FILENAME("kxkbrc");
 
32
static const QString CONFIG_GROUPNAME("Layout");
 
33
 
 
34
const int KeyboardConfig::NO_LOOPING = -1;
 
35
 
 
36
QString KeyboardConfig::getSwitchingPolicyString(SwitchingPolicy switchingPolicy) {
 
37
        return SWITCHING_POLICIES[switchingPolicy];
 
38
}
 
39
 
 
40
static int findStringIndex(const char* strings[], const QString& toFind, int defaultIndex)
 
41
{
 
42
        for(int i=0; strings[i] != NULL; i++) {
 
43
                if( toFind == strings[i] ) {
 
44
                        return i;
 
45
                }
 
46
        }
 
47
        return defaultIndex;
 
48
}
 
49
 
 
50
void KeyboardConfig::setDefaults()
 
51
{
 
52
        keyboardModel = DEFAULT_MODEL;
 
53
        resetOldXkbOptions = false;
 
54
        xkbOptions.clear();
 
55
 
 
56
        // init layouts options
 
57
        configureLayouts = false;
 
58
        layouts.clear();
 
59
//      layouts.append(LayoutUnit(DEFAULT_LAYOUT));
 
60
        layoutLoopCount = NO_LOOPING;
 
61
 
 
62
        // switch cotrol options
 
63
        switchingPolicy = SWITCH_POLICY_GLOBAL;
 
64
//      stickySwitching = false;
 
65
//      stickySwitchingDepth = 2;
 
66
 
 
67
        // display options
 
68
        showIndicator = true;
 
69
        showFlag = true;
 
70
        showSingle = false;
 
71
}
 
72
 
 
73
 
 
74
void KeyboardConfig::load()
 
75
{
 
76
    KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);
 
77
 
 
78
    keyboardModel = config.readEntry("Model", "");
 
79
 
 
80
    resetOldXkbOptions = config.readEntry("ResetOldOptions", false);
 
81
    QString options = config.readEntry("Options", "");
 
82
    xkbOptions = options.split(LIST_SEPARATOR, QString::SkipEmptyParts);
 
83
 
 
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);
 
89
//    }
 
90
    layouts.clear();
 
91
    foreach(const QString& layoutString, layoutStrings) {
 
92
        layouts.append(LayoutUnit(layoutString));
 
93
    }
 
94
    if( layouts.isEmpty() ) {
 
95
        configureLayouts = false;
 
96
    }
 
97
 
 
98
    layoutLoopCount = config.readEntry("LayoutLoopCount", NO_LOOPING);
 
99
 
 
100
        QString layoutMode = config.readEntry("SwitchMode", "Global");
 
101
        switchingPolicy = static_cast<SwitchingPolicy>(findStringIndex(SWITCHING_POLICIES, layoutMode, SWITCH_POLICY_GLOBAL));
 
102
 
 
103
        showIndicator = config.readEntry("ShowLayoutIndicator", true);
 
104
        showFlag = config.readEntry("ShowFlag", false);
 
105
        showSingle = config.readEntry("ShowSingle", false);
 
106
 
 
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]);
 
112
        }
 
113
    }
 
114
 
 
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]));
 
120
//      }
 
121
//    }
 
122
 
 
123
        kDebug() << "configuring layouts" << configureLayouts << "configuring options" << resetOldXkbOptions;
 
124
}
 
125
 
 
126
void KeyboardConfig::save()
 
127
{
 
128
    KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);
 
129
 
 
130
    config.writeEntry("Model", keyboardModel);
 
131
 
 
132
    config.writeEntry("ResetOldOptions", resetOldXkbOptions);
 
133
    if( resetOldXkbOptions ) {
 
134
        config.writeEntry("Options", xkbOptions.join(LIST_SEPARATOR));
 
135
    }
 
136
    else {
 
137
        config.deleteEntry("Options");
 
138
    }
 
139
 
 
140
    config.writeEntry("Use", configureLayouts);
 
141
 
 
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());
 
149
    }
 
150
    config.writeEntry("LayoutList", layoutStrings.join(LIST_SEPARATOR));
 
151
    config.writeEntry("DisplayNames", displayNames.join(LIST_SEPARATOR));
 
152
//    config.writeEntry("LayoutShortcuts", shortcuts.join(LIST_SEPARATOR));
 
153
 
 
154
    config.writeEntry("LayoutLoopCount", layoutLoopCount);
 
155
 
 
156
        config.writeEntry("SwitchMode", SWITCHING_POLICIES[switchingPolicy]);
 
157
 
 
158
        config.writeEntry("ShowLayoutIndicator", showIndicator);
 
159
        config.writeEntry("ShowFlag", showFlag);
 
160
        config.writeEntry("ShowSingle", showSingle);
 
161
 
 
162
        config.sync();
 
163
}
 
164
 
 
165
QList<LayoutUnit> KeyboardConfig::getDefaultLayouts() const
 
166
{
 
167
        QList<LayoutUnit> defaultLayoutList;
 
168
        int i = 0;
 
169
        foreach(const LayoutUnit& layoutUnit, layouts) {
 
170
                defaultLayoutList.append(layoutUnit);
 
171
                if( layoutLoopCount != KeyboardConfig::NO_LOOPING && i >= layoutLoopCount-1 )
 
172
                        break;
 
173
                i++;
 
174
        }
 
175
        return defaultLayoutList;
 
176
}
 
177
 
 
178
QList<LayoutUnit> KeyboardConfig::getExtraLayouts() const
 
179
{
 
180
        if( layoutLoopCount == KeyboardConfig::NO_LOOPING )
 
181
                return QList<LayoutUnit>();
 
182
 
 
183
        return layouts.mid(layoutLoopCount, layouts.size());
 
184
}