summaryrefslogtreecommitdiff
path: root/TelepathyQt4/key-file.cpp
blob: d800bb1482e0f57406333e4ea15226eae9790a51 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2008-2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2008-2009 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt4/KeyFile>

#include "TelepathyQt4/debug-internal.h"

#include <TelepathyQt4/Utils>

#include <QtCore/QByteArray>
#include <QtCore/QFile>
#include <QtCore/QHash>
#include <QtCore/QString>
#include <QtCore/QStringList>

namespace Tp
{

struct TELEPATHY_QT4_NO_EXPORT KeyFile::Private
{
    Private();
    Private(const QString &fName);

    void setFileName(const QString &fName);
    void setError(KeyFile::Status status, const QString &reason);
    bool read();

    bool validateKey(const QByteArray &data, int from, int to, QString &result);

    QStringList allGroups() const;
    QStringList allKeys() const;
    QStringList keys() const;
    bool contains(const QString &key) const;
    QString rawValue(const QString &key) const;
    QString value(const QString &key) const;
    QStringList valueAsStringList(const QString &key) const;

    QString fileName;
    KeyFile::Status status;
    QHash<QString, QHash<QString, QByteArray> > groups;
    QString currentGroup;
};

KeyFile::Private::Private()
    : status(KeyFile::None)
{
}

KeyFile::Private::Private(const QString &fName)
    : fileName(fName),
      status(KeyFile::NoError)
{
    read();
}

void KeyFile::Private::setFileName(const QString &fName)
{
    fileName = fName;
    status = KeyFile::NoError;
    currentGroup = QString();
    groups.clear();
    read();
}

void KeyFile::Private::setError(KeyFile::Status st, const QString &reason)
{
    warning() << QString(QLatin1String("ERROR: filename(%1) reason(%2)"))
                         .arg(fileName).arg(reason);
    status = st;
    groups.clear();
}

bool KeyFile::Private::read()
{
    QFile file(fileName);
    if (!file.exists()) {
        setError(KeyFile::NotFoundError,
                 QLatin1String("file does not exist"));
        return false;
    }

    if (!file.open(QFile::ReadOnly)) {
        setError(KeyFile::AccessError,
                 QLatin1String("cannot open file for readonly access"));
        return false;
    }

    QByteArray data;
    QByteArray group;
    QString currentGroup;
    QHash<QString, QByteArray> groupMap;
    QByteArray rawValue;
    int line = 0;
    int idx;
    while (!file.atEnd()) {
        data = file.readLine().trimmed();
        line++;

        if (data.size() == 0) {
            // skip empty lines
            continue;
        }

        char ch = data.at(0);
        if (ch == '#') {
            // skip comments
            continue;
        }
        else if (ch == '[') {
            if (groupMap.size()) {
                groups[currentGroup] = groupMap;
                groupMap.clear();
            }

            idx = data.indexOf(']');
            if (idx == -1) {
                // line starts with [ and it's not a group
                setError(KeyFile::FormatError,
                         QString(QLatin1String("invalid group at line %2 - missing ']'"))
                                 .arg(line));
                return false;
            }

            group = data.mid(1, idx - 1).trimmed();
            if (groups.contains(QLatin1String(group))) {
                setError(KeyFile::FormatError,
                         QString(QLatin1String("duplicated group '%1' at line %2"))
                                 .arg(QLatin1String(group)).arg(line));
                return false;
            }

            currentGroup = QLatin1String("");
            if (!unescapeString(group, 0, group.size(), currentGroup)) {
                setError(KeyFile::FormatError,
                         QString(QLatin1String("invalid group '%1' at line %2"))
                                 .arg(currentGroup).arg(line));
                return false;
            }
        }
        else {
            idx = data.indexOf('=');
            if (idx == -1) {
                setError(KeyFile::FormatError,
                         QString(QLatin1String("format error at line %1 - missing '='"))
                                 .arg(line));
                return false;
            }

            // remove trailing spaces
            char ch;
            int idxKeyEnd = idx;
            while ((ch = data.at(idxKeyEnd - 1)) == ' ' || ch == '\t') {
                --idxKeyEnd;
            }

            QString key;
            if (!validateKey(data, 0, idxKeyEnd, key)) {
                setError(KeyFile::FormatError,
                         QString(QLatin1String("invalid key '%1' at line %2"))
                                 .arg(key).arg(line));
                return false;
            }

            if (groupMap.contains(key)) {
                setError(KeyFile::FormatError,
                         QString(QLatin1String("duplicated key '%1' on group '%2' at line %3"))
                                 .arg(key).arg(currentGroup).arg(line));
                return false;
            }

            data = data.mid(idx + 1).trimmed();
            rawValue = data.mid(0, data.size());
            groupMap[key] = rawValue;
        }
    }

    if (groupMap.size()) {
        groups[currentGroup] = groupMap;
        groupMap.clear();
    }

    return true;
}

bool KeyFile::Private::validateKey(const QByteArray &data, int from, int to, QString &result)
{
    int i = from;
    bool ret = true;
    while (i < to) {
        uint ch = data.at(i++);
        // as an extension to the Desktop Entry spec, we allow " ", "_", "." and "@"
        // as valid key characters - "_" and "." are needed for keys that are
        // D-Bus property names, and GKeyFile and KConfigIniBackend also accept
        // all four of those characters.
        if (!((ch >= 'a' && ch <= 'z') ||
              (ch >= 'A' && ch <= 'Z') ||
              (ch >= '0' && ch <= '9') ||
              (ch == ' ') ||
              (ch == '-') || (ch == '_') ||
              (ch == '.') || (ch == '@'))) {
            ret = false;
        }
        result += ch;
    }
    return ret;
}

QStringList KeyFile::Private::allGroups() const
{
    return groups.keys();
}

QStringList KeyFile::Private::allKeys() const
{
    QStringList keys;
    QHash<QString, QHash<QString, QByteArray> >::const_iterator itrGroups = groups.begin();
    while (itrGroups != groups.end()) {
        keys << itrGroups.value().keys();
        ++itrGroups;
    }
    return keys;
}

QStringList KeyFile::Private::keys() const
{
    QHash<QString, QByteArray> groupMap = groups[currentGroup];
    return groupMap.keys();
}

bool KeyFile::Private::contains(const QString &key) const
{
    QHash<QString, QByteArray> groupMap = groups[currentGroup];
    return groupMap.contains(key);
}

QString KeyFile::Private::rawValue(const QString &key) const
{
    QHash<QString, QByteArray> groupMap = groups[currentGroup];
    QByteArray rawValue = groupMap.value(key);
    return QLatin1String(rawValue);
}

QString KeyFile::Private::value(const QString &key) const
{
    QHash<QString, QByteArray> groupMap = groups[currentGroup];
    QString result;
    QByteArray rawValue = groupMap.value(key);
    if (unescapeString(rawValue, 0, rawValue.size(), result)) {
        return result;
    }
    return QString();
}

QStringList KeyFile::Private::valueAsStringList(const QString &key) const
{
    QHash<QString, QByteArray> groupMap = groups[currentGroup];
    QStringList result;
    QByteArray rawValue = groupMap.value(key);
    if (unescapeStringList(rawValue, 0, rawValue.size(), result)) {
        return result;
    }
    return QStringList();
}

/**
 * \class KeyFile
 * \headerfile TelepathyQt4/key-file.h <TelepathyQt4/KeyFile>
 *
 * \brief The KeyFile class provides an easy way to read key-pair files such as
 * INI style files and .desktop files.
 *
 * It follows the rules regarding string escaping as defined in
 * http://standards.freedesktop.org/desktop-entry-spec/latest/index.html
 */

/**
 * Create a KeyFile object used to read (key-pair) compliant files.
 *
 * The status will be KeyFile::None
 * \sa setFileName()
 */
KeyFile::KeyFile()
    : mPriv(new Private())
{
}

/**
 * Create a KeyFile object used to read (key-pair) compliant files.
 *
 * \param fileName Name of the file to be read.
 */
KeyFile::KeyFile(const QString &fileName)
    : mPriv(new Private(fileName))
{
}

/**
 * Create a KeyFile object used to read (key-pair) compliant files.
 */
KeyFile::KeyFile(const KeyFile &other)
    : mPriv(new Private())
{
    mPriv->fileName = other.mPriv->fileName;
    mPriv->status = other.mPriv->status;
    mPriv->groups = other.mPriv->groups;
    mPriv->currentGroup = other.mPriv->currentGroup;
}

/**
 * Class destructor.
 */
KeyFile::~KeyFile()
{
    delete mPriv;
}

KeyFile &KeyFile::operator=(const KeyFile &other)
{
    mPriv->fileName = other.mPriv->fileName;
    mPriv->status = other.mPriv->status;
    mPriv->groups = other.mPriv->groups;
    mPriv->currentGroup = other.mPriv->currentGroup;
    return *this;
}

/**
 * Set the name of the file to be read.
 *
 * \param fileName Name of the file to be read.
 */
void KeyFile::setFileName(const QString &fileName)
{
    mPriv->setFileName(fileName);
}

/**
 * Return the name of the file associated with this object.
 *
 * \return Name of the file associated with this object.
 */
QString KeyFile::fileName() const
{
    return mPriv->fileName;
}

/**
 * Return a status code indicating the first error that was met by #KeyFile,
 * or KeyFile::NoError if no error occurred.
 *
 * Make sure to use this method if you set the filename to be read using
 * setFileName().
 *
 * \return Status code.
 * \sa setFileName()
 */
KeyFile::Status KeyFile::status() const
{
    return mPriv->status;
}

/**
 * Set the current group to be used while reading keys.
 *
 * Query functions such as keys(), contains() and value() are based on this
 * group.
 *
 * By default a empty group is used as the group for global
 * keys and is used as the default group if none is set.
 *
 * \param group Name of the group to be used.
 * \sa group()
 */
void KeyFile::setGroup(const QString &group)
{
    mPriv->currentGroup = group;
}

/**
 * Return the current group.
 *
 * \return Name of the current group.
 * \sa setGroup()
 */
QString KeyFile::group() const
{
    return mPriv->currentGroup;
}

/**
 * Return all groups in the desktop file.
 *
 * Global keys will be added to a empty group.
 *
 * \return List of all groups in the desktop file.
 */
QStringList KeyFile::allGroups() const
{
    return mPriv->allGroups();
}

/**
 * Return all keys described in the desktop file.
 *
 * \return List of all keys in the desktop file.
 */
QStringList KeyFile::allKeys() const
{
    return mPriv->allKeys();
}

/**
 * Return a list of keys in the current group.
 *
 * \return List of all keys in the current group.
 * \sa group(), setGroup()
 */
QStringList KeyFile::keys() const
{
    return mPriv->keys();
}

/**
 * Check if the current group contains a key named \a key.
 *
 * \return true if \a key exists, false otherwise.
 * \sa group(), setGroup()
 */
bool KeyFile::contains(const QString &key) const
{
    return mPriv->contains(key);
}

/**
 * Get the raw value for the key in the current group named \a key.
 *
 * The raw value is the value as is in the key file.
 *
 * \return Value of \a key, empty string if not found.
 * \sa group(), setGroup()
 */
QString KeyFile::rawValue(const QString &key) const
{
    return mPriv->rawValue(key);
}

/**
 * Get the value for the key in the current group named \a key.
 *
 * Escape sequences in the value are interpreted as defined in:
 * http://standards.freedesktop.org/desktop-entry-spec/latest/
 *
 * \return Value of \a key, empty string if not found or an error occurred.
 * \sa group(), setGroup()
 */
QString KeyFile::value(const QString &key) const
{
    return mPriv->value(key);
}

/**
 * Get the value for the key in the current group named \a key as a list.
 *
 * Return a list containing all strings on this key separated by ';'.
 * Escape sequences in the value are interpreted as defined in:
 * http://standards.freedesktop.org/desktop-entry-spec/latest/
 *
 * \return Value of \a key as a list, empty string list if not found or an error occurred.
 * \sa group(), setGroup()
 */
QStringList KeyFile::valueAsStringList(const QString &key) const
{
    return mPriv->valueAsStringList(key);
}

bool KeyFile::unescapeString(const QByteArray &data, int from, int to, QString &result)
{
    int i = from;
    while (i < to) {
        uint ch = data.at(i++);

        if (ch == '\\') {
            if (i == to) {
                result += QLatin1String("\\");
                return true;
            }

            char nextCh = data.at(i++);
            switch (nextCh) {
                case 's':
                    result += QLatin1String(" ");
                    break;
                case 'n':
                    result += QLatin1String("\n");
                    break;
                case 't':
                    result += QLatin1String("\t");
                    break;
                case 'r':
                    result += QLatin1String("\r");
                    break;
                case ';':
                    result += QLatin1String(";");
                    break;
                case '\\':
                    result += QLatin1String("\\");
                    break;
                default:
                    return false;
            }
        } else {
            result += ch;
        }
    }

    return true;
}

bool KeyFile::unescapeStringList(const QByteArray &data, int from, int to, QStringList &result)
{
    QByteArray value;
    QList<QByteArray> valueList;
    int i = from;
    char ch;
    while (i < to) {
        ch = data.at(i++);

        if (ch == '\\') {
            value += ch;
            if (i < to) {
                value += data.at(i++);
                continue;
            } else {
                valueList << value;
                break;
            }
        } else if (ch == ';') {
            valueList << value;
            value = "";
        } else {
            value += ch;
            if (i == to) {
                valueList << value;
            }
        }
    }

    foreach (value, valueList) {
        QString str;
        if (!unescapeString(value, 0, value.size(), str)) {
            return false;
        }
        result << str;
    }

    return true;
}

} // Tp