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