File indexing completed on 2024-10-06 05:04:24

0001 /***************************************************************************
0002  *  Copyright (C) 2010 by Renaud Guezennec                             *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "preferences/preferencesmanager.h"
0021 #include "preferences/preferenceslistener.h"
0022 #include <QDebug>
0023 #include <QDir>
0024 #include <QMetaType>
0025 #include <QSettings>
0026 
0027 PreferencesManager* PreferencesManager::m_singleton= nullptr;
0028 
0029 PreferencesManager::PreferencesManager(const QString& applicationName, const QString& subname)
0030     : m_optionDictionary(nullptr), m_applicationName(applicationName), m_subname(subname)
0031 {
0032     m_optionDictionary= new QMap<QString, QVariant>;
0033 
0034     // Default value
0035     m_optionDictionary->insert("MusicDirectory", QDir::homePath());
0036     m_optionDictionary->insert("ImageDirectory", QDir::homePath());
0037     m_optionDictionary->insert("MapDirectory", QDir::homePath());
0038     m_optionDictionary->insert("SessionDirectory", QDir::homePath());
0039     m_optionDictionary->insert("MinutesDirectory", QDir::homePath());
0040     m_optionDictionary->insert("ChatDirectory", QDir::homePath());
0041     m_optionDictionary->insert("DataDirectory", QDir::homePath());
0042 
0043     // TODO To remove
0044     m_singleton= this;
0045 }
0046 
0047 PreferencesManager::~PreferencesManager()
0048 {
0049     delete m_optionDictionary;
0050 }
0051 
0052 PreferencesManager* PreferencesManager::getInstance()
0053 {
0054     if(m_singleton == nullptr)
0055     {
0056         m_singleton= new PreferencesManager({}, {});
0057     }
0058 
0059     return m_singleton;
0060 }
0061 bool PreferencesManager::registerValue(const QString& key, QVariant value, bool overwrite)
0062 {
0063     if((overwrite) || (!m_optionDictionary->contains(key)))
0064     {
0065         QVariant oldValue;
0066         if(m_optionDictionary->contains(key))
0067         {
0068             oldValue= m_optionDictionary->value(key);
0069         }
0070         m_optionDictionary->insert(key, value);
0071         if(oldValue != value)
0072         {
0073             writeSettings();
0074             emit dataChanged(key, value);
0075             notifyListener(key, value);
0076         }
0077         return true;
0078     }
0079     else
0080         return false;
0081 }
0082 const QVariant PreferencesManager::value(const QString& key, const QVariant& defaultValue)
0083 {
0084     return m_optionDictionary->contains(key) ? m_optionDictionary->value(key) : defaultValue;
0085 }
0086 void PreferencesManager::readSettings()
0087 {
0088     QSettings settings(m_applicationName, m_subname);
0089     settings.beginGroup("rolisteam/preferences");
0090     int size= settings.beginReadArray("preferenceMap");
0091     for(int i= 0; i < size; ++i)
0092     {
0093         settings.setArrayIndex(i);
0094         QString key= settings.value("key").toString();
0095         QVariant value= settings.value("value");
0096         m_optionDictionary->insert(key, value);
0097     }
0098     settings.endArray();
0099     settings.endGroup();
0100 
0101     notifyAllListener();
0102 }
0103 void PreferencesManager::writeSettings()
0104 {
0105     QSettings settings(m_applicationName, m_subname);
0106 
0107     settings.beginGroup("rolisteam/preferences");
0108     settings.beginWriteArray("preferenceMap");
0109     settings.clear();
0110     auto const& keys= m_optionDictionary->keys();
0111     for(int i= 0; i < m_optionDictionary->size(); ++i)
0112     {
0113         settings.setArrayIndex(i);
0114         QString key= keys.at(i);
0115         QVariant var= m_optionDictionary->value(key);
0116         settings.setValue("key", key);
0117         settings.setValue("value", var);
0118     }
0119     settings.endArray();
0120     settings.endGroup();
0121 }
0122 void PreferencesManager::registerListener(const QString& str, PreferencesListener* listerner)
0123 {
0124     m_listernerMap.insert(str, listerner);
0125 }
0126 void PreferencesManager::registerLambda(const QString& key, std::function<void(QVariant)> func)
0127 {
0128     m_lambdaMap.insert({key, func});
0129 }
0130 
0131 void PreferencesManager::notifyAllListener()
0132 {
0133     for(const auto& key : m_optionDictionary->keys())
0134     {
0135         notifyListener(key, m_optionDictionary->value(key));
0136     }
0137 }
0138 
0139 void PreferencesManager::notifyListener(const QString& key, const QVariant& value)
0140 {
0141     PreferencesListener* tmp= m_listernerMap.value(key);
0142     if(nullptr != tmp)
0143     {
0144         tmp->preferencesHasChanged(key);
0145     }
0146 
0147     auto it= m_lambdaMap.find(key);
0148     if(it != m_lambdaMap.end())
0149     {
0150         auto func= it->second;
0151         if(nullptr != func)
0152             func(value);
0153     }
0154 }