File indexing completed on 2024-05-26 04:57:43

0001 /**
0002  * \file configstore.cpp
0003  * Configuration storage.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 09 Jul 2011
0008  *
0009  * Copyright (C) 2011-2024  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 "configstore.h"
0028 #include "generalconfig.h"
0029 #include "isettings.h"
0030 
0031 namespace {
0032 
0033 /**
0034  * Current configuration file version.
0035  * 0: <= 3.0.2
0036  * 1: 3.1
0037  * 2: 3.2
0038  * 3: 3.3
0039  * 4: 3.7
0040  * 5: 3.8.3
0041  * 6: 3.8.5
0042  * 7: 3.9.0
0043  * 8: 3.9.3
0044  */
0045 constexpr int CONFIG_VERSION = 8;
0046 
0047 }
0048 
0049 /** Version of configuration file read, -1 if not read yet. */
0050 int ConfigStore::s_configVersion = -1;
0051 
0052 
0053 ConfigStore* ConfigStore::s_self = nullptr;
0054 
0055 /**
0056  * Constructor.
0057  * @param config application settings
0058  */
0059 ConfigStore::ConfigStore(ISettings* config) : m_config(config)
0060 {
0061   Q_ASSERT_X(!s_self, "ConfigStore", "there should be only one config store");
0062   s_self = this;
0063 }
0064 
0065 /**
0066  * Destructor.
0067  */
0068 ConfigStore::~ConfigStore()
0069 {
0070   qDeleteAll(m_configurations);
0071 }
0072 
0073 /**
0074  * Persist all added configurations.
0075  */
0076 void ConfigStore::writeToConfig()
0077 {
0078   const auto cfgs = m_configurations;
0079   for (const GeneralConfig* cfg : cfgs) {
0080     cfg->writeToConfig(m_config);
0081   }
0082   m_config->beginGroup(QLatin1String("ConfigStore"));
0083   m_config->setValue(QLatin1String("ConfigVersion"), QVariant(
0084       s_configVersion > CONFIG_VERSION ? s_configVersion : CONFIG_VERSION));
0085   m_config->endGroup();
0086 }
0087 
0088 /**
0089  * Add a configuration.
0090  * The configuration will be read from the application settings.
0091  *
0092  * @param cfg configuration, ownership is taken
0093  * @return index of configuration.
0094  */
0095 int ConfigStore::addConfiguration(GeneralConfig* cfg)
0096 {
0097   Q_ASSERT(cfg);
0098   if (!cfg)
0099     return -1;
0100 
0101   if (s_configVersion == -1) {
0102     m_config->beginGroup(QLatin1String("ConfigStore"));
0103     s_configVersion =
0104         m_config->value(QLatin1String("ConfigVersion"), 0).toInt();
0105     m_config->endGroup();
0106   }
0107   int index = m_configurations.size();
0108   m_configurations.append(cfg);
0109   cfg->readFromConfig(m_config);
0110   return index;
0111 }