123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*
- GNU General Public License version 3 notice
- Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see < http://www.gnu.org/licenses/ >.
- */
- #include "Settings.h"
- #include <QSettings>
- #include <QCoreApplication>
- #include <QStringList>
- #include <stdio.h>
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Settings* Settings::ourInstance = NULL;
- QSettings* Settings::ourSettings;
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Settings::Settings()
- {
- ourSettings = new QSettings(QCoreApplication::applicationDirPath() + "/cimsqwbot.cfg", QSettings::IniFormat);
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Settings::~Settings()
- {
- delete ourSettings;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- bool Settings::changeConfigFile(const QString &fileName)
- {
- delete ourSettings;
- ourSettings = new QSettings(fileName, QSettings::IniFormat);
- QSettings::Status status = ourSettings->status();
- if(status != QSettings::NoError)
- {
- printf("Failed to load config file [%s]. Falling back to default config file.\n", fileName.toAscii().data());
- delete ourSettings;
- ourSettings = NULL;
- return false;
- }
- return true;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Settings* Settings::globalInstance()
- {
- if(!ourInstance)
- ourInstance = new Settings();
- return ourInstance;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- QStringList Settings::serverList()
- {
- QStringList list;
- int size = ourSettings->beginReadArray("servers");
- for(int i = 0; i < size; ++i)
- {
- ourSettings->setArrayIndex(i);
- list.append(ourSettings->value("address").toString());
- }
- ourSettings->endArray();
- return list;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void Settings::setServerList(QStringList &list)
- {
- ourSettings->beginWriteArray("servers");
- for(int i = 0; i < list.size(); ++i)
- {
- ourSettings->setArrayIndex(i);
- ourSettings->setValue("address", list.at(i));
- }
- ourSettings->endArray();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- QString Settings::quakeFolder() const
- {
- return ourSettings->value("quakeFolder", QCoreApplication::applicationDirPath()).toString();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- QString Settings::botName() const
- {
- return ourSettings->value("botName", "[ServeMe]").toString();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::botPing() const
- {
- return ourSettings->value("botPing", 666).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::botTopColor() const
- {
- return ourSettings->value("botTopColor", 11).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::botBottomColor() const
- {
- return ourSettings->value("botBottomColor", 12).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- bool Settings::botSpectator() const
- {
- return ourSettings->value("botSpectator", true).toBool();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::floodProtTime() const
- {
- return qBound<int>(6, ourSettings->value("floodProtTime", 6).toInt(), 9999);
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::qwFloodProtTime() const
- {
- return ourSettings->value("qwFloodProtTime", 600).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::spamFloodProtTime() const
- {
- return ourSettings->value("spamFloodProtTime", 300).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- unsigned int Settings::queryInterval() const
- {
- return ourSettings->value("queryInterval", 1000).toUInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::timeToSayHiAfterConnected() const
- {
- return ourSettings->value("timeToSayHiAfterConnected", 7).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::timeToWaitForCountReply() const
- {
- return ourSettings->value("timeToWaitForCountReply", 7).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- bool Settings::developerMode() const
- {
- return ourSettings->value("developerMode", false).toBool();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void Settings::save()
- {
- ourSettings->setValue("quakeFolder", quakeFolder());
- ourSettings->setValue("botName", botName());
- ourSettings->setValue("botPing", botPing());
- ourSettings->setValue("botTopColor", botTopColor());
- ourSettings->setValue("botBottomColor", botBottomColor());
- ourSettings->setValue("botSpectator", botSpectator());
- ourSettings->setValue("floodProtTime", floodProtTime());
- ourSettings->setValue("queryInterval", queryInterval());
- ourSettings->setValue("qwFloodProtTime", qwFloodProtTime());
- ourSettings->setValue("spamFloodProtTime", spamFloodProtTime());
- ourSettings->setValue("timeToSayHiAfterConnected", timeToSayHiAfterConnected());
- ourSettings->setValue("timeToWaitForCountReply", timeToWaitForCountReply());
- ourSettings->setValue("developerMode", developerMode());
- QStringList list = serverList();
- setServerList(list);
- }
|