File indexing completed on 2024-05-19 04:55:51

0001 /**
0002  * \file kdesettings.cpp
0003  * Wrapper for KDE application settings.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 07 Apr 2013
0008  *
0009  * Copyright (C) 2013-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "kdesettings.h"
0028 #include <QtConfig>
0029 #include <KConfigGroup>
0030 
0031 /**
0032  * Constructor.
0033  * @param config KDE settings
0034  * @param stateConfig state information
0035  */
0036 KdeSettings::KdeSettings(KSharedConfigPtr config, KSharedConfigPtr stateConfig)
0037   : m_config(config), m_stateConfig(stateConfig)
0038 {
0039   migrateOldSettings();
0040 }
0041 
0042 /**
0043  * Destructor.
0044  */
0045 KdeSettings::~KdeSettings()
0046 {
0047   // Must not be inline because of forwared declared QScopedPointer.
0048 }
0049 
0050 /**
0051  * Use settings subgroup.
0052  * @param prefix group name
0053  * @param forState true if this group stores state information
0054  */
0055 void KdeSettings::beginGroup(const QString& prefix, bool forState)
0056 {
0057   m_group.reset(new KConfigGroup(forState ? m_stateConfig : m_config, prefix));
0058 }
0059 
0060 /**
0061  * Finnish using settings subgroup.
0062  */
0063 void KdeSettings::endGroup()
0064 {
0065   m_group.reset();
0066 }
0067 
0068 /**
0069  * Set value for setting.
0070  * @param key name of setting
0071  * @param value value for setting
0072  */
0073 void KdeSettings::setValue(const QString& key, const QVariant& value)
0074 {
0075   if (m_group) {
0076     m_group->writeEntry(key, value);
0077   }
0078 }
0079 
0080 /**
0081  * Get value for setting.
0082  * @param key name of setting
0083  * @param defaultValue default value
0084  * @return value of setting as variant.
0085  */
0086 QVariant KdeSettings::value(const QString& key,
0087                             const QVariant& defaultValue) const
0088 {
0089   if (m_group) {
0090     return m_group->readEntry(key, defaultValue);
0091   }
0092   return QVariant();
0093 }
0094 
0095 /**
0096  * Remove setting.
0097  * @param key name of setting
0098  */
0099 void KdeSettings::remove(const QString& key)
0100 {
0101   if (m_group) {
0102     m_group->deleteEntry(key);
0103   }
0104 }
0105 
0106 /**
0107  * Check if setting exists.
0108  * @param key name of setting
0109  * @return true if setting exists.
0110  */
0111 bool KdeSettings::contains(const QString& key) const
0112 {
0113   if (m_group) {
0114     return m_group->hasKey(key);
0115   }
0116   return false;
0117 }
0118 
0119 /**
0120  * Write unsaved changes to permanent storage.
0121  */
0122 void KdeSettings::sync()
0123 {
0124   m_config->sync();
0125   m_stateConfig->sync();
0126 }