1/***************************************************************************
2 * Copyright (C) 2005-2014 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 <QSettings>
22
23#include "presetnetworks.h"
24#include "quassel.h"
25
26QString PresetNetworks::_networksIniPath = QString();
27
28// ====================
29// Public:
30// ====================
31QStringList PresetNetworks::names(bool onlyDefault)
32{
33 // lazily find the file, make sure to not call one of the other preset functions first (they'll fail else)
34 if (_networksIniPath.isNull()) {
35 _networksIniPath = Quassel::findDataFilePath("networks.ini");
36 if (_networksIniPath.isNull()) {
37 _networksIniPath = ""; // now we won't check again, as it's not null anymore
38 return QStringList();
39 }
40 }
41 if (!_networksIniPath.isEmpty()) {
42 QSettings s(_networksIniPath, QSettings::IniFormat);
43 QStringList networks = s.childGroups();
44 if (!networks.isEmpty()) {
45 // we sort the list case-insensitive
46 QMap<QString, QString> sorted;
47 foreach(QString net, networks) {
48 if (onlyDefault && !s.value(QString("%1/Default").arg(net)).toBool())
49 continue;
50 sorted[net.toLower()] = net;
51 }
52 return sorted.values();
53 }
54 }
55 return QStringList();
56}
57
58
59QStringList PresetNetworks::defaultChannels(const QString &networkName)
60{
61 if (_networksIniPath.isEmpty()) // be sure to have called presetNetworks() first, else this always fails
62 return QStringList();
63 QSettings s(_networksIniPath, QSettings::IniFormat);
64 return s.value(QString("%1/DefaultChannels").arg(networkName)).toStringList();
65}
66
67
68NetworkInfo PresetNetworks::networkInfo(const QString &networkName)
69{
70 NetworkInfo info;
71 if (!_networksIniPath.isEmpty()) {
72 info.networkName = networkName;
73 QSettings s(_networksIniPath, QSettings::IniFormat);
74 s.beginGroup(info.networkName);
75 foreach(QString server, s.value("Servers").toStringList()) {
76 bool ssl = false;
77 QStringList splitserver = server.split(':', QString::SkipEmptyParts);
78 if (splitserver.count() != 2) {
79 qWarning() << "Invalid server entry in networks.conf:" << server;
80 continue;
81 }
82 if (splitserver[1].at(0) == '+')
83 ssl = true;
84 uint port = splitserver[1].toUInt();
85 if (!port) {
86 qWarning() << "Invalid port entry in networks.conf:" << server;
87 continue;
88 }
89 info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl);
90 }
91 }
92 return info;
93}
94