1 /*
2  * %kadu copyright begin%
3  * Copyright 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
4  * Copyright 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
5  * %kadu copyright end%
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <QtXml/QDomElement>
22 
23 #include "configuration/configuration-api.h"
24 #include "configuration/configuration-manager.h"
25 #include "configuration/configuration.h"
26 #include "configuration/deprecated-configuration-api.h"
27 #include "network/proxy/network-proxy-storage.h"
28 #include "storage/storage-point.h"
29 
30 #include "network-proxy-manager.h"
31 
NetworkProxyManager(QObject * parent)32 NetworkProxyManager::NetworkProxyManager(QObject *parent) :
33 		SimpleManager<NetworkProxy>{parent}
34 {
35 }
36 
~NetworkProxyManager()37 NetworkProxyManager::~NetworkProxyManager()
38 {
39 }
40 
setConfigurationManager(ConfigurationManager * configurationManager)41 void NetworkProxyManager::setConfigurationManager(ConfigurationManager *configurationManager)
42 {
43 	m_configurationManager = configurationManager;
44 }
45 
setConfiguration(Configuration * configuration)46 void NetworkProxyManager::setConfiguration(Configuration *configuration)
47 {
48 	m_configuration = configuration;
49 }
50 
setNetworkProxyStorage(NetworkProxyStorage * networkProxyStorage)51 void NetworkProxyManager::setNetworkProxyStorage(NetworkProxyStorage *networkProxyStorage)
52 {
53 	m_networkProxyStorage = networkProxyStorage;
54 }
55 
init()56 void NetworkProxyManager::init()
57 {
58 	m_configurationManager->registerStorableObject(this);
59 	configurationUpdated();
60 }
61 
done()62 void NetworkProxyManager::done()
63 {
64 	m_configurationManager->unregisterStorableObject(this);
65 }
66 
load()67 void NetworkProxyManager::load()
68 {
69 	QMutexLocker locker(&mutex());
70 
71 	SimpleManager<NetworkProxy>::load();
72 }
73 
store()74 void NetworkProxyManager::store()
75 {
76 	QMutexLocker locker(&mutex());
77 
78 	SimpleManager<NetworkProxy>::store();
79 }
80 
loadStubFromStorage(const std::shared_ptr<StoragePoint> & storagePoint)81 NetworkProxy NetworkProxyManager::loadStubFromStorage(const std::shared_ptr<StoragePoint> &storagePoint)
82 {
83 	return m_networkProxyStorage->loadStubFromStorage(storagePoint);
84 }
85 
configurationUpdated()86 void NetworkProxyManager::configurationUpdated()
87 {
88 	DefaultProxy = byUuid(m_configuration->deprecatedApi()->readEntry("Network", "DefaultProxy"));
89 }
90 
setDefaultProxy(const NetworkProxy & proxy)91 void NetworkProxyManager::setDefaultProxy(const NetworkProxy &proxy)
92 {
93 	DefaultProxy = proxy;
94 	m_configuration->deprecatedApi()->writeEntry("Network", "DefaultProxy", DefaultProxy.uuid().toString());
95 }
96 
defaultProxy()97 const NetworkProxy & NetworkProxyManager::defaultProxy()
98 {
99 	return DefaultProxy;
100 }
101 
byConfiguration(const QString & address,int port,const QString & user,const QString & password,NotFoundAction action)102 NetworkProxy NetworkProxyManager::byConfiguration(const QString &address, int port,
103                                                   const QString &user, const QString &password, NotFoundAction action)
104 {
105 	foreach (const NetworkProxy &networkProxy, items())
106 	{
107 		if (networkProxy.address() == address &&
108 		       networkProxy.port() == port &&
109 		       networkProxy.user() == user &&
110 		       networkProxy.password() == password)
111 			return  networkProxy;
112 	}
113 
114 	if (ActionReturnNull == action)
115 		return NetworkProxy::null;
116 
117 	auto networkProxy = m_networkProxyStorage->create();
118 	networkProxy.setAddress(address);
119 	networkProxy.setPort(port);
120 	networkProxy.setUser(user);
121 	networkProxy.setPassword(password);
122 
123 	if (ActionCreateAndAdd == action)
124 		addItem(networkProxy);
125 
126 	return networkProxy;
127 }
128 
networkProxyDataUpdated()129 void NetworkProxyManager::networkProxyDataUpdated()
130 {
131 	NetworkProxy networkProxy(sender());
132 	if (!networkProxy.isNull())
133 		emit networkProxyUpdated(networkProxy);
134 }
135 
itemAboutToBeAdded(NetworkProxy item)136 void NetworkProxyManager::itemAboutToBeAdded(NetworkProxy item)
137 {
138 	connect(item, SIGNAL(updated()), this, SLOT(networkProxyDataUpdated()));
139 	emit networkProxyAboutToBeAdded(item);
140 }
141 
itemAdded(NetworkProxy item)142 void NetworkProxyManager::itemAdded(NetworkProxy item)
143 {
144 	emit networkProxyAdded(item);
145 }
146 
itemAboutToBeRemoved(NetworkProxy item)147 void NetworkProxyManager::itemAboutToBeRemoved(NetworkProxy item)
148 {
149 	emit networkProxyAboutToBeRemoved(item);
150 }
151 
itemRemoved(NetworkProxy item)152 void NetworkProxyManager::itemRemoved(NetworkProxy item)
153 {
154 	disconnect(item, 0, this, 0);
155 	emit networkProxyRemoved(item);
156 }
157 
158 #include "moc_network-proxy-manager.cpp"
159