ircconfig.cpp
1 //------------------------------------------------------------------------------
2 // ircconfig.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 "datapaths.h"
26 #include "ini/settingsproviderqt.h"
27 #include "irc/configuration/chatnetworkscfg.h"
28 #include "log.h"
29 #include "plugins/engineplugin.h"
30 #include "plugins/pluginloader.h"
31 #include "version.h"
32 
33 #include <QMessageBox>
34 
35 IRCConfig *IRCConfig::instance = nullptr;
36 
37 IRCConfig::IRCConfig()
38 {
39 }
40 
41 IRCConfig::~IRCConfig()
42 {
43 }
44 
46 {
47  if (instance == nullptr)
48  instance = new IRCConfig();
49 
50  return *instance;
51 }
52 
54 {
55  if (instance != nullptr)
56  {
57  delete instance;
58  instance = nullptr;
59  }
60 }
61 
62 void IRCConfig::loadNetworksFromPlugins()
63 {
64  QList<IRCNetworkEntity> networks = ChatNetworksCfg().networks();
65  bool shouldSave = false;
66  // Go through the plugins and register their IRC servers.
67  for (unsigned int i = 0; i < gPlugins->numPlugins(); i++)
68  {
69  if (gPlugins->plugin(i)->info()->data()->ircChannels.size() == 0)
70  continue;
71 
72  // OK so maybe registering only on first run is a good idea after all...
73  IniVariable registered = gPlugins->plugin(i)->info()->data()->pConfig->createSetting("IRCRegistered", false);
74  if (!registered)
75  {
76  shouldSave = true;
77  registered = true;
78 
79  for (const IRCNetworkEntity &entity : gPlugins->plugin(i)->info()->data()->ircChannels)
80  {
81  // If we have a unique server add it to the list...
82  if (!networks.contains(entity))
83  networks << entity;
84  else
85  {
86  // otherwise add the channel to the auto join.
87  IRCNetworkEntity &existingEntity = networks[networks.indexOf(entity)];
88  if (existingEntity.autojoinChannels().contains(entity.autojoinChannels()[0]))
89  continue;
90  if (existingEntity.isAutojoinNetwork())
91  {
92  // If we have this set to auto join ask first.
93  if (QMessageBox::question(nullptr, QObject::tr("Add plugin's IRC channel?"),
94  QObject::tr("Would you like the %1 plugin to add its channel to %2's auto join?")
95  .arg(entity.description()).arg(existingEntity.description()),
96  QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
97  continue;
98  }
99 
100  existingEntity.autojoinChannels() << entity.autojoinChannels()[0];
101  }
102  }
103  }
104  }
105  if (shouldSave)
106  ChatNetworksCfg().setNetworks(networks);
107 }
108 
110 {
111  if (pIni == nullptr)
112  return false;
113 
114  IniSection section;
115 
116  section = pIni->section(AppearanceCfg::SECTION_NAME);
117  appearance.load(section);
118 
119  section = pIni->section(GeneralCfg::SECTION_NAME);
120  general.load(section);
121 
122  section = pIni->section(PersonalCfg::SECTION_NAME);
123  personal.load(section);
124 
125  section = pIni->section(SoundsCfg::SECTION_NAME);
126  sounds.load(section);
127 
128  loadNetworksFromPlugins();
129 
130  return true;
131 }
132 
134 {
135  if (pIni == nullptr)
136  return false;
137 
138  IniSection section;
139 
140  section = pIni->section(AppearanceCfg::SECTION_NAME);
141  appearance.save(section);
142 
143  section = pIni->section(GeneralCfg::SECTION_NAME);
144  general.save(section);
145 
146  section = pIni->section(PersonalCfg::SECTION_NAME);
147  personal.save(section);
148 
149  section = pIni->section(SoundsCfg::SECTION_NAME);
150  sounds.save(section);
151 
152  if (settings->isWritable())
153  {
154  settings->sync();
155  return true;
156  }
157  return false;
158 }
159 
160 bool IRCConfig::setIniFile(const QString &filePath)
161 {
162  pIni.reset();
163  settingsProvider.reset();
164  settings.reset();
165 
166  gLog << QObject::tr("Setting IRC INI file: %1").arg(filePath);
167  settings.reset(new QSettings(filePath, QSettings::IniFormat));
168  settingsProvider.reset(new SettingsProviderQt(settings.data()));
169  pIni.reset(new Ini(settingsProvider.data()));
170 
171  IniSection section;
172 
173  section = this->pIni->section(AppearanceCfg::SECTION_NAME);
174  appearance.init(section);
175 
176  return true;
177 }
178 
180 const QString IRCConfig::AppearanceCfg::SECTION_NAME = "Appearance";
181 
182 IRCConfig::AppearanceCfg::AppearanceCfg()
183 {
184  this->backgroundColor = "#000000";
185  this->channelActionColor = "#008000";
186  this->ctcpColor = "#de5aff";
187  this->defaultTextColor = "#b9b9b9";
188  this->errorColor = "#ff0000";
189  this->mainFont = QFont("Courier");
190  this->networkActionColor = "#079CFF";
191  this->timestamps = true;
192  this->userListFont = QFont("Courier");
193  this->userListSelectedTextColor = "#cbcb0f";
194  this->userListSelectedBackgroundColor = "#B74600";
195  this->urlColor = "#00F6FF";
196  windowAlertOnImportantChatEvent = true;
197 }
198 
199 void IRCConfig::AppearanceCfg::init(IniSection &section)
200 {
201  section.createSetting("BackgroundColor", this->backgroundColor);
202  section.createSetting("ChannelActionColor", this->channelActionColor);
203  section.createSetting("CtcpColor", this->ctcpColor);
204  section.createSetting("DefaultTextColor", this->defaultTextColor);
205  section.createSetting("ErrorColor", this->errorColor);
206  section.createSetting("MainFont", this->mainFont.toString());
207  section.createSetting("NetworkActionColor", this->networkActionColor);
208  section.createSetting("TimeStamps", this->timestamps);
209  section.createSetting("UserListFont", this->userListFont.toString());
210  section.createSetting("UserListSelectedTextColor", this->userListSelectedTextColor);
211  section.createSetting("UserListSelectedBackgroundColor", this->userListSelectedBackgroundColor);
212  section.createSetting("UrlColor", this->urlColor);
213 }
214 
215 void IRCConfig::AppearanceCfg::load(IniSection &section)
216 {
217  this->backgroundColor = (const QString &)section["BackgroundColor"];
218  this->channelActionColor = (const QString &)section["ChannelActionColor"];
219  this->ctcpColor = (const QString &)section["CtcpColor"];
220  this->defaultTextColor = (const QString &)section["DefaultTextColor"];
221  this->errorColor = (const QString &)section["ErrorColor"];
222  this->mainFont.fromString(section["MainFont"]);
223  this->networkActionColor = (const QString &)section["NetworkActionColor"];
224  this->timestamps = section["TimeStamps"];
225  this->userListFont.fromString(section["UserListFont"]);
226  this->urlColor = (const QString &)section["UrlColor"];
227  this->userListSelectedTextColor = (const QString &)section["UserListSelectedTextColor"];
228  this->userListSelectedBackgroundColor = (const QString &)section["UserListSelectedBackgroundColor"];
229  windowAlertOnImportantChatEvent = section.value("WindowAlertOnImportantChatEvent", true).toBool();
230 }
231 
232 void IRCConfig::AppearanceCfg::save(IniSection &section)
233 {
234  section["BackgroundColor"] = this->backgroundColor;
235  section["ChannelActionColor"] = this->channelActionColor;
236  section["CtcpColor"] = this->ctcpColor;
237  section["DefaultTextColor"] = this->defaultTextColor;
238  section["ErrorColor"] = this->errorColor;
239  section["MainFont"] = this->mainFont.toString();
240  section["NetworkActionColor"] = this->networkActionColor;
241  section["TimeStamps"] = this->timestamps;
242  section["UserListFont"] = this->userListFont.toString();
243  section["UserListSelectedTextColor"] = this->userListSelectedTextColor;
244  section["UserListSelectedBackgroundColor"] = this->userListSelectedBackgroundColor;
245  section["UrlColor"] = this->urlColor;
246  section.setValue("WindowAlertOnImportantChatEvent", windowAlertOnImportantChatEvent);
247 }
248 
250 const QString IRCConfig::GeneralCfg::SECTION_NAME = "General";
251 
252 IRCConfig::GeneralCfg::GeneralCfg()
253 {
254 }
255 
256 void IRCConfig::GeneralCfg::load(IniSection &section)
257 {
258  Q_UNUSED(section);
259 }
260 
261 void IRCConfig::GeneralCfg::save(IniSection &section)
262 {
263  Q_UNUSED(section);
264 }
266 const QString IRCConfig::PersonalCfg::SECTION_NAME = "Personal";
267 
268 IRCConfig::PersonalCfg::PersonalCfg()
269 {
270 }
271 
272 void IRCConfig::PersonalCfg::load(IniSection &section)
273 {
274  this->alternativeNickname = static_cast<QString>(section["AlternativeNickname"]);
275  this->fullName = static_cast<QString>(section["FullName"]);
276  this->nickname = static_cast<QString>(section["Nickname"]);
277  this->quitMessage = section.value("QuitMessage", "Doomseeker End of Line").toString();
278  this->userName = static_cast<QString>(section["UserName"]);
279 }
280 
281 void IRCConfig::PersonalCfg::save(IniSection &section)
282 {
283  section["AlternativeNickname"] = this->alternativeNickname;
284  section["FullName"] = this->fullName;
285  section["Nickname"] = this->nickname;
286  section["QuitMessage"] = this->quitMessage;
287  section["UserName"] = this->userName;
288 }
290 const QString IRCConfig::SoundsCfg::SECTION_NAME = "Sounds";
291 
292 IRCConfig::SoundsCfg::SoundsCfg()
293 {
294  this->bUseNicknameUsedSound = false;
295  this->bUsePrivateMessageReceivedSound = false;
296 }
297 
298 void IRCConfig::SoundsCfg::load(IniSection &section)
299 {
300  this->bUseNicknameUsedSound = section["bUseNicknameUsedSound"];
301  this->bUsePrivateMessageReceivedSound = section["bUsePrivateMessageReceivedSound"];
302  this->nicknameUsedSound = (const QString &)section["NicknameUsedSound"];
303  this->privateMessageReceivedSound = (const QString &)section["PrivateMessageReceivedSound"];
304 }
305 
306 void IRCConfig::SoundsCfg::save(IniSection &section)
307 {
308  section["bUseNicknameUsedSound"] = this->bUseNicknameUsedSound;
309  section["bUsePrivateMessageReceivedSound"] = this->bUsePrivateMessageReceivedSound;
310  section["NicknameUsedSound"] = this->nicknameUsedSound;
311  section["PrivateMessageReceivedSound"] = this->privateMessageReceivedSound;
312 }