not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kxkb / rules.cpp
blob69c18d07dc2628de112f78babde5effdbf5c6ba4
1 /*
2 * Copyright (C) 2003-2006 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 <QtGui/QWidgetList>
20 #include <QX11Info>
21 #include <QRegExp>
22 #include <QTextStream>
23 #include <QFile>
24 #include <QDir>
26 #include <kstandarddirs.h>
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <kdebug.h>
30 #include <config-workspace.h>
32 #include "x11helper.h"
33 #include "rules.h"
35 #ifdef HAVE_XKLAVIER
36 #include "xklavier_adaptor.h"
37 #endif
39 #include "kxkbconfig.h"
42 static const QRegExp NON_CLEAN_LAYOUT_REGEXP("[^a-z]");
44 //bool XkbRules::m_layoutsClean = true;
46 XkbRules::XkbRules(bool layoutsOnly)
48 #ifdef HAVE_XKLAVIER
49 loadNewRules(layoutsOnly);
50 #else
51 X11_DIR = X11Helper::findX11Dir();
53 if( X11_DIR == NULL ) {
54 kError() << "Cannot find X11 directory!" << endl;
55 // throw Exception();
56 return;
59 QString rulesFile = X11Helper::findXkbRulesFile(X11_DIR, QX11Info::display());
61 if( rulesFile.isEmpty() ) {
62 kError() << "Cannot find rules file in " << X11_DIR << endl;
63 // throw Exception();
64 return;
67 loadRules(rulesFile, layoutsOnly);
68 #endif
73 #ifdef HAVE_XKLAVIER
75 void XkbRules::loadNewRules(bool layoutsOnly)
77 XKlavierAdaptor* xklAdaptor = XKlavierAdaptor::getInstance(QX11Info::display());
78 xklAdaptor->loadXkbConfig(layoutsOnly);
80 m_layouts = xklAdaptor->getLayouts();
81 if( layoutsOnly == false ) {
82 m_models = xklAdaptor->getModels();
83 m_varLists = xklAdaptor->getVariants();
84 m_optionGroups = xklAdaptor->getOptionGroups();
85 m_options = xklAdaptor->getOptions();
87 QHashIterator<QString, XkbOption> it( m_options );
88 for (; it.hasNext(); ) {
89 const XkbOption& option = it.next().value();
90 option.group->options.append(option);
95 #else
97 void XkbRules::loadRules(const QString &file, bool layoutsOnly)
99 RulesInfo* rules = X11Helper::loadRules(file, layoutsOnly);
101 if (rules == NULL) {
102 kDebug() << "Unable to load rules";
103 return;
106 m_layouts = rules->layouts;
108 if( layoutsOnly == false ) {
109 m_models = rules->models;
110 // m_varLists = rules->variants;
111 m_options = rules->options;
112 m_optionGroups = rules->optionGroups;
114 QHashIterator<QString, XkbOption> it( m_options );
115 for (; it.hasNext(); ) {
116 const XkbOption& option = it.next().value();
117 option.group->options.append(option);
123 #endif
125 QList<XkbVariant>
126 XkbRules::getAvailableVariants(const QString& layout)
128 if( layout.isEmpty() || !layouts().contains(layout) )
129 return QList<XkbVariant>();
131 QList<XkbVariant>* result1 = m_varLists[layout];
133 #ifdef HAVE_XKLAVIER
134 return *result1;
135 #else
136 if( result1 )
137 return *result1;
139 QList<XkbVariant>* result = X11Helper::getVariants(layout, X11_DIR);
141 m_varLists.insert(layout, result);
142 return *result;
143 #endif