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 "ircnetworkselectionbox.h"
24 #include "ui_ircnetworkselectionbox.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 "qtmetapointer.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  {
56  QDialog::accept();
57  }
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  {
99  updateCurrentNetwork(editedNetwork);
100  }
101  }
102 }
103 
104 void IRCNetworkSelectionBox::fetchNetworks()
105 {
106  ChatNetworksCfg cfg;
107  QList<IRCNetworkEntity> networks = cfg.networks();
108  qSort(networks);
109  d->cboNetwork->blockSignals(true);
110  d->cboNetwork->clear();
111 
112  foreach (const IRCNetworkEntity& network, networks)
113  {
114  addNetworkToComboBox(network);
115  }
116 
117  IRCNetworkEntity lastUsedNetwork = cfg.lastUsedNetwork();
118  if (lastUsedNetwork.isValid())
119  {
120  setNetworkMatchingDescriptionAsCurrent(lastUsedNetwork.description());
121  }
122 
123  updateNetworkInfo();
124  d->cboNetwork->blockSignals(false);
125 }
126 
127 void IRCNetworkSelectionBox::initWidgets()
128 {
129  d->leAlternateNick->setText(gIRCConfig.personal.alternativeNickname);
130  d->leNick->setText(gIRCConfig.personal.nickname);
131  d->leRealName->setText(gIRCConfig.personal.fullName);
132  d->leUserName->setText(gIRCConfig.personal.userName);
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  {
148  updateNetworkInfo();
149  }
150 }
151 
152 IRCNetworkEntity IRCNetworkSelectionBox::networkCurrent() const
153 {
154  return networkAtRow(d->cboNetwork->currentIndex());
155 }
156 
157 IRCNetworkEntity IRCNetworkSelectionBox::networkAtRow(int row) const
158 {
159  if (row < 0 || row >= d->cboNetwork->count())
160  {
161  return IRCNetworkEntity();
162  }
163  return IRCNetworkEntity::deserializeQVariant(d->cboNetwork->itemData(row));
164 }
165 
166 IRCNetworkConnectionInfo IRCNetworkSelectionBox::networkConnectionInfo() const
167 {
168  IRCNetworkConnectionInfo outInfo;
169 
170  outInfo.alternateNick = d->leAlternateNick->text();
171  outInfo.nick = d->leNick->text();
172  outInfo.realName = d->leRealName->text();
173  outInfo.userName = d->leUserName->text();
174 
175  outInfo.networkEntity = this->network();
176 
177  return outInfo;
178 }
179 
180 void IRCNetworkSelectionBox::setNetworkMatchingDescriptionAsCurrent(const QString &description)
181 {
182  for (int row = 0; row < d->cboNetwork->count(); ++row)
183  {
184  IRCNetworkEntity candidate = networkAtRow(row);
185  if (candidate.description() == description)
186  {
187  d->cboNetwork->setCurrentIndex(row);
188  break;
189  }
190  }
191 }
192 
193 void IRCNetworkSelectionBox::updateCurrentNetwork(const IRCNetworkEntity &network)
194 {
195  d->cboNetwork->setItemText(d->cboNetwork->currentIndex(), buildTitle(network));
196  d->cboNetwork->setItemData(d->cboNetwork->currentIndex(), network.serializeQVariant());
197  updateNetworkInfo();
198 }
199 
200 void IRCNetworkSelectionBox::removeCurrentNetwork()
201 {
202  IRCNetworkEntity network = networkCurrent();
203  if (!network.isValid())
204  {
205  QMessageBox::critical(this, tr("Doomseeker - remove IRC network"),
206  tr("Cannot remove as no valid network is selected."));
207  return;
208  }
209  if (QMessageBox::question(this, tr("Doomseeker - remove IRC network"),
210  tr("Are you sure you wish to remove network '%1'?").arg(network.description()),
211  QMessageBox::Yes | QMessageBox::No)
212  == QMessageBox::Yes)
213  {
214  ChatNetworksCfg cfg;
215  cfg.removeNetwork(network);
216  fetchNetworks();
217  }
218 }
219 
220 bool IRCNetworkSelectionBox::replaceNetworkInConfig(const IRCNetworkEntity &oldNetwork, const IRCNetworkEntity &newNetwork)
221 {
222  ChatNetworksCfg cfg;
223  return cfg.replaceNetwork(oldNetwork.description(), newNetwork, this);
224 }
225 
226 void IRCNetworkSelectionBox::updateNetworkInfo()
227 {
228  IRCNetworkEntity network = networkCurrent();
229 
230  d->leServerAddress->setText(network.address());
231  d->spinPort->setValue(network.port());
232  d->lePassword->setText(network.password());
233 }
234 
235 bool IRCNetworkSelectionBox::validate()
236 {
237  const static QString ERROR_TITLE = tr("IRC connection error");
238  IRCNetworkConnectionInfo connectionInfo = this->networkConnectionInfo();
239 
240  if (connectionInfo.nick.isEmpty())
241  {
242  QMessageBox::warning(NULL, ERROR_TITLE, tr("You must specify a nick."));
243  return false;
244  }
245 
246  if (connectionInfo.networkEntity.address().isEmpty())
247  {
248  QMessageBox::warning(NULL, ERROR_TITLE, tr("You must specify a network address."));
249  return false;
250  }
251 
252  return true;
253 }
QString userName
User name sent in /user command.
QString alternateNick
Alternate nickname in case if &#39; nick &#39; is taken when connecting.
unsigned short port() const
Port of the server or network to connect to.
const QString & description() const
A short, human-readable description for the network. (Preferably a single word).
IRCNetworkEntity network() const
Extracts network specified in this dialog.
QString nick
Original nickname. This variable will always store the current nickname of the client.
Struct containing information about client&#39;s connection to the IRC server.
bool replaceNetwork(const QString &oldDescription, const IRCNetworkEntity &newNetwork, QWidget *errorDisplayParentWidget)
Replace network definition in config and move around log files.
QString realName
User&#39;s real name. Optional.
IRCNetworkEntity networkEntity
Information about the network to which we will connect.
Data structure that describes and defines a connection to an IRC network or server.
void removeNetwork(const IRCNetworkEntity &network)
Remove network definition from config.
const QString & password() const
Password for the server or network. Ignored if empty.
const QString & address() const
Address of the server or network to connect to.