[go: up one dir, main page]

File: toconfiguration.cpp

package info (click to toggle)
tora 1.3.23-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,984 kB
  • ctags: 14,460
  • sloc: cpp: 123,554; sh: 16,181; makefile: 966; xml: 69
file content (352 lines) | stat: -rw-r--r-- 9,374 bytes parent folder | download
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
#include "toconfiguration.h"

#include <qapplication.h>
#include <qregexp.h>

#include "utils.h"
#ifdef WIN32
#include "windows/cregistry.h"
#endif

#ifdef WIN32
#  ifdef TOAD
#  define APPLICATION_NAME "SOFTWARE\\Quest Software\\Toad for MySQL\\"
#  else
#  define APPLICATION_NAME "SOFTWARE\\Quest Software\\tora\\"
#  define FALLBACK_NAME    "SOFTWARE\\Underscore\\tora\\"
#  endif

static char *toKeyPath(const QString &str, CRegistry &registry)
{
    static char *buf = NULL;
    int pos = str.length() - 1;
    while (pos >= 0 && str.at(pos) != '\\')
        pos--;
    if (pos < 0)
        throw QT_TRANSLATE_NOOP("toKeyPath", "Couldn't find \\ in path");
    QString ret = str.mid(0, pos);
    if (buf)
        free(buf);
    buf = strdup(ret);
    registry.CreateKey(HKEY_CURRENT_USER, buf);
    return buf;
}

static char *toKeyValue(const QString &str)
{
    static char *buf = NULL;
    int pos = str.length() - 1;
    while (pos >= 0 && str.at(pos) != '\\')
        pos--;
    if (pos < 0)
        throw QT_TRANSLATE_NOOP("toKeyValue", "Couldn't find \\ in path");
    if (buf)
        free(buf);
    buf = strdup(str.mid(pos + 1));
    return buf;
}

#endif

toConfiguration::toConfiguration()
{

}

toConfiguration::~toConfiguration()
{
}

void toConfiguration::loadConfig()
{
   Configuration.clear();

#ifndef WIN32

    QString conf;
    if (getenv("HOME"))
    {
        conf = QString::fromLatin1(getenv("HOME"));
    }
    conf.append(QString::fromLatin1(CONFIG_FILE));
    try
    {
        loadMap(conf, Configuration);
    }
    catch (...)
    {
        try
        {
            loadMap(QString::fromLatin1(DEF_CONFIG_FILE), Configuration);
        }
        catch (...)
        {}
    }
#endif
}

void toConfiguration::loadMap(const QString &filename, std::map<QCString, QString> &pairs)
{
    QCString data = toReadFile(filename);

    int pos = 0;
    int bol = 0;
    int endtag = -1;
    int wpos = 0;
    int size = data.length();
    while (pos < size)
    {
        switch (data[pos])
        {
        case '\n':
            data[wpos] = 0;
            if (endtag == -1)
                throw QT_TRANSLATE_NOOP("toTool", "Malformed tag in config file. Missing = on row. (%1)").arg(data.mid(bol, wpos - bol));
            {
                QCString tag = ((const char *)data) + bol;
                QCString val = ((const char *)data) + endtag + 1;
                pairs[tag] = QString::fromUtf8(val);
            }
            bol = pos + 1;
            endtag = -1;
            wpos = pos;
            break;
        case '=':
            if (endtag == -1)
            {
                endtag = pos;
                data[wpos] = 0;
                wpos = pos;
            }
            else
                data[wpos] = data[pos];
            break;
        case '\\':
            pos++;
            switch (data[pos])
            {
            case 'n':
                data[wpos] = '\n';
                break;
            case '\\':
                if (endtag >= 0)
                    data[wpos] = '\\';
                else
                    data[wpos] = ':';
                break;
            default:
                throw QT_TRANSLATE_NOOP("toTool", "Unknown escape character in string (Only \\\\ and \\n recognised)");
            }
            break;
        default:
            data[wpos] = data[pos];
        }
        wpos++;
        pos++;
    }
    return ;
}

void toConfiguration::saveConfig()
{
        try
    {
#ifdef WIN32
        CRegistry registry;
        QRegExp re(":");
        for (std::map<QCString, QString>::iterator i = Configuration.begin();i != Configuration.end();i++)
        {
            QCString path = (*i).first;
            QString value = (*i).second;
            path.prepend(APPLICATION_NAME);
            path.replace(re, "\\");
            if (value.isEmpty())
            {
                if (!registry.SetStringValue(HKEY_CURRENT_USER,
                                             toKeyPath(path, registry),
                                             toKeyValue(path),
                                             ""))
                    toStatusMessage(QT_TRANSLATE_NOOP("toTool", "Couldn't save empty value at key %1").arg(path));
            }
            else
            {
                char *t = strdup(value.utf8());
                if (!registry.SetStringValue(HKEY_CURRENT_USER,
                                             toKeyPath(path, registry),
                                             toKeyValue(path),
                                             t))
                    toStatusMessage(QT_TRANSLATE_NOOP("toTool", "Couldn't save %1 value at key %2").arg(value).arg(path));
                free(t);
            }
        }
#else
        if ( Configuration.empty() )
           return ;
        QCString conf;
        if (getenv("HOME"))
        {
            conf = getenv("HOME");
        }
        conf.append(CONFIG_FILE);
        saveMap(conf, Configuration);
#endif

    }
    TOCATCH

}

bool toConfiguration::saveMap(const QString &file, std::map<QCString, QString> &pairs)
{
    QCString data;

    {
        QRegExp newline(QString::fromLatin1("\n"));
        QRegExp backslash(QString::fromLatin1("\\"));
        for (std::map<QCString, QString>::iterator i = pairs.begin();i != pairs.end();i++)
        {
            QCString str = (*i).first;
            str.append(QString::fromLatin1("="));
            str.replace(backslash, QString::fromLatin1("\\\\"));
            str.replace(newline, QString::fromLatin1("\\n"));
            QString line = (*i).second;
            line.replace(backslash, QString::fromLatin1("\\\\"));
            line.replace(newline, QString::fromLatin1("\\n"));
            str += line.utf8();
            str += QString::fromLatin1("\n");
            data += str;
        }
    }
    return toWriteFile(file, data);
}

const QString &toConfiguration::globalConfig(const QCString &tag, const QCString &def)
{
    if ( Configuration.empty() )
       loadConfig();

    std::map<QCString, QString>::iterator i = Configuration.find(tag);
    if (i == Configuration.end())
    {
#if defined(WIN32)
        CRegistry registry;
        QRegExp re(QString::fromLatin1(":"));
        QCString path = tag;
        path.prepend(APPLICATION_NAME);
        path.replace(re, "\\");
        DWORD siz = 1024;
        char buffer[1024];
        try
        {
            if (registry.GetStringValue(HKEY_CURRENT_USER,
                                        toKeyPath(path, registry),
                                        toKeyValue(path),
                                        buffer, siz))
            {
                if (siz > 0)
                {
                    QString ret = QString::fromUtf8(buffer);
                    (Configuration)[tag] = ret;
                }
                else
                {
                    (Configuration)[tag] = "";
                }
                return (Configuration)[tag];
            }
        }
        catch (...)
        {
#ifdef FALLBACK_NAME
            try
            {
                path = tag;
                path.prepend(FALLBACK_NAME);
                path.replace(re, "\\");
                if (registry.GetStringValue(HKEY_CURRENT_USER,
                                            toKeyPath(path, registry),
                                            toKeyValue(path),
                                            buffer, siz))
                {
                    if (siz > 0)
                    {
                        QString ret = QString::fromUtf8(buffer);
                        (Configuration)[tag] = ret;
                    }
                    else
                    {
                        (Configuration)[tag] = "";
                    }
                    return (Configuration)[tag];
                }
            }
            catch (...)
            {}
#endif

        }
#endif

        (Configuration)[tag] = QString::fromLatin1(def);
        return (Configuration)[tag];
    }
    return (*i).second;
}



const QString& toConfiguration::config(const QCString &tag, const QCString &def, const QCString &name)
{
    QCString str = name;
    str.append(":");
    str.append(tag);
    return globalConfig(str, def);
}

void toConfiguration::eraseConfig(const QCString &tag, const QCString &name)
{
    QCString str = name;
    str.append(":");
    str.append(tag);
    globalEraseConfig(str);
}

void toConfiguration::setConfig(const QCString &tag, const QString &def, const QCString name)
{
    QCString str = name;
    str.append(":");
    str.append(tag);
    globalSetConfig(str, def);
}


void toConfiguration::globalEraseConfig(const QCString &tag)
{
    if ( Configuration.empty() )
       loadConfig();
    std::map<QCString, QString>::iterator i = Configuration.find(tag);
    if (i != Configuration.end())
    {
        Configuration.erase(i);
#if defined(WIN32)

        CRegistry registry;
        QRegExp re(QString::fromLatin1(":"));
        QCString path = tag;
        path.prepend(APPLICATION_NAME);
        path.replace(re, "\\");
        registry.DeleteKey(HKEY_CURRENT_USER, path); // Don't really care if it works.
#endif

    }
}

void toConfiguration::globalSetConfig(const QCString &tag, const QString &value)
{
    if ( Configuration.empty() )
       loadConfig();

    (Configuration)[tag] = value;
}