File: oldconfig.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (194 lines) | stat: -rw-r--r-- 5,102 bytes parent folder | download | duplicates (2)
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
/*
 * oldconfig.cpp
 *
 * (c) 2004,2009 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file oldconfig.cpp
 * Source file for OldConfig
 */

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>

#include "oldconfig.h"

/**
 * Constructor.  The name parameter can be either "portabase" or "qpe" (the
 * latter indicates data which was shared between applications on the Sharp
 * Zaurus).
 *
 * @param name The name of the settings category to retrieve
 */
OldConfig::OldConfig(const QString &name)
{
    QDir dir = (QDir::homePath() + "/.portabase");
    filename = dir.path() + "/" + name + ".conf";
    git = groups.end();
    read();
}

/**
 * Treat future requests for settings as coming from the specified group
 * until told to do otherwise.
 *
 * @param gname The name of the group to read from
 */
void OldConfig::setGroup(const QString &gname)
{
    QMap< QString, ConfigGroup>::Iterator it = groups.find(gname);
    if (it == groups.end()) {
        git = groups.insert(gname, ConfigGroup());
        return;
    }
    git = it;
}

/**
 * Get the value of a string-valued setting from the current group.
 *
 * @param key The name of the setting to retrieve
 * @param deflt The default value to use if the setting is absent
 * @return The requested setting value
 */
QString OldConfig::readEntry(const QString &key, const QString &deflt)
{
    if (git == groups.end()) {
        return deflt;
    }
    ConfigGroup::ConstIterator it = (*git).find(key);
    if (it != ( *git ).end()) {
        return *it;
    }
    else {
        return deflt;
    }
}

/**
 * Read the settings file and parse the data it contains for later use.
 */
void OldConfig::read()
{
    fileExists = QFileInfo(filename).exists();
    if (!fileExists) {
        git = groups.end();
        return;
    }

    QFile f(filename);
    if (!f.open(QFile::ReadOnly)) {
        git = groups.end();
        return;
    }

    QTextStream s(&f);
    s.setCodec("UTF-8");

    QStringList list(s.readAll().split('\n'));
    f.close();

    for (QStringList::Iterator it = list.begin(); it != list.end(); ++it) {
        if (!parse( *it )) {
            git = groups.end();
            return;
        }
    }
}

/**
 * Determine if an old configuration file exists to load settings from.
 *
 * @return True if the file exists, false otherwise
 */
bool OldConfig::exists() const
{
    return fileExists;
}

/**
 * Parse a single line from the settings file.
 *
 * @param line The line to be parsed
 * @return False if the line doesn't make sense in context, true otherwise
 */
bool OldConfig::parse(const QString &line)
{
    QString text = line.trimmed();
    if (text[0] == QChar('[')) {
        QString gname = text;
        gname = gname.remove(0, 1);
        if (gname[(int)gname.length() - 1] == QChar(']')) {
            gname = gname.remove(gname.length() - 1, 1);
        }
        git = groups.insert(gname, ConfigGroup());
    }
    else if (!text.isEmpty()) {
        if (git == groups.end()) {
            return false;
        }
        int eq = text.indexOf('=');
        if (eq == -1) {
            return false;
        }
        QString key = text.left(eq).trimmed();
        QString value = text.mid(eq+1).trimmed();
        (*git).insert(key, value);
    }
    return true;
}

/**
 * Migrate the specified group of old settings into the current QSettings
 * storage.
 *
 * @param group The name of the group of settings to migrate
 * @param settings The QSettings object to put the migrated settings into
 */
void OldConfig::migrate(const QString &group, QSettings &settings)
{
    QStringList boolEntries;
    boolEntries.append("MONDAY");
    boolEntries.append("SHOWSECONDS");
    boolEntries.append("AMPM");
    boolEntries.append("NoteWrap");
    boolEntries.append("WrapAnywhere");
    boolEntries.append("ConfirmDeletions");
    boolEntries.append("BooleanToggle");
    QStringList numEntries;
    numEntries.append("ShortOrder");
    numEntries.append("LongOrder");
    numEntries.append("Size");
    QStringList stringEntries;
    stringEntries.append("DocPath");
    stringEntries.append("Name");
    stringEntries.append("EvenRows");
    stringEntries.append("OddRows");
    stringEntries.append("LastDir");
    stringEntries.append("View");
    stringEntries.append("Separator");
    setGroup(group);
    settings.beginGroup(group);
    ConfigGroup::Iterator iter;
    for (iter = (*git).begin(); iter != (*git).end(); ++iter) {
        QString key = iter.key();
        QString value = iter.value();
        if (boolEntries.contains(key)) {
            settings.setValue(key, (bool)value.toInt());
        }
        else if (numEntries.contains(key)) {
            settings.setValue(key, value.toInt());
        }
        else {
            settings.setValue(key, value);
        }
    }
    settings.endGroup();
}