123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /*
- 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;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Settings::ServerList Settings::serverList()
- {
- ServerList list;
- int size = ourSettings->beginReadArray("servers");
- for(int i = 0; i < size; ++i)
- {
- ourSettings->setArrayIndex(i);
- QStringList svaddr = ourSettings->value("address").toString().split(":");
- Server sv;
- if(svaddr.size() >= 2)
- {
- sv.address = svaddr.at(0);
- sv.port = svaddr.at(1).toUShort();
- if(svaddr.size() > 2)
- sv.password = svaddr.at(2);
- }
- else
- continue;
- list.append(sv);
- }
- ourSettings->endArray();
- return list;
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- void Settings::setServerList(ServerList &list)
- {
- ourSettings->beginWriteArray("servers");
- for(int i = 0; i < list.size(); ++i)
- {
- ourSettings->setArrayIndex(i);
- ourSettings->setValue("address", list.at(i).address + ":" + QString::number(list.at(i).port) + ":" + list.at(i).password);
- }
- 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();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- QString Settings::sshHostName() const
- {
- return ourSettings->value("sshHostName", "b4r.org").toString();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- QString Settings::sshUserName() const
- {
- return ourSettings->value("sshUserName", "stomp").toString();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- quint16 Settings::sshPort() const
- {
- return ourSettings->value("sshPort", 22).toUInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::refreshHostNamesHour() const
- {
- return ourSettings->value("refreshHostNamesHour", 21).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- int Settings::maxServers() const
- {
- return ourSettings->value("maxServers", 100).toInt();
- }
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 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());
- ourSettings->setValue("sshHostName", sshHostName());
- ourSettings->setValue("sshUserName", sshUserName());
- ourSettings->setValue("sshPort", sshPort());
- ourSettings->setValue("refreshHostNamesHour", refreshHostNamesHour());
- ourSettings->setValue("maxServers", maxServers());
- ServerList list = serverList();
- setServerList(list);
- }
|