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