bcdc6cfd897d42145a4b96fe5c1868ae727c6af3
[quassel.git] / src / qtui / settingspages / networkssettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QHeaderView>
22 #include <QIcon>
23 #include <QMessageBox>
24 #include <QTextCodec>
25
26 #include "networkssettingspage.h"
27
28 #include "client.h"
29 #include "identity.h"
30 #include "network.h"
31 #include "presetnetworks.h"
32 #include "settingspagedlg.h"
33 #include "util.h"
34
35 #include "settingspages/identitiessettingspage.h"
36
37 NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
38     : SettingsPage(tr("IRC"), tr("Networks"), parent)
39 #ifdef HAVE_SSL
40       , _cid(0)
41 #endif
42 {
43     ui.setupUi(this);
44
45     // hide SASL options for older cores
46     if (!(Client::coreFeatures() & Quassel::SaslAuthentication))
47         ui.sasl->hide();
48     if (!(Client::coreFeatures() & Quassel::SaslExternal))
49         ui.saslExtInfo->hide();
50 #ifndef HAVE_SSL
51     ui.saslExtInfo->hide();
52 #endif
53
54     // set up icons
55     ui.renameNetwork->setIcon(QIcon::fromTheme("edit-rename"));
56     ui.addNetwork->setIcon(QIcon::fromTheme("list-add"));
57     ui.deleteNetwork->setIcon(QIcon::fromTheme("edit-delete"));
58     ui.addServer->setIcon(QIcon::fromTheme("list-add"));
59     ui.deleteServer->setIcon(QIcon::fromTheme("edit-delete"));
60     ui.editServer->setIcon(QIcon::fromTheme("configure"));
61     ui.upServer->setIcon(QIcon::fromTheme("go-up"));
62     ui.downServer->setIcon(QIcon::fromTheme("go-down"));
63     ui.editIdentities->setIcon(QIcon::fromTheme("configure"));
64
65     _ignoreWidgetChanges = false;
66
67     connectedIcon = QIcon::fromTheme("network-connect");
68     connectingIcon = QIcon::fromTheme("network-wired"); // FIXME network-connecting
69     disconnectedIcon = QIcon::fromTheme("network-disconnect");
70
71     foreach(int mib, QTextCodec::availableMibs()) {
72         QByteArray codec = QTextCodec::codecForMib(mib)->name();
73         ui.sendEncoding->addItem(codec);
74         ui.recvEncoding->addItem(codec);
75         ui.serverEncoding->addItem(codec);
76     }
77     ui.sendEncoding->model()->sort(0);
78     ui.recvEncoding->model()->sort(0);
79     ui.serverEncoding->model()->sort(0);
80     currentId = 0;
81     setEnabled(Client::isConnected()); // need a core connection!
82     setWidgetStates();
83     connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
84     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
85     connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
86     connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
87     connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
88
89     connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
90     //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
91     connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
92     connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
93     connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
94     connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
95     connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
96     connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
97     connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
98     connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
99     connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
100     connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
101     connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
102     connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
103     connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
104     connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
105     connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
106     connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
107
108     // Core features can change during a reconnect.  Always connect these here, delaying testing for
109     // the core feature flag in load().
110     connect(ui.useCustomMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
111     connect(ui.messageRateBurstSize, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
112     connect(ui.messageRateDelay, SIGNAL(valueChanged(double)), this, SLOT(widgetHasChanged()));
113     connect(ui.unlimitedMessageRate, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
114
115     // Add additional widgets here
116     //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
117     //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
118
119     foreach(IdentityId id, Client::identityIds()) {
120         clientIdentityAdded(id);
121     }
122 }
123
124
125 void NetworksSettingsPage::save()
126 {
127     setEnabled(false);
128     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
129
130     QList<NetworkInfo> toCreate, toUpdate;
131     QList<NetworkId> toRemove;
132     QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
133     while (i != networkInfos.end()) {
134         NetworkId id = (*i).networkId;
135         if (id < 0) {
136             toCreate.append(*i);
137             //if(id == currentId) currentId = 0;
138             //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
139             //if(items.count()) {
140             //  Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
141             //  delete items[0];
142             //}
143             //i = networkInfos.erase(i);
144             ++i;
145         }
146         else {
147             if ((*i) != Client::network((*i).networkId)->networkInfo()) {
148                 toUpdate.append(*i);
149             }
150             ++i;
151         }
152     }
153     foreach(NetworkId id, Client::networkIds()) {
154         if (!networkInfos.contains(id)) toRemove.append(id);
155     }
156     SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
157     int ret = dlg.exec();
158     if (ret == QDialog::Rejected) {
159         // canceled -> reload everything to be safe
160         load();
161     }
162     setChangedState(false);
163     setEnabled(true);
164 }
165
166
167 void NetworksSettingsPage::load()
168 {
169     reset();
170
171     // Handle UI dependent on core feature flags here
172     if (Client::coreFeatures() & Quassel::CustomRateLimits) {
173         // Custom rate limiting supported, allow toggling
174         ui.useCustomMessageRate->setEnabled(true);
175         // Reset tooltip to default.
176         ui.useCustomMessageRate->setToolTip(QString("%1").arg(
177                                           tr("<p>Override default message rate limiting.</p>"
178                                              "<p><b>Setting limits too low may get you disconnected"
179                                              " from the server!</b></p>")));
180         // If changed, update the message below!
181     } else {
182         // Custom rate limiting not supported, disallow toggling
183         ui.useCustomMessageRate->setEnabled(false);
184         // Split up the message to allow re-using translations:
185         // [Original tool-tip]
186         // [Bold 'does not support feature' message]
187         // [Specific version needed and feature details]
188         ui.useCustomMessageRate->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
189                                           tr("<p>Override default message rate limiting.</p>"
190                                              "<p><b>Setting limits too low may get you disconnected"
191                                              " from the server!</b></p>"),
192                                           tr("Your Quassel core does not support this feature"),
193                                           tr("You need a Quassel core v0.13.0 or newer in order to "
194                                           "modify message rate limits.")));
195     }
196
197 #ifdef HAVE_SSL
198     // Hide the SASL EXTERNAL notice until a network's shown.  Stops it from showing while loading
199     // backlog from the core.
200     sslUpdated();
201 #endif
202
203     foreach(NetworkId netid, Client::networkIds()) {
204         clientNetworkAdded(netid);
205     }
206     ui.networkList->setCurrentRow(0);
207
208     setChangedState(false);
209 }
210
211
212 void NetworksSettingsPage::reset()
213 {
214     currentId = 0;
215     ui.networkList->clear();
216     networkInfos.clear();
217 }
218
219
220 bool NetworksSettingsPage::aboutToSave()
221 {
222     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
223     QList<int> errors;
224     foreach(NetworkInfo info, networkInfos.values()) {
225         if (!info.serverList.count()) errors.append(1);
226     }
227     if (!errors.count()) return true;
228     QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
229     if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
230     error += tr("</ul>");
231     QMessageBox::warning(this, tr("Invalid Network Settings"), error);
232     return false;
233 }
234
235
236 void NetworksSettingsPage::widgetHasChanged()
237 {
238     if (_ignoreWidgetChanges) return;
239     bool changed = testHasChanged();
240     if (changed != hasChanged()) setChangedState(changed);
241 }
242
243
244 bool NetworksSettingsPage::testHasChanged()
245 {
246     if (currentId != 0) {
247         saveToNetworkInfo(networkInfos[currentId]);
248     }
249     if (Client::networkIds().count() != networkInfos.count()) return true;
250     foreach(NetworkId id, networkInfos.keys()) {
251         if (id < 0) return true;
252         if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
253     }
254     return false;
255 }
256
257
258 void NetworksSettingsPage::setWidgetStates()
259 {
260     // network list
261     if (ui.networkList->selectedItems().count()) {
262         ui.detailsBox->setEnabled(true);
263         ui.renameNetwork->setEnabled(true);
264         ui.deleteNetwork->setEnabled(true);
265
266         /* button disabled for now
267         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
268         const Network *net = id > 0 ? Client::network(id) : 0;
269         ui.connectNow->setEnabled(net);
270         //    && (Client::network(id)->connectionState() == Network::Initialized
271         //    || Client::network(id)->connectionState() == Network::Disconnected));
272         if(net) {
273           if(net->connectionState() == Network::Disconnected) {
274             ui.connectNow->setIcon(connectedIcon);
275             ui.connectNow->setText(tr("Connect"));
276           } else {
277             ui.connectNow->setIcon(disconnectedIcon);
278             ui.connectNow->setText(tr("Disconnect"));
279           }
280         } else {
281           ui.connectNow->setIcon(QIcon());
282           ui.connectNow->setText(tr("Apply first!"));
283         } */
284     }
285     else {
286         ui.renameNetwork->setEnabled(false);
287         ui.deleteNetwork->setEnabled(false);
288         //ui.connectNow->setEnabled(false);
289         ui.detailsBox->setEnabled(false);
290     }
291     // network details
292     if (ui.serverList->selectedItems().count()) {
293         ui.editServer->setEnabled(true);
294         ui.deleteServer->setEnabled(true);
295         ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
296         ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
297     }
298     else {
299         ui.editServer->setEnabled(false);
300         ui.deleteServer->setEnabled(false);
301         ui.upServer->setEnabled(false);
302         ui.downServer->setEnabled(false);
303     }
304 }
305
306
307 void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
308 {
309     if (!item && !(item = networkItem(id))) return;
310     const Network *net = Client::network(id);
311     if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
312     else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
313     if (net && net->connectionState() == Network::Initialized) {
314         item->setIcon(connectedIcon);
315     }
316     else if (net && net->connectionState() != Network::Disconnected) {
317         item->setIcon(connectingIcon);
318     }
319     else {
320         item->setIcon(disconnectedIcon);
321     }
322     if (net) {
323         bool select = false;
324         // check if we already have another net of this name in the list, and replace it
325         QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
326         if (items.count()) {
327             foreach(QListWidgetItem *i, items) {
328                 NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
329                 if (oldid > 0) continue;  // only locally created nets should be replaced
330                 if (oldid == currentId) {
331                     select = true;
332                     currentId = 0;
333                     ui.networkList->clearSelection();
334                 }
335                 int row = ui.networkList->row(i);
336                 if (row >= 0) {
337                     QListWidgetItem *olditem = ui.networkList->takeItem(row);
338                     Q_ASSERT(olditem);
339                     delete olditem;
340                 }
341                 networkInfos.remove(oldid);
342                 break;
343             }
344         }
345         item->setText(net->networkName());
346         if (select) item->setSelected(true);
347     }
348 }
349
350
351 void NetworksSettingsPage::coreConnectionStateChanged(bool state)
352 {
353     this->setEnabled(state);
354     if (state) {
355         load();
356     }
357     else {
358         // reset
359         //currentId = 0;
360     }
361 }
362
363
364 void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
365 {
366     const Identity *identity = Client::identity(id);
367     connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));
368
369     QString name = identity->identityName();
370     for (int j = 0; j < ui.identityList->count(); j++) {
371         if ((j > 0 || ui.identityList->itemData(0).toInt() != 1) && name.localeAwareCompare(ui.identityList->itemText(j)) < 0) {
372             ui.identityList->insertItem(j, name, id.toInt());
373             widgetHasChanged();
374             return;
375         }
376     }
377     // append
378     ui.identityList->insertItem(ui.identityList->count(), name, id.toInt());
379     widgetHasChanged();
380 }
381
382
383 void NetworksSettingsPage::clientIdentityUpdated()
384 {
385     const Identity *identity = qobject_cast<const Identity *>(sender());
386     if (!identity) {
387         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
388         return;
389     }
390     int row = ui.identityList->findData(identity->id().toInt());
391     if (row < 0) {
392         qWarning() << "NetworksSettingsPage: Invalid identity to update!";
393         return;
394     }
395     if (ui.identityList->itemText(row) != identity->identityName()) {
396         ui.identityList->setItemText(row, identity->identityName());
397     }
398 }
399
400
401 void NetworksSettingsPage::clientIdentityRemoved(IdentityId id)
402 {
403     IdentityId defaultId = defaultIdentity();
404     if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
405     foreach(NetworkInfo info, networkInfos.values()) {
406         if (info.identity == id) {
407             if (info.networkId == currentId)
408                 ui.identityList->setCurrentIndex(0);
409             info.identity = defaultId;
410             networkInfos[info.networkId] = info;
411             if (info.networkId > 0) Client::updateNetwork(info);
412         }
413     }
414     ui.identityList->removeItem(ui.identityList->findData(id.toInt()));
415     widgetHasChanged();
416 }
417
418
419 QListWidgetItem *NetworksSettingsPage::networkItem(NetworkId id) const
420 {
421     for (int i = 0; i < ui.networkList->count(); i++) {
422         QListWidgetItem *item = ui.networkList->item(i);
423         if (item->data(Qt::UserRole).value<NetworkId>() == id) return item;
424     }
425     return 0;
426 }
427
428
429 void NetworksSettingsPage::clientNetworkAdded(NetworkId id)
430 {
431     insertNetwork(id);
432     //connect(Client::network(id), SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
433     connect(Client::network(id), SIGNAL(configChanged()), this, SLOT(clientNetworkUpdated()));
434
435     connect(Client::network(id), SIGNAL(connectionStateSet(Network::ConnectionState)), this, SLOT(networkConnectionStateChanged(Network::ConnectionState)));
436     connect(Client::network(id), SIGNAL(connectionError(const QString &)), this, SLOT(networkConnectionError(const QString &)));
437 }
438
439
440 void NetworksSettingsPage::clientNetworkUpdated()
441 {
442     const Network *net = qobject_cast<const Network *>(sender());
443     if (!net) {
444         qWarning() << "Update request for unknown network received!";
445         return;
446     }
447     networkInfos[net->networkId()] = net->networkInfo();
448     setItemState(net->networkId());
449     if (net->networkId() == currentId) displayNetwork(net->networkId());
450     setWidgetStates();
451     widgetHasChanged();
452 }
453
454
455 void NetworksSettingsPage::clientNetworkRemoved(NetworkId id)
456 {
457     if (!networkInfos.contains(id)) return;
458     if (id == currentId) displayNetwork(0);
459     NetworkInfo info = networkInfos.take(id);
460     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
461     foreach(QListWidgetItem *item, items) {
462         if (item->data(Qt::UserRole).value<NetworkId>() == id)
463             delete ui.networkList->takeItem(ui.networkList->row(item));
464     }
465     setWidgetStates();
466     widgetHasChanged();
467 }
468
469
470 void NetworksSettingsPage::networkConnectionStateChanged(Network::ConnectionState state)
471 {
472     Q_UNUSED(state);
473     const Network *net = qobject_cast<const Network *>(sender());
474     if (!net) return;
475     /*
476     if(net->networkId() == currentId) {
477       ui.connectNow->setEnabled(state == Network::Initialized || state == Network::Disconnected);
478     }
479     */
480     setItemState(net->networkId());
481     setWidgetStates();
482 }
483
484
485 void NetworksSettingsPage::networkConnectionError(const QString &)
486 {
487 }
488
489
490 QListWidgetItem *NetworksSettingsPage::insertNetwork(NetworkId id)
491 {
492     NetworkInfo info = Client::network(id)->networkInfo();
493     networkInfos[id] = info;
494     return insertNetwork(info);
495 }
496
497
498 QListWidgetItem *NetworksSettingsPage::insertNetwork(const NetworkInfo &info)
499 {
500     QListWidgetItem *item = 0;
501     QList<QListWidgetItem *> items = ui.networkList->findItems(info.networkName, Qt::MatchExactly);
502     if (!items.count()) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
503     else {
504         // we overwrite an existing net if it a) has the same name and b) has a negative ID meaning we created it locally before
505         // -> then we can be sure that this is the core-side replacement for the net we created
506         foreach(QListWidgetItem *i, items) {
507             NetworkId id = i->data(Qt::UserRole).value<NetworkId>();
508             if (id < 0) { item = i; break; }
509         }
510         if (!item) item = new QListWidgetItem(disconnectedIcon, info.networkName, ui.networkList);
511     }
512     item->setData(Qt::UserRole, QVariant::fromValue<NetworkId>(info.networkId));
513     setItemState(info.networkId, item);
514     widgetHasChanged();
515     return item;
516 }
517
518
519 void NetworksSettingsPage::displayNetwork(NetworkId id)
520 {
521     _ignoreWidgetChanges = true;
522     if (id != 0) {
523         NetworkInfo info = networkInfos[id];
524
525 #ifdef HAVE_SSL
526         // this is only needed when the core supports SASL EXTERNAL
527         if (Client::coreFeatures() & Quassel::SaslExternal) {
528             if (_cid) {
529                 disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
530                 delete _cid;
531             }
532             _cid = new CertIdentity(*Client::identity(info.identity), this);
533             _cid->enableEditSsl(true);
534             connect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
535         }
536 #endif
537
538         ui.identityList->setCurrentIndex(ui.identityList->findData(info.identity.toInt()));
539         ui.serverList->clear();
540         foreach(Network::Server server, info.serverList) {
541             QListWidgetItem *item = new QListWidgetItem(QString("%1:%2").arg(server.host).arg(server.port));
542             if (server.useSsl)
543                 item->setIcon(QIcon::fromTheme("document-encrypt"));
544             ui.serverList->addItem(item);
545         }
546         //setItemState(id);
547         //ui.randomServer->setChecked(info.useRandomServer);
548         ui.performEdit->setPlainText(info.perform.join("\n"));
549         ui.autoIdentify->setChecked(info.useAutoIdentify);
550         ui.autoIdentifyService->setText(info.autoIdentifyService);
551         ui.autoIdentifyPassword->setText(info.autoIdentifyPassword);
552         ui.sasl->setChecked(info.useSasl);
553         ui.saslAccount->setText(info.saslAccount);
554         ui.saslPassword->setText(info.saslPassword);
555         if (info.codecForEncoding.isEmpty()) {
556             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(Network::defaultCodecForEncoding()));
557             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(Network::defaultCodecForDecoding()));
558             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(Network::defaultCodecForServer()));
559             ui.useCustomEncodings->setChecked(false);
560         }
561         else {
562             ui.sendEncoding->setCurrentIndex(ui.sendEncoding->findText(info.codecForEncoding));
563             ui.recvEncoding->setCurrentIndex(ui.recvEncoding->findText(info.codecForDecoding));
564             ui.serverEncoding->setCurrentIndex(ui.serverEncoding->findText(info.codecForServer));
565             ui.useCustomEncodings->setChecked(true);
566         }
567         ui.autoReconnect->setChecked(info.useAutoReconnect);
568         ui.reconnectInterval->setValue(info.autoReconnectInterval);
569         ui.reconnectRetries->setValue(info.autoReconnectRetries);
570         ui.unlimitedRetries->setChecked(info.unlimitedReconnectRetries);
571         ui.rejoinOnReconnect->setChecked(info.rejoinChannels);
572         // Custom rate limiting
573         ui.unlimitedMessageRate->setChecked(info.unlimitedMessageRate);
574         // Set 'ui.useCustomMessageRate' after 'ui.unlimitedMessageRate' so if the latter is
575         // disabled, 'ui.messageRateDelayFrame' will remain disabled.
576         ui.useCustomMessageRate->setChecked(info.useCustomMessageRate);
577         ui.messageRateBurstSize->setValue(info.messageRateBurstSize);
578         // Convert milliseconds (integer) into seconds (double)
579         ui.messageRateDelay->setValue(info.messageRateDelay / 1000.0f);
580     }
581     else {
582         // just clear widgets
583 #ifdef HAVE_SSL
584         if (_cid) {
585             disconnect(_cid, SIGNAL(sslSettingsUpdated()), this, SLOT(sslUpdated()));
586             delete _cid;
587         }
588 #endif
589         ui.identityList->setCurrentIndex(-1);
590         ui.serverList->clear();
591         ui.performEdit->clear();
592         ui.autoIdentifyService->clear();
593         ui.autoIdentifyPassword->clear();
594         ui.saslAccount->clear();
595         ui.saslPassword->clear();
596         setWidgetStates();
597     }
598     _ignoreWidgetChanges = false;
599     currentId = id;
600 }
601
602
603 void NetworksSettingsPage::saveToNetworkInfo(NetworkInfo &info)
604 {
605     info.identity = ui.identityList->itemData(ui.identityList->currentIndex()).toInt();
606     //info.useRandomServer = ui.randomServer->isChecked();
607     info.perform = ui.performEdit->toPlainText().split("\n");
608     info.useAutoIdentify = ui.autoIdentify->isChecked();
609     info.autoIdentifyService = ui.autoIdentifyService->text();
610     info.autoIdentifyPassword = ui.autoIdentifyPassword->text();
611     info.useSasl = ui.sasl->isChecked();
612     info.saslAccount = ui.saslAccount->text();
613     info.saslPassword = ui.saslPassword->text();
614     if (!ui.useCustomEncodings->isChecked()) {
615         info.codecForEncoding.clear();
616         info.codecForDecoding.clear();
617         info.codecForServer.clear();
618     }
619     else {
620         info.codecForEncoding = ui.sendEncoding->currentText().toLatin1();
621         info.codecForDecoding = ui.recvEncoding->currentText().toLatin1();
622         info.codecForServer = ui.serverEncoding->currentText().toLatin1();
623     }
624     info.useAutoReconnect = ui.autoReconnect->isChecked();
625     info.autoReconnectInterval = ui.reconnectInterval->value();
626     info.autoReconnectRetries = ui.reconnectRetries->value();
627     info.unlimitedReconnectRetries = ui.unlimitedRetries->isChecked();
628     info.rejoinChannels = ui.rejoinOnReconnect->isChecked();
629     // Custom rate limiting
630     info.useCustomMessageRate = ui.useCustomMessageRate->isChecked();
631     info.messageRateBurstSize = ui.messageRateBurstSize->value();
632     // Convert seconds (double) into milliseconds (integer)
633     info.messageRateDelay = static_cast<quint32>((ui.messageRateDelay->value() * 1000));
634     info.unlimitedMessageRate = ui.unlimitedMessageRate->isChecked();
635 }
636
637
638 #ifdef HAVE_SSL
639 void NetworksSettingsPage::sslUpdated()
640 {
641     if (_cid && !_cid->sslKey().isNull()) {
642         ui.saslAccount->setDisabled(true);
643         ui.saslAccountLabel->setDisabled(true);
644         ui.saslPassword->setDisabled(true);
645         ui.saslPasswordLabel->setDisabled(true);
646         ui.saslExtInfo->setHidden(false);
647     } else {
648         ui.saslAccount->setDisabled(false);
649         ui.saslAccountLabel->setDisabled(false);
650         ui.saslPassword->setDisabled(false);
651         ui.saslPasswordLabel->setDisabled(false);
652         ui.saslExtInfo->setHidden(true);
653     }
654 }
655 #endif
656
657
658 /*** Network list ***/
659
660 void NetworksSettingsPage::on_networkList_itemSelectionChanged()
661 {
662     if (currentId != 0) {
663         saveToNetworkInfo(networkInfos[currentId]);
664     }
665     if (ui.networkList->selectedItems().count()) {
666         NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
667         currentId = id;
668         displayNetwork(id);
669         ui.serverList->setCurrentRow(0);
670     }
671     else {
672         currentId = 0;
673     }
674     setWidgetStates();
675 }
676
677
678 void NetworksSettingsPage::on_addNetwork_clicked()
679 {
680     QStringList existing;
681     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
682     NetworkAddDlg dlg(existing, this);
683     if (dlg.exec() == QDialog::Accepted) {
684         NetworkInfo info = dlg.networkInfo();
685         if (info.networkName.isEmpty())
686             return;  // sanity check
687
688         NetworkId id;
689         for (id = 1; id <= networkInfos.count(); id++) {
690             widgetHasChanged();
691             if (!networkInfos.keys().contains(-id.toInt())) break;
692         }
693         id = -id.toInt();
694         info.networkId = id;
695         info.identity = defaultIdentity();
696         networkInfos[id] = info;
697         QListWidgetItem *item = insertNetwork(info);
698         ui.networkList->setCurrentItem(item);
699         setWidgetStates();
700     }
701 }
702
703
704 void NetworksSettingsPage::on_deleteNetwork_clicked()
705 {
706     if (ui.networkList->selectedItems().count()) {
707         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
708         int ret = QMessageBox::question(this, tr("Delete Network?"),
709             tr("Do you really want to delete the network \"%1\" and all related settings, including the backlog?").arg(networkInfos[netid].networkName),
710             QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
711         if (ret == QMessageBox::Yes) {
712             currentId = 0;
713             networkInfos.remove(netid);
714             delete ui.networkList->takeItem(ui.networkList->row(ui.networkList->selectedItems()[0]));
715             ui.networkList->setCurrentRow(qMin(ui.networkList->currentRow()+1, ui.networkList->count()-1));
716             setWidgetStates();
717             widgetHasChanged();
718         }
719     }
720 }
721
722
723 void NetworksSettingsPage::on_renameNetwork_clicked()
724 {
725     if (!ui.networkList->selectedItems().count()) return;
726     QString old = ui.networkList->selectedItems()[0]->text();
727     QStringList existing;
728     for (int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text();
729     NetworkEditDlg dlg(old, existing, this);
730     if (dlg.exec() == QDialog::Accepted) {
731         ui.networkList->selectedItems()[0]->setText(dlg.networkName());
732         NetworkId netid = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
733         networkInfos[netid].networkName = dlg.networkName();
734         widgetHasChanged();
735     }
736 }
737
738
739 /*
740 void NetworksSettingsPage::on_connectNow_clicked() {
741   if(!ui.networkList->selectedItems().count()) return;
742   NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
743   const Network *net = Client::network(id);
744   if(!net) return;
745   if(net->connectionState() == Network::Disconnected) net->requestConnect();
746   else net->requestDisconnect();
747 }
748 */
749
750 /*** Server list ***/
751
752 void NetworksSettingsPage::on_serverList_itemSelectionChanged()
753 {
754     setWidgetStates();
755 }
756
757
758 void NetworksSettingsPage::on_addServer_clicked()
759 {
760     if (currentId == 0) return;
761     ServerEditDlg dlg(Network::Server(), this);
762     if (dlg.exec() == QDialog::Accepted) {
763         networkInfos[currentId].serverList.append(dlg.serverData());
764         displayNetwork(currentId);
765         ui.serverList->setCurrentRow(ui.serverList->count()-1);
766         widgetHasChanged();
767     }
768 }
769
770
771 void NetworksSettingsPage::on_editServer_clicked()
772 {
773     if (currentId == 0) return;
774     int cur = ui.serverList->currentRow();
775     ServerEditDlg dlg(networkInfos[currentId].serverList[cur], this);
776     if (dlg.exec() == QDialog::Accepted) {
777         networkInfos[currentId].serverList[cur] = dlg.serverData();
778         displayNetwork(currentId);
779         ui.serverList->setCurrentRow(cur);
780         widgetHasChanged();
781     }
782 }
783
784
785 void NetworksSettingsPage::on_deleteServer_clicked()
786 {
787     if (currentId == 0) return;
788     int cur = ui.serverList->currentRow();
789     networkInfos[currentId].serverList.removeAt(cur);
790     displayNetwork(currentId);
791     ui.serverList->setCurrentRow(qMin(cur, ui.serverList->count()-1));
792     widgetHasChanged();
793 }
794
795
796 void NetworksSettingsPage::on_upServer_clicked()
797 {
798     int cur = ui.serverList->currentRow();
799     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
800     networkInfos[currentId].serverList.insert(cur-1, server);
801     displayNetwork(currentId);
802     ui.serverList->setCurrentRow(cur-1);
803     widgetHasChanged();
804 }
805
806
807 void NetworksSettingsPage::on_downServer_clicked()
808 {
809     int cur = ui.serverList->currentRow();
810     Network::Server server = networkInfos[currentId].serverList.takeAt(cur);
811     networkInfos[currentId].serverList.insert(cur+1, server);
812     displayNetwork(currentId);
813     ui.serverList->setCurrentRow(cur+1);
814     widgetHasChanged();
815 }
816
817
818 void NetworksSettingsPage::on_editIdentities_clicked()
819 {
820     SettingsPageDlg dlg(new IdentitiesSettingsPage(this), this);
821     dlg.exec();
822 }
823
824
825 IdentityId NetworksSettingsPage::defaultIdentity() const
826 {
827     IdentityId defaultId = 0;
828     QList<IdentityId> ids = Client::identityIds();
829     foreach(IdentityId id, ids) {
830         if (defaultId == 0 || id < defaultId)
831             defaultId = id;
832     }
833     return defaultId;
834 }
835
836
837 /**************************************************************************
838 * NetworkAddDlg
839 *************************************************************************/
840
841 NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
842 {
843     ui.setupUi(this);
844     ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
845
846     // Whenever useSSL is toggled, update the port number if not changed from the default
847     connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
848     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
849     // If useSSL is later changed to be checked by default, change port's default value, too.
850
851     if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
852         // Synchronize requiring SSL with the use SSL checkbox
853         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
854         connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
855     } else {
856         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
857         ui.sslVerify->setEnabled(false);
858         ui.sslVerify->setChecked(false);
859         // Split up the message to allow re-using translations:
860         // [Original tool-tip]
861         // [Bold 'does not support feature' message]
862         // [Specific version needed and feature details]
863         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
864                                        ui.sslVerify->toolTip(),
865                                        tr("Your Quassel core does not support this feature"),
866                                        tr("You need a Quassel core v0.13.0 or newer in order to "
867                                           "verify connection security.")));
868     }
869
870     // read preset networks
871     QStringList networks = PresetNetworks::names();
872     foreach(QString s, existing)
873     networks.removeAll(s);
874     if (networks.count())
875         ui.presetList->addItems(networks);
876     else {
877         ui.useManual->setChecked(true);
878         ui.usePreset->setEnabled(false);
879     }
880     connect(ui.networkName, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
881     connect(ui.serverAddress, SIGNAL(textChanged(const QString &)), SLOT(setButtonStates()));
882     setButtonStates();
883 }
884
885
886 NetworkInfo NetworkAddDlg::networkInfo() const
887 {
888     if (ui.useManual->isChecked()) {
889         NetworkInfo info;
890         info.networkName = ui.networkName->text().trimmed();
891         info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(),
892                                            ui.serverPassword->text(), ui.useSSL->isChecked(),
893                                            ui.sslVerify->isChecked());
894         return info;
895     }
896     else
897         return PresetNetworks::networkInfo(ui.presetList->currentText());
898 }
899
900
901 void NetworkAddDlg::setButtonStates()
902 {
903     bool ok = false;
904     if (ui.usePreset->isChecked() && ui.presetList->count())
905         ok = true;
906     else if (ui.useManual->isChecked()) {
907         ok = !ui.networkName->text().trimmed().isEmpty() && !existing.contains(ui.networkName->text().trimmed())
908              && !ui.serverAddress->text().isEmpty();
909     }
910     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
911 }
912
913
914 void NetworkAddDlg::updateSslPort(bool isChecked)
915 {
916     // "Use encrypted connection" was toggled, check the state...
917     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
918         // Had been using the plain-text port, use the SSL default
919         ui.port->setValue(Network::PORT_SSL);
920     } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
921         // Had been using the SSL port, use the plain-text default
922         ui.port->setValue(Network::PORT_PLAINTEXT);
923     }
924 }
925
926
927 /**************************************************************************
928  * NetworkEditDlg
929  *************************************************************************/
930
931 NetworkEditDlg::NetworkEditDlg(const QString &old, const QStringList &exist, QWidget *parent) : QDialog(parent), existing(exist)
932 {
933     ui.setupUi(this);
934
935     if (old.isEmpty()) {
936         // new network
937         setWindowTitle(tr("Add Network"));
938         on_networkEdit_textChanged(""); // disable ok button
939     }
940     else ui.networkEdit->setText(old);
941 }
942
943
944 QString NetworkEditDlg::networkName() const
945 {
946     return ui.networkEdit->text().trimmed();
947 }
948
949
950 void NetworkEditDlg::on_networkEdit_textChanged(const QString &text)
951 {
952     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(text.isEmpty() || existing.contains(text.trimmed()));
953 }
954
955
956 /**************************************************************************
957  * ServerEditDlg
958  *************************************************************************/
959 ServerEditDlg::ServerEditDlg(const Network::Server &server, QWidget *parent) : QDialog(parent)
960 {
961     ui.setupUi(this);
962     ui.useSSL->setIcon(QIcon::fromTheme("document-encrypt"));
963     ui.host->setText(server.host);
964     ui.host->setFocus();
965     ui.port->setValue(server.port);
966     ui.password->setText(server.password);
967     ui.useSSL->setChecked(server.useSsl);
968     ui.sslVerify->setChecked(server.sslVerify);
969     ui.sslVersion->setCurrentIndex(server.sslVersion);
970     ui.useProxy->setChecked(server.useProxy);
971     ui.proxyType->setCurrentIndex(server.proxyType == QNetworkProxy::Socks5Proxy ? 0 : 1);
972     ui.proxyHost->setText(server.proxyHost);
973     ui.proxyPort->setValue(server.proxyPort);
974     ui.proxyUsername->setText(server.proxyUser);
975     ui.proxyPassword->setText(server.proxyPass);
976
977     // This is a dirty hack to display the core->IRC SSL protocol dropdown
978     // only if the core won't use autonegotiation to determine the best
979     // protocol.  When autonegotiation was introduced, it would have been
980     // a good idea to use the CoreFeatures enum to accomplish this.
981     // However, since multiple versions have been released since then, that
982     // is no longer possible.  Instead, we rely on the fact that the
983     // Datastream protocol was introduced in the same version (0.10) as SSL
984     // autonegotiation.  Because of that, we can display the dropdown only
985     // if the Legacy protocol is in use.  If any other RemotePeer protocol
986     // is in use, that means a newer protocol is in use and therefore the
987     // core will use autonegotiation.
988     if (Client::coreConnection()->peer()->protocol() != Protocol::LegacyProtocol) {
989         ui.label_3->hide();
990         ui.sslVersion->hide();
991     }
992
993     // Whenever useSSL is toggled, update the port number if not changed from the default
994     connect(ui.useSSL, SIGNAL(toggled(bool)), SLOT(updateSslPort(bool)));
995     // Do NOT call updateSslPort when loading settings, otherwise port settings may be overriden.
996     // If useSSL is later changed to be checked by default, change port's default value, too.
997
998     if (Client::coreFeatures() & Quassel::VerifyServerSSL) {
999         // Synchronize requiring SSL with the use SSL checkbox
1000         ui.sslVerify->setEnabled(ui.useSSL->isChecked());
1001         connect(ui.useSSL, SIGNAL(toggled(bool)), ui.sslVerify, SLOT(setEnabled(bool)));
1002     } else {
1003         // Core isn't new enough to allow requiring SSL; disable checkbox and uncheck
1004         ui.sslVerify->setEnabled(false);
1005         ui.sslVerify->setChecked(false);
1006         // Split up the message to allow re-using translations:
1007         // [Original tool-tip]
1008         // [Bold 'does not support feature' message]
1009         // [Specific version needed and feature details]
1010         ui.sslVerify->setToolTip(QString("%1<br/><b>%2</b><br/>%3").arg(
1011                                        ui.sslVerify->toolTip(),
1012                                        tr("Your Quassel core does not support this feature"),
1013                                        tr("You need a Quassel core v0.13.0 or newer in order to "
1014                                           "verify connection security.")));
1015     }
1016
1017     on_host_textChanged();
1018 }
1019
1020
1021 Network::Server ServerEditDlg::serverData() const
1022 {
1023     Network::Server server(ui.host->text().trimmed(), ui.port->value(), ui.password->text(),
1024                            ui.useSSL->isChecked(), ui.sslVerify->isChecked());
1025     server.sslVersion = ui.sslVersion->currentIndex();
1026     server.useProxy = ui.useProxy->isChecked();
1027     server.proxyType = ui.proxyType->currentIndex() == 0 ? QNetworkProxy::Socks5Proxy : QNetworkProxy::HttpProxy;
1028     server.proxyHost = ui.proxyHost->text();
1029     server.proxyPort = ui.proxyPort->value();
1030     server.proxyUser = ui.proxyUsername->text();
1031     server.proxyPass = ui.proxyPassword->text();
1032     return server;
1033 }
1034
1035
1036 void ServerEditDlg::on_host_textChanged()
1037 {
1038     ui.buttonBox->button(QDialogButtonBox::Ok)->setDisabled(ui.host->text().trimmed().isEmpty());
1039 }
1040
1041
1042 void ServerEditDlg::updateSslPort(bool isChecked)
1043 {
1044     // "Use encrypted connection" was toggled, check the state...
1045     if (isChecked && ui.port->value() == Network::PORT_PLAINTEXT) {
1046         // Had been using the plain-text port, use the SSL default
1047         ui.port->setValue(Network::PORT_SSL);
1048     } else if (!isChecked && ui.port->value() == Network::PORT_SSL) {
1049         // Had been using the SSL port, use the plain-text default
1050         ui.port->setValue(Network::PORT_PLAINTEXT);
1051     }
1052 }
1053
1054
1055 /**************************************************************************
1056  * SaveNetworksDlg
1057  *************************************************************************/
1058
1059 SaveNetworksDlg::SaveNetworksDlg(const QList<NetworkInfo> &toCreate, const QList<NetworkInfo> &toUpdate, const QList<NetworkId> &toRemove, QWidget *parent) : QDialog(parent)
1060 {
1061     ui.setupUi(this);
1062
1063     numevents = toCreate.count() + toUpdate.count() + toRemove.count();
1064     rcvevents = 0;
1065     if (numevents) {
1066         ui.progressBar->setMaximum(numevents);
1067         ui.progressBar->setValue(0);
1068
1069         connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientEvent()));
1070         connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientEvent()));
1071
1072         foreach(NetworkId id, toRemove) {
1073             Client::removeNetwork(id);
1074         }
1075         foreach(NetworkInfo info, toCreate) {
1076             Client::createNetwork(info);
1077         }
1078         foreach(NetworkInfo info, toUpdate) {
1079             const Network *net = Client::network(info.networkId);
1080             if (!net) {
1081                 qWarning() << "Invalid client network!";
1082                 numevents--;
1083                 continue;
1084             }
1085             // FIXME this only checks for one changed item rather than all!
1086             connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientEvent()));
1087             Client::updateNetwork(info);
1088         }
1089     }
1090     else {
1091         qWarning() << "Sync dialog called without stuff to change!";
1092         accept();
1093     }
1094 }
1095
1096
1097 void SaveNetworksDlg::clientEvent()
1098 {
1099     ui.progressBar->setValue(++rcvevents);
1100     if (rcvevents >= numevents) accept();
1101 }