ircnetworkselectionbox.cpp
1 //------------------------------------------------------------------------------
2 // ircnetworkselectionbox.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 "gui/configuration/irc/cfgircdefinenetworkdialog.h"
24 #include "irc/configuration/chatnetworkscfg.h"
25 #include "irc/configuration/ircconfig.h"
26 #include "irc/ircnetworkconnectioninfo.h"
27 #include "ircnetworkselectionbox.h"
28 #include "qtmetapointer.h"
29 #include "ui_ircnetworkselectionbox.h"
30 #include <QMessageBox>
31 
32 DClass<IRCNetworkSelectionBox> : public Ui::IRCNetworkSelectionBox
33 {
34 };
35 
36 DPointered(IRCNetworkSelectionBox)
37 
39  : QDialog(parent)
40 {
41  d->setupUi(this);
42 
43  connect(d->cboNetwork, SIGNAL(currentIndexChanged(int)), SLOT(networkChanged(int)));
44 
45  initWidgets();
46 }
47 
48 IRCNetworkSelectionBox::~IRCNetworkSelectionBox()
49 {
50 }
51 
52 void IRCNetworkSelectionBox::accept()
53 {
54  if (validate())
55  QDialog::accept();
56 }
57 
58 void IRCNetworkSelectionBox::addNetworkToComboBox(const IRCNetworkEntity &network)
59 {
60  d->cboNetwork->addItem(buildTitle(network), network.serializeQVariant());
61 }
62 
63 QString IRCNetworkSelectionBox::buildTitle(const IRCNetworkEntity &network) const
64 {
65  return QString("%1 [%2:%3]").arg(network.description()).arg(network.address()).arg(network.port());
66 }
67 
68 void IRCNetworkSelectionBox::createNewNetwork()
69 {
70  CFGIRCDefineNetworkDialog dialog(this);
71  if (dialog.exec() == QDialog::Accepted)
72  {
73  ChatNetworksCfg cfg;
74  QList<IRCNetworkEntity> networks = cfg.networks();
75  networks << dialog.getNetworkEntity();
76  cfg.setNetworks(networks);
77 
78  fetchNetworks();
79  }
80 }
81 
82 void IRCNetworkSelectionBox::editCurrentNetwork()
83 {
84  IRCNetworkEntity network = networkCurrent();
85  if (!network.isValid())
86  {
87  QMessageBox::critical(this, tr("Doomseeker - edit IRC network"),
88  tr("Cannot edit as no valid network is selected."));
89  return;
90  }
91  CFGIRCDefineNetworkDialog dialog(network, this);
92  if (dialog.exec() == QDialog::Accepted)
93  {
94  IRCNetworkEntity editedNetwork = dialog.getNetworkEntity();
95  if (replaceNetworkInConfig(network, editedNetwork))
96  updateCurrentNetwork(editedNetwork);
97  }
98 }
99 
100 void IRCNetworkSelectionBox::fetchNetworks()
101 {
102  ChatNetworksCfg cfg;
103  QList<IRCNetworkEntity> networks = cfg.networks();
104  std::sort(networks.begin(), networks.end());
105  d->cboNetwork->blockSignals(true);
106  d->cboNetwork->clear();
107 
108  for (const IRCNetworkEntity &network : networks)
109  {
110  addNetworkToComboBox(network);
111  }
112 
113  IRCNetworkEntity lastUsedNetwork = cfg.lastUsedNetwork();
114  if (lastUsedNetwork.isValid())
115  setNetworkMatchingDescriptionAsCurrent(lastUsedNetwork.description());
116 
117  updateNetworkInfo();
118  d->cboNetwork->blockSignals(false);
119 }
120 
121 void IRCNetworkSelectionBox::initWidgets()
122 {
123  d->leAlternateNick->setText(gIRCConfig.personal.alternativeNickname);
124  d->leNick->setText(gIRCConfig.personal.nickname);
125  d->leRealName->setText(gIRCConfig.personal.fullName);
126  d->leUserName->setText(gIRCConfig.personal.userName);
127 
128  fetchNetworks();
129 }
130 
132 {
133  IRCNetworkEntity networkEntity = networkCurrent();
134  networkEntity.setPassword(d->lePassword->text());
135  return networkEntity;
136 }
137 
138 void IRCNetworkSelectionBox::networkChanged(int index)
139 {
140  if (index >= 0)
141  updateNetworkInfo();
142 }
143 
144 IRCNetworkEntity IRCNetworkSelectionBox::networkCurrent() const
145 {
146  return networkAtRow(d->cboNetwork->currentIndex());
147 }
148 
149 IRCNetworkEntity IRCNetworkSelectionBox::networkAtRow(int row) const
150 {
151  if (row < 0 || row >= d->cboNetwork->count())
152  return IRCNetworkEntity();
153  return IRCNetworkEntity::deserializeQVariant(d->cboNetwork->itemData(row));
154 }
155 
156 IRCNetworkConnectionInfo IRCNetworkSelectionBox::networkConnectionInfo() const
157 {
158  IRCNetworkConnectionInfo outInfo;
159 
160  outInfo.alternateNick = d->leAlternateNick->text();
161  outInfo.nick = d->leNick->text();
162  outInfo.realName = d->leRealName->text();
163  outInfo.userName = d->leUserName->text();
164 
165  outInfo.networkEntity = this->network();
166 
167  return outInfo;
168 }
169 
170 void IRCNetworkSelectionBox::setNetworkMatchingDescriptionAsCurrent(const QString &description)
171 {
172  for (int row = 0; row < d->cboNetwork->count(); ++row)
173  {
174  IRCNetworkEntity candidate = networkAtRow(row);
175  if (candidate.description() == description)
176  {
177  d->cboNetwork->setCurrentIndex(row);
178  break;
179  }
180  }
181 }
182 
183 void IRCNetworkSelectionBox::updateCurrentNetwork(const IRCNetworkEntity &network)
184 {
185  d->cboNetwork->setItemText(d->cboNetwork->currentIndex(), buildTitle(network));
186  d->cboNetwork->setItemData(d->cboNetwork->currentIndex(), network.serializeQVariant());
187  updateNetworkInfo();
188 }
189 
190 void IRCNetworkSelectionBox::removeCurrentNetwork()
191 {
192  IRCNetworkEntity network = networkCurrent();
193  if (!network.isValid())
194  {
195  QMessageBox::critical(this, tr("Doomseeker - remove IRC network"),
196  tr("Cannot remove as no valid network is selected."));
197  return;
198  }
199  if (QMessageBox::question(this, tr("Doomseeker - remove IRC network"),
200  tr("Are you sure you wish to remove network '%1'?").arg(network.description()),
201  QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
202  {
203  ChatNetworksCfg cfg;
204  cfg.removeNetwork(network);
205  fetchNetworks();
206  }
207 }
208 
209 bool IRCNetworkSelectionBox::replaceNetworkInConfig(const IRCNetworkEntity &oldNetwork, const IRCNetworkEntity &newNetwork)
210 {
211  ChatNetworksCfg cfg;
212  return cfg.replaceNetwork(oldNetwork.description(), newNetwork, this);
213 }
214 
215 void IRCNetworkSelectionBox::updateNetworkInfo()
216 {
217  IRCNetworkEntity network = networkCurrent();
218 
219  d->leServerAddress->setText(network.address());
220  d->spinPort->setValue(network.port());
221  d->lePassword->setText(network.password());
222 }
223 
224 bool IRCNetworkSelectionBox::validate()
225 {
226  const static QString ERROR_TITLE = tr("IRC connection error");
227  IRCNetworkConnectionInfo connectionInfo = this->networkConnectionInfo();
228 
229  if (connectionInfo.nick.isEmpty())
230  {
231  QMessageBox::warning(nullptr, ERROR_TITLE, tr("You must specify a nick."));
232  return false;
233  }
234 
235  if (connectionInfo.networkEntity.address().isEmpty())
236  {
237  QMessageBox::warning(nullptr, ERROR_TITLE, tr("You must specify a network address."));
238  return false;
239  }
240 
241  return true;
242 }