source: calamares/trunk/fuentes/src/modules/locale/timezonewidget/localeglobal.cpp @ 7538

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

sync with github

File size: 5.0 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2016, 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 "localeglobal.h"
24
25#include <QTimeZone>
26
27//###
28//### Private variables
29//###
30
31QHash<QString, QHash<QString, QList<LocaleGlobal::Locale> > > LocaleGlobal::locales;
32QHash<QString, QList<LocaleGlobal::Location> > LocaleGlobal::locations;
33
34
35//###
36//### Public methods
37//###
38
39
40QString
41LocaleGlobal::Location::pretty( const QString& s )
42{
43    return QString( s ).replace( '_', ' ' ).simplified();
44}
45
46
47QString
48LocaleGlobal::Location::comment() const
49{
50    QTimeZone qtz = QTimeZone( QString( "%1/%2" )
51                                    .arg( region )
52                                    .arg( zone ).toLatin1() );
53    return qtz.comment();
54}
55
56
57void
58LocaleGlobal::init() {
59    // TODO: Error handling
60    initLocales();
61    initLocations();
62}
63
64
65
66QHash< QString, QHash< QString, QList< LocaleGlobal::Locale > > >
67LocaleGlobal::getLocales() {
68    return locales;
69}
70
71
72
73QHash< QString, QList< LocaleGlobal::Location > >
74LocaleGlobal::getLocations() {
75    return locations;
76}
77
78
79//###
80//### Private methods
81//###
82
83
84void
85LocaleGlobal::initLocales() {
86    locales.clear();
87
88    QStringList files = QDir(LOCALESDIR).entryList(QDir::Files, QDir::Name);
89
90    for (int i = 0; i < files.size(); ++i) {
91        QString filename = files.at(i);
92        QFile file(QString(LOCALESDIR) + "/" + filename);
93        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
94            continue;
95
96        QTextStream in(&file);
97        QString commentChar = "%";
98        Locale locale;
99        QString lang, territory;
100
101        locale.locale = filename;
102
103        while (!in.atEnd()) {
104            QString line = in.readLine().trimmed();
105            QStringList split = line.split(commentChar, QString::KeepEmptyParts).first().split(QRegExp(" (?=[^\"]*(\"[^\"]*\"[^\"]*)*$)"), QString::SkipEmptyParts);
106
107            if (split.size() < 2)
108                continue;
109
110            QString sub1 = QString(split.at(0)).remove("\"");
111            QString sub2 = QString(split.at(1)).remove("\"");
112
113            if (sub1 == "comment_char")
114                commentChar = sub2;
115            else if (sub1 == "title")
116                locale.description = sub2;
117            else if (sub1 == "territory")
118                territory= sub2;
119            else if (sub1 == "language")
120                lang = sub2;
121        }
122
123        if (lang.isEmpty() || territory.isEmpty())
124            continue;
125
126        locales[lang][territory].append(locale);
127    }
128}
129
130
131
132void
133LocaleGlobal::initLocations() {
134    locations.clear();
135
136    QFile file(TZ_DATA_FILE);
137    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
138        return;
139
140    QTextStream in(&file);
141    while (!in.atEnd()) {
142        QString line = in.readLine().trimmed().split('#', QString::KeepEmptyParts).first().trimmed();
143        if (line.isEmpty())
144            continue;
145
146        QStringList list = line.split(QRegExp("[\t ]"), QString::SkipEmptyParts);
147        if (list.size() < 3)
148            continue;
149
150        Location location;
151        QStringList timezone = list.at(2).split('/', QString::SkipEmptyParts);
152        int cooSplitPos = QString(list.at(1)).remove(0, 1).indexOf(QRegExp("[-+]")) + 1;
153
154        if (timezone.size() < 2)
155            continue;
156
157        QString countryCode = list.at(0).trimmed();
158        if (countryCode.size() != 2)
159            continue;
160
161        location.region = timezone.takeFirst();
162        location.zone = timezone.join( '/' );
163        location.latitude = getRightGeoLocation(list.at(1).mid(0, cooSplitPos));
164        location.longitude = getRightGeoLocation(list.at(1).mid(cooSplitPos));
165        location.country = countryCode;
166
167        locations[location.region].append(location);
168    }
169}
170
171
172
173double
174LocaleGlobal::getRightGeoLocation(QString str) {
175    double sign = 1, num = 0.00;
176
177    // Determind sign
178    if (str.startsWith('-')) {
179        sign = -1;
180        str.remove(0, 1);
181    }
182    else if (str.startsWith('+')) {
183        str.remove(0, 1);
184    }
185
186
187    if (str.length() == 4 || str.length() == 6)
188        num = str.mid(0, 2).toDouble() + str.mid(2, 2).toDouble() / 60;
189    else if (str.length() == 5 || str.length() == 7)
190        num = str.mid(0, 3).toDouble() + str.mid(3, 2).toDouble() / 60;
191
192    return sign * num;
193}
194
Note: See TracBrowser for help on using the repository browser.