Falkon Develop
Cross-platform Qt-based web browser
sbi_networkmanager.cpp
Go to the documentation of this file.
1/* ============================================================
2* StatusBarIcons - Extra icons in statusbar for Falkon
3* Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com>
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 3 of the License, or
8* (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "sbi_networkmanager.h"
19#include "sbi_networkproxy.h"
20#include "mainapplication.h"
21#include "networkmanager.h"
22#include "datapaths.h"
23
24#include <QSettings>
25
26SBI_NetworkManager* SBI_NetworkManager::s_instance = nullptr;
27
28SBI_NetworkManager::SBI_NetworkManager(const QString &settingsPath, QObject* parent)
29 : QObject(parent)
30 , m_settingsFile(settingsPath + QL1S("/networkicon.ini"))
31 , m_currentProxy(nullptr)
32{
33 s_instance = this;
34
36}
37
39{
40 return s_instance;
41}
42
44{
45 QSettings settings(m_settingsFile, QSettings::IniFormat);
46
47 const auto groups = settings.childGroups();
48 for (const QString &group : groups) {
49 if (group.isEmpty()) {
50 continue;
51 }
52
53 auto* proxy = new SBI_NetworkProxy;
54
55 settings.beginGroup(group);
56 proxy->loadFromSettings(settings);
57 settings.endGroup();
58
59 m_proxies[group] = proxy;
60 }
61
62 const QString currentName = settings.value(QSL("CurrentProxy"), QString()).toString();
63 m_currentProxy = m_proxies.contains(currentName) ? m_proxies.value(currentName) : nullptr;
64
65 applyCurrentProxy();
66}
67
69{
70 return m_proxies.key(m_currentProxy);
71}
72
74{
75 return m_currentProxy;
76}
77
78void SBI_NetworkManager::setCurrentProxy(const QString &name)
79{
80 QSettings settings(m_settingsFile, QSettings::IniFormat);
81 settings.setValue(QSL("CurrentProxy"), name);
82
83 m_currentProxy = m_proxies.contains(name) ? m_proxies.value(name) : nullptr;
84 applyCurrentProxy();
85}
86
87void SBI_NetworkManager::saveProxy(const QString &name, SBI_NetworkProxy* proxy)
88{
89 if (name.isEmpty()) {
90 return;
91 }
92
93 QSettings settings(m_settingsFile, QSettings::IniFormat);
94 settings.beginGroup(name);
95 proxy->saveToSettings(settings);
96 settings.endGroup();
97
98 m_proxies[name] = proxy;
99}
100
101void SBI_NetworkManager::removeProxy(const QString &name)
102{
103 if (name.isEmpty()) {
104 return;
105 }
106
107 QSettings settings(m_settingsFile, QSettings::IniFormat);
108 settings.beginGroup(name);
109 settings.remove(QString()); // Removes all keys in current group
110 settings.endGroup();
111
112 m_proxies.remove(name);
113}
114
115QHash<QString, SBI_NetworkProxy*> SBI_NetworkManager::proxies() const
116{
117 return m_proxies;
118}
119
120void SBI_NetworkManager::applyCurrentProxy()
121{
122 if (!m_currentProxy) {
123 return;
124 }
125
126 m_currentProxy->applyProxy();
127}
128
129void SBI_NetworkManager::deleteProxies()
130{
131 qDeleteAll(m_proxies);
132 m_proxies.clear();
133}
134
136{
137 deleteProxies();
138}
void saveProxy(const QString &name, SBI_NetworkProxy *proxy)
SBI_NetworkProxy * currentProxy() const
SBI_NetworkManager(const QString &settingsPath, QObject *parent=nullptr)
void setCurrentProxy(const QString &name)
static SBI_NetworkManager * instance()
QString currentProxyName() const
void removeProxy(const QString &name)
QHash< QString, SBI_NetworkProxy * > proxies() const
void saveToSettings(QSettings &settings) const
void loadFromSettings(const QSettings &settings)
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40