ircconfig.cpp
1 //------------------------------------------------------------------------------
2 // ircconfig.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircconfig.h"
24 
25 #include "irc/configuration/chatnetworkscfg.h"
26 #include "ini/settingsproviderqt.h"
27 #include "plugins/engineplugin.h"
28 #include "plugins/pluginloader.h"
29 #include "datapaths.h"
30 #include "log.h"
31 #include "version.h"
32 
33 #include <QMessageBox>
34 
35 IRCConfig* IRCConfig::instance = NULL;
36 
37 IRCConfig::IRCConfig()
38 {
39 }
40 
41 IRCConfig::~IRCConfig()
42 {
43 }
44 
46 {
47  if (instance == NULL)
48  {
49  instance = new IRCConfig();
50  }
51 
52  return *instance;
53 }
54 
56 {
57  if (instance != NULL)
58  {
59  delete instance;
60  instance = NULL;
61  }
62 }
63 
64 void IRCConfig::loadNetworksFromPlugins()
65 {
66  QList<IRCNetworkEntity> networks = ChatNetworksCfg().networks();
67  bool shouldSave = false;
68  // Go through the plugins and register their IRC servers.
69  for(unsigned int i = 0;i < gPlugins->numPlugins();i++)
70  {
71  if(gPlugins->plugin(i)->info()->data()->ircChannels.size() == 0)
72  continue;
73 
74  // OK so maybe registering only on first run is a good idea after all...
75  IniVariable registered = gPlugins->plugin(i)->info()->data()->pConfig->createSetting("IRCRegistered", false);
76  if(!registered)
77  {
78  shouldSave = true;
79  registered = true;
80 
81  foreach(const IRCNetworkEntity &entity, gPlugins->plugin(i)->info()->data()->ircChannels)
82  {
83  // If we have a unique server add it to the list...
84  if(!networks.contains(entity))
85  networks << entity;
86  else
87  {
88  // otherwise add the channel to the auto join.
89  IRCNetworkEntity &existingEntity = networks[networks.indexOf(entity)];
90  if(existingEntity.autojoinChannels().contains(entity.autojoinChannels()[0]))
91  continue;
92  if(existingEntity.isAutojoinNetwork())
93  {
94  // If we have this set to auto join ask first.
95  if(QMessageBox::question(NULL, QObject::tr("Add plugin's IRC channel?"),
96  QObject::tr("Would you like the %1 plugin to add its channel to %2's auto join?")
97  .arg(entity.description()).arg(existingEntity.description()),
98  QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
99  continue;
100  }
101 
102  existingEntity.autojoinChannels() << entity.autojoinChannels()[0];
103  }
104  }
105  }
106  }
107  if (shouldSave)
108  {
109  ChatNetworksCfg().setNetworks(networks);
110  }
111 }
112 
114 {
115  if (pIni == NULL)
116  {
117  return false;
118  }
119 
120  IniSection section;
121 
122  section = pIni->section(AppearanceCfg::SECTION_NAME);
123  appearance.load(section);
124 
125  section = pIni->section(GeneralCfg::SECTION_NAME);
126  general.load(section);
127 
128  section = pIni->section(PersonalCfg::SECTION_NAME);
129  personal.load(section);
130 
131  section = pIni->section(SoundsCfg::SECTION_NAME);
132  sounds.load(section);
133 
134  loadNetworksFromPlugins();
135 
136  return true;
137 }
138 
140 {
141  if (pIni == NULL)
142  {
143  return false;
144  }
145 
146  IniSection section;
147 
148  section = pIni->section(AppearanceCfg::SECTION_NAME);
149  appearance.save(section);
150 
151  section = pIni->section(GeneralCfg::SECTION_NAME);
152  general.save(section);
153 
154  section = pIni->section(PersonalCfg::SECTION_NAME);
155  personal.save(section);
156 
157  section = pIni->section(SoundsCfg::SECTION_NAME);
158  sounds.save(section);
159 
160  if (settings->isWritable())
161  {
162  settings->sync();
163  return true;
164  }
165  return false;
166 }
167 
168 bool IRCConfig::setIniFile(const QString& filePath)
169 {
170  pIni.reset();
171  settingsProvider.reset();
172  settings.reset();
173 
174  gLog << QObject::tr("Setting IRC INI file: %1").arg(filePath);
175  settings.reset(new QSettings(filePath, QSettings::IniFormat));
176  settingsProvider.reset(new SettingsProviderQt(settings.data()));
177  pIni.reset(new Ini(settingsProvider.data()));
178 
179  IniSection section;
180 
181  section = this->pIni->section(AppearanceCfg::SECTION_NAME);
182  appearance.init(section);
183 
184  return true;
185 }
186 
188 const QString IRCConfig::AppearanceCfg::SECTION_NAME = "Appearance";
189 
190 IRCConfig::AppearanceCfg::AppearanceCfg()
191 {
192  this->backgroundColor = "#000000";
193  this->channelActionColor = "#008000";
194  this->ctcpColor = "#de5aff";
195  this->defaultTextColor = "#b9b9b9";
196  this->errorColor = "#ff0000";
197  this->mainFont = QFont("Courier");
198  this->networkActionColor = "#079CFF";
199  this->timestamps = true;
200  this->userListFont = QFont("Courier");
201  this->userListSelectedTextColor = "#cbcb0f";
202  this->userListSelectedBackgroundColor = "#B74600";
203  this->urlColor = "#00F6FF";
204  windowAlertOnImportantChatEvent = true;
205 }
206 
207 void IRCConfig::AppearanceCfg::init(IniSection& section)
208 {
209  section.createSetting("BackgroundColor", this->backgroundColor);
210  section.createSetting("ChannelActionColor", this->channelActionColor);
211  section.createSetting("CtcpColor", this->ctcpColor);
212  section.createSetting("DefaultTextColor", this->defaultTextColor);
213  section.createSetting("ErrorColor", this->errorColor);
214  section.createSetting("MainFont", this->mainFont.toString());
215  section.createSetting("NetworkActionColor", this->networkActionColor);
216  section.createSetting("TimeStamps", this->timestamps);
217  section.createSetting("UserListFont", this->userListFont.toString());
218  section.createSetting("UserListSelectedTextColor", this->userListSelectedTextColor);
219  section.createSetting("UserListSelectedBackgroundColor", this->userListSelectedBackgroundColor);
220  section.createSetting("UrlColor", this->urlColor);
221 }
222 
223 void IRCConfig::AppearanceCfg::load(IniSection& section)
224 {
225  this->backgroundColor = (const QString &)section["BackgroundColor"];
226  this->channelActionColor = (const QString &)section["ChannelActionColor"];
227  this->ctcpColor = (const QString &)section["CtcpColor"];
228  this->defaultTextColor = (const QString &)section["DefaultTextColor"];
229  this->errorColor = (const QString &)section["ErrorColor"];
230  this->mainFont.fromString(section["MainFont"]);
231  this->networkActionColor = (const QString &)section["NetworkActionColor"];
232  this->timestamps = section["TimeStamps"];
233  this->userListFont.fromString(section["UserListFont"]);
234  this->urlColor = (const QString &)section["UrlColor"];
235  this->userListSelectedTextColor = (const QString &)section["UserListSelectedTextColor"];
236  this->userListSelectedBackgroundColor = (const QString &)section["UserListSelectedBackgroundColor"];
237  windowAlertOnImportantChatEvent = section.value("WindowAlertOnImportantChatEvent", true).toBool();
238 }
239 
240 void IRCConfig::AppearanceCfg::save(IniSection& section)
241 {
242  section["BackgroundColor"] = this->backgroundColor;
243  section["ChannelActionColor"] = this->channelActionColor;
244  section["CtcpColor"] = this->ctcpColor;
245  section["DefaultTextColor"] = this->defaultTextColor;
246  section["ErrorColor"] = this->errorColor;
247  section["MainFont"] = this->mainFont.toString();
248  section["NetworkActionColor"] = this->networkActionColor;
249  section["TimeStamps"] = this->timestamps;
250  section["UserListFont"] = this->userListFont.toString();
251  section["UserListSelectedTextColor"] = this->userListSelectedTextColor;
252  section["UserListSelectedBackgroundColor"] = this->userListSelectedBackgroundColor;
253  section["UrlColor"] = this->urlColor;
254  section.setValue("WindowAlertOnImportantChatEvent", windowAlertOnImportantChatEvent);
255 }
256 
258 const QString IRCConfig::GeneralCfg::SECTION_NAME = "General";
259 
260 IRCConfig::GeneralCfg::GeneralCfg()
261 {
262 
263 }
264 
265 void IRCConfig::GeneralCfg::load(IniSection& section)
266 {
267 }
268 
269 void IRCConfig::GeneralCfg::save(IniSection& section)
270 {
271 
272 }
274 const QString IRCConfig::PersonalCfg::SECTION_NAME = "Personal";
275 
276 IRCConfig::PersonalCfg::PersonalCfg()
277 {
278 }
279 
280 void IRCConfig::PersonalCfg::load(IniSection& section)
281 {
282  this->alternativeNickname = (const QString &)section["AlternativeNickname"];
283  this->fullName = (const QString &)section["FullName"];
284  this->nickname = (const QString &)section["Nickname"];
285  this->quitMessage = (const QString &)section.value("QuitMessage", "Doomseeker End of Line").toString();
286 }
287 
288 void IRCConfig::PersonalCfg::save(IniSection& section)
289 {
290  section["AlternativeNickname"] = this->alternativeNickname;
291  section["FullName"] = this->fullName;
292  section["Nickname"] = this->nickname;
293  section["QuitMessage"] = this->quitMessage;
294 }
296 const QString IRCConfig::SoundsCfg::SECTION_NAME = "Sounds";
297 
298 IRCConfig::SoundsCfg::SoundsCfg()
299 {
300  this->bUseNicknameUsedSound = false;
301  this->bUsePrivateMessageReceivedSound = false;
302 }
303 
304 void IRCConfig::SoundsCfg::load(IniSection& section)
305 {
306  this->bUseNicknameUsedSound = section["bUseNicknameUsedSound"];
307  this->bUsePrivateMessageReceivedSound = section["bUsePrivateMessageReceivedSound"];
308  this->nicknameUsedSound = (const QString&)section["NicknameUsedSound"];
309  this->privateMessageReceivedSound = (const QString&)section["PrivateMessageReceivedSound"];
310 }
311 
312 void IRCConfig::SoundsCfg::save(IniSection& section)
313 {
314  section["bUseNicknameUsedSound"] = this->bUseNicknameUsedSound;
315  section["bUsePrivateMessageReceivedSound"] = this->bUsePrivateMessageReceivedSound;
316  section["NicknameUsedSound"] = this->nicknameUsedSound;
317  section["PrivateMessageReceivedSound"] = this->privateMessageReceivedSound;
318 }
IniVariable createSetting(const QString &name, const QVariant &data)
Inits specified variable with specified data.
Definition: inisection.cpp:57
static IRCConfig & config()
Returns the Singleton.
Definition: ircconfig.cpp:45
INI variable representation.
Definition: inivariable.h:41
const QStringList & autojoinChannels() const
List of channels to which a /join command will be issued automatically when a connection with this ne...
This Singleton holds most of Doomseeker IRC configuration in memory.
Definition: ircconfig.h:44
const QString & description() const
A short, human-readable description for the network. (Preferably a single word).
bool isAutojoinNetwork() const
Join this network when Doomseeker starts up.
QVariant value(const QString &key) const
Retrieves a variable directly; omits the IniVariable system.
Definition: inisection.cpp:164
bool saveToFile()
Saves current settings to ini file. This file must be previously set by setIniFile() method...
Definition: ircconfig.cpp:139
Configuration handler.
Definition: ini.h:69
bool readFromFile()
Reads settings from ini file. This file must be previously set by setIniFile() method.
Definition: ircconfig.cpp:113
static void dispose()
Disposes of the Singleton.
Definition: ircconfig.cpp:55
void setValue(const QString &key, const QVariant &value)
Sets a variable directly. Omits the IniVariable system.
Definition: inisection.cpp:154
Data structure that describes and defines a connection to an IRC network or server.
INI section representation.
Definition: inisection.h:40
bool setIniFile(const QString &filePath)
Initializes the Ini class instance to point to a file.
Definition: ircconfig.cpp:168