chatnetworkscfg.cpp
1 //------------------------------------------------------------------------------
2 // chatnetworkscfg.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "chatnetworkscfg.h"
24 
25 #include "irc/chatlogs.h"
26 #include "irc/configuration/ircconfig.h"
27 #include "ini/inisection.h"
28 #include "patternlist.h"
29 #include <QString>
30 
31 const QString ChatNetworksCfg::SECTIONS_NAMES_PREFIX = "Network.";
32 
33 
34 QList<IRCNetworkEntity> ChatNetworksCfg::autoJoinNetworks() const
35 {
36  QList<IRCNetworkEntity> result;
37  foreach (const IRCNetworkEntity& network, networks())
38  {
39  if (network.isAutojoinNetwork())
40  {
41  result << network;
42  }
43  }
44 
45  return result;
46 }
47 
48 bool ChatNetworksCfg::isAnyNetworkOnAutoJoin() const
49 {
50  foreach (const IRCNetworkEntity& network, networks())
51  {
52  if (network.isAutojoinNetwork())
53  {
54  return true;
55  }
56  }
57 
58  return false;
59 }
60 
61 IRCNetworkEntity ChatNetworksCfg::lastUsedNetwork() const
62 {
63  QString networkName = ini().section("LastUsedNetwork").value("Description").toString();
64  foreach (const IRCNetworkEntity &network, networks())
65  {
66  if (network.description() == networkName)
67  {
68  return network;
69  }
70  }
71  return IRCNetworkEntity();
72 }
73 
74 void ChatNetworksCfg::setLastUsedNetwork(const IRCNetworkEntity &network)
75 {
76  // LastUsedNetwork section had more data in the past. To prevent
77  // obscuring of the .ini file with this old data, we'll delete the
78  // section and promptly recreate it.
79  ini().deleteSection("LastUsedNetwork");
80  ini().section("LastUsedNetwork").setValue("Description", network.description());
81 }
82 
83 QList<IRCNetworkEntity> ChatNetworksCfg::networks() const
84 {
85  QList<IRCNetworkEntity> result;
86  foreach (const IniSection &section, allNetworksSections())
87  {
88  result << loadNetwork(section);
89  }
90  return result;
91 }
92 
93 void ChatNetworksCfg::setNetworks(const QList<IRCNetworkEntity> &networks)
94 {
95  // Erase all previously stored networks.
96  // We need to rebuild these sections from scratch.
97  clearNetworkSections();
98  int id = 0;
99  foreach (const IRCNetworkEntity &network, networks)
100  {
101  saveNetwork(networkSection(id), network);
102  ++id;
103  }
104 }
105 
106 IRCNetworkEntity ChatNetworksCfg::network(const QString &description)
107 {
108  foreach (const IRCNetworkEntity &network, networks())
109  {
110  if (network.description() == description)
111  {
112  return network;
113  }
114  }
115  return IRCNetworkEntity();
116 }
117 
118 bool ChatNetworksCfg::replaceNetwork(const QString &oldDescription, const IRCNetworkEntity &newNetwork, QWidget *errorDisplayParentWidget)
119 {
120  if (!ChatLogs().renameNetwork(errorDisplayParentWidget, oldDescription, newNetwork.description()))
121  {
122  return false;
123  }
124  QList<IRCNetworkEntity> networks = this->networks();
125  QMutableListIterator<IRCNetworkEntity> it(networks);
126  while (it.hasNext())
127  {
128  IRCNetworkEntity &network = it.next();
129  if (network.description() == oldDescription)
130  {
131  network = newNetwork;
132  break;
133  }
134  }
135  setNetworks(networks);
136  return true;
137 }
138 
139 void ChatNetworksCfg::clearNetworkSections()
140 {
141  foreach (const IniSection& section, allNetworksSections())
142  {
143  ini().deleteSection(section.name());
144  }
145 }
146 
147 IRCNetworkEntity ChatNetworksCfg::loadNetwork(const IniSection& section) const
148 {
149  IRCNetworkEntity network;
150  network.setAddress(section["Address"]);
151  network.setAutojoinNetwork(section["bAutojoinNetwork"]);
152  network.setAutojoinChannels(static_cast<QString>(section["AutojoinChannels"])
153  .split(" ", QString::SkipEmptyParts));
154  network.setAutojoinCommands(section.value("AutojoinCommands").toStringList());
155  network.setDescription(section["Description"]);
156  network.setIgnoredUsers(PatternList::deserializeQVariant(section.value("IgnoredUsers")));
157  network.setNickservCommand(section["NickservCommand"]);
158  network.setNickservPassword(section["NickservPassword"]);
159  network.setPassword(section["Password"]);
160  network.setPort(section["Port"]);
161  return network;
162 }
163 
164 void ChatNetworksCfg::saveNetwork(IniSection section, const IRCNetworkEntity& network)
165 {
166  section["Address"] = network.address();
167  section["bAutojoinNetwork"] = network.isAutojoinNetwork();
168  section["AutojoinChannels"] = network.autojoinChannels().join(" ");
169  section["AutojoinCommands"].setValue(network.autojoinCommands());
170  section["Description"] = network.description();
171  section.setValue("IgnoredUsers", network.ignoredUsers().serializeQVariant());
172  section["NickservCommand"] = network.nickservCommand();
173  section["NickservPassword"] = network.nickservPassword();
174  section["Password"] = network.password();
175  section["Port"] = network.port();
176 }
177 
178 QVector<IniSection> ChatNetworksCfg::allNetworksSections() const
179 {
180  return ini().sectionsArray("^" + SECTIONS_NAMES_PREFIX);
181 }
182 
183 IniSection ChatNetworksCfg::networkSection(int id)
184 {
185  return ini().section(SECTIONS_NAMES_PREFIX + id);
186 }
187 
188 Ini& ChatNetworksCfg::ini() const
189 {
190  return *gIRCConfig.ini();
191 }
unsigned short port() const
Port of the server or network to connect to.
const QString & name() const
A name (or path) of this section with lettercase preserved.
Definition: inisection.cpp:95
const QStringList & autojoinChannels() const
List of channels to which a /join command will be issued automatically when a connection with this ne...
QVector< IniSection > sectionsArray(const QString &regexPattern)
Definition: ini.cpp:101
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 replaceNetwork(const QString &oldDescription, const IRCNetworkEntity &newNetwork, QWidget *errorDisplayParentWidget)
Replace network definition in config and move around log files.
Configuration handler.
Definition: ini.h:69
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
const QStringList & autojoinCommands() const
List of commands executed on network join.
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91
void deleteSection(const QString &sectionname)
Definition: ini.cpp:60
const QString & password() const
Password for the server or network. Ignored if empty.
const QString & address() const
Address of the server or network to connect to.