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