Settings.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "Settings.h"
  16. #include <QSettings>
  17. #include <QCoreApplication>
  18. #include <QStringList>
  19. #include <stdio.h>
  20. Settings* Settings::ourInstance = NULL;
  21. QSettings* Settings::ourSettings;
  22. Settings::Settings()
  23. {
  24. ourSettings = new QSettings(QCoreApplication::applicationDirPath() + "/cimsqwbot.cfg", QSettings::IniFormat);
  25. }
  26. Settings::~Settings()
  27. {
  28. delete ourSettings;
  29. }
  30. bool Settings::changeConfigFile(const QString &fileName)
  31. {
  32. delete ourSettings;
  33. ourSettings = new QSettings(fileName, QSettings::IniFormat);
  34. QSettings::Status status = ourSettings->status();
  35. if(status != QSettings::NoError)
  36. {
  37. printf("Failed to load config file [%s]. Falling back to default config file.\n", fileName.toAscii().data());
  38. delete ourSettings;
  39. ourSettings = NULL;
  40. return false;
  41. }
  42. return true;
  43. }
  44. Settings* Settings::globalInstance()
  45. {
  46. if(!ourInstance)
  47. ourInstance = new Settings();
  48. return ourInstance;
  49. }
  50. QStringList Settings::serverList()
  51. {
  52. QStringList list;
  53. int size = ourSettings->beginReadArray("servers");
  54. for(int i = 0; i < size; ++i)
  55. {
  56. ourSettings->setArrayIndex(i);
  57. list.append(ourSettings->value("address").toString());
  58. }
  59. ourSettings->endArray();
  60. return list;
  61. }
  62. void Settings::setServerList(QStringList &list)
  63. {
  64. ourSettings->beginWriteArray("servers");
  65. for(int i = 0; i < list.size(); ++i)
  66. {
  67. ourSettings->setArrayIndex(i);
  68. ourSettings->setValue("address", list.at(i));
  69. }
  70. ourSettings->endArray();
  71. }
  72. QString Settings::quakeFolder() const
  73. {
  74. return ourSettings->value("quakeFolder", QCoreApplication::applicationDirPath()).toString();
  75. }
  76. QString Settings::botName() const
  77. {
  78. return ourSettings->value("botName", "[ServeMe]").toString();
  79. }
  80. int Settings::botPing() const
  81. {
  82. return ourSettings->value("botPing", 666).toInt();
  83. }
  84. int Settings::botTopColor() const
  85. {
  86. return ourSettings->value("botTopColor", 11).toInt();
  87. }
  88. int Settings::botBottomColor() const
  89. {
  90. return ourSettings->value("botBottomColor", 12).toInt();
  91. }
  92. bool Settings::botSpectator() const
  93. {
  94. return ourSettings->value("botSpectator", true).toBool();
  95. }
  96. int Settings::floodProtTime() const
  97. {
  98. return qBound<int>(6, ourSettings->value("floodProtTime", 6).toInt(), 9999);
  99. }
  100. int Settings::qwFloodProtTime() const
  101. {
  102. return ourSettings->value("qwFloodProtTime", 600).toInt();
  103. }
  104. int Settings::spamFloodProtTime() const
  105. {
  106. return ourSettings->value("spamFloodProtTime", 300).toInt();
  107. }
  108. unsigned int Settings::queryInterval() const
  109. {
  110. return ourSettings->value("queryInterval", 1000).toUInt();
  111. }
  112. int Settings::timeToSayHiAfterConnected() const
  113. {
  114. return ourSettings->value("timeToSayHiAfterConnected", 7).toInt();
  115. }
  116. int Settings::timeToWaitForCountReply() const
  117. {
  118. return ourSettings->value("timeToWaitForCountReply", 7).toInt();
  119. }
  120. bool Settings::developerMode() const
  121. {
  122. return ourSettings->value("developerMode", false).toBool();
  123. }
  124. void Settings::save()
  125. {
  126. ourSettings->setValue("quakeFolder", quakeFolder());
  127. ourSettings->setValue("botName", botName());
  128. ourSettings->setValue("botPing", botPing());
  129. ourSettings->setValue("botTopColor", botTopColor());
  130. ourSettings->setValue("botBottomColor", botBottomColor());
  131. ourSettings->setValue("botSpectator", botSpectator());
  132. ourSettings->setValue("floodProtTime", floodProtTime());
  133. ourSettings->setValue("queryInterval", queryInterval());
  134. ourSettings->setValue("qwFloodProtTime", qwFloodProtTime());
  135. ourSettings->setValue("spamFloodProtTime", spamFloodProtTime());
  136. ourSettings->setValue("timeToSayHiAfterConnected", timeToSayHiAfterConnected());
  137. ourSettings->setValue("timeToWaitForCountReply", timeToWaitForCountReply());
  138. ourSettings->setValue("developerMode", developerMode());
  139. QStringList list = serverList();
  140. setServerList(list);
  141. }