|
|
|
@ -3,6 +3,7 @@
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
|
|
|
|
|
#include "setting.hpp"
|
|
|
|
|
#include "settingmanager.hpp"
|
|
|
|
@ -20,15 +21,6 @@ CSMSettings::SettingManager::SettingManager(QObject *parent) :
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSMSettings::SettingManager::dumpModel()
|
|
|
|
|
{
|
|
|
|
|
foreach (Setting *setting, mSettings)
|
|
|
|
|
{
|
|
|
|
|
if (setting->proxyLists().isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSMSettings::Setting *CSMSettings::SettingManager::createSetting
|
|
|
|
|
(CSMSettings::SettingType typ, const QString &page, const QString &name)
|
|
|
|
|
{
|
|
|
|
@ -36,7 +28,7 @@ CSMSettings::Setting *CSMSettings::SettingManager::createSetting
|
|
|
|
|
if (findSetting (page, name))
|
|
|
|
|
{
|
|
|
|
|
qWarning() << "Duplicate declaration encountered: "
|
|
|
|
|
<< (name + '.' + page);
|
|
|
|
|
<< (name + '/' + page);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -49,182 +41,6 @@ CSMSettings::Setting *CSMSettings::SettingManager::createSetting
|
|
|
|
|
return setting;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSMSettings::DefinitionPageMap
|
|
|
|
|
CSMSettings::SettingManager::readFilestream (QTextStream *stream)
|
|
|
|
|
{
|
|
|
|
|
//regEx's for page names and keys / values
|
|
|
|
|
QRegExp pageRegEx ("^\\[([^]]+)\\]");
|
|
|
|
|
QRegExp keyRegEx ("^([^=]+)\\s*=\\s*(.+)$");
|
|
|
|
|
|
|
|
|
|
QString currPage = "Unassigned";
|
|
|
|
|
|
|
|
|
|
DefinitionPageMap pageMap;
|
|
|
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
|
{
|
|
|
|
|
displayFileErrorMessage(mReadWriteMessage, false);
|
|
|
|
|
return pageMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream->atEnd())
|
|
|
|
|
return pageMap;
|
|
|
|
|
|
|
|
|
|
DefinitionMap *settingMap = new DefinitionMap();
|
|
|
|
|
pageMap[currPage] = settingMap;
|
|
|
|
|
|
|
|
|
|
while (!stream->atEnd())
|
|
|
|
|
{
|
|
|
|
|
QString line = stream->readLine().simplified();
|
|
|
|
|
|
|
|
|
|
if (line.isEmpty() || line.startsWith("#"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
//page name found
|
|
|
|
|
if (pageRegEx.exactMatch(line))
|
|
|
|
|
{
|
|
|
|
|
currPage = pageRegEx.cap(1).simplified().trimmed();
|
|
|
|
|
settingMap = new DefinitionMap();
|
|
|
|
|
pageMap[currPage] = settingMap;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//setting definition found
|
|
|
|
|
if ( (keyRegEx.indexIn(line) != -1))
|
|
|
|
|
{
|
|
|
|
|
QString settingName = keyRegEx.cap(1).simplified();
|
|
|
|
|
QString settingValue = keyRegEx.cap(2).simplified();
|
|
|
|
|
|
|
|
|
|
if (!settingMap->contains (settingName))
|
|
|
|
|
settingMap->insert (settingName, new QStringList());
|
|
|
|
|
|
|
|
|
|
settingMap->value(settingName)->append(settingValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//return empty map if no settings were ever added to
|
|
|
|
|
if (pageMap.size() == 1)
|
|
|
|
|
{
|
|
|
|
|
QString pageKey = pageMap.keys().at(0);
|
|
|
|
|
if (pageMap[pageKey]->size() == 0)
|
|
|
|
|
pageMap.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pageMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CSMSettings::SettingManager::writeFilestream(QTextStream *stream,
|
|
|
|
|
const QMap <QString, QStringList > &settingListMap)
|
|
|
|
|
{
|
|
|
|
|
if (!stream)
|
|
|
|
|
{
|
|
|
|
|
displayFileErrorMessage(mReadWriteMessage, false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//disabled after rolling selector class into view. Need to
|
|
|
|
|
//iterate views to get setting definitions before writing to file
|
|
|
|
|
|
|
|
|
|
QStringList sectionKeys;
|
|
|
|
|
|
|
|
|
|
foreach (const QString &key, settingListMap.keys())
|
|
|
|
|
{
|
|
|
|
|
QStringList names = key.split('.');
|
|
|
|
|
QString section = names.at(0);
|
|
|
|
|
|
|
|
|
|
if (!sectionKeys.contains(section))
|
|
|
|
|
if (!settingListMap.value(key).isEmpty())
|
|
|
|
|
sectionKeys.append (section);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (const QString §ion, sectionKeys)
|
|
|
|
|
{
|
|
|
|
|
*stream << '[' << section << "]\n";
|
|
|
|
|
foreach (const QString &key, settingListMap.keys())
|
|
|
|
|
{
|
|
|
|
|
QStringList names = key.split('.');
|
|
|
|
|
|
|
|
|
|
if (names.at(0) != section)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QStringList list = settingListMap.value(key);
|
|
|
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QString name = names.at(1);
|
|
|
|
|
|
|
|
|
|
foreach (const QString value, list)
|
|
|
|
|
{
|
|
|
|
|
if (value.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
*stream << name << " = " << value << '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroyStream (stream);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSMSettings::SettingManager::mergeSettings(DefinitionPageMap &destMap, DefinitionPageMap &srcMap)
|
|
|
|
|
{
|
|
|
|
|
if (srcMap.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (const QString &pageKey, srcMap.keys())
|
|
|
|
|
{
|
|
|
|
|
DefinitionMap *srcSetting = srcMap.value(pageKey);
|
|
|
|
|
//Unique Page:
|
|
|
|
|
//insertfrom the source map
|
|
|
|
|
if (!destMap.keys().contains (pageKey))
|
|
|
|
|
{
|
|
|
|
|
destMap.insert (pageKey, srcSetting);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DefinitionMap *destSetting = destMap.value(pageKey);
|
|
|
|
|
|
|
|
|
|
//Duplicate Page:
|
|
|
|
|
//iterate the settings in the source and check for duplicates in the
|
|
|
|
|
//destination
|
|
|
|
|
foreach (const QString &srcKey, srcSetting->keys())
|
|
|
|
|
{
|
|
|
|
|
//insert into destination if unique
|
|
|
|
|
if (!destSetting->keys().contains (srcKey))
|
|
|
|
|
destSetting->insert(srcKey, srcSetting->value (srcKey));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextStream *CSMSettings::SettingManager::openFilestream (const QString &filePath,
|
|
|
|
|
bool isReadOnly) const
|
|
|
|
|
{
|
|
|
|
|
QIODevice::OpenMode openFlags = QIODevice::Text;
|
|
|
|
|
|
|
|
|
|
if (isReadOnly)
|
|
|
|
|
openFlags = QIODevice::ReadOnly | openFlags;
|
|
|
|
|
else
|
|
|
|
|
openFlags = QIODevice::ReadWrite | QIODevice::Truncate | openFlags;
|
|
|
|
|
|
|
|
|
|
QFile *file = new QFile(filePath);
|
|
|
|
|
QTextStream *stream = 0;
|
|
|
|
|
|
|
|
|
|
if (file->open(openFlags))
|
|
|
|
|
stream = new QTextStream(file);
|
|
|
|
|
|
|
|
|
|
if (stream)
|
|
|
|
|
stream->setCodec(QTextCodec::codecForName("UTF-8"));
|
|
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSMSettings::SettingManager::destroyStream(QTextStream *stream) const
|
|
|
|
|
{
|
|
|
|
|
stream->device()->close();
|
|
|
|
|
|
|
|
|
|
delete stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSMSettings::SettingManager::displayFileErrorMessage(const QString &message,
|
|
|
|
|
bool isReadOnly) const
|
|
|
|
|
{
|
|
|
|
@ -242,29 +58,29 @@ void CSMSettings::SettingManager::displayFileErrorMessage(const QString &message
|
|
|
|
|
msgBox.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSMSettings::SettingManager::addDefinitions (DefinitionPageMap &pageMap)
|
|
|
|
|
void CSMSettings::SettingManager::addDefinitions (const QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
foreach (QString pageName, pageMap.keys())
|
|
|
|
|
foreach (const QString &key, settings->allKeys())
|
|
|
|
|
{
|
|
|
|
|
DefinitionMap *settingMap = pageMap.value (pageName);
|
|
|
|
|
QStringList names = key.split('/');
|
|
|
|
|
|
|
|
|
|
Setting *setting = findSetting (names.at(0), names.at(1));
|
|
|
|
|
|
|
|
|
|
foreach (QString settingName, (*settingMap).keys())
|
|
|
|
|
if (!setting)
|
|
|
|
|
{
|
|
|
|
|
QStringList *values = settingMap->value (settingName);
|
|
|
|
|
Setting *setting = findSetting (pageName, settingName);
|
|
|
|
|
qWarning() << "Found definitions for undeclared setting "
|
|
|
|
|
<< names.at(0) << "." << names.at(1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!setting)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << "Found definitions for undeclared setting "
|
|
|
|
|
<< pageName << "." << settingName;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
QStringList values = settings->value (key).toStringList();
|
|
|
|
|
|
|
|
|
|
if (values->size() == 0)
|
|
|
|
|
values->append (setting->defaultValues());
|
|
|
|
|
if (values.isEmpty())
|
|
|
|
|
values.append (setting->defaultValues());
|
|
|
|
|
|
|
|
|
|
setting->setDefinedValues (*values);
|
|
|
|
|
}
|
|
|
|
|
setting->setDefinedValues (values);
|
|
|
|
|
|
|
|
|
|
qDebug() << "added definitons " << values;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -297,7 +113,7 @@ CSMSettings::Setting *CSMSettings::SettingManager::findSetting
|
|
|
|
|
{
|
|
|
|
|
foreach (Setting *setting, mSettings)
|
|
|
|
|
{
|
|
|
|
|
if (setting->name() == settingName)
|
|
|
|
|
if (settingName.isEmpty() || (setting->name() == settingName))
|
|
|
|
|
{
|
|
|
|
|
if (setting->page() == pageName)
|
|
|
|
|
return setting;
|
|
|
|
@ -305,7 +121,7 @@ CSMSettings::Setting *CSMSettings::SettingManager::findSetting
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
QList <CSMSettings::Setting *> CSMSettings::SettingManager::findSettings
|
|
|
|
|
(const QString &pageName)
|
|
|
|
|
{
|
|
|
|
@ -318,7 +134,7 @@ QList <CSMSettings::Setting *> CSMSettings::SettingManager::findSettings
|
|
|
|
|
}
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
CSMSettings::SettingPageMap CSMSettings::SettingManager::settingPageMap() const
|
|
|
|
|
{
|
|
|
|
|
SettingPageMap pageMap;
|
|
|
|
@ -332,7 +148,7 @@ CSMSettings::SettingPageMap CSMSettings::SettingManager::settingPageMap() const
|
|
|
|
|
void CSMSettings::SettingManager::updateUserSetting(const QString &settingKey,
|
|
|
|
|
const QStringList &list)
|
|
|
|
|
{
|
|
|
|
|
QStringList names = settingKey.split('.');
|
|
|
|
|
QStringList names = settingKey.split('/');
|
|
|
|
|
|
|
|
|
|
Setting *setting = findSetting (names.at(0), names.at(1));
|
|
|
|
|
|
|
|
|
|