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