1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
5 SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org>
6 SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#ifndef KCONFIGGROUP_H
12#define KCONFIGGROUP_H
13
14#include "kconfigbase.h"
15
16#include <kconfigcore_export.h>
17
18#include <QExplicitlySharedDataPointer>
19#include <QStringList>
20#include <QVariant>
21
22class KConfig;
23class KConfigGroupPrivate;
24class KSharedConfig;
25
26/*!
27 * \class KConfigGroup
28 * \inmodule KConfigCore
29 *
30 * \brief A class for one specific group in a KConfig object.
31 *
32 * If you want to access the top-level entries of a KConfig
33 * object, which are not associated with any group, use an
34 * empty group name.
35 *
36 * A KConfigGroup will be read-only if it is constructed from a
37 * const config object or from another read-only group.
38 */
39class KCONFIGCORE_EXPORT KConfigGroup : public KConfigBase
40{
41public:
42 /*!
43 * Constructs an invalid group.
44 *
45 * \sa isValid
46 */
47 KConfigGroup();
48
49 /*!
50 * Construct a config group corresponding to \a group in \a master.
51 *
52 * This allows the creation of subgroups by passing another
53 * group as \a master.
54 *
55 * \a group name of group
56 */
57 KConfigGroup(KConfigBase *master, const QString &group);
58
59 /*!
60 * Construct a read-only config group.
61 *
62 * A read-only group will silently ignore any attempts to write to it.
63 *
64 * This allows the creation of subgroups by passing an existing group
65 * as \a master.
66 */
67 KConfigGroup(const KConfigBase *master, const QString &group);
68
69 /*!
70 * \overload
71 * Overload for KConfigGroup(const KConfigBase*,const QString&)
72 */
73 KConfigGroup(const QExplicitlySharedDataPointer<KSharedConfig> &master, const QString &group);
74
75 KConfigGroup(const KConfigGroup &);
76 KConfigGroup &operator=(const KConfigGroup &);
77
78 ~KConfigGroup() override;
79
80 /*!
81 * Whether the group is valid.
82 *
83 * A group is invalid if it was constructed without arguments.
84 *
85 * You should not call any functions on an invalid group.
86 *
87 * Returns \c true if the group is valid, \c false if it is invalid.
88 */
89 bool isValid() const;
90
91 /*!
92 * The name of this group.
93 *
94 * The root group is named "<default>".
95 */
96 QString name() const;
97
98 /*!
99 * Check whether the containing KConfig object actually contains a
100 * group with this name.
101 */
102 bool exists() const;
103
104 /*!
105 * \reimp
106 *
107 * Syncs the parent config.
108 */
109 bool sync() override;
110
111 void markAsClean() override;
112
113 AccessMode accessMode() const override;
114
115 /*!
116 * Return the config object that this group belongs to
117 */
118 KConfig *config();
119 /*!
120 * Return the config object that this group belongs to
121 */
122 const KConfig *config() const;
123
124 /*!
125 * Copies the entries in this group to another configuration object
126 *
127 * \note \a other can be either another group or a different file.
128 *
129 * \a other the configuration object to copy this group's entries to
130 *
131 * \a pFlags the flags to use when writing the entries to the
132 * other configuration object
133 *
134 * \since 4.1
135 */
136 void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const;
137
138 /*!
139 * Changes the configuration object that this group belongs to
140 *
141 * If \a parent is already the parent of this group, this method will have
142 * no effect.
143 *
144 * \a parent the config object to place this group under
145 *
146 * \a pFlags the flags to use in determining which storage source to
147 * write the data to
148 *
149 * \since 4.1
150 */
151 void reparent(KConfigBase *parent, WriteConfigFlags pFlags = Normal);
152
153 /*!
154 * Moves the key-value pairs from one config group to the other.
155 * In case the entries do not exist the key is ignored.
156 *
157 * \since 5.88
158 */
159 void moveValuesTo(const QList<const char *> &keys, KConfigGroup &other, WriteConfigFlags pFlags = Normal);
160
161 /*!
162 * Moves the key-value pairs from one config group to the other.
163 *
164 * \since 6.3
165 */
166 void moveValuesTo(KConfigGroup &other, WriteConfigFlags pFlags = Normal);
167
168 /*!
169 * Returns the group that this group belongs to
170 *
171 * Returns the parent group, or an invalid group if this is a top-level
172 * group
173 *
174 * \since 4.1
175 */
176 KConfigGroup parent() const;
177
178 QStringList groupList() const override;
179
180 /*!
181 * Returns a list of keys this group contains
182 */
183 QStringList keyList() const;
184
185 /*!
186 * Delete all entries in the entire group
187 *
188 * \a pFlags flags passed to KConfig::deleteGroup
189 *
190 * \sa deleteEntry()
191 */
192 void deleteGroup(WriteConfigFlags pFlags = Normal);
193 using KConfigBase::deleteGroup;
194
195 /*!
196 * Reads the value of an entry specified by \a pKey in the current group
197 *
198 * This template method makes it possible to write
199 * QString foo = readEntry("...", QString("default"));
200 * and the same with all other types supported by QVariant.
201 *
202 * The return type of the method is simply the same as the type of the default value.
203 *
204 * \note readEntry("...", Qt::white) will not compile because Qt::white is an enum.
205 * You must turn it into readEntry("...", QColor(Qt::white)).
206 *
207 * \note Only the following QVariant types are allowed : String,
208 * StringList, List, Font, Point, PointF, Rect, RectF, Size, SizeF, Color, Int, UInt, Bool,
209 * Double, LongLong, ULongLong, DateTime and Date.
210 *
211 * \a key The key to search for
212 *
213 * \a aDefault A default value returned if the key was not found
214 *
215 * Returns the value for this key, or \a aDefault.
216 *
217 * \sa writeEntry(), deleteEntry(), hasKey()
218 */
219 template<typename T>
220 T readEntry(const QString &key, const T &aDefault) const
221 {
222 return readEntry(key.toUtf8().constData(), aDefault);
223 }
224 /*!
225 * \overload
226 *
227 * Overload for readEntry<T>(const QString&, const T&) const
228 *
229 * \a key name of key, encoded in UTF-8
230 */
231 template<typename T>
232 T readEntry(const char *key, const T &aDefault) const;
233
234 /*!
235 * Reads the value of an entry specified by \a key in the current group
236 *
237 * \a key the key to search for
238 *
239 * \a aDefault a default value returned if the key was not found
240 *
241 * Returns the value for this key, or \a aDefault if the key was not found
242 *
243 * \sa writeEntry(), deleteEntry(), hasKey()
244 */
245 QVariant readEntry(const QString &key, const QVariant &aDefault) const;
246 /*!
247 * Overload for readEntry(const QString&, const QVariant&) const
248 * \a key name of key, encoded in UTF-8
249 */
250 QVariant readEntry(const char *key, const QVariant &aDefault) const;
251
252 /*!
253 * Reads the string value of an entry specified by \a key in the current group
254 *
255 * If you want to read a path, please use readPathEntry().
256 *
257 * \a key the key to search for
258 *
259 * \a aDefault a default value returned if the key was not found
260 *
261 * Returns the value for this key, or \a aDefault if the key was not found
262 *
263 * \sa readPathEntry(), writeEntry(), deleteEntry(), hasKey()
264 */
265 QString readEntry(const QString &key, const QString &aDefault) const;
266 /*!
267 * \overload
268 *
269 * Overload for readEntry(const QString&, const QString&) const
270 *
271 * \a key name of key, encoded in UTF-8
272 */
273 QString readEntry(const char *key, const QString &aDefault) const;
274
275 /*!
276 * \overload
277 *
278 * Overload for readEntry(const QString&, const QString&) const
279 */
280 QString readEntry(const QString &key, const char *aDefault = nullptr) const;
281 /*!
282 * \overload
283 *
284 * Overload for readEntry(const QString&, const QString&) const
285 *
286 * \a key name of key, encoded in UTF-8
287 */
288 QString readEntry(const char *key, const char *aDefault = nullptr) const;
289
290 /*!
291 * \sa readEntry()
292 *
293 * \warning This function doesn't convert the items returned
294 * to any type. It's actually a list of QVariant::String's. If you
295 * want the items converted to a specific type use
296 * readEntry(const char*, const QList<T>&) const
297 */
298 QVariantList readEntry(const QString &key, const QVariantList &aDefault) const;
299 /*!
300 * \overload
301 *
302 * Overload for readEntry(const QString&, const QVariantList&) const
303 *
304 * \a key name of key, encoded in UTF-8
305 */
306 QVariantList readEntry(const char *key, const QVariantList &aDefault) const;
307
308 /*!
309 * Reads a list of strings from the config object
310 *
311 * \a key The key to search for
312 *
313 * \a aDefault The default value to use if the key does not exist
314 *
315 * Returns the list, or \a aDefault if \a key does not exist
316 *
317 * \sa readXdgListEntry(), writeEntry(), deleteEntry(), hasKey()
318 */
319 QStringList readEntry(const QString &key, const QStringList &aDefault) const;
320 /*!
321 * \overload
322 *
323 * Overload for readEntry(const QString&, const QStringList&) const
324 *
325 * \a key name of key, encoded in UTF-8
326 */
327 QStringList readEntry(const char *key, const QStringList &aDefault) const;
328
329 /*!
330 * Reads a list of values from the config object
331 *
332 * \a key the key to search for
333 *
334 * \a aDefault the default value to use if the key does not exist
335 *
336 * Returns the list, or \a aDefault if \a key does not exist
337 *
338 * \sa readXdgListEntry(), writeEntry(), deleteEntry(), hasKey()
339 */
340 template<typename T>
341 QList<T> readEntry(const QString &key, const QList<T> &aDefault) const
342 {
343 return readEntry(key.toUtf8().constData(), aDefault);
344 }
345 /*!
346 * \overload
347 *
348 * Overload for readEntry<T>(const QString&, const QList<T>&) const
349 *
350 * \a key name of key, encoded in UTF-8
351 */
352 template<typename T>
353 QList<T> readEntry(const char *key, const QList<T> &aDefault) const;
354
355 /*!
356 * Reads a list of strings from the config object with semicolons separating
357 * them (i.e. following desktop entry spec separator semantics).
358 *
359 * \a pKey the key to search for
360 *
361 * \a aDefault the default value to use if the key does not exist
362 *
363 * Returns the list, or \a aDefault if \a pKey does not exist
364 *
365 * \sa readEntry(const QString&, const QStringList&)
366 */
367 QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault = QStringList()) const;
368 /*!
369 * Overload for readXdgListEntry(const QString&, const QStringList&) const
370 *
371 * \a key name of key, encoded in UTF-8
372 */
373 QStringList readXdgListEntry(const char *key, const QStringList &aDefault = QStringList()) const;
374
375 /*!
376 * Reads a path
377 *
378 * Read the value of an entry specified by \a pKey in the current group
379 * and interpret it as a path. This means, dollar expansion is activated
380 * for this value, so that e.g. $HOME gets expanded.
381 *
382 * \a pKey The key to search for.
383 *
384 * \a aDefault A default value returned if the key was not found.
385 *
386 * Returns The value for this key. Can be QString() if \a aDefault is null.
387 */
388 QString readPathEntry(const QString &pKey, const QString &aDefault) const;
389 /*!
390 * Overload for readPathEntry(const QString&, const QString&) const
391 *
392 * \a key name of key, encoded in UTF-8
393 */
394 QString readPathEntry(const char *key, const QString &aDefault) const;
395
396 /*!
397 * Reads a list of paths
398 *
399 * Read the value of an entry specified by \a pKey in the current group
400 * and interpret it as a list of paths. This means, dollar expansion is activated
401 * for this value, so that e.g. $HOME gets expanded.
402 *
403 * \a pKey the key to search for
404 *
405 * \a aDefault a default value returned if the key was not found
406 *
407 * Returns the list, or \a aDefault if the key does not exist
408 */
409 QStringList readPathEntry(const QString &pKey, const QStringList &aDefault) const;
410 /*!
411 * Overload for readPathEntry(const QString&, const QStringList&) const
412 *
413 * \a key name of key, encoded in UTF-8
414 */
415 QStringList readPathEntry(const char *key, const QStringList &aDefault) const;
416
417 /*!
418 * Reads an untranslated string entry
419 *
420 * You should not normally need to use this.
421 *
422 * \a pKey the key to search for
423 *
424 * \a aDefault a default value returned if the key was not found
425 *
426 * Returns the value for this key, or \a aDefault if the key does not exist
427 */
428 QString readEntryUntranslated(const QString &pKey, const QString &aDefault = QString()) const;
429 /*!
430 * \overload
431 *
432 * Overload for readEntryUntranslated(const QString&, const QString&) const
433 *
434 * \a key name of key, encoded in UTF-8
435 */
436 QString readEntryUntranslated(const char *key, const QString &aDefault = QString()) const;
437
438 /*!
439 * Writes a value to the configuration object.
440 *
441 * \a key the key to write to
442 *
443 * \a value the value to write
444 *
445 * \a pFlags the flags to use when writing this entry
446 *
447 * \sa readEntry(), writeXdgListEntry(), deleteEntry()
448 */
449 void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags = Normal);
450 /*!
451 * \overload
452 *
453 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
454 * \a key name of key, encoded in UTF-8
455 */
456 void writeEntry(const char *key, const QVariant &value, WriteConfigFlags pFlags = Normal);
457
458 /*!
459 * \overload
460 *
461 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
462 */
463 void writeEntry(const QString &key, const QString &value, WriteConfigFlags pFlags = Normal);
464 /*!
465 * \overload
466 *
467 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
468 *
469 * \a key name of key, encoded in UTF-8
470 */
471 void writeEntry(const char *key, const QString &value, WriteConfigFlags pFlags = Normal);
472
473 /*!
474 * \overload
475 *
476 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
477 */
478 void writeEntry(const QString &key, const QByteArray &value, WriteConfigFlags pFlags = Normal);
479 /*!
480 * \overload
481 *
482 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
483 *
484 * \a key name of key, encoded in UTF-8
485 */
486 void writeEntry(const char *key, const QByteArray &value, WriteConfigFlags pFlags = Normal);
487
488 /*!
489 * \overload
490 *
491 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
492 */
493 void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal);
494 /*!
495 * \overload
496 *
497 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
498 *
499 * \a key name of key, encoded in UTF-8
500 */
501 void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal);
502
503 /*!
504 * \overload
505 *
506 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
507 *
508 * \a key name of key, encoded in UTF-8
509 */
510 template<typename T>
511 void writeEntry(const char *key, const T &value, WriteConfigFlags pFlags = Normal);
512 /*!
513 * \overload
514 *
515 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
516 */
517 template<typename T>
518 void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags = Normal)
519 {
520 writeEntry(key.toUtf8().constData(), value, pFlags);
521 }
522
523 /*!
524 * \overload
525 *
526 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
527 */
528 void writeEntry(const QString &key, const QStringList &value, WriteConfigFlags pFlags = Normal);
529 /*!
530 * \overload
531 *
532 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
533 *
534 * \a key name of key, encoded in UTF-8
535 */
536 void writeEntry(const char *key, const QStringList &value, WriteConfigFlags pFlags = Normal);
537
538 /*!
539 * \overload
540 *
541 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
542 */
543 void writeEntry(const QString &key, const QVariantList &value, WriteConfigFlags pFlags = Normal);
544 /*!
545 * \overload
546 *
547 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
548 *
549 * \a key name of key, encoded in UTF-8
550 */
551 void writeEntry(const char *key, const QVariantList &value, WriteConfigFlags pFlags = Normal);
552
553 /*!
554 * \overload
555 *
556 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
557 */
558 template<typename T>
559 void writeEntry(const QString &key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
560 {
561 writeEntry(key.toUtf8().constData(), value, pFlags);
562 }
563 /*!
564 * \overload
565 *
566 * Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
567 *
568 * \a key name of key, encoded in UTF-8
569 */
570 template<typename T>
571 void writeEntry(const char *key, const QList<T> &value, WriteConfigFlags pFlags = Normal);
572
573 /*!
574 * Writes a list of strings to the config object, following XDG
575 * desktop entry spec separator semantics
576 *
577 * \a pKey the key to write to
578 *
579 * \a value the list to write
580 *
581 * \a pFlags the flags to use when writing this entry
582 *
583 * \sa writeEntry(), readXdgListEntry()
584 */
585 void writeXdgListEntry(const QString &pKey, const QStringList &value, WriteConfigFlags pFlags = Normal);
586 /*!
587 * \overload
588 *
589 * Overload for writeXdgListEntry(const QString&, const QStringList&, WriteConfigFlags)
590 *
591 * \a key name of key, encoded in UTF-8
592 */
593 void writeXdgListEntry(const char *key, const QStringList &value, WriteConfigFlags pFlags = Normal);
594
595 /*!
596 * Writes a file path to the configuration
597 *
598 * If the path is located under $HOME, the user's home directory
599 * is replaced with $HOME in the persistent storage.
600 * The path should therefore be read back with readPathEntry()
601 *
602 * \a pKey the key to write to
603 *
604 * \a path the path to write
605 *
606 * \a pFlags the flags to use when writing this entry
607 *
608 * \sa writeEntry(), readPathEntry()
609 */
610 void writePathEntry(const QString &pKey, const QString &path, WriteConfigFlags pFlags = Normal);
611 /*!
612 * \overload
613 *
614 * Overload for writePathEntry(const QString&, const QString&, WriteConfigFlags)
615 *
616 * \a key name of key, encoded in UTF-8
617 */
618 void writePathEntry(const char *Key, const QString &path, WriteConfigFlags pFlags = Normal);
619
620 /*!
621 * Writes a list of paths to the configuration
622 *
623 * If any of the paths are located under $HOME, the user's home directory
624 * is replaced with $HOME in the persistent storage.
625 * The paths should therefore be read back with readPathEntry()
626 *
627 * \a pKey the key to write to
628 *
629 * \a value the list to write
630 *
631 * \a pFlags the flags to use when writing this entry
632 *
633 * \sa writeEntry(), readPathEntry()
634 */
635 void writePathEntry(const QString &pKey, const QStringList &value, WriteConfigFlags pFlags = Normal);
636 /*!
637 * \overload
638 *
639 * Overload for writePathEntry(const QString&, const QStringList&, WriteConfigFlags)
640 *
641 * \a key name of key, encoded in UTF-8
642 */
643 void writePathEntry(const char *key, const QStringList &value, WriteConfigFlags pFlags = Normal);
644
645 /*!
646 * Deletes the entry specified by \a pKey in the current group
647 *
648 * This also hides system wide defaults.
649 *
650 * \a pKey the key to delete
651 *
652 * \a pFlags the flags to use when deleting this entry
653 *
654 * \sa deleteGroup(), readEntry(), writeEntry()
655 */
656 void deleteEntry(const QString &pKey, WriteConfigFlags pFlags = Normal);
657 /*!
658 * \overload
659 *
660 * Overload for deleteEntry(const QString&, WriteConfigFlags)
661 *
662 * \a key name of key, encoded in UTF-8
663 */
664 void deleteEntry(const char *key, WriteConfigFlags pFlags = Normal);
665
666 /*!
667 * Checks whether the key has an entry in this group
668 *
669 * Use this to determine if a key is not specified for the current
670 * group (hasKey() returns false).
671 *
672 * If this returns \c false for a key, readEntry() (and its variants)
673 * will return the default value passed to them.
674 *
675 * \a key the key to search for
676 *
677 * Returns \c true if the key is defined in this group by any of the
678 * configuration sources, \c false otherwise
679 *
680 * \sa readEntry()
681 */
682 bool hasKey(const QString &key) const;
683 /*!
684 * \overload
685 *
686 * Overload for hasKey(const QString&) const
687 *
688 * \a key name of key, encoded in UTF-8
689 */
690 bool hasKey(const char *key) const;
691
692 /*!
693 * Whether this group may be changed
694 *
695 * Returns \c false if the group may be changed, \c true otherwise
696 */
697 bool isImmutable() const override;
698
699 /*!
700 * Checks if it is possible to change the given entry
701 *
702 * If isImmutable() returns \c true, then this method will return
703 * \c true for all inputs.
704 *
705 * \a key the key to check
706 *
707 * Returns \c false if the key may be changed using this configuration
708 * group object, \c true otherwise
709 */
710 bool isEntryImmutable(const QString &key) const;
711 /*!
712 * \overload
713 *
714 * Overload for isEntryImmutable(const QString&) const
715 *
716 * \a key name of key, encoded in UTF-8
717 */
718 bool isEntryImmutable(const char *key) const;
719
720 /*!
721 * Reverts an entry to the default settings.
722 *
723 * Reverts the entry with key \a key in the current group in the
724 * application specific config file to either the system wide (default)
725 * value or the value specified in the global KDE config file.
726 *
727 * To revert entries in the global KDE config file, the global KDE config
728 * file should be opened explicitly in a separate config object.
729 *
730 * \note This is not the same as deleting the key, as instead the
731 * global setting will be copied to the configuration file that this
732 * object manipulates.
733 *
734 * \a key The key of the entry to revert.
735 */
736 void revertToDefault(const QString &key, WriteConfigFlags pFlag = WriteConfigFlags());
737
738 /*!
739 * Overload for revertToDefault(const QString&, WriteConfigFlags)
740 *
741 * \a key name of key, encoded in UTF-8
742 */
743 void revertToDefault(const char *key, WriteConfigFlags pFlag = WriteConfigFlags());
744
745 /*!
746 * Whether a default is specified for an entry in either the
747 * system wide configuration file or the global KDE config file
748 *
749 * If an application computes a default value at runtime for
750 * a certain entry, e.g. like:
751 * \code
752 * QColor computedDefault = qApp->palette().color(QPalette::Active, QPalette::Text);
753 * QColor color = group.readEntry(key, computedDefault);
754 * \endcode
755 * then it may wish to make the following check before
756 * writing back changes:
757 * \code
758 * if ( (value == computedDefault) && !group.hasDefault(key) )
759 * group.revertToDefault(key);
760 * else
761 * group.writeEntry(key, value);
762 * \endcode
763 *
764 * This ensures that as long as the entry is not modified to differ from
765 * the computed default, the application will keep using the computed default
766 * and will follow changes the computed default makes over time.
767 *
768 * \a key the key of the entry to check
769 *
770 * Returns \c true if the global or system settings files specify a default
771 * for \a key in this group, \c false otherwise
772 */
773 bool hasDefault(const QString &key) const;
774 /*!
775 * \overload
776 *
777 * Overload for hasDefault(const QString&) const
778 *
779 * \a key name of key, encoded in UTF-8
780 */
781 bool hasDefault(const char *key) const;
782
783 /*!
784 * Returns a map (tree) of entries for all entries in this group
785 *
786 * Only the actual entry string is returned, none of the
787 * other internal data should be included.
788 *
789 * Returns a map of entries in this group, indexed by key
790 */
791 QMap<QString, QString> entryMap() const;
792
793protected:
794 bool hasGroupImpl(const QString &groupName) const override;
795 KConfigGroup groupImpl(const QString &groupName) override;
796 const KConfigGroup groupImpl(const QString &groupName) const override;
797 void deleteGroupImpl(const QString &groupName, WriteConfigFlags flags) override;
798 bool isGroupImmutableImpl(const QString &groupName) const override;
799
800private:
801 QExplicitlySharedDataPointer<KConfigGroupPrivate> d;
802
803 friend class KConfigGroupPrivate;
804
805 /*!
806 * \internal
807 *
808 * Return the data in \a value converted to a QVariant
809 *
810 * \a pKey the name of the entry being converted, this is only used for error
811 * reporting
812 *
813 * \a value the UTF-8 data to be converted
814 *
815 * \a aDefault the default value if \a pKey is not found
816 *
817 * Returns \a value converted to QVariant, or \a aDefault if \a value is invalid or cannot be converted.
818 */
819 static QVariant convertToQVariant(const char *pKey, const QByteArray &value, const QVariant &aDefault);
820
821 KCONFIGCORE_NO_EXPORT void moveValue(const char *key, KConfigGroup &other, WriteConfigFlags pFlags);
822
823 // exported for usage by KServices' KService & KServiceAction
824 friend class KServicePrivate; // XXX yeah, ugly^5
825 friend class KServiceAction;
826};
827
828Q_DECLARE_TYPEINFO(KConfigGroup, Q_RELOCATABLE_TYPE);
829
830#define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) "The Qt MetaObject system does not seem to know about \"" ENUM "\" please use Q_ENUM or Q_FLAG to register it."
831
832/*!
833 * \macro KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum)
834 * \relates KConfigGroup
835 * To add support for your own enums in KConfig, you can declare them with Q_ENUM()
836 * in a QObject subclass (which will make moc generate the code to turn the
837 * enum into a string and vice-versa), and then (in the cpp code)
838 * use the macro
839 * \code
840 * KCONFIGGROUP_DECLARE_ENUM_QOBJECT(MyClass, MyEnum)
841 * \endcode
842 *
843 */
844#define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum) \
845 template<> \
846 Class::Enum KConfigGroup::readEntry(const char *key, const Class::Enum &def) const \
847 { \
848 const QMetaObject *M_obj = &Class::staticMetaObject; \
849 const int M_index = M_obj->indexOfEnumerator(#Enum); \
850 if (M_index == -1) \
851 qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Enum)); \
852 const QMetaEnum M_enum = M_obj->enumerator(M_index); \
853 const QByteArray M_data = readEntry(key, QByteArray(M_enum.valueToKey(def))); \
854 return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData())); \
855 } \
856 template<> \
857 void KConfigGroup::writeEntry(const char *key, const Class::Enum &value, KConfigBase::WriteConfigFlags flags) \
858 { \
859 const QMetaObject *M_obj = &Class::staticMetaObject; \
860 const int M_index = M_obj->indexOfEnumerator(#Enum); \
861 if (M_index == -1) \
862 qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Enum)); \
863 const QMetaEnum M_enum = M_obj->enumerator(M_index); \
864 writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags); \
865 }
866
867/*!
868 * \macro KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags)
869 * \relates KConfigGroup
870 * Similar to KCONFIGGROUP_DECLARE_ENUM_QOBJECT but for flags declared with Q_FLAG()
871 * (where multiple values can be set at the same time)
872 */
873#define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags) \
874 template<> \
875 Class::Flags KConfigGroup::readEntry(const char *key, const Class::Flags &def) const \
876 { \
877 const QMetaObject *M_obj = &Class::staticMetaObject; \
878 const int M_index = M_obj->indexOfEnumerator(#Flags); \
879 if (M_index == -1) \
880 qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Flags)); \
881 const QMetaEnum M_enum = M_obj->enumerator(M_index); \
882 const QByteArray M_data = readEntry(key, QByteArray(M_enum.valueToKeys(def))); \
883 return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData())); \
884 } \
885 template<> \
886 void KConfigGroup::writeEntry(const char *key, const Class::Flags &value, KConfigBase::WriteConfigFlags flags) \
887 { \
888 const QMetaObject *M_obj = &Class::staticMetaObject; \
889 const int M_index = M_obj->indexOfEnumerator(#Flags); \
890 if (M_index == -1) \
891 qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Flags)); \
892 const QMetaEnum M_enum = M_obj->enumerator(M_index); \
893 writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags); \
894 }
895
896#include "kconfigconversioncheck_p.h"
897
898template<typename T>
899T KConfigGroup::readEntry(const char *key, const T &defaultValue) const
900{
901 KConfigConversionCheck::to_QVariant<T>();
902 return qvariant_cast<T>(readEntry(key, QVariant::fromValue(defaultValue)));
903}
904
905template<typename T>
906QList<T> KConfigGroup::readEntry(const char *key, const QList<T> &defaultValue) const
907{
908 KConfigConversionCheck::to_QVariant<T>();
909 KConfigConversionCheck::to_QString<T>();
910
911 QVariantList data;
912
913 for (const T &value : defaultValue) {
914 data.append(QVariant::fromValue(value));
915 }
916
917 QList<T> list;
918 const auto variantList = readEntry<QVariantList>(key, defaultValue: data);
919 for (const QVariant &value : variantList) {
920 Q_ASSERT(value.canConvert<T>());
921 list.append(qvariant_cast<T>(value));
922 }
923
924 return list;
925}
926
927template<typename T>
928void KConfigGroup::writeEntry(const char *key, const T &value, WriteConfigFlags pFlags)
929{
930 KConfigConversionCheck::to_QVariant<T>();
931 writeEntry(key, QVariant::fromValue(value), pFlags);
932}
933
934template<typename T>
935void KConfigGroup::writeEntry(const char *key, const QList<T> &list, WriteConfigFlags pFlags)
936{
937 KConfigConversionCheck::to_QVariant<T>();
938 KConfigConversionCheck::to_QString<T>();
939 QVariantList data;
940 for (const T &value : list) {
941 data.append(QVariant::fromValue(value));
942 }
943
944 writeEntry(key, value: data, pFlags);
945}
946
947#endif // KCONFIGGROUP_H
948

source code of kconfig/src/core/kconfiggroup.h