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

0001 /**
0002  * \file generalconfig.h
0003  * General configuration.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 17 Sep 2003
0008  *
0009  * Copyright (C) 2003-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 #pragma once
0028 
0029 #include <QObject>
0030 #include <QString>
0031 #include <QStringList>
0032 #include "configstore.h"
0033 
0034 class ISettings;
0035 
0036 /**
0037  * Abstract base class for configurations.
0038  */
0039 class KID3_CORE_EXPORT GeneralConfig : public QObject {
0040   Q_OBJECT
0041 public:
0042   /**
0043    * Constructor.
0044    * Set default configuration.
0045    *
0046    * @param grp configuration group
0047    */
0048   explicit GeneralConfig(const QString& grp);
0049 
0050   /**
0051    * Destructor.
0052    */
0053   ~GeneralConfig() override = default;
0054 
0055   /**
0056    * Persist configuration.
0057    *
0058    * @param config KDE configuration
0059    */
0060   virtual void writeToConfig(ISettings* config) const = 0;
0061 
0062   /**
0063    * Read persisted configuration.
0064    *
0065    * @param config KDE configuration
0066    */
0067   virtual void readFromConfig(ISettings* config) = 0;
0068 
0069   /**
0070    * String list of available text codecs.
0071    *
0072    * @return list of codec names.
0073    */
0074   Q_INVOKABLE static QStringList getTextCodecNames();
0075 
0076 protected:
0077   /**
0078    * Convert list of integers to list of strings.
0079    * @param intList list of integers
0080    * @return list of strings.
0081    */
0082   static QStringList intListToStringList(const QList<int>& intList);
0083 
0084   /**
0085    * Convert list of strings to list of integers.
0086    * @param strList list of strings
0087    * @return list of integers.
0088    */
0089   static QList<int> stringListToIntList(const QStringList& strList);
0090 
0091   /**
0092    * Remove aliases in braces from text encoding name.
0093    *
0094    * @param comboEntry text encoding name
0095    *
0096    * @return codec name.
0097    */
0098   static QString getTextCodecName(const QString& comboEntry);
0099 
0100   /**
0101    * Get index of text encoding in getTextCodecNames().
0102    * @param textEncoding text encoding name
0103    * @return index of encoding.
0104    */
0105   static int indexFromTextCodecName(const QString& textEncoding);
0106 
0107   /**
0108    * Get text encoding name from index in getTextCodecNames().
0109    * @param index index of encoding
0110    * @return text encoding name, null if index invalid.
0111    */
0112   static QString indexToTextCodecName(int index);
0113 
0114   /** Configuration group. */
0115   QString m_group;
0116 };
0117 
0118 /**
0119  * Template to inject a static instance() method into a configuration class.
0120  * This is an application of the "curiously recurring template pattern" so that
0121  * the instance() method returns the type of the derived class.
0122  * A typical usage is
0123  * @code
0124  * class SpecializedConfig : public StoredConfig<SpecializedConfig> {
0125  * public:
0126  *   explicit SpecializedConfig(const QString& grp) : StoredConfig(grp) {
0127  *     (..)
0128  *   }
0129  * };
0130  * @endcode
0131  *
0132  * SpecializedConfig::instance() returns a reference to a stored instance
0133  * of this class. There can only be one such instance per class.
0134  *
0135  * @tparam Derived derived class
0136  * @tparam Base base class, default is GeneralConfig
0137  */
0138 template <class Derived, class Base = GeneralConfig>
0139 class StoredConfig : public Base {
0140 public:
0141   /**
0142    * Constructor.
0143    * Set default configuration.
0144    *
0145    * @param grp configuration group
0146    */
0147   explicit StoredConfig(const QString& grp) : Base(grp) {}
0148 
0149   /**
0150    * Get stored instance of class.
0151    *
0152    * @return instance.
0153    */
0154   static Derived& instance();
0155 };
0156 
0157 template <class Derived, class Base>
0158 Derived& StoredConfig<Derived, Base>::instance() {
0159   Derived* obj = nullptr;
0160   ConfigStore* store = ConfigStore::instance();
0161   if (Derived::s_index >= 0) {
0162     obj = static_cast<Derived*>(store->configuration(Derived::s_index));
0163   } else {
0164     obj = new Derived;
0165     obj->setParent(store);
0166     Derived::s_index = store->addConfiguration(obj);
0167   }
0168   return *obj;
0169 }