diff --git a/src/config.cc b/src/config.cc index fe2b892d..6583fd37 100644 --- a/src/config.cc +++ b/src/config.cc @@ -36,6 +36,10 @@ namespace } } +ProxyServer::ProxyServer(): enabled( false ), type( Socks5 ), port( 3128 ) +{ +} + Preferences::Preferences(): enableTrayIcon( true ), startToTray( false ), @@ -186,7 +190,19 @@ Class load() throw( exError ) c.preferences.enableScanPopup = ( preferences.namedItem( "enableScanPopup" ).toElement().text() == "1" ); c.preferences.startWithScanPopupOn = ( preferences.namedItem( "startWithScanPopupOn" ).toElement().text() == "1" ); c.preferences.enableScanPopupModifiers = ( preferences.namedItem( "enableScanPopupModifiers" ).toElement().text() == "1" ); - c.preferences.scanPopupModifiers = ( preferences.namedItem( "scanPopupModifiers" ).toElement().text().toULong() ); + c.preferences.scanPopupModifiers = ( preferences.namedItem( "scanPopupModifiers" ).toElement().text().toULong() ); + + QDomNode proxy = preferences.namedItem( "proxyserver" ); + + if ( !proxy.isNull() ) + { + c.preferences.proxyServer.enabled = ( proxy.toElement().attribute( "enabled" ) == "1" ); + c.preferences.proxyServer.type = ( ProxyServer::Type ) proxy.namedItem( "type" ).toElement().text().toULong(); + c.preferences.proxyServer.host = proxy.namedItem( "host" ).toElement().text(); + c.preferences.proxyServer.port = proxy.namedItem( "port" ).toElement().text().toULong(); + c.preferences.proxyServer.user = proxy.namedItem( "user" ).toElement().text(); + c.preferences.proxyServer.password = proxy.namedItem( "password" ).toElement().text(); + } } c.lastMainGroup = root.namedItem( "lastMainGroup" ).toElement().text(); @@ -336,6 +352,35 @@ void save( Class const & c ) throw( exError ) opt = dd.createElement( "scanPopupModifiers" ); opt.appendChild( dd.createTextNode( QString::number( c.preferences.scanPopupModifiers ) ) ); preferences.appendChild( opt ); + + { + QDomElement proxy = dd.createElement( "proxyserver" ); + preferences.appendChild( proxy ); + + QDomAttr enabled = dd.createAttribute( "enabled" ); + enabled.setValue( c.preferences.proxyServer.enabled ? "1" : "0" ); + proxy.setAttributeNode( enabled ); + + opt = dd.createElement( "type" ); + opt.appendChild( dd.createTextNode( QString::number( c.preferences.proxyServer.type ) ) ); + proxy.appendChild( opt ); + + opt = dd.createElement( "host" ); + opt.appendChild( dd.createTextNode( c.preferences.proxyServer.host ) ); + proxy.appendChild( opt ); + + opt = dd.createElement( "port" ); + opt.appendChild( dd.createTextNode( QString::number( c.preferences.proxyServer.port ) ) ); + proxy.appendChild( opt ); + + opt = dd.createElement( "user" ); + opt.appendChild( dd.createTextNode( c.preferences.proxyServer.user ) ); + proxy.appendChild( opt ); + + opt = dd.createElement( "password" ); + opt.appendChild( dd.createTextNode( c.preferences.proxyServer.password ) ); + proxy.appendChild( opt ); + } } { diff --git a/src/config.hh b/src/config.hh index 86611c85..a99010da 100644 --- a/src/config.hh +++ b/src/config.hh @@ -50,6 +50,25 @@ struct Group /// All the groups typedef vector< Group > Groups; +/// Proxy server configuration +struct ProxyServer +{ + bool enabled; + + enum Type + { + Socks5 = 0, + HttpConnect, + HttpGet + } type; + + QString host; + unsigned port; + QString user, password; + + ProxyServer(); +}; + /// Various user preferences struct Preferences { @@ -61,6 +80,8 @@ struct Preferences bool enableScanPopupModifiers; unsigned long scanPopupModifiers; // Combination of KeyboardState::Modifier + ProxyServer proxyServer; + Preferences(); }; diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 28143048..1956066a 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -130,6 +130,8 @@ MainWindow::MainWindow(): ui.translateLine->installEventFilter( this ); ui.wordList->installEventFilter( this ); + applyProxySettings(); + makeDictionaries(); addNewTab(); @@ -276,6 +278,42 @@ void MainWindow::closeEvent( QCloseEvent * ev ) ev->accept(); } +void MainWindow::applyProxySettings() +{ + QNetworkProxy proxy; + + if ( cfg.preferences.proxyServer.enabled ) + { + switch( cfg.preferences.proxyServer.type ) + { + case Config::ProxyServer::Socks5: + proxy.setType( QNetworkProxy::Socks5Proxy ); + break; + case Config::ProxyServer::HttpConnect: + proxy.setType( QNetworkProxy::HttpProxy ); + break; + case Config::ProxyServer::HttpGet: + proxy.setType( QNetworkProxy::HttpCachingProxy ); + break; + default: + proxy.setType( QNetworkProxy::NoProxy ); + } + + proxy.setHostName( cfg.preferences.proxyServer.host ); + proxy.setPort( cfg.preferences.proxyServer.port ); + + if ( cfg.preferences.proxyServer.user.size() ) + proxy.setUser( cfg.preferences.proxyServer.user ); + + if ( cfg.preferences.proxyServer.password.size() ) + proxy.setPassword( cfg.preferences.proxyServer.password ); + } + else + proxy.setType( QNetworkProxy::NoProxy ); + + QNetworkProxy::setApplicationProxy( proxy ); +} + void MainWindow::makeDictionaries() { scanPopup.reset(); @@ -559,6 +597,7 @@ void MainWindow::editPreferences() enableScanPopup->setChecked( false ); updateTrayIcon(); + applyProxySettings(); makeScanPopup(); Config::save( cfg ); } diff --git a/src/mainwindow.hh b/src/mainwindow.hh index 7bb067cc..042a0f16 100644 --- a/src/mainwindow.hh +++ b/src/mainwindow.hh @@ -98,6 +98,7 @@ private: void closeEvent( QCloseEvent * ); + void applyProxySettings(); void makeDictionaries(); void updateStatusLine(); void updateGroupList(); diff --git a/src/preferences.cc b/src/preferences.cc index 2b4e0b93..28e8b287 100644 --- a/src/preferences.cc +++ b/src/preferences.cc @@ -64,6 +64,22 @@ Preferences::Preferences( QWidget * parent, Config::Preferences const & p ): ui.leftShift->hide(); ui.rightShift->hide(); #endif + + // Proxy server + + ui.useProxyServer->setChecked( p.proxyServer.enabled ); + + ui.proxyType->addItem( "SOCKS5" ); + ui.proxyType->addItem( "HTTP Transp." ); + ui.proxyType->addItem( "HTTP Caching" ); + + ui.proxyType->setCurrentIndex( p.proxyServer.type ); + + ui.proxyHost->setText( p.proxyServer.host ); + ui.proxyPort->setValue( p.proxyServer.port ); + + ui.proxyUser->setText( p.proxyServer.user ); + ui.proxyPassword->setText( p.proxyServer.password ); } Config::Preferences Preferences::getPreferences() @@ -88,6 +104,16 @@ Config::Preferences Preferences::getPreferences() p.scanPopupModifiers += ui.leftShift->isChecked() ? KeyboardState::LeftShift: 0; p.scanPopupModifiers += ui.rightShift->isChecked() ? KeyboardState::RightShift: 0; + p.proxyServer.enabled = ui.useProxyServer->isChecked(); + + p.proxyServer.type = ( Config::ProxyServer::Type ) ui.proxyType->currentIndex(); + + p.proxyServer.host = ui.proxyHost->text(); + p.proxyServer.port = ( unsigned ) ui.proxyPort->value(); + + p.proxyServer.user = ui.proxyUser->text(); + p.proxyServer.password = ui.proxyPassword->text(); + return p; } diff --git a/src/preferences.ui b/src/preferences.ui index 2ca5060d..93305797 100644 --- a/src/preferences.ui +++ b/src/preferences.ui @@ -7,7 +7,7 @@ 0 0 400 - 331 + 392 @@ -264,6 +264,94 @@ in the pressed state when the word selection changes. + + + + Enable if you wish to use a proxy server +for all program's network requests. + + + Use proxy server + + + true + + + false + + + + + + + + Type: + + + + + + + + + + Host: + + + + + + + + + + Port: + + + + + + + 65535 + + + 8080 + + + + + + + + + + + User: + + + + + + + + + + Password: + + + + + + + QLineEdit::Password + + + + + + + +