source: qutecom-coip/libs/coipmanager/src/connectmanager/ConnectManager.cpp @ 125:d648f4cb122f

Last change on this file since 125:d648f4cb122f was 125:d648f4cb122f, checked in by laurent, 3 years ago

wengo => qutecom

File size: 9.0 KB
Line 
1/*
2 * QuteCom, a voice over Internet phone
3 * Copyright (C) 2010 Mbdsys
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) 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#include <coipmanager/connectmanager/ConnectManager.h>
21
22#include <coipmanager/AccountMutator.h>
23#include <coipmanager/CoIpManager.h>
24#include <coipmanager/CoIpManagerConfig.h>
25#include <coipmanager/CoIpManagerPluginLoader.h>
26#include <coipmanager/connectmanager/IConnectManagerPlugin.h>
27#include <coipmanager/datamanager/UserProfileManager.h>
28
29#include <coipmanager_base/account/Account.h>
30#include <coipmanager_base/account/AccountList.h>
31#include <coipmanager_base/userprofile/UserProfile.h>
32
33#include <networkdiscovery/NetworkDiscovery.h>
34
35#include <util/Logger.h>
36#include <util/Path.h>
37#include <util/SafeConnect.h>
38#include <util/SafeDelete.h>
39
40static const std::string CONNECTMANAGER_PLUGIN_DIR = "coip-plugins/connectmanager/";
41static const std::string CONNECTSESSION_PLUGIN_NAME = "connect";
42
43ConnectManager::ConnectManager(CoIpManager & coIpManager)
44        : ICoIpManager(coIpManager) {
45
46        _httpProxySet = false;
47
48        typedef IConnectManagerPlugin * (*coip_plugin_init_function)(CoIpManager &);
49        std::list<void *> pluginList =
50                CoIpManagerPluginLoader::loadPlugins(_coIpManager.getCoIpManagerConfig().getCoIpPluginsPath(),
51                        CONNECTMANAGER_PLUGIN_DIR, CONNECTSESSION_PLUGIN_NAME);
52
53        for (std::list<void *>::const_iterator it = pluginList.begin(); it != pluginList.end(); ++it) {
54                coip_plugin_init_function coip_plugin_init = (coip_plugin_init_function) (*it);
55                if (coip_plugin_init) {
56                        IConnectManagerPlugin * plugin = coip_plugin_init(_coIpManager);
57                        _iCoIpManagerPluginList.push_back(plugin);
58                }
59        }
60}
61
62ConnectManager::~ConnectManager() {
63        uninitialize();
64}
65
66void ConnectManager::initialize() {
67        ICoIpManager::initialize();
68
69        getHttpProxy();
70}
71
72void ConnectManager::uninitialize() {
73        disconnectAllAccounts();
74
75        for (std::map<std::string, IConnectPlugin *>::iterator it = _iConnectMap.begin();
76                it != _iConnectMap.end();
77                ++it) {
78                OWSAFE_DELETE((*it).second);
79        }
80        _iConnectMap.clear();
81
82        _httpProxySet = false;
83
84        ICoIpManager::uninitialize();
85}
86
87void ConnectManager::getHttpProxy() {
88        if (_coIpManager.getCoIpManagerConfig().autoDetectHttpProxy()) {
89                NetworkProxy networkProxy;
90
91                networkProxy.setLogin(_coIpManager.getCoIpManagerConfig().getHttpProxyLogin());
92                networkProxy.setPassword(_coIpManager.getCoIpManagerConfig().getHttpProxyPassword());
93                networkProxy.setProxyAuthType(_coIpManager.getCoIpManagerConfig().getHttpProxyAuthType());
94
95                NetworkProxyDiscovery::NetworkProxyDiscoveryError error = 
96                        NetworkProxyDiscovery::getInstance().getNetworkProxy(networkProxy);
97               
98                if (error == NetworkProxyDiscovery::NetworkProxyDiscoveryErrorNeedsAuthentication) {
99                        proxyNeedsAuthenticationSignal(networkProxy);
100                        _httpProxySet = false;
101                        return;
102                }
103        }
104
105        _httpProxySet = true;
106}
107
108void ConnectManager::setHttpProxy(NetworkProxy networkProxy) {
109        _coIpManager.getCoIpManagerConfig().set(CoIpManagerConfig::HTTP_PROXY_SERVER_KEY,
110                networkProxy.getServer());
111        _coIpManager.getCoIpManagerConfig().set(CoIpManagerConfig::HTTP_PROXY_SERVER_PORT_KEY,
112                networkProxy.getServerPort());
113        _coIpManager.getCoIpManagerConfig().set(CoIpManagerConfig::HTTP_PROXY_LOGIN_KEY,
114                networkProxy.getLogin());
115        _coIpManager.getCoIpManagerConfig().set(CoIpManagerConfig::HTTP_PROXY_PASSWORD_KEY,
116                networkProxy.getPassword());
117        _coIpManager.getCoIpManagerConfig().set(CoIpManagerConfig::HTTP_PROXY_AUTH_TYPE_KEY, 
118                EnumProxyAuthType::toString(networkProxy.getProxyAuthType()));
119
120        NetworkDiscovery::getInstance().setNetworkProxy(networkProxy);
121
122        // Restart proxy detection.
123        getHttpProxy();
124}
125
126bool ConnectManager::checkValidity(const Account & account) {
127        //TODO
128        return true;
129}
130
131void ConnectManager::connect(const std::string & accountId) {
132        if (!_httpProxySet) {
133                LOG_ERROR("Proxy is not set. You cannot connect.");
134                return;
135        }
136
137        IConnectPlugin * iConnect = NULL;
138
139        // Look for an existing IConnectPlugin associated with this accountId
140        std::map<std::string, IConnectPlugin *>::const_iterator it = _iConnectMap.find(accountId);
141        if (it != _iConnectMap.end()) {
142                iConnect = (*it).second;
143        } else {
144                AccountList accountList = _coIpManager.getUserProfileManager().getCopyOfUserProfile().getAccountList();
145                Account * account = AccountListHelper::getCopyOfAccount(accountList, accountId);
146                if (account) {
147                        for (ICoIpManagerPluginList::const_iterator icmIt = _iCoIpManagerPluginList.begin();
148                                icmIt != _iCoIpManagerPluginList.end();
149                                ++icmIt) {
150                                IConnectManagerPlugin * iConnectManagerPlugin = static_cast<IConnectManagerPlugin *>(*icmIt);
151                                if (iConnectManagerPlugin->isProtocolSupported(account->getAccountType())) {
152                                        iConnect = iConnectManagerPlugin->createIConnectPlugin(*account);
153                                        _iConnectMap[accountId] = iConnect;
154
155                                        SAFE_CONNECT(iConnect,
156                                                SIGNAL(accountConnectionStateSignal(std::string, EnumConnectionState::ConnectionState)),
157                                                SLOT(accountConnectionStateSlot(std::string, EnumConnectionState::ConnectionState)));
158                                        SAFE_CONNECT(iConnect,
159                                                SIGNAL(accountConnectionErrorSignal(std::string, EnumConnectionError::ConnectionError)),
160                                                SLOT(accountConnectionErrorSlot(std::string, EnumConnectionError::ConnectionError)));
161                                        SAFE_CONNECT(iConnect,
162                                                SIGNAL(accountConnectionProgressSignal(std::string, unsigned, unsigned, std::string)),
163                                                SLOT(accountConnectionProgressSlot(std::string, unsigned, unsigned, std::string)));
164                                        break;
165                                }
166                        }
167                } else {
168                        LOG_FATAL("account not in AccountList, cannot connect");
169                }
170                ////
171        }
172
173        if (!iConnect) {
174                LOG_DEBUG("no IConnectPlugin implementation available for this account");
175                accountConnectionErrorSlot(accountId, EnumConnectionError::ConnectionErrorNoImplementation);
176                return;
177        }
178        ////
179
180        // Connecting
181        iConnect->connect();
182        ////
183}
184
185void ConnectManager::disconnect(const std::string & accountId) {
186        std::map<std::string, IConnectPlugin *>::const_iterator it = _iConnectMap.find(accountId);
187        if (it != _iConnectMap.end()) {
188                (*it).second->disconnect();
189        }
190}
191
192void ConnectManager::connectAllAccounts() {
193        AccountList accountList = _coIpManager.getUserProfileManager().getCopyOfUserProfile().getAccountList();
194
195        for (AccountList::const_iterator it = accountList.begin();
196                it != accountList.end();
197                ++it) {
198                connect((*it).getUUID());
199        }
200}
201
202void ConnectManager::disconnectAllAccounts() {
203        AccountList accountList = _coIpManager.getUserProfileManager().getCopyOfUserProfile().getAccountList();
204
205        for (AccountList::const_iterator it = accountList.begin();
206                it != accountList.end();
207                ++it) {
208                disconnect((*it).getUUID());
209        }
210}
211
212void ConnectManager::accountConnectionStateSlot(std::string accountId,
213        EnumConnectionState::ConnectionState state) {
214
215        QMutexLocker lock(_mutex);
216
217        AccountList accountList = _coIpManager.getUserProfileManager().getCopyOfUserProfile().getAccountList();
218        Account * account = AccountListHelper::getCopyOfAccount(accountList, accountId);
219        if (account) {
220                account->setConnectionState(state);
221
222                switch(state) {
223                case EnumConnectionState::ConnectionStateConnected:
224                        account->setPresenceState(EnumPresenceState::PresenceStateOnline);
225                        break;
226                default:
227                        account->setPresenceState(EnumPresenceState::PresenceStateOffline);
228                        break;
229                }
230
231                AccountMutator::emitAccountUpdatedSignal(*account, EnumUpdateSection::UpdateSectionPresenceState);
232                OWSAFE_DELETE(account);
233        }
234
235        LOG_DEBUG("account: " + accountId + " change its state - " + EnumConnectionState::toString(state));
236
237        accountConnectionStateSignal(accountId, state);
238}
239
240void ConnectManager::accountConnectionErrorSlot(std::string accountId,
241        EnumConnectionError::ConnectionError errorCode) {
242
243        QMutexLocker lock(_mutex);
244
245        LOG_DEBUG("account: " + accountId
246                + " had error during connection: " + EnumConnectionError::toString(errorCode));
247
248        accountConnectionErrorSignal(accountId, errorCode);
249}
250
251void ConnectManager::accountConnectionProgressSlot(std::string accountId,
252        unsigned currentStep, unsigned totalSteps, std::string infoMessage) {
253        QMutexLocker lock(_mutex);
254
255        LOG_DEBUG("connection progress of account " + accountId + ": step "
256                + String::fromNumber(currentStep) + "/" + String::fromNumber(totalSteps)
257                + ": " + infoMessage);
258
259        accountConnectionProgressSignal(accountId, currentStep, totalSteps, infoMessage);
260}
Note: See TracBrowser for help on using the repository browser.