File indexing completed on 2024-05-19 04:59:19
0001 /* ============================================================ 0002 * StatusBarIcons - Extra icons in statusbar for Falkon 0003 * Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "sbi_networkmanager.h" 0019 #include "sbi_networkproxy.h" 0020 #include "mainapplication.h" 0021 #include "networkmanager.h" 0022 #include "datapaths.h" 0023 0024 #include <QSettings> 0025 0026 SBI_NetworkManager* SBI_NetworkManager::s_instance = nullptr; 0027 0028 SBI_NetworkManager::SBI_NetworkManager(const QString &settingsPath, QObject* parent) 0029 : QObject(parent) 0030 , m_settingsFile(settingsPath + QL1S("/networkicon.ini")) 0031 , m_currentProxy(nullptr) 0032 { 0033 s_instance = this; 0034 0035 loadSettings(); 0036 } 0037 0038 SBI_NetworkManager* SBI_NetworkManager::instance() 0039 { 0040 return s_instance; 0041 } 0042 0043 void SBI_NetworkManager::loadSettings() 0044 { 0045 QSettings settings(m_settingsFile, QSettings::IniFormat); 0046 0047 const auto groups = settings.childGroups(); 0048 for (const QString &group : groups) { 0049 if (group.isEmpty()) { 0050 continue; 0051 } 0052 0053 auto* proxy = new SBI_NetworkProxy; 0054 0055 settings.beginGroup(group); 0056 proxy->loadFromSettings(settings); 0057 settings.endGroup(); 0058 0059 m_proxies[group] = proxy; 0060 } 0061 0062 const QString currentName = settings.value(QSL("CurrentProxy"), QString()).toString(); 0063 m_currentProxy = m_proxies.contains(currentName) ? m_proxies.value(currentName) : nullptr; 0064 0065 applyCurrentProxy(); 0066 } 0067 0068 QString SBI_NetworkManager::currentProxyName() const 0069 { 0070 return m_proxies.key(m_currentProxy); 0071 } 0072 0073 SBI_NetworkProxy* SBI_NetworkManager::currentProxy() const 0074 { 0075 return m_currentProxy; 0076 } 0077 0078 void SBI_NetworkManager::setCurrentProxy(const QString &name) 0079 { 0080 QSettings settings(m_settingsFile, QSettings::IniFormat); 0081 settings.setValue(QSL("CurrentProxy"), name); 0082 0083 m_currentProxy = m_proxies.contains(name) ? m_proxies.value(name) : nullptr; 0084 applyCurrentProxy(); 0085 } 0086 0087 void SBI_NetworkManager::saveProxy(const QString &name, SBI_NetworkProxy* proxy) 0088 { 0089 if (name.isEmpty()) { 0090 return; 0091 } 0092 0093 QSettings settings(m_settingsFile, QSettings::IniFormat); 0094 settings.beginGroup(name); 0095 proxy->saveToSettings(settings); 0096 settings.endGroup(); 0097 0098 m_proxies[name] = proxy; 0099 } 0100 0101 void SBI_NetworkManager::removeProxy(const QString &name) 0102 { 0103 if (name.isEmpty()) { 0104 return; 0105 } 0106 0107 QSettings settings(m_settingsFile, QSettings::IniFormat); 0108 settings.beginGroup(name); 0109 settings.remove(QString()); // Removes all keys in current group 0110 settings.endGroup(); 0111 0112 m_proxies.remove(name); 0113 } 0114 0115 QHash<QString, SBI_NetworkProxy*> SBI_NetworkManager::proxies() const 0116 { 0117 return m_proxies; 0118 } 0119 0120 void SBI_NetworkManager::applyCurrentProxy() 0121 { 0122 if (!m_currentProxy) { 0123 return; 0124 } 0125 0126 m_currentProxy->applyProxy(); 0127 } 0128 0129 void SBI_NetworkManager::deleteProxies() 0130 { 0131 qDeleteAll(m_proxies); 0132 m_proxies.clear(); 0133 } 0134 0135 SBI_NetworkManager::~SBI_NetworkManager() 0136 { 0137 deleteProxies(); 0138 }