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