We use this in Client as well, so avoid code duplication here.
#include "network.h"
#include <QDebug>
+#include <QSettings>
#include <QTextCodec>
QTextCodec *Network::_defaultCodecForServer = 0;
QTextCodec *Network::_defaultCodecForEncoding = 0;
QTextCodec *Network::_defaultCodecForDecoding = 0;
+QString Network::_networksIniPath = QString();
// ====================
// Public:
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:
// ====================
* 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;
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);
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;
QByteArray codecForEncoding;
QByteArray codecForDecoding;
- // Server entry: QString "Host", uint "Port", QString "Password", bool "UseSSL"
Network::ServerList serverList;
bool useRandomServer;
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);
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);
}
}
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() {
private:
Ui::NetworkAddDlg ui;
- QString networksFilePath;
QStringList existing;
};