1 /**
2 * \file kdesettings.cpp
3 * Wrapper for KDE application settings.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 07 Apr 2013
8 *
9 * Copyright (C) 2013-2018 Urs Fleisch
10 *
11 * This file is part of Kid3.
12 *
13 * Kid3 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Kid3 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include "kdesettings.h"
28 #include <QtConfig>
29 #include <KConfig>
30 #include <KConfigGroup>
31
32 /**
33 * Constructor.
34 */
KdeSettings(KConfig * config)35 KdeSettings::KdeSettings(KConfig* config) : m_config(config)
36 {
37 migrateOldSettings();
38 }
39
40 /**
41 * Destructor.
42 */
~KdeSettings()43 KdeSettings::~KdeSettings()
44 {
45 // Must not be inline because of forwared declared QScopedPointer.
46 }
47
48 /**
49 * Use settings subgroup.
50 * @param prefix group name
51 */
beginGroup(const QString & prefix)52 void KdeSettings::beginGroup(const QString& prefix)
53 {
54 m_group.reset(new KConfigGroup(m_config, prefix));
55 }
56
57 /**
58 * Finnish using settings subgroup.
59 */
endGroup()60 void KdeSettings::endGroup()
61 {
62 m_group.reset();
63 }
64
65 /**
66 * Set value for setting.
67 * @param key name of setting
68 * @param value value for setting
69 */
setValue(const QString & key,const QVariant & value)70 void KdeSettings::setValue(const QString& key, const QVariant& value)
71 {
72 if (m_group) {
73 m_group->writeEntry(key, value);
74 }
75 }
76
77 /**
78 * Get value for setting.
79 * @param key name of setting
80 * @param defaultValue default value
81 * @return value of setting as variant.
82 */
value(const QString & key,const QVariant & defaultValue) const83 QVariant KdeSettings::value(const QString& key,
84 const QVariant& defaultValue) const
85 {
86 if (m_group) {
87 return m_group->readEntry(key, defaultValue);
88 }
89 return QVariant();
90 }
91
92 /**
93 * Remove setting.
94 * @param key name of setting
95 */
remove(const QString & key)96 void KdeSettings::remove(const QString& key)
97 {
98 if (m_group) {
99 m_group->deleteEntry(key);
100 }
101 }
102
103 /**
104 * Check if setting exists.
105 * @param key name of setting
106 * @return true if setting exists.
107 */
contains(const QString & key) const108 bool KdeSettings::contains(const QString& key) const
109 {
110 if (m_group) {
111 return m_group->hasKey(key);
112 }
113 return false;
114 }
115
116 /**
117 * Write unsaved changes to permanent storage.
118 */
sync()119 void KdeSettings::sync()
120 {
121 m_config->sync();
122 }
123