ircnetworkselectionbox.cpp
1 //------------------------------------------------------------------------------
2 // ircnetworkselectionbox.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 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->clear();
110 
111  foreach (const IRCNetworkEntity& network, networks)
112  {
113  addNetworkToComboBox(network);
114  }
115 
116  IRCNetworkEntity lastUsedNetwork = cfg.lastUsedNetwork();
117  if (lastUsedNetwork.isValid())
118  {
119  setNetworkMatchingDescriptionAsCurrent(lastUsedNetwork.description());
120  }
121 
122  updateNetworkInfo();
123 }
124 
125 void IRCNetworkSelectionBox::initWidgets()
126 {
127  d->leAlternateNick->setText(gIRCConfig.personal.alternativeNickname);
128  d->leNick->setText(gIRCConfig.personal.nickname);
129  d->leRealName->setText(gIRCConfig.personal.fullName);
130 
131  fetchNetworks();
132 }
133 
135 {
136  IRCNetworkEntity networkEntity = networkCurrent();
137  networkEntity.setPassword(d->lePassword->text());
138  return networkEntity;
139 }
140 
141 void IRCNetworkSelectionBox::networkChanged(int index)
142 {
143  if (index >= 0)
144  {
145  updateNetworkInfo();
146  }
147 }
148 
149 IRCNetworkEntity IRCNetworkSelectionBox::networkCurrent() const
150 {
151  return networkAtRow(d->cboNetwork->currentIndex());
152 }
153 
154 IRCNetworkEntity IRCNetworkSelectionBox::networkAtRow(int row) const
155 {
156  if (row < 0 || row >= d->cboNetwork->count())
157  {
158  return IRCNetworkEntity();
159  }
160  return IRCNetworkEntity::deserializeQVariant(d->cboNetwork->itemData(row));
161 }
162 
163 IRCNetworkConnectionInfo IRCNetworkSelectionBox::networkConnectionInfo() const
164 {
165  IRCNetworkConnectionInfo outInfo;
166 
167  outInfo.alternateNick = d->leAlternateNick->text();
168  outInfo.nick = d->leNick->text();
169  outInfo.realName = d->leRealName->text();
170 
171  outInfo.networkEntity = this->network();
172 
173  return outInfo;
174 }
175 
176 void IRCNetworkSelectionBox::setNetworkMatchingDescriptionAsCurrent(const QString &description)
177 {
178  for (int row = 0; row < d->cboNetwork->count(); ++row)
179  {
180  IRCNetworkEntity candidate = networkAtRow(row);
181  if (candidate.description() == description)
182  {
183  d->cboNetwork->setCurrentIndex(row);
184  break;
185  }
186  }
187 }
188 
189 void IRCNetworkSelectionBox::updateCurrentNetwork(const IRCNetworkEntity &network)
190 {
191  d->cboNetwork->setItemText(d->cboNetwork->currentIndex(), buildTitle(network));
192  d->cboNetwork->setItemData(d->cboNetwork->currentIndex(), network.serializeQVariant());
193  updateNetworkInfo();
194 }
195 
196 bool IRCNetworkSelectionBox::replaceNetworkInConfig(const IRCNetworkEntity &oldNetwork, const IRCNetworkEntity &newNetwork)
197 {
198  ChatNetworksCfg cfg;
199  return cfg.replaceNetwork(oldNetwork.description(), newNetwork, this);
200 }
201 
202 void IRCNetworkSelectionBox::updateNetworkInfo()
203 {
204  IRCNetworkEntity network = networkCurrent();
205 
206  d->leServerAddress->setText(network.address());
207  d->spinPort->setValue(network.port());
208  d->lePassword->setText(network.password());
209 }
210 
211 bool IRCNetworkSelectionBox::validate()
212 {
213  const static QString ERROR_TITLE = tr("IRC connection error");
214  IRCNetworkConnectionInfo connectionInfo = this->networkConnectionInfo();
215 
216  if (connectionInfo.nick.isEmpty())
217  {
218  QMessageBox::warning(NULL, ERROR_TITLE, tr("You must specify a nick."));
219  return false;
220  }
221 
222  if (connectionInfo.networkEntity.address().isEmpty())
223  {
224  QMessageBox::warning(NULL, ERROR_TITLE, tr("You must specify a network address."));
225  return false;
226  }
227 
228  return true;
229 }
unsigned short port() const
Port of the server or network to connect to.
Struct containing information about client&#39;s connection to the IRC server.
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.
IRCNetworkEntity networkEntity
Information about the network to which we will connect.
bool replaceNetwork(const QString &oldDescription, const IRCNetworkEntity &newNetwork, QWidget *errorDisplayParentWidget)
Replace network definition in config and move around log files.
Data structure that describes and defines a connection to an IRC network or server.
QString realName
User&#39;s real name. Optional.
const QString & password() const
Password for the server or network. Ignored if empty.
QString alternateNick
Alternate nickname in case if &#39; nick &#39; is taken when connecting.
const QString & address() const
Address of the server or network to connect to.
QString nick
Original nickname. This variable will always store the current nickname of the client.