Settings.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  21. Settings* Settings::ourInstance = NULL;
  22. QSettings* Settings::ourSettings;
  23. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  24. Settings::Settings()
  25. {
  26. ourSettings = new QSettings(QCoreApplication::applicationDirPath() + "/cimsqwbot.cfg", QSettings::IniFormat);
  27. }
  28. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  29. Settings::~Settings()
  30. {
  31. delete ourSettings;
  32. }
  33. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  34. bool Settings::changeConfigFile(const QString &fileName)
  35. {
  36. delete ourSettings;
  37. ourSettings = new QSettings(fileName, QSettings::IniFormat);
  38. QSettings::Status status = ourSettings->status();
  39. if(status != QSettings::NoError)
  40. {
  41. printf("Failed to load config file [%s]. Falling back to default config file.\n", fileName.toAscii().data());
  42. delete ourSettings;
  43. ourSettings = NULL;
  44. return false;
  45. }
  46. return true;
  47. }
  48. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49. Settings* Settings::globalInstance()
  50. {
  51. if(!ourInstance)
  52. ourInstance = new Settings();
  53. return ourInstance;
  54. }
  55. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  56. Settings::ServerList Settings::serverList()
  57. {
  58. ServerList list;
  59. int size = ourSettings->beginReadArray("servers");
  60. for(int i = 0; i < size; ++i)
  61. {
  62. ourSettings->setArrayIndex(i);
  63. QStringList svaddr = ourSettings->value("address").toString().split(":");
  64. Server sv;
  65. if(svaddr.size() >= 2)
  66. {
  67. sv.address = svaddr.at(0);
  68. sv.port = svaddr.at(1).toUShort();
  69. if(svaddr.size() > 2)
  70. sv.password = svaddr.at(2);
  71. }
  72. else
  73. continue;
  74. list.append(sv);
  75. }
  76. ourSettings->endArray();
  77. return list;
  78. }
  79. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  80. void Settings::setServerList(ServerList &list)
  81. {
  82. ourSettings->beginWriteArray("servers");
  83. for(int i = 0; i < list.size(); ++i)
  84. {
  85. ourSettings->setArrayIndex(i);
  86. ourSettings->setValue("address", list.at(i).address + ":" + QString::number(list.at(i).port) + ":" + list.at(i).password);
  87. }
  88. ourSettings->endArray();
  89. }
  90. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  91. QString Settings::quakeFolder() const
  92. {
  93. return ourSettings->value("quakeFolder", QCoreApplication::applicationDirPath()).toString();
  94. }
  95. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  96. QString Settings::botName() const
  97. {
  98. return ourSettings->value("botName", "[ServeMe]").toString();
  99. }
  100. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  101. int Settings::botPing() const
  102. {
  103. return ourSettings->value("botPing", 666).toInt();
  104. }
  105. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  106. int Settings::botTopColor() const
  107. {
  108. return ourSettings->value("botTopColor", 11).toInt();
  109. }
  110. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  111. int Settings::botBottomColor() const
  112. {
  113. return ourSettings->value("botBottomColor", 12).toInt();
  114. }
  115. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  116. bool Settings::botSpectator() const
  117. {
  118. return ourSettings->value("botSpectator", true).toBool();
  119. }
  120. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  121. int Settings::floodProtTime() const
  122. {
  123. return qBound<int>(6, ourSettings->value("floodProtTime", 6).toInt(), 9999);
  124. }
  125. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  126. int Settings::qwFloodProtTime() const
  127. {
  128. return ourSettings->value("qwFloodProtTime", 600).toInt();
  129. }
  130. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  131. int Settings::spamFloodProtTime() const
  132. {
  133. return ourSettings->value("spamFloodProtTime", 300).toInt();
  134. }
  135. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  136. unsigned int Settings::queryInterval() const
  137. {
  138. return ourSettings->value("queryInterval", 1000).toUInt();
  139. }
  140. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  141. int Settings::timeToSayHiAfterConnected() const
  142. {
  143. return ourSettings->value("timeToSayHiAfterConnected", 7).toInt();
  144. }
  145. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  146. int Settings::timeToWaitForCountReply() const
  147. {
  148. return ourSettings->value("timeToWaitForCountReply", 7).toInt();
  149. }
  150. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  151. bool Settings::developerMode() const
  152. {
  153. return ourSettings->value("developerMode", false).toBool();
  154. }
  155. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  156. QString Settings::sshHostName() const
  157. {
  158. return ourSettings->value("sshHostName", "b4r.org").toString();
  159. }
  160. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  161. QString Settings::sshUserName() const
  162. {
  163. return ourSettings->value("sshUserName", "stomp").toString();
  164. }
  165. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  166. quint16 Settings::sshPort() const
  167. {
  168. return ourSettings->value("sshPort", 22).toUInt();
  169. }
  170. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  171. int Settings::refreshHostNamesHour() const
  172. {
  173. return ourSettings->value("refreshHostNamesHour", 21).toInt();
  174. }
  175. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  176. int Settings::maxServers() const
  177. {
  178. return ourSettings->value("maxServers", 100).toInt();
  179. }
  180. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  181. void Settings::save()
  182. {
  183. ourSettings->setValue("quakeFolder", quakeFolder());
  184. ourSettings->setValue("botName", botName());
  185. ourSettings->setValue("botPing", botPing());
  186. ourSettings->setValue("botTopColor", botTopColor());
  187. ourSettings->setValue("botBottomColor", botBottomColor());
  188. ourSettings->setValue("botSpectator", botSpectator());
  189. ourSettings->setValue("floodProtTime", floodProtTime());
  190. ourSettings->setValue("queryInterval", queryInterval());
  191. ourSettings->setValue("qwFloodProtTime", qwFloodProtTime());
  192. ourSettings->setValue("spamFloodProtTime", spamFloodProtTime());
  193. ourSettings->setValue("timeToSayHiAfterConnected", timeToSayHiAfterConnected());
  194. ourSettings->setValue("timeToWaitForCountReply", timeToWaitForCountReply());
  195. ourSettings->setValue("developerMode", developerMode());
  196. ourSettings->setValue("sshHostName", sshHostName());
  197. ourSettings->setValue("sshUserName", sshUserName());
  198. ourSettings->setValue("sshPort", sshPort());
  199. ourSettings->setValue("refreshHostNamesHour", refreshHostNamesHour());
  200. ourSettings->setValue("maxServers", maxServers());
  201. ServerList list = serverList();
  202. setServerList(list);
  203. }