~ubuntu-branches/debian/jessie/ugene/jessie

« back to all changes in this revision

Viewing changes to src/corelibs/U2Core/src/globals/NetworkConfiguration.cpp

  • Committer: Package Import Robot
  • Author(s): Steffen Moeller
  • Date: 2011-11-02 13:29:07 UTC
  • mfrom: (1.2.1) (3.1.11 natty)
  • Revision ID: package-import@ubuntu.com-20111102132907-o34gwnt0uj5g6hen
Tags: 1.9.8+repack-1
* First release to Debian
  - added README.Debian
  - increased policy version to 3.9.2
  - added URLs for version control system
* Added debug package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * UGENE - Integrated Bioinformatics Tools.
 
3
 * Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
 
4
 * http://ugene.unipro.ru
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
 * MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include <QtCore/QUrl>
 
23
#include <U2Core/AppContext.h>
 
24
#include <U2Core/Settings.h>
 
25
#include "NetworkConfiguration.h"
 
26
 
 
27
namespace U2
 
28
{
 
29
 
 
30
static const char * SETTINGS_HTTP_PROXY_HOST = "network_settings/http_proxy/host";
 
31
static const char * SETTINGS_HTTP_PROXY_PORT = "network_settings/http_proxy/port";
 
32
static const char * SETTINGS_HTTP_PROXY_USER = "network_settings/http_proxy/user";
 
33
static const char * SETTINGS_HTTP_PROXY_PASSWORD = "network_settings/http_proxy/password";
 
34
static const char * SETTINGS_HTTP_PROXY_ENABLED = "network_settings/http_proxy/enabled";
 
35
static const char * SETTINGS_PROXY_EXCEPTED_URLS = "network_settings/proxy_exc_urls";
 
36
static const char * SETTINGS_PROXY_EXCEPTED_URLS_ENABLED = "network_settings/proxy_exc_urls_enabled";
 
37
static const char * SETTINGS_SSL_PROTOCOL = "network_settings/ssl_protocol";
 
38
static const char * SETTINGS_REMOTE_REQUEST_TIMEOUT = "network_settings/remote_request/timeout";
 
39
 
 
40
const QString SslConfig::TLSV1 = "TlsV1";
 
41
const QString SslConfig::SSLV2 = "SslV2";
 
42
const QString SslConfig::SSLV3 = "SslV3";
 
43
 
 
44
const int RemoteRequestConfig::DEFAULT_REMOTE_REQUEST_TIMEOUT_SECONDS = 60;
 
45
 
 
46
 
 
47
NetworkConfiguration::NetworkConfiguration() {
 
48
    Settings * s = AppContext::getSettings();
 
49
    pc.excepted_addr_enabled = s->getValue( SETTINGS_PROXY_EXCEPTED_URLS_ENABLED ).toBool();
 
50
    pc.excepted_addr = s->getValue( SETTINGS_PROXY_EXCEPTED_URLS ).toStringList();
 
51
 
 
52
    QString httpProxyHost = s->getValue( SETTINGS_HTTP_PROXY_HOST ).toString();
 
53
    int httpProxyPort = s->getValue( SETTINGS_HTTP_PROXY_PORT ).toInt();
 
54
    
 
55
    if( !httpProxyHost.isEmpty() && httpProxyPort ) {
 
56
        QNetworkProxy httpProxy( QNetworkProxy::HttpProxy, httpProxyHost, httpProxyPort );
 
57
 
 
58
        QString user = s->getValue( SETTINGS_HTTP_PROXY_USER ).toString();
 
59
        if (!user.isEmpty())  {
 
60
            QByteArray passwordEncoded = s->getValue( SETTINGS_HTTP_PROXY_PASSWORD ).toByteArray();
 
61
            QString passwordDecoded = QByteArray::fromBase64(passwordEncoded);
 
62
            httpProxy.setUser(user);
 
63
            httpProxy.setPassword(passwordDecoded);
 
64
        }
 
65
 
 
66
        pc.proxyz[QNetworkProxy::HttpProxy] = httpProxy;
 
67
        pc.proxyz_usage[QNetworkProxy::HttpProxy] = s->getValue( SETTINGS_HTTP_PROXY_ENABLED ).toBool();
 
68
    }
 
69
 
 
70
    sslConfig.currentProtocol = s->getValue(SETTINGS_SSL_PROTOCOL, SslConfig::SSLV3).toString();
 
71
 
 
72
    rrConfig.remoteRequestTimeout = s->getValue(SETTINGS_REMOTE_REQUEST_TIMEOUT, RemoteRequestConfig::DEFAULT_REMOTE_REQUEST_TIMEOUT_SECONDS).toInt();
 
73
 
 
74
}
 
75
 
 
76
NetworkConfiguration::~NetworkConfiguration() {
 
77
    Settings * s = AppContext::getSettings();
 
78
    s->setValue( SETTINGS_PROXY_EXCEPTED_URLS_ENABLED, pc.excepted_addr_enabled );
 
79
    s->setValue( SETTINGS_PROXY_EXCEPTED_URLS, pc.excepted_addr );
 
80
    s->setValue( SETTINGS_SSL_PROTOCOL, sslConfig.currentProtocol );
 
81
    s->setValue( SETTINGS_REMOTE_REQUEST_TIMEOUT, rrConfig.remoteRequestTimeout );
 
82
 
 
83
    QNetworkProxy httpP = getProxy( QNetworkProxy::HttpProxy );
 
84
    
 
85
    if( !httpP.hostName().isEmpty() ) {
 
86
        s->setValue( SETTINGS_HTTP_PROXY_HOST, httpP.hostName() );
 
87
        s->setValue( SETTINGS_HTTP_PROXY_PORT, httpP.port() );
 
88
        s->setValue( SETTINGS_HTTP_PROXY_USER, httpP.user());
 
89
        s->setValue( SETTINGS_HTTP_PROXY_PASSWORD, httpP.password().toAscii().toBase64());
 
90
        s->setValue( SETTINGS_HTTP_PROXY_ENABLED, isProxyUsed(QNetworkProxy::HttpProxy) );
 
91
        
 
92
    }
 
93
}
 
94
 
 
95
int NetworkConfiguration::addProxy( const QNetworkProxy & p )
 
96
{
 
97
    int ret = !pc.proxyz.contains( p.type() );
 
98
    pc.proxyz.insert( p.type(), p );
 
99
    pc.proxyz_usage.insert( p.type(), false ); //needs explicit enabling
 
100
    return ret;
 
101
}
 
102
 
 
103
QNetworkProxy NetworkConfiguration::getProxyByUrl( const QUrl & url ) const
 
104
{
 
105
    Proxy_t prtype = url2type( url );
 
106
    if( pc.proxyz.contains( prtype ) ) {
 
107
        assert( pc.proxyz_usage.contains(prtype) );
 
108
        if( pc.proxyz_usage[prtype] ) {
 
109
            return ( pc.excepted_addr_enabled && pc.excepted_addr.contains( url.toString() ) ? 
 
110
                QNetworkProxy() : pc.proxyz[prtype] );
 
111
        }
 
112
    }
 
113
    return QNetworkProxy();
 
114
}
 
115
 
 
116
QNetworkProxy NetworkConfiguration::getProxy( Proxy_t prtype ) const {
 
117
    return (pc.proxyz.contains( prtype ) ? pc.proxyz[prtype] : QNetworkProxy() );
 
118
}
 
119
 
 
120
void NetworkConfiguration::removeProxy( Proxy_t prtype ) {
 
121
    pc.proxyz.remove( prtype );
 
122
}
 
123
 
 
124
bool NetworkConfiguration::isProxyUsed( Proxy_t prtype ) const {
 
125
    return pc.proxyz_usage.contains( prtype ) ? pc.proxyz_usage[prtype] : false;
 
126
}
 
127
 
 
128
void NetworkConfiguration::setExceptionsList( const QStringList & exc_addr ) {
 
129
    pc.excepted_addr = exc_addr;
 
130
}
 
131
 
 
132
void NetworkConfiguration::setProxyUsed( Proxy_t prtype, bool flag ) {
 
133
    if( pc.proxyz_usage.contains(prtype) ) {
 
134
        pc.proxyz_usage[prtype] = flag;
 
135
    }
 
136
}
 
137
 
 
138
Proxy_t NetworkConfiguration::url2type( const QUrl & url ) {
 
139
    if( "http" == url.scheme() || "https" == url.scheme() ) {
 
140
        return QNetworkProxy::HttpProxy;
 
141
    } 
 
142
    if( "ftp" == url.scheme() ) {
 
143
        return QNetworkProxy::FtpCachingProxy;
 
144
    }
 
145
    assert( false );
 
146
    return QNetworkProxy::NoProxy;
 
147
}
 
148
 
 
149
void NetworkConfiguration::copyFrom(const NetworkConfiguration& image) {
 
150
    pc = image.pc;
 
151
    sslConfig = image.sslConfig;
 
152
    rrConfig = image.rrConfig;
 
153
}
 
154
 
 
155
#ifndef QT_NO_OPENSSL
 
156
 
 
157
QSsl::SslProtocol NetworkConfiguration::getSslProtocol() const
 
158
{
 
159
    if (sslConfig.currentProtocol == SslConfig::SSLV2) {
 
160
        return QSsl::SslV2;
 
161
    } else if (sslConfig.currentProtocol == SslConfig::SSLV3) {
 
162
        return QSsl::SslV3;
 
163
    } else if (sslConfig.currentProtocol == SslConfig::TLSV1) {
 
164
        return QSsl::TlsV1;
 
165
    } else {
 
166
        return QSsl::SslV3;
 
167
    }
 
168
}
 
169
 
 
170
#endif
 
171
 
 
172
QString NetworkConfiguration::getSslProtocolName() const
 
173
{
 
174
    if (sslConfig.currentProtocol.isEmpty()) {
 
175
        return SslConfig::SSLV3;
 
176
    } else {
 
177
        return sslConfig.currentProtocol;
 
178
    }
 
179
}
 
180
 
 
181
void NetworkConfiguration::setSslProtocol( const QString& name )
 
182
{
 
183
    sslConfig.currentProtocol = name;    
 
184
}
 
185
 
 
186
void NetworkConfiguration::setRequestTimeout( const int timeout ) 
 
187
{
 
188
    rrConfig.remoteRequestTimeout = timeout;
 
189
}
 
190
 
 
191
 
 
192
} //namespace