From 6c19be8efa70b6fb76d2c43aa96ab5c908e039e3 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Tue, 13 Jan 2009 23:40:17 +0100 Subject: [PATCH] Move handling of networks.ini from NetworksSettingsPage in Network We use this in Client as well, so avoid code duplication here. --- src/common/network.cpp | 79 +++++++++++++++++++ src/common/network.h | 10 ++- .../settingspages/networkssettingspage.cpp | 69 ++++------------ src/qtui/settingspages/networkssettingspage.h | 1 - 4 files changed, 102 insertions(+), 57 deletions(-) diff --git a/src/common/network.cpp b/src/common/network.cpp index fc8b508d..9a4994f0 100644 --- a/src/common/network.cpp +++ b/src/common/network.cpp @@ -20,11 +20,13 @@ #include "network.h" #include +#include #include QTextCodec *Network::_defaultCodecForServer = 0; QTextCodec *Network::_defaultCodecForEncoding = 0; QTextCodec *Network::_defaultCodecForDecoding = 0; +QString Network::_networksIniPath = QString(); // ==================== // Public: @@ -396,6 +398,68 @@ QByteArray Network::encodeServerString(const QString &string) const { return string.toAscii(); } +/*** Handle networks.ini ***/ + +QStringList Network::presetNetworks(bool onlyDefault) { + // lazily find the file, make sure to not call one of the other preset functions first (they'll fail else) + if(_networksIniPath.isNull()) { + _networksIniPath = findDataFilePath("networks.ini"); + if(_networksIniPath.isNull()) { + _networksIniPath = ""; // now we won't check again, as it's not null anymore + return QStringList(); + } + } + if(!_networksIniPath.isEmpty()) { + QSettings s(_networksIniPath, QSettings::IniFormat); + QStringList networks = s.childGroups(); + if(!networks.isEmpty()) { + // we sort the list case-insensitive + QMap sorted; + foreach(QString net, networks) { + if(onlyDefault && !s.value(QString("%1/Default").arg(net)).toBool()) + continue; + sorted[net.toLower()] = net; + } + return sorted.values(); + } + } + return QStringList(); +} + +QStringList Network::presetDefaultChannels(const QString &networkName) { + if(_networksIniPath.isEmpty()) // be sure to have called presetNetworks() first, else this always fails + return QStringList(); + QSettings s(_networksIniPath, QSettings::IniFormat); + return s.value(QString("%1/DefaultChannels").arg(networkName)).toStringList(); +} + +NetworkInfo Network::networkInfoFromPreset(const QString &networkName) { + NetworkInfo info; + if(!_networksIniPath.isEmpty()) { + info.networkName = networkName; + QSettings s(_networksIniPath, QSettings::IniFormat); + s.beginGroup(info.networkName); + foreach(QString server, s.value("Servers").toStringList()) { + bool ssl = false; + QStringList splitserver = server.split(':', QString::SkipEmptyParts); + if(splitserver.count() != 2) { + qWarning() << "Invalid server entry in networks.conf:" << server; + continue; + } + if(splitserver[1].at(0) == '+') + ssl = true; + uint port = splitserver[1].toUInt(); + if(!port) { + qWarning() << "Invalid port entry in networks.conf:" << server; + continue; + } + info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl); + } + } + return info; +} + + // ==================== // Public Slots: // ==================== @@ -661,6 +725,21 @@ void Network::determinePrefixes() { * NetworkInfo ************************************************************************/ +NetworkInfo::NetworkInfo() +: networkId(0), + identity(1), + useRandomServer(false), + useAutoIdentify(false), + autoIdentifyService("NickServ"), + useAutoReconnect(true), + autoReconnectInterval(60), + autoReconnectRetries(20), + unlimitedReconnectRetries(false), + rejoinChannels(true) +{ + +} + bool NetworkInfo::operator==(const NetworkInfo &other) const { if(networkId != other.networkId) return false; if(networkName != other.networkName) return false; diff --git a/src/common/network.h b/src/common/network.h index bee38e96..704d67aa 100644 --- a/src/common/network.h +++ b/src/common/network.h @@ -203,6 +203,10 @@ public: inline bool autoAwayActive() const { return _autoAwayActive; } inline void setAutoAwayActive(bool active) { _autoAwayActive = active; } + static QStringList presetNetworks(bool onlyDefault = false); + static QStringList presetDefaultChannels(const QString &networkName); + static NetworkInfo networkInfoFromPreset(const QString &networkName); + public slots: void setNetworkName(const QString &networkName); void setCurrentServer(const QString ¤tServer); @@ -350,12 +354,17 @@ private: bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output + static QString _networksIniPath; + friend class IrcUser; friend class IrcChannel; }; //! Stores all editable information about a network (as opposed to runtime state). struct NetworkInfo { + // set some default values, note that this does not initialize e.g. name and id + NetworkInfo(); + NetworkId networkId; QString networkName; IdentityId identity; @@ -365,7 +374,6 @@ struct NetworkInfo { QByteArray codecForEncoding; QByteArray codecForDecoding; - // Server entry: QString "Host", uint "Port", QString "Password", bool "UseSSL" Network::ServerList serverList; bool useRandomServer; diff --git a/src/qtui/settingspages/networkssettingspage.cpp b/src/qtui/settingspages/networkssettingspage.cpp index 23026302..9d543379 100644 --- a/src/qtui/settingspages/networkssettingspage.cpp +++ b/src/qtui/settingspages/networkssettingspage.cpp @@ -521,29 +521,17 @@ void NetworksSettingsPage::on_addNetwork_clicked() { for(int i = 0; i < ui.networkList->count(); i++) existing << ui.networkList->item(i)->text(); NetworkAddDlg dlg(existing, this); if(dlg.exec() == QDialog::Accepted) { + NetworkInfo info = dlg.networkInfo(); + if(info.networkName.isEmpty()) + return; // sanity check + NetworkId id; for(id = 1; id <= networkInfos.count(); id++) { widgetHasChanged(); if(!networkInfos.keys().contains(-id.toInt())) break; } id = -id.toInt(); - - NetworkInfo info = dlg.networkInfo(); - if(info.networkName.isEmpty()) - return; // sanity check info.networkId = id; - info.identity = 1; - - // defaults - info.useRandomServer = false; - info.useAutoReconnect = true; - info.autoReconnectInterval = 60; - info.autoReconnectRetries = 20; - info.unlimitedReconnectRetries = false; - info.useAutoIdentify = false; - info.autoIdentifyService = "NickServ"; - info.rejoinChannels = true; - networkInfos[id] = info; QListWidgetItem *item = insertNetwork(info); ui.networkList->setCurrentItem(item); @@ -663,20 +651,12 @@ NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialo ui.useSSL->setIcon(SmallIcon("document-encrypt")); // read preset networks - networksFilePath = findDataFilePath("networks.ini"); - if(!networksFilePath.isEmpty()) { - QSettings s(networksFilePath, QSettings::IniFormat); - QStringList networks = s.childGroups(); - foreach(QString s, existing) - networks.removeAll(s); - if(!networks.isEmpty()) { - QMap sorted; - foreach(QString net, networks) - sorted[net.toLower()] = net; - ui.presetList->addItems(sorted.values()); - } - } - if(!ui.presetList->count()) { + QStringList networks = Network::presetNetworks(); + foreach(QString s, existing) + networks.removeAll(s); + if(networks.count()) + ui.presetList->addItems(networks); + else { ui.useManual->setChecked(true); ui.usePreset->setEnabled(false); } @@ -686,34 +666,13 @@ NetworkAddDlg::NetworkAddDlg(const QStringList &exist, QWidget *parent) : QDialo } NetworkInfo NetworkAddDlg::networkInfo() const { - NetworkInfo info; - if(ui.useManual->isChecked()) { + NetworkInfo info; info.networkName = ui.networkName->text().trimmed(); info.serverList << Network::Server(ui.serverAddress->text().trimmed(), ui.port->value(), ui.serverPassword->text(), ui.useSSL->isChecked()); - } else { - info.networkName = ui.presetList->currentText(); - QSettings s(networksFilePath, QSettings::IniFormat); - s.beginGroup(info.networkName); - foreach(QString server, s.value("Servers").toStringList()) { - bool ssl = false; - QStringList splitserver = server.split(':', QString::SkipEmptyParts); - if(splitserver.count() != 2) { - qWarning() << "Invalid server entry in networks.conf:" << server; - continue; - } - if(splitserver[1].at(0) == '+') - ssl = true; - uint port = splitserver[1].toUInt(); - if(!port) { - qWarning() << "Invalid port entry in networks.conf:" << server; - continue; - } - info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl); - } - } - - return info; + return info; + } else + return Network::networkInfoFromPreset(ui.presetList->currentText()); } void NetworkAddDlg::setButtonStates() { diff --git a/src/qtui/settingspages/networkssettingspage.h b/src/qtui/settingspages/networkssettingspage.h index 65b132f1..40d0276f 100644 --- a/src/qtui/settingspages/networkssettingspage.h +++ b/src/qtui/settingspages/networkssettingspage.h @@ -109,7 +109,6 @@ class NetworkAddDlg : public QDialog { private: Ui::NetworkAddDlg ui; - QString networksFilePath; QStringList existing; }; -- 2.20.1