Move handling of networks.ini from NetworksSettingsPage in Network
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 13 Jan 2009 22:40:17 +0000 (23:40 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 13 Jan 2009 22:59:25 +0000 (23:59 +0100)
We use this in Client as well, so avoid code duplication here.

src/common/network.cpp
src/common/network.h
src/qtui/settingspages/networkssettingspage.cpp
src/qtui/settingspages/networkssettingspage.h

index fc8b508..9a4994f 100644 (file)
 #include "network.h"
 
 #include <QDebug>
 #include "network.h"
 
 #include <QDebug>
+#include <QSettings>
 #include <QTextCodec>
 
 QTextCodec *Network::_defaultCodecForServer = 0;
 QTextCodec *Network::_defaultCodecForEncoding = 0;
 QTextCodec *Network::_defaultCodecForDecoding = 0;
 #include <QTextCodec>
 
 QTextCodec *Network::_defaultCodecForServer = 0;
 QTextCodec *Network::_defaultCodecForEncoding = 0;
 QTextCodec *Network::_defaultCodecForDecoding = 0;
+QString Network::_networksIniPath = QString();
 
 // ====================
 //  Public:
 
 // ====================
 //  Public:
@@ -396,6 +398,68 @@ QByteArray Network::encodeServerString(const QString &string) const {
   return string.toAscii();
 }
 
   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<QString, QString> 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:
 // ====================
 // ====================
 //  Public Slots:
 // ====================
@@ -661,6 +725,21 @@ void Network::determinePrefixes() {
  * NetworkInfo
  ************************************************************************/
 
  * 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;
 bool NetworkInfo::operator==(const NetworkInfo &other) const {
   if(networkId != other.networkId) return false;
   if(networkName != other.networkName) return false;
index bee38e9..704d67a 100644 (file)
@@ -203,6 +203,10 @@ public:
   inline bool autoAwayActive() const { return _autoAwayActive; }
   inline void setAutoAwayActive(bool active) { _autoAwayActive = active; }
 
   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 &currentServer);
 public slots:
   void setNetworkName(const QString &networkName);
   void setCurrentServer(const QString &currentServer);
@@ -350,12 +354,17 @@ private:
 
   bool _autoAwayActive; // when this is active handle305 and handle306 don't trigger any output
 
 
   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 {
   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;
   NetworkId networkId;
   QString networkName;
   IdentityId identity;
@@ -365,7 +374,6 @@ struct NetworkInfo {
   QByteArray codecForEncoding;
   QByteArray codecForDecoding;
 
   QByteArray codecForEncoding;
   QByteArray codecForDecoding;
 
-  // Server entry: QString "Host", uint "Port", QString "Password", bool "UseSSL"
   Network::ServerList serverList;
   bool useRandomServer;
 
   Network::ServerList serverList;
   bool useRandomServer;
 
index 2302630..9d54337 100644 (file)
@@ -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) {
   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();
     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.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);
     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
   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<QString, QString> 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);
   }
     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 NetworkAddDlg::networkInfo() const {
-  NetworkInfo info;
-
   if(ui.useManual->isChecked()) {
   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());
     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() {
 }
 
 void NetworkAddDlg::setButtonStates() {
index 65b132f..40d0276 100644 (file)
@@ -109,7 +109,6 @@ class NetworkAddDlg : public QDialog {
   private:
     Ui::NetworkAddDlg ui;
 
   private:
     Ui::NetworkAddDlg ui;
 
-    QString networksFilePath;
     QStringList existing;
 };
 
     QStringList existing;
 };