source: calamares/trunk/fuentes/src/modules/keyboard/keyboardwidget/keyboardglobal.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.1 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Originally from the Manjaro Installation Framework
6 *   by Roland Singer <roland@manjaro.org>
7 *   Copyright (C) 2007 Free Software Foundation, Inc.
8 *
9 *   Calamares is free software: you can redistribute it and/or modify
10 *   it under the terms of the GNU General Public License as published by
11 *   the Free Software Foundation, either version 3 of the License, or
12 *   (at your option) any later version.
13 *
14 *   Calamares is distributed in the hope that it will be useful,
15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 *   GNU General Public License for more details.
18 *
19 *   You should have received a copy of the GNU General Public License
20 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "keyboardglobal.h"
24
25//###
26//### Public methods
27//###
28
29
30QMap<QString, KeyboardGlobal::KeyboardInfo> KeyboardGlobal::getKeyboardLayouts() {
31    return parseKeyboardLayouts(XKB_FILE);
32}
33
34
35QMap<QString, QString> KeyboardGlobal::getKeyboardModels() {
36    return parseKeyboardModels(XKB_FILE);
37}
38
39
40
41//###
42//### Private methods
43//###
44
45
46//### Source by Georg Grabler <ggrabler@gmail.com> ###//
47QMap<QString, QString> KeyboardGlobal::parseKeyboardModels(QString filepath)
48{
49    QMap<QString, QString> models;
50
51    QFile fh(filepath);
52    fh.open(QIODevice::ReadOnly);
53
54    if (!fh.isOpen()) {
55        qDebug() << "X11 Keyboard model definitions not found!";
56        return models;
57    }
58
59    bool modelsFound = false;
60    // read the file until the end or until we break the loop
61    while (!fh.atEnd()) {
62        QByteArray line = fh.readLine();
63
64        // check if we start with the model section in the file
65        if (!modelsFound && line.startsWith("! model"))
66            modelsFound = true;
67        else if (modelsFound && line.startsWith ("!"))
68            break;
69        else if (!modelsFound)
70            continue;
71
72        // here we are in the model section, otherwhise we would continue or break
73        QRegExp rx;
74        rx.setPattern("^\\s+(\\S+)\\s+(\\w.*)\n$");
75
76        // insert into the model map
77        if (rx.indexIn(line) != -1) {
78            QString modelDesc = rx.cap(2);
79            QString model = rx.cap(1);
80
81            if (model == "pc105")
82                modelDesc += "  -  " + QObject::tr("Default Keyboard Model");
83
84            models.insert(modelDesc, model);
85        }
86    }
87
88    return models;
89}
90
91
92
93QMap< QString, KeyboardGlobal::KeyboardInfo > KeyboardGlobal::parseKeyboardLayouts(QString filepath)
94{
95    QMap< QString, KeyboardInfo >  layouts;
96
97    //### Get Layouts ###//
98
99    QFile fh(filepath);
100    fh.open(QIODevice::ReadOnly);
101
102    if (!fh.isOpen()) {
103        qDebug() << "X11 Keyboard layout definitions not found!";
104        return layouts;
105    }
106
107    bool layoutsFound = false;
108    // read the file until the end or we break the loop
109    while (!fh.atEnd()) {
110        QByteArray line = fh.readLine();
111
112        // find the layout section otherwhise continue. If the layout section is at it's end, break the loop
113        if (!layoutsFound && line.startsWith("! layout"))
114            layoutsFound = true;
115        else if (layoutsFound && line.startsWith ("!"))
116            break;
117        else if (!layoutsFound)
118            continue;
119
120        QRegExp rx;
121        rx.setPattern("^\\s+(\\S+)\\s+(\\w.*)\n$");
122
123        // insert into the layout map
124        if (rx.indexIn(line) != -1) {
125            KeyboardInfo info;
126            info.description = rx.cap(2);
127            info.variants.insert(QObject::tr("Default"), "");
128            layouts.insert(rx.cap(1), info);
129        }
130    }
131
132    fh.reset();
133
134
135    //### Get Variants ###//
136
137    bool variantsFound = false;
138    // read the file until the end or until we break
139    while (!fh.atEnd()) {
140        QByteArray line = fh.readLine();
141
142        // continue until we found the variant section. If found, read until the next section is found
143        if (!variantsFound && line.startsWith("! variant")) {
144            variantsFound = true;
145            continue;
146        } else if (variantsFound && line.startsWith ("!"))
147            break;
148        else if (!variantsFound)
149            continue;
150
151        QRegExp rx;
152        rx.setPattern("^\\s+(\\S+)\\s+(\\S+): (\\w.*)\n$");
153
154        // insert into the variants multimap, if the pattern matches
155        if (rx.indexIn(line) != -1) {
156            if (layouts.find(rx.cap(2)) != layouts.end()) {
157                // in this case we found an entry in the multimap, and add the values to the multimap
158                layouts.find(rx.cap(2)).value().variants.insert(rx.cap(3), rx.cap(1));
159            } else {
160                // create a new map in the multimap - the value was not found.
161                KeyboardInfo info;
162                info.description = rx.cap(2);
163                info.variants.insert(QObject::tr("Default"), "");
164                info.variants.insert(rx.cap(3), rx.cap(1));
165                layouts.insert(rx.cap(2), info);
166            }
167        }
168    }
169
170    return layouts;
171}
Note: See TracBrowser for help on using the repository browser.