File indexing completed on 2024-05-19 04:55:56

0001 /**
0002  * \file networkconfig.cpp
0003  * Network related configuration.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Jun 2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "networkconfig.h"
0028 #include <cstdlib>
0029 #include "isettings.h"
0030 
0031 namespace {
0032 
0033 /** Default value for web browser */
0034 #ifdef Q_OS_MAC
0035 const char* const defaultBrowser = "open";
0036 #else
0037 const char* const defaultBrowser = "xdg-open";
0038 #endif
0039 
0040 }
0041 
0042 int NetworkConfig::s_index = -1;
0043 
0044 /**
0045  * Constructor.
0046  */
0047 NetworkConfig::NetworkConfig()
0048   : StoredConfig(QLatin1String("Network")),
0049     m_useProxy(false),
0050     m_useProxyAuthentication(false)
0051 {
0052 }
0053 
0054 /**
0055  * Persist configuration.
0056  *
0057  * @param config configuration
0058  */
0059 void NetworkConfig::writeToConfig(ISettings* config) const
0060 {
0061   config->beginGroup(m_group);
0062   config->setValue(QLatin1String("UseProxy"), QVariant(m_useProxy));
0063   config->setValue(QLatin1String("Proxy"), QVariant(m_proxy));
0064   config->setValue(QLatin1String("UseProxyAuthentication"),
0065                    QVariant(m_useProxyAuthentication));
0066   config->setValue(QLatin1String("ProxyUserName"), QVariant(m_proxyUserName));
0067   config->setValue(QLatin1String("ProxyPassword"), QVariant(m_proxyPassword));
0068   config->setValue(QLatin1String("Browser"), QVariant(m_browser));
0069   config->endGroup();
0070 }
0071 
0072 /**
0073  * Read persisted configuration.
0074  *
0075  * @param config configuration
0076  */
0077 void NetworkConfig::readFromConfig(ISettings* config)
0078 {
0079   config->beginGroup(m_group);
0080   m_useProxy = config->value(QLatin1String("UseProxy"), m_useProxy).toBool();
0081   m_proxy = config->value(QLatin1String("Proxy"), m_proxy).toString();
0082   m_useProxyAuthentication = config->value(QLatin1String("UseProxyAuthentication"),
0083                                            m_useProxyAuthentication).toBool();
0084   m_proxyUserName = config->value(QLatin1String("ProxyUserName"),
0085                                   m_proxyUserName).toString();
0086   m_proxyPassword = config->value(QLatin1String("ProxyPassword"),
0087                                   m_proxyPassword).toString();
0088   m_browser = config->value(QLatin1String("Browser"), QString()).toString();
0089   if (m_browser.isEmpty()) {
0090     setDefaultBrowser();
0091   }
0092   config->endGroup();
0093 }
0094 
0095 void NetworkConfig::setDefaultBrowser()
0096 {
0097 #ifdef Q_OS_WIN32
0098   if (m_browser.isEmpty()) {
0099     m_browser = QString::fromLocal8Bit(qgetenv("ProgramFiles"));
0100     m_browser += QLatin1String("\\Internet Explorer\\IEXPLORE.EXE");
0101   }
0102 #else
0103   m_browser = QString::fromLatin1(defaultBrowser);
0104 #endif
0105 }
0106 
0107 void NetworkConfig::setProxy(const QString& proxy)
0108 {
0109   if (m_proxy != proxy) {
0110     m_proxy = proxy;
0111     emit proxyChanged(m_proxy);
0112   }
0113 }
0114 
0115 void NetworkConfig::setProxyUserName(const QString& proxyUserName)
0116 {
0117   if (m_proxyUserName != proxyUserName) {
0118     m_proxyUserName = proxyUserName;
0119     emit proxyUserNameChanged(m_proxyUserName);
0120   }
0121 }
0122 
0123 void NetworkConfig::setProxyPassword(const QString& proxyPassword)
0124 {
0125   if (m_proxyPassword != proxyPassword) {
0126     m_proxyPassword = proxyPassword;
0127     emit proxyPasswordChanged(m_proxyPassword);
0128   }
0129 }
0130 
0131 void NetworkConfig::setBrowser(const QString& browser)
0132 {
0133   if (m_browser != browser) {
0134     m_browser = browser;
0135     emit browserChanged(m_browser);
0136   }
0137 }
0138 
0139 void NetworkConfig::setUseProxy(bool useProxy)
0140 {
0141   if (m_useProxy != useProxy) {
0142     m_useProxy = useProxy;
0143     emit useProxyChanged(m_useProxy);
0144   }
0145 }
0146 
0147 void NetworkConfig::setUseProxyAuthentication(bool useProxyAuthentication)
0148 {
0149   if (m_useProxyAuthentication != useProxyAuthentication) {
0150     m_useProxyAuthentication = useProxyAuthentication;
0151     emit useProxyAuthenticationChanged(m_useProxyAuthentication);
0152   }
0153 }