keyboard_config.cpp 7.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "keyboard_config.h"
20
#include "debug.h"
21

Laurent Montel's avatar
Laurent Montel committed
22 23
#include <KSharedConfig>
#include <KConfigGroup>
24 25


26
static const char* const SWITCHING_POLICIES[] = {"Global", "Desktop", "WinClass", "Window", nullptr };
27
static const char LIST_SEPARATOR[] = ",";
28
//static const char* DEFAULT_LAYOUT = "us";
29
static const char DEFAULT_MODEL[] = "pc104";
30

31 32
static const QString CONFIG_FILENAME(QStringLiteral("kxkbrc"));
static const QString CONFIG_GROUPNAME(QStringLiteral("Layout"));
33 34 35 36 37 38 39 40 41 42 43 44

const int KeyboardConfig::NO_LOOPING = -1;

KeyboardConfig::KeyboardConfig()
{
    setDefaults();
}

QString KeyboardConfig::getSwitchingPolicyString(SwitchingPolicy switchingPolicy) {
	return SWITCHING_POLICIES[switchingPolicy];
}

45
static int findStringIndex(const char* const strings[], const QString& toFind, int defaultIndex)
46
{
47
	for(int i=0; strings[i] != nullptr; i++) {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		if( toFind == strings[i] ) {
			return i;
		}
	}
	return defaultIndex;
}

void KeyboardConfig::setDefaults()
{
	keyboardModel = DEFAULT_MODEL;
	resetOldXkbOptions = false;
	xkbOptions.clear();

	// init layouts options
	configureLayouts = false;
	layouts.clear();
//	layouts.append(LayoutUnit(DEFAULT_LAYOUT));
	layoutLoopCount = NO_LOOPING;

Yuri Chornoivan's avatar
Yuri Chornoivan committed
67
	// switch control options
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	switchingPolicy = SWITCH_POLICY_GLOBAL;
//	stickySwitching = false;
//	stickySwitchingDepth = 2;

	// display options
	showIndicator = true;
	indicatorType = SHOW_LABEL;
	showSingle = false;
}

static
KeyboardConfig::IndicatorType getIndicatorType(bool showFlag, bool showLabel)
{
	if( showFlag ) {
		if( showLabel )
			return KeyboardConfig::SHOW_LABEL_ON_FLAG;
		else
			return KeyboardConfig::SHOW_FLAG;
	}
	else {
		return KeyboardConfig::SHOW_LABEL;
	}
}


void KeyboardConfig::load()
{
    KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);

    keyboardModel = config.readEntry("Model", "");

    resetOldXkbOptions = config.readEntry("ResetOldOptions", false);
    QString options = config.readEntry("Options", "");
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
101
    xkbOptions = options.split(LIST_SEPARATOR, Qt::SkipEmptyParts);
102 103

    configureLayouts = config.readEntry("Use", false);
104 105
    const QStringList layoutStrings = config.readEntry("LayoutList", QStringList());
    const QStringList variants = config.readEntry("VariantList", QStringList());
106
    layouts.clear();
107 108 109 110 111 112
    if (layoutStrings.isEmpty()) {
        QList<LayoutUnit> x11layouts = X11Helper::getLayoutsList();
        for (const LayoutUnit& layoutUnit : x11layouts) {
            layouts.append(layoutUnit);
        }
    } else {
113 114 115 116 117 118 119
        QStringList::ConstIterator layout = layoutStrings.begin();
        const int range = qMin(layoutStrings.size(), variants.size());
        for (int i = 0; i < range; ++i) {
            layouts.append({*layout++, variants.at(i)});
        }
        while (layout != layoutStrings.end()) {
            layouts.append(LayoutUnit(*layout++));
120
        }
121
    }
122
    configureLayouts = !layouts.isEmpty();
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    layoutLoopCount = config.readEntry("LayoutLoopCount", NO_LOOPING);

	QString layoutMode = config.readEntry("SwitchMode", "Global");
	switchingPolicy = static_cast<SwitchingPolicy>(findStringIndex(SWITCHING_POLICIES, layoutMode, SWITCH_POLICY_GLOBAL));

	showIndicator = config.readEntry("ShowLayoutIndicator", true);

	bool showFlag = config.readEntry("ShowFlag", false);
	bool showLabel = config.readEntry("ShowLabel", true);
	indicatorType = getIndicatorType(showFlag, showLabel);

	showSingle = config.readEntry("ShowSingle", false);

    QString labelsStr = config.readEntry("DisplayNames", "");
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
138
    QStringList labels = labelsStr.split(LIST_SEPARATOR, Qt::KeepEmptyParts);
139
    for(int i=0; i<labels.count() && i<layouts.count(); i++) {
140
        if( !labels[i].isEmpty() && labels[i] != layouts[i].layout() ) {
141 142 143 144 145 146 147 148 149 150 151 152
    		layouts[i].setDisplayName(labels[i]);
    	}
    }

//    QString shortcutsStr = config.readEntry("LayoutShortcuts", "");
//    QStringList shortcutsList = shortcutsStr.split(LIST_SEPARATOR, QString::KeepEmptyParts);
//    for(int i=0; i<shortcutsList.count() && i<layouts.count(); i++) {
//    	if( !shortcutsList[i].isEmpty() ) {
//    		layouts[i].setShortcut(QKeySequence(shortcutsList[i]));
//    	}
//    }

Andriy Rysin's avatar
Andriy Rysin committed
153
	qCDebug(KCM_KEYBOARD) << "configuring layouts" << configureLayouts << "configuring options" << resetOldXkbOptions;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

void KeyboardConfig::save()
{
    KConfigGroup config(KSharedConfig::openConfig( CONFIG_FILENAME, KConfig::NoGlobals ), CONFIG_GROUPNAME);

    config.writeEntry("Model", keyboardModel);

    config.writeEntry("ResetOldOptions", resetOldXkbOptions);
    if( resetOldXkbOptions ) {
    	config.writeEntry("Options", xkbOptions.join(LIST_SEPARATOR));
    }
    else {
        config.deleteEntry("Options");
    }

    config.writeEntry("Use", configureLayouts);

    QStringList layoutStrings;
173
    QStringList variants;
174 175 176
    QStringList displayNames;
//    QStringList shortcuts;
    foreach(const LayoutUnit& layoutUnit, layouts) {
177 178 179
        layoutStrings.append(layoutUnit.layout());
        variants.append(layoutUnit.variant());
        displayNames.append(layoutUnit.getRawDisplayName());
180 181
//    	shortcuts.append(layoutUnit.getShortcut().toString());
    }
182 183 184 185 186 187 188 189 190 191

    auto cleanTail = [](QStringList &list)
    {
        while (!list.isEmpty() && list.constLast().isEmpty()) {
            list.removeLast();
        }
    };
    cleanTail(variants);
    cleanTail(displayNames);

192
    config.writeEntry("LayoutList", layoutStrings.join(LIST_SEPARATOR));
193
    config.writeEntry("VariantList", variants);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    config.writeEntry("DisplayNames", displayNames.join(LIST_SEPARATOR));
//    config.writeEntry("LayoutShortcuts", shortcuts.join(LIST_SEPARATOR));

    config.writeEntry("LayoutLoopCount", layoutLoopCount);

	config.writeEntry("SwitchMode", SWITCHING_POLICIES[switchingPolicy]);

	config.writeEntry("ShowLayoutIndicator", showIndicator);
	config.writeEntry("ShowFlag", indicatorType == SHOW_FLAG || indicatorType == SHOW_LABEL_ON_FLAG);
	config.writeEntry("ShowLabel", indicatorType == SHOW_LABEL || indicatorType == SHOW_LABEL_ON_FLAG);
	config.writeEntry("ShowSingle", showSingle);

	config.sync();
}

QList<LayoutUnit> KeyboardConfig::getDefaultLayouts() const
{
	QList<LayoutUnit> defaultLayoutList;
	int i = 0;
	foreach(const LayoutUnit& layoutUnit, layouts) {
		defaultLayoutList.append(layoutUnit);
		if( layoutLoopCount != KeyboardConfig::NO_LOOPING && i >= layoutLoopCount-1 )
			break;
		i++;
	}
	return defaultLayoutList;
}

QList<LayoutUnit> KeyboardConfig::getExtraLayouts() const
{
	if( layoutLoopCount == KeyboardConfig::NO_LOOPING )
		return QList<LayoutUnit>();

	return layouts.mid(layoutLoopCount, layouts.size());
}