aboutsummaryrefslogtreecommitdiff
path: root/src/core/kconfigdata.cpp
blob: 109063d65e97bcb7ba08cf6e5a6f3acb885fe845 (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
/*
   This file is part of the KDE libraries
   Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
   Copyright (c) 1999-2000 Preston Brown <pbrown@kde.org>
   Copyright (C) 1996-2000 Matthias Kalle Dalheimer <kalle@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <kconfigdata.h>

QDebug operator<<(QDebug dbg, const KEntryKey &key)
{
    dbg.nospace() << "[" << key.mGroup << ", " << key.mKey << (key.bLocal ? " localized" : "") <<
                  (key.bDefault ? " default" : "") << (key.bRaw ? " raw" : "") << "]";
    return dbg.space();
}

QDebug operator<<(QDebug dbg, const KEntry &entry)
{
    dbg.nospace() << "[" << entry.mValue << (entry.bDirty ? " dirty" : "") <<
                  (entry.bGlobal ? " global" : "") << (entry.bImmutable ? " immutable" : "") <<
                  (entry.bDeleted ? " deleted" : "") << (entry.bReverted ? " reverted" : "") <<
                  (entry.bExpand ? " expand" : "") << "]";

    return dbg.space();
}

QMap< KEntryKey, KEntry >::Iterator KEntryMap::findExactEntry(const QByteArray &group, const QByteArray &key, KEntryMap::SearchFlags flags)
{
    KEntryKey theKey(group, key, bool(flags & SearchLocalized), bool(flags & SearchDefaults));
    return find(theKey);
}

QMap< KEntryKey, KEntry >::Iterator KEntryMap::findEntry(const QByteArray &group, const QByteArray &key, KEntryMap::SearchFlags flags)
{
    KEntryKey theKey(group, key, false, bool(flags & SearchDefaults));

    // try the localized key first
    if (flags & SearchLocalized) {
        theKey.bLocal = true;

        Iterator it = find(theKey);
        if (it != end()) {
            return it;
        }

        theKey.bLocal = false;
    }
    return find(theKey);
}

QMap< KEntryKey, KEntry >::ConstIterator KEntryMap::findEntry(const QByteArray &group, const QByteArray &key, KEntryMap::SearchFlags flags) const
{
    KEntryKey theKey(group, key, false, bool(flags & SearchDefaults));

    // try the localized key first
    if (flags & SearchLocalized) {
        theKey.bLocal = true;

        ConstIterator it = find(theKey);
        if (it != constEnd()) {
            return it;
        }

        theKey.bLocal = false;
    }
    return find(theKey);
}

bool KEntryMap::setEntry(const QByteArray &group, const QByteArray &key, const QByteArray &value, KEntryMap::EntryOptions options)
{
    KEntryKey k;
    KEntry e;
    bool newKey = false;

    const Iterator it = findExactEntry(group, key, SearchFlags(options >> 16));

    if (key.isEmpty()) { // inserting a group marker
        k.mGroup = group;
        e.bImmutable = (options & EntryImmutable);
        if (options & EntryDeleted) {
            qWarning("Internal KConfig error: cannot mark groups as deleted");
        }
        if (it == end()) {
            insert(k, e);
            return true;
        } else if (it.value() == e) {
            return false;
        }

        it.value() = e;
        return true;
    }

    if (it != end()) {
        if (it->bImmutable) {
            return false;    // we cannot change this entry. Inherits group immutability.
        }
        k = it.key();
        e = *it;
        //qDebug() << "found existing entry for key" << k;
    } else {
        // make sure the group marker is in the map
        KEntryMap const *that = this;
        ConstIterator cit = that->findEntry(group);
        if (cit == constEnd()) {
            insert(KEntryKey(group), KEntry());
        } else if (cit->bImmutable) {
            return false;    // this group is immutable, so we cannot change this entry.
        }

        k = KEntryKey(group, key);
        newKey = true;
    }

    // set these here, since we may be changing the type of key from the one we found
    k.bLocal = (options & EntryLocalized);
    k.bDefault = (options & EntryDefault);
    k.bRaw = (options & EntryRawKey);

    e.mValue = value;
    e.bDirty = e.bDirty || (options & EntryDirty);
    e.bGlobal = (options & EntryGlobal); //we can't use || here, because changes to entries in
    //kdeglobals would be written to kdeglobals instead
    //of the local config file, regardless of the globals flag
    e.bImmutable = e.bImmutable || (options & EntryImmutable);
    if (value.isNull()) {
        e.bDeleted = e.bDeleted || (options & EntryDeleted);
    } else {
        e.bDeleted = false;    // setting a value to a previously deleted entry
    }
    e.bExpand = (options & EntryExpansion);
    e.bReverted = false;

    if (newKey) {
        //qDebug() << "inserting" << k << "=" << value;
        insert(k, e);
        if (k.bDefault) {
            k.bDefault = false;
            //qDebug() << "also inserting" << k << "=" << value;
            insert(k, e);
        }
        // TODO check for presence of unlocalized key
        return true;
    } else {
//                KEntry e2 = it.value();
        if (it.value() != e) {
            //qDebug() << "changing" << k << "from" << e.mValue << "to" << value;
            it.value() = e;
            if (k.bDefault) {
                KEntryKey nonDefaultKey(k);
                nonDefaultKey.bDefault = false;
                insert(nonDefaultKey, e);
            }
            if (!(options & EntryLocalized)) {
                KEntryKey theKey(group, key, true, false);
                //qDebug() << "non-localized entry, remove localized one:" << theKey;
                remove(theKey);
                if (k.bDefault) {
                    theKey.bDefault = true;
                    remove(theKey);
                }
            }
            return true;
        } else {
            //qDebug() << k << "was already set to" << e.mValue;
            if (!(options & EntryLocalized)) {
                //qDebug() << "unchanged non-localized entry, remove localized one.";
                KEntryKey theKey(group, key, true, false);
                bool ret = false;
                Iterator cit = find(theKey);
                if (cit != end()) {
                    erase(cit);
                    ret = true;
                }
                if (k.bDefault) {
                    theKey.bDefault = true;
                    Iterator cit = find(theKey);
                    if (cit != end()) {
                        erase(cit);
                        return true;
                    }
                }
                return ret;
            }
            //qDebug() << "localized entry, unchanged, return false";
            // When we are writing a default, we know that the non-
            // default is the same as the default, so we can simply
            // use the same branch.
            return false;
        }
    }
}

QString KEntryMap::getEntry(const QByteArray &group, const QByteArray &key, const QString &defaultValue, KEntryMap::SearchFlags flags, bool *expand) const
{
    const ConstIterator it = findEntry(group, key, flags);
    QString theValue = defaultValue;

    if (it != constEnd() && !it->bDeleted) {
        if (!it->mValue.isNull()) {
            const QByteArray data = it->mValue;
            theValue = QString::fromUtf8(data.constData(), data.length());
            if (expand) {
                *expand = it->bExpand;
            }
        }
    }

    return theValue;
}

bool KEntryMap::hasEntry(const QByteArray &group, const QByteArray &key, KEntryMap::SearchFlags flags) const
{
    const ConstIterator it = findEntry(group, key, flags);
    if (it == constEnd()) {
        return false;
    }
    if (it->bDeleted) {
        return false;
    }
    if (key.isNull()) { // looking for group marker
        return it->mValue.isNull();
    }
    // if it->bReverted, we'll just return true; the real answer depends on lookup up with SearchDefaults, though.
    return true;
}

bool KEntryMap::getEntryOption(const QMap< KEntryKey, KEntry >::ConstIterator &it, KEntryMap::EntryOption option) const
{
    if (it != constEnd()) {
        switch (option) {
        case EntryDirty:
            return it->bDirty;
        case EntryLocalized:
            return it.key().bLocal;
        case EntryGlobal:
            return it->bGlobal;
        case EntryImmutable:
            return it->bImmutable;
        case EntryDeleted:
            return it->bDeleted;
        case EntryExpansion:
            return it->bExpand;
        default:
            break; // fall through
        }
    }

    return false;
}

void KEntryMap::setEntryOption(QMap< KEntryKey, KEntry >::Iterator it, KEntryMap::EntryOption option, bool bf)
{
    if (it != end()) {
        switch (option) {
        case EntryDirty:
            it->bDirty = bf;
            break;
        case EntryGlobal:
            it->bGlobal = bf;
            break;
        case EntryImmutable:
            it->bImmutable = bf;
            break;
        case EntryDeleted:
            it->bDeleted = bf;
            break;
        case EntryExpansion:
            it->bExpand = bf;
            break;
        default:
            break; // fall through
        }
    }
}

bool KEntryMap::revertEntry(const QByteArray &group, const QByteArray &key, KEntryMap::SearchFlags flags)
{
    Q_ASSERT((flags & KEntryMap::SearchDefaults) == 0);
    Iterator entry = findEntry(group, key, flags);
    if (entry != end()) {
        //qDebug() << "reverting" << entry.key() << " = " << entry->mValue;
        if (entry->bReverted) { // already done before
            return false;
        }

        KEntryKey defaultKey(entry.key());
        defaultKey.bDefault = true;
        //qDebug() << "looking up default entry with key=" << defaultKey;
        const ConstIterator defaultEntry = constFind(defaultKey);
        if (defaultEntry != constEnd()) {
            Q_ASSERT(defaultEntry.key().bDefault);
            //qDebug() << "found, update entry";
            *entry = *defaultEntry; // copy default value, for subsequent lookups
        } else {
            entry->mValue = QByteArray();
        }
        entry->bDirty = true;
        entry->bReverted = true; // skip it when writing out to disk

        //qDebug() << "Here's what we have now:" << *this;
        return true;
    }
    return false;
}