aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/theme/mlogicalvalues.cpp
blob: dd3ed229935c6db9471d0aba627588f912b7fd1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mlogicalvalues.h"
#include "mlogicalvalues_p.h"
#include "mstylesheetattribute.h"
#include "mdebug.h"
#include "msystemdirectories.h"

#include <QDir>
#include <QFile>
#include <QStringList>
#include <QTextCodec>
#include <QDateTime>
#include <QCoreApplication>

namespace {
    const unsigned int CACHE_VERSION = 1;
}

MLogicalValues::MLogicalValues() :
    d_ptr(new MLogicalValuesPrivate)
{
}

MLogicalValues::~MLogicalValues()
{
    delete d_ptr;
}

bool MLogicalValuesPrivate::parse(const QFileInfo &fileInfo, Groups &groups)
{
    QFile file(fileInfo.filePath());
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QByteArray group = "General";
    groups.insert("General", Values());

    while (!file.atEnd()) {
        QByteArray line = file.readLine().trimmed();
        // skip comments
        if (line.startsWith("[")) {
            // parse group header
            int index = line.indexOf("]", 1);
            if (index == -1) {
                mWarning("MLogicalValues") << "Error occurred when parsing .ini file:" << line;
                file.close();
                return false;
            }
            // this will be the currently active group
            group = line.mid(1, index - 1);
        } else {

            // key/value pair
            QByteArray key, value;
            QByteArray *target = &key;

            // stores the last 'good' character
            int truncation = 0;
            // go through whole line
            for (int i = 0; i < line.length(); i++) {
                QChar character = line.at(i);
                if (character == ';') {
                    break;
                } else if (character == '=') {
                    // remove trailing whitespaces
                    target->truncate(truncation);
                    // start to parse value
                    target = &value;
                    truncation = 0;
                } else {
                    if (target->isEmpty() && character.isSpace()) {
                        // do not add whitespaces at the beginning
                    } else {
                        (*target) += character;
                        if (!character.isSpace())
                            truncation = target->length();
                    }
                }
            }
            // remove trailing whitespaces
            target->truncate(truncation);

            // consistency check
            if (!line.startsWith(';') && line.length() > 0) {
                if (key.isEmpty() || value.isEmpty()) {
                    mWarning("MLogicalValues") << "Error occurred when parsing .ini file:" << line;
                    file.close();
                    return false;
                }
                // store
                Values &values = groups[group];

                if (!values.contains(key)) {
                    values.insert(key, value);
                }
            }
        }
    }

    saveToBinaryCache(fileInfo, groups);
    file.close();
    return true;
}

bool MLogicalValuesPrivate::loadFromBinaryCache(const QFileInfo &fileInfo, Groups &groups) {
    const QString cacheFileName = createBinaryFilename(fileInfo);

    if (QFile::exists(cacheFileName)) {
        QFile file(cacheFileName);
        if (file.open(QFile::ReadOnly)) {
            QDataStream stream(&file);
            uint version;
            stream >> version;
            if (version != CACHE_VERSION) {
                // will be replaced with up to date version
                file.close();
                return false;
            }
            uint timestamp;
            stream >> timestamp;
            if (timestamp != fileInfo.lastModified().toTime_t()) {
                // will be replaced with up to date version
                file.close();
                return false;
            }

            stream >> groups;

            file.close();
            return true;
        } else {
            mDebug("MLogicalValuesPrivate") << "Failed to load values from cache" << cacheFileName;
        }
    }

    return false;
}

bool MLogicalValuesPrivate::saveToBinaryCache(const QFileInfo &fileInfo, const Groups &groups) const {
    const QString cacheFileName = createBinaryFilename(fileInfo);

    QFile file(cacheFileName);
    if (!file.open(QFile::WriteOnly)) {
        //Maybe it failed because the directory doesn't exist
        QDir().mkpath(QFileInfo(cacheFileName).absolutePath());
        if (!file.open(QFile::WriteOnly)) {
            mDebug("MLogicalValuesPrivate") << "Failed to save cache file for" << fileInfo.fileName() << "to" << cacheFileName;
            return false;
        }
    }

    QDataStream stream(&file);
    stream << CACHE_VERSION;
    stream << fileInfo.lastModified().toTime_t();
    stream << groups;

    file.close();
    return true;
}

QString MLogicalValuesPrivate::createBinaryFilename(const QFileInfo &fileInfo) const {
    QString binaryDirectory = MSystemDirectories::cacheDirectory() + QLatin1String("logicalValues") + QDir::separator();
    QString binaryFilename(binaryDirectory);

    QString absoluteFilePathEncoded(fileInfo.absoluteFilePath());
    absoluteFilePathEncoded.replace('_', "__");
    absoluteFilePathEncoded.replace('/', "_.");
    binaryFilename += absoluteFilePathEncoded;
    return binaryFilename;
}

void MLogicalValuesPrivate::mergeGroups(const Groups &groups)
{
    Groups::const_iterator i = groups.constBegin();
    while (i != groups.constEnd()) {
        Values &values = data[i.key()];
        Values::const_iterator j = i.value().constBegin();
        while (j != i.value().constEnd()) {
            if (!values.contains(j.key())) {
                values.insert(j.key(), j.value());
            }
            ++j;
        }
        ++i;
    }
}

bool MLogicalValues::append(const QString &fileName)
{
    Q_D(MLogicalValues);

    // make sure that the file exists
    if (!QFile(fileName).exists())
        return false;

    Groups groups;
    QFileInfo fileInfo(fileName);
    if (!d->loadFromBinaryCache(fileInfo, groups)) {
        if (!d->parse(fileInfo, groups)) {
            return false;
        }
    }

    d->timestamps << fileInfo.lastModified().toTime_t();

    d->mergeGroups(groups);

    return true;
}

void MLogicalValues::load(const QStringList &themeInheritanceChain, const QString &locale)
{
    Q_D(MLogicalValues);

    d->data.clear();
    d->timestamps.clear();

    // load locale-specific constant definitions
    if (!locale.isEmpty()) {
        // go through whole inheritance hierarchy
        foreach(QString path, themeInheritanceChain) {
            append(path + QString("meegotouch") + QDir::separator() + QString("locale") + QDir::separator() + locale + QDir::separator() + QString("constants.ini"));
        }
    }

    // go through whole inheritance hierarchy
    foreach(QString path, themeInheritanceChain) {
        append(path + QString("meegotouch") + QDir::separator() + QString("constants.ini"));
    }
}

bool MLogicalValues::findKey(const QByteArray &key, QByteArray &group, QByteArray &value) const
{
    Q_D(const MLogicalValues);

    // search from every group
    for (Groups::const_iterator iterator = d->data.begin(); iterator != d->data.end(); iterator++) {
        // get values from this group
        const Values &values = iterator.value();

        // check if this group contains the key
        if (values.contains(key)) {
            group = iterator.key();
            value = values.value(key);
            return true;
        }
    }

    return false;
}


bool MLogicalValues::value(const QByteArray &group, const QByteArray &key, QByteArray &value) const
{
    Q_D(const MLogicalValues);
    if (!d->data.contains(group)) {
        mWarning("MLogicalValues") << "No such group:" << group;
        return false;
    }

    const Values &values = d->data[group];

    if (!values.contains(key)) {
        mWarning("MLogicalValues") << "No such key:" << group << '/' << key;
        return false;
    }

    value = values.value(key);
    return true;
}


QColor MLogicalValues::color(const QByteArray &group, const QByteArray &key) const
{
    QByteArray string;
    if (!value(group, key, string)) {
        return QColor();
    }

    bool conversionOk = false;
    QColor color = MStyleSheetAttribute::colorFromString(string, &conversionOk);
    if (!conversionOk) {
        mWarning("MLogicalValues") << "Invalid logical color definition for" << key << ':' << string;
    }
    return color;
}

QFont MLogicalValues::font(const QByteArray &group, const QByteArray &key) const
{
    QByteArray string;
    if (!value(group, key, string)) {
        return QFont();
    }

    bool conversionOk = false;
    QFont font = MStyleSheetAttribute::fontFromString(string, &conversionOk);
    if (!conversionOk) {
        mWarning("MLogicalValues") << "Invalid logical font definition for" << key << ':' << string;
    }
    return font;
}

QList<uint> MLogicalValues::timestamps() const
{
    Q_D(const MLogicalValues);
    return d->timestamps;
}