+#include "settingspages/identitiessettingspage.h"
+
+NetworksSettingsPage::NetworksSettingsPage(QWidget *parent)
+ : SettingsPage(tr("IRC"), tr("Networks"), parent)
+#ifdef HAVE_SSL
+ , _cid(0)
+#endif
+{
+ ui.setupUi(this);
+
+ // hide SASL options for older cores
+ if (!(Client::coreFeatures() & Quassel::SaslAuthentication))
+ ui.sasl->hide();
+ if (!(Client::coreFeatures() & Quassel::SaslExternal))
+ ui.saslExtInfo->hide();
+#ifndef HAVE_SSL
+ ui.saslExtInfo->hide();
+#endif
+
+ // set up icons
+ ui.renameNetwork->setIcon(QIcon::fromTheme("edit-rename"));
+ ui.addNetwork->setIcon(QIcon::fromTheme("list-add"));
+ ui.deleteNetwork->setIcon(QIcon::fromTheme("edit-delete"));
+ ui.addServer->setIcon(QIcon::fromTheme("list-add"));
+ ui.deleteServer->setIcon(QIcon::fromTheme("edit-delete"));
+ ui.editServer->setIcon(QIcon::fromTheme("configure"));
+ ui.upServer->setIcon(QIcon::fromTheme("go-up"));
+ ui.downServer->setIcon(QIcon::fromTheme("go-down"));
+ ui.editIdentities->setIcon(QIcon::fromTheme("configure"));
+
+ _ignoreWidgetChanges = false;
+
+ connectedIcon = QIcon::fromTheme("network-connect");
+ connectingIcon = QIcon::fromTheme("network-wired"); // FIXME network-connecting
+ disconnectedIcon = QIcon::fromTheme("network-disconnect");
+
+ foreach(int mib, QTextCodec::availableMibs()) {
+ QByteArray codec = QTextCodec::codecForMib(mib)->name();
+ ui.sendEncoding->addItem(codec);
+ ui.recvEncoding->addItem(codec);
+ ui.serverEncoding->addItem(codec);
+ }
+ ui.sendEncoding->model()->sort(0);
+ ui.recvEncoding->model()->sort(0);
+ ui.serverEncoding->model()->sort(0);
+ currentId = 0;
+ setEnabled(Client::isConnected()); // need a core connection!
+ setWidgetStates();
+ connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
+ connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkAdded(NetworkId)));
+ connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
+ connect(Client::instance(), SIGNAL(identityCreated(IdentityId)), this, SLOT(clientIdentityAdded(IdentityId)));
+ connect(Client::instance(), SIGNAL(identityRemoved(IdentityId)), this, SLOT(clientIdentityRemoved(IdentityId)));
+
+ connect(ui.identityList, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
+ //connect(ui.randomServer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.performEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
+ connect(ui.autoIdentify, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.autoIdentifyService, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
+ connect(ui.autoIdentifyPassword, SIGNAL(textEdited(const QString &)), this, SLOT(widgetHasChanged()));
+ connect(ui.sasl, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.saslAccount, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
+ connect(ui.saslPassword, SIGNAL(textEdited(QString)), this, SLOT(widgetHasChanged()));
+ connect(ui.useCustomEncodings, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.sendEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
+ connect(ui.recvEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
+ connect(ui.serverEncoding, SIGNAL(currentIndexChanged(int)), this, SLOT(widgetHasChanged()));
+ connect(ui.autoReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.reconnectInterval, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
+ connect(ui.reconnectRetries, SIGNAL(valueChanged(int)), this, SLOT(widgetHasChanged()));
+ connect(ui.unlimitedRetries, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ connect(ui.rejoinOnReconnect, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
+ //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
+ //connect(ui., SIGNAL(), this, SLOT(widgetHasChanged()));
+
+ foreach(IdentityId id, Client::identityIds()) {
+ clientIdentityAdded(id);
+ }
+}
+
+
+void NetworksSettingsPage::save()
+{
+ setEnabled(false);
+ if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
+
+ QList<NetworkInfo> toCreate, toUpdate;
+ QList<NetworkId> toRemove;
+ QHash<NetworkId, NetworkInfo>::iterator i = networkInfos.begin();
+ while (i != networkInfos.end()) {
+ NetworkId id = (*i).networkId;
+ if (id < 0) {
+ toCreate.append(*i);
+ //if(id == currentId) currentId = 0;
+ //QList<QListWidgetItem *> items = ui.networkList->findItems((*i).networkName, Qt::MatchExactly);
+ //if(items.count()) {
+ // Q_ASSERT(items[0]->data(Qt::UserRole).value<NetworkId>() == id);
+ // delete items[0];
+ //}
+ //i = networkInfos.erase(i);
+ ++i;
+ }
+ else {
+ if ((*i) != Client::network((*i).networkId)->networkInfo()) {
+ toUpdate.append(*i);
+ }
+ ++i;
+ }
+ }
+ foreach(NetworkId id, Client::networkIds()) {
+ if (!networkInfos.contains(id)) toRemove.append(id);
+ }
+ SaveNetworksDlg dlg(toCreate, toUpdate, toRemove, this);
+ int ret = dlg.exec();
+ if (ret == QDialog::Rejected) {
+ // canceled -> reload everything to be safe
+ load();
+ }
+ setChangedState(false);
+ setEnabled(true);
+}
+
+
+void NetworksSettingsPage::load()
+{
+ reset();
+ foreach(NetworkId netid, Client::networkIds()) {
+ clientNetworkAdded(netid);
+ }
+ ui.networkList->setCurrentRow(0);
+ setChangedState(false);
+}
+
+
+void NetworksSettingsPage::reset()
+{
+ currentId = 0;
+ ui.networkList->clear();
+ networkInfos.clear();
+}
+
+
+bool NetworksSettingsPage::aboutToSave()
+{
+ if (currentId != 0) saveToNetworkInfo(networkInfos[currentId]);
+ QList<int> errors;
+ foreach(NetworkInfo info, networkInfos.values()) {
+ if (!info.serverList.count()) errors.append(1);
+ }
+ if (!errors.count()) return true;
+ QString error(tr("<b>The following problems need to be corrected before your changes can be applied:</b><ul>"));
+ if (errors.contains(1)) error += tr("<li>All networks need at least one server defined</li>");
+ error += tr("</ul>");
+ QMessageBox::warning(this, tr("Invalid Network Settings"), error);
+ return false;
+}
+
+
+void NetworksSettingsPage::widgetHasChanged()
+{
+ if (_ignoreWidgetChanges) return;
+ bool changed = testHasChanged();
+ if (changed != hasChanged()) setChangedState(changed);
+}
+
+
+bool NetworksSettingsPage::testHasChanged()
+{
+ if (currentId != 0) {
+ saveToNetworkInfo(networkInfos[currentId]);
+ }
+ if (Client::networkIds().count() != networkInfos.count()) return true;
+ foreach(NetworkId id, networkInfos.keys()) {
+ if (id < 0) return true;
+ if (Client::network(id)->networkInfo() != networkInfos[id]) return true;
+ }
+ return false;
+}
+
+
+void NetworksSettingsPage::setWidgetStates()
+{
+ // network list
+ if (ui.networkList->selectedItems().count()) {
+ ui.detailsBox->setEnabled(true);
+ ui.renameNetwork->setEnabled(true);
+ ui.deleteNetwork->setEnabled(true);
+
+ /* button disabled for now
+ NetworkId id = ui.networkList->selectedItems()[0]->data(Qt::UserRole).value<NetworkId>();
+ const Network *net = id > 0 ? Client::network(id) : 0;
+ ui.connectNow->setEnabled(net);
+ // && (Client::network(id)->connectionState() == Network::Initialized
+ // || Client::network(id)->connectionState() == Network::Disconnected));
+ if(net) {
+ if(net->connectionState() == Network::Disconnected) {
+ ui.connectNow->setIcon(connectedIcon);
+ ui.connectNow->setText(tr("Connect"));
+ } else {
+ ui.connectNow->setIcon(disconnectedIcon);
+ ui.connectNow->setText(tr("Disconnect"));
+ }
+ } else {
+ ui.connectNow->setIcon(QIcon());
+ ui.connectNow->setText(tr("Apply first!"));
+ } */
+ }
+ else {
+ ui.renameNetwork->setEnabled(false);
+ ui.deleteNetwork->setEnabled(false);
+ //ui.connectNow->setEnabled(false);
+ ui.detailsBox->setEnabled(false);
+ }
+ // network details
+ if (ui.serverList->selectedItems().count()) {
+ ui.editServer->setEnabled(true);
+ ui.deleteServer->setEnabled(true);
+ ui.upServer->setEnabled(ui.serverList->currentRow() > 0);
+ ui.downServer->setEnabled(ui.serverList->currentRow() < ui.serverList->count() - 1);
+ }
+ else {
+ ui.editServer->setEnabled(false);
+ ui.deleteServer->setEnabled(false);
+ ui.upServer->setEnabled(false);
+ ui.downServer->setEnabled(false);
+ }
+}
+
+
+void NetworksSettingsPage::setItemState(NetworkId id, QListWidgetItem *item)
+{
+ if (!item && !(item = networkItem(id))) return;
+ const Network *net = Client::network(id);
+ if (!net || net->isInitialized()) item->setFlags(item->flags() | Qt::ItemIsEnabled);
+ else item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
+ if (net && net->connectionState() == Network::Initialized) {
+ item->setIcon(connectedIcon);
+ }
+ else if (net && net->connectionState() != Network::Disconnected) {
+ item->setIcon(connectingIcon);
+ }
+ else {
+ item->setIcon(disconnectedIcon);
+ }
+ if (net) {
+ bool select = false;
+ // check if we already have another net of this name in the list, and replace it
+ QList<QListWidgetItem *> items = ui.networkList->findItems(net->networkName(), Qt::MatchExactly);
+ if (items.count()) {
+ foreach(QListWidgetItem *i, items) {
+ NetworkId oldid = i->data(Qt::UserRole).value<NetworkId>();
+ if (oldid > 0) continue; // only locally created nets should be replaced
+ if (oldid == currentId) {
+ select = true;
+ currentId = 0;
+ ui.networkList->clearSelection();
+ }
+ int row = ui.networkList->row(i);
+ if (row >= 0) {
+ QListWidgetItem *olditem = ui.networkList->takeItem(row);
+ Q_ASSERT(olditem);
+ delete olditem;
+ }
+ networkInfos.remove(oldid);
+ break;
+ }
+ }
+ item->setText(net->networkName());
+ if (select) item->setSelected(true);
+ }
+}
+
+
+void NetworksSettingsPage::coreConnectionStateChanged(bool state)
+{
+ this->setEnabled(state);
+ if (state) {
+ load();
+ }
+ else {
+ // reset
+ //currentId = 0;
+ }
+}
+
+
+void NetworksSettingsPage::clientIdentityAdded(IdentityId id)
+{
+ const Identity *identity = Client::identity(id);
+ connect(identity, SIGNAL(updatedRemotely()), this, SLOT(clientIdentityUpdated()));