diff --git a/src/libtomahawk/TomahawkSettings.cpp b/src/libtomahawk/TomahawkSettings.cpp index 5881800b3..9e1605e85 100644 --- a/src/libtomahawk/TomahawkSettings.cpp +++ b/src/libtomahawk/TomahawkSettings.cpp @@ -1286,20 +1286,20 @@ TomahawkSettings::removeAccount( const QString& accountId ) } -TomahawkSettings::ExternalAddressMode +Tomahawk::Network::ExternalAddress::Mode TomahawkSettings::externalAddressMode() { if ( value( "network/prefer-static-host-and-port", false ).toBool() ) { remove( "network/prefer-static-host-and-port" ); - setValue( "network/external-address-mode", TomahawkSettings::Static ); + setValue( "network/external-address-mode", Tomahawk::Network::ExternalAddress::Static ); } - return (TomahawkSettings::ExternalAddressMode) value( "network/external-address-mode", TomahawkSettings::Upnp ).toInt(); + return (Tomahawk::Network::ExternalAddress::Mode) value( "network/external-address-mode", Tomahawk::Network::ExternalAddress::Upnp ).toInt(); } void -TomahawkSettings::setExternalAddressMode( ExternalAddressMode externalAddressMode ) +TomahawkSettings::setExternalAddressMode( Tomahawk::Network::ExternalAddress::Mode externalAddressMode ) { setValue( "network/external-address-mode", externalAddressMode ); } diff --git a/src/libtomahawk/TomahawkSettings.h b/src/libtomahawk/TomahawkSettings.h index 1eebb9987..6edbcad6e 100644 --- a/src/libtomahawk/TomahawkSettings.h +++ b/src/libtomahawk/TomahawkSettings.h @@ -21,11 +21,13 @@ #ifndef TOMAHAWK_SETTINGS_H #define TOMAHAWK_SETTINGS_H +#include "network/Enums.h" + #include "DllMacro.h" #include "Typedefs.h" #include -#include +#include #include #define TOMAHAWK_SETTINGS_VERSION 15 @@ -134,9 +136,8 @@ public: QString bookmarkPlaylist() const; /// Network settings - enum ExternalAddressMode { Lan, Upnp, Static }; - ExternalAddressMode externalAddressMode(); - void setExternalAddressMode( ExternalAddressMode externalAddressMode ); + Tomahawk::Network::ExternalAddress::Mode externalAddressMode(); + void setExternalAddressMode( Tomahawk::Network::ExternalAddress::Mode externalAddressMode ); bool httpEnabled() const; /// true by default void setHttpEnabled( bool enable ); diff --git a/src/libtomahawk/network/Enums.h b/src/libtomahawk/network/Enums.h new file mode 100644 index 000000000..bf9bf5164 --- /dev/null +++ b/src/libtomahawk/network/Enums.h @@ -0,0 +1,39 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef ENUMS_H +#define ENUMS_H + +namespace Tomahawk +{ + +namespace Network +{ + +namespace ExternalAddress +{ + +enum Mode { Lan, Upnp, Static }; + +} + +} + +} + +#endif // ENUMS_H diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp index b2e476e9e..4702ce7c9 100644 --- a/src/libtomahawk/network/Servent.cpp +++ b/src/libtomahawk/network/Servent.cpp @@ -44,7 +44,6 @@ #include "Source.h" #include "SourceList.h" #include "StreamConnection.h" -#include "TomahawkSettings.h" #include #include @@ -125,29 +124,28 @@ Servent::~Servent() bool -Servent::startListening( QHostAddress ha, bool upnp, int port ) +Servent::startListening( QHostAddress ha, bool upnp, int port, Tomahawk::Network::ExternalAddress::Mode mode, int defaultPort, bool autoDetectExternalIp, const QString& externalHost, int externalPort ) { Q_D( Servent ); d_func()->externalAddresses = QList(); d_func()->port = port; - int defPort = TomahawkSettings::instance()->defaultPort(); // Listen on both the selected port and, if not the same, the default port -- the latter sometimes necessary for zeroconf // TODO: only listen on both when zeroconf sip is enabled // TODO: use a real zeroconf system instead of a simple UDP broadcast? if ( !listen( ha, d_func()->port ) ) { - if ( d_func()->port != defPort ) + if ( d_func()->port != defaultPort ) { - if ( !listen( ha, defPort ) ) + if ( !listen( ha, defaultPort ) ) { - tLog() << Q_FUNC_INFO << "Failed to listen on both port" << d_func()->port << "and port" << defPort; + tLog() << Q_FUNC_INFO << "Failed to listen on both port" << d_func()->port << "and port" << defaultPort; tLog() << Q_FUNC_INFO << "Error string is:" << errorString(); return false; } else - d_func()->port = defPort; + d_func()->port = defaultPort; } } @@ -176,15 +174,14 @@ Servent::startListening( QHostAddress ha, bool upnp, int port ) } // If we only accept connections via localhost, we'll announce nothing. - TomahawkSettings::ExternalAddressMode mode = TomahawkSettings::instance()->externalAddressMode(); tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Servent listening on port" << d_func()->port << "- servent thread:" << thread() << "- address mode:" << (int)( mode ); switch ( mode ) { - case TomahawkSettings::Static: - d->externalPort = TomahawkSettings::instance()->externalPort(); - if ( TomahawkSettings::instance()->autoDetectExternalIp() ) + case Tomahawk::Network::ExternalAddress::Static: + d->externalPort = externalPort; + if ( autoDetectExternalIp ) { QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( "http://toma.hk/?stat=1" ) ) ); connect( reply, SIGNAL( finished() ), SLOT( ipDetected() ) ); @@ -192,20 +189,20 @@ Servent::startListening( QHostAddress ha, bool upnp, int port ) } else { - d->externalHostname = TomahawkSettings::instance()->externalHostname(); + d->externalHostname = externalHost; d->ready = true; // All setup is made, were done. emit ready(); } break; - case TomahawkSettings::Lan: + case Tomahawk::Network::ExternalAddress::Lan: // Nothing has to be done here. d_func()->ready = true; emit ready(); break; - case TomahawkSettings::Upnp: + case Tomahawk::Network::ExternalAddress::Upnp: if ( upnp ) { // upnp could be turned of on the cli with --noupnp diff --git a/src/libtomahawk/network/Servent.h b/src/libtomahawk/network/Servent.h index 018b2f1e8..229293385 100644 --- a/src/libtomahawk/network/Servent.h +++ b/src/libtomahawk/network/Servent.h @@ -25,15 +25,17 @@ // time before new connection terminate if it could not be established #define CONNECT_TIMEOUT 10000 +#include "network/Enums.h" + +#include "DllMacro.h" +#include "Typedefs.h" + #include #include #include #include #include -#include "DllMacro.h" -#include "Typedefs.h" - class Connection; class Connector; class ControlConnection; @@ -67,7 +69,7 @@ public: explicit Servent( QObject* parent = 0 ); virtual ~Servent(); - bool startListening( QHostAddress ha, bool upnp, int port ); + bool startListening( QHostAddress ha, bool upnp, int port, Tomahawk::Network::ExternalAddress::Mode mode, int defaultPort, bool autoDetectExternalIp = false, const QString& externalHost = "", int externalPort = -1 ); // creates new token that allows a controlconnection to be set up QString createConnectionKey( const QString& name = "", const QString &nodeid = "", const QString &key = "", bool onceOnly = true ); diff --git a/src/tomahawk/SettingsDialog.cpp b/src/tomahawk/SettingsDialog.cpp index afb793632..bfc0775ab 100644 --- a/src/tomahawk/SettingsDialog.cpp +++ b/src/tomahawk/SettingsDialog.cpp @@ -99,10 +99,10 @@ SettingsDialog::SettingsDialog(QObject *parent ) #endif //Network settings - TomahawkSettings::ExternalAddressMode mode = TomahawkSettings::instance()->externalAddressMode(); - if ( mode == TomahawkSettings::Lan ) + Tomahawk::Network::ExternalAddress::Mode mode = TomahawkSettings::instance()->externalAddressMode(); + if ( mode == Tomahawk::Network::ExternalAddress::Lan ) m_advancedWidgetUi->lanOnlyRadioButton->setChecked( true ); - else if ( mode == TomahawkSettings::Static ) + else if ( mode == Tomahawk::Network::ExternalAddress::Static ) m_advancedWidgetUi->staticIpRadioButton->setChecked( true ); else m_advancedWidgetUi->upnpRadioButton->setChecked( true ); @@ -269,7 +269,7 @@ SettingsDialog::saveSettings() s->setHttpEnabled( m_advancedWidgetUi->checkBoxHttp->checkState() == Qt::Checked ); s->setSongChangeNotificationEnabled( m_advancedWidgetUi->checkBoxSongChangeNotifications->checkState() == Qt::Checked ); s->setProxyType( m_advancedWidgetUi->enableProxyCheckBox->isChecked() ? QNetworkProxy::Socks5Proxy : QNetworkProxy::NoProxy ); - s->setExternalAddressMode( m_advancedWidgetUi->upnpRadioButton->isChecked() ? TomahawkSettings::Upnp : ( m_advancedWidgetUi->lanOnlyRadioButton->isChecked() ? TomahawkSettings::Lan : TomahawkSettings::Static ) ); + s->setExternalAddressMode( m_advancedWidgetUi->upnpRadioButton->isChecked() ? Tomahawk::Network::ExternalAddress::Upnp : ( m_advancedWidgetUi->lanOnlyRadioButton->isChecked() ? Tomahawk::Network::ExternalAddress::Lan : Tomahawk::Network::ExternalAddress::Static ) ); s->setAutoDetectExternalIp( m_advancedWidgetUi->autoDetectIpCheckBox->isChecked() ); s->setExternalHostname( m_advancedWidgetUi->staticHostName->text() ); diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index 53cdaf962..6a0ce86fd 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -545,10 +545,15 @@ TomahawkApp::initServent() bool upnp = !arguments().contains( "--noupnp" ); int port = TomahawkSettings::instance()->externalPort(); connect( Servent::instance(), SIGNAL( ipDetectionFailed( QNetworkReply::NetworkError, QString ) ), this, SLOT( ipDetectionFailed( QNetworkReply::NetworkError, QString ) ) ); + int defaultPort = TomahawkSettings::instance()->defaultPort(); + Tomahawk::Network::ExternalAddress::Mode mode = TomahawkSettings::instance()->externalAddressMode(); + const QString externalHostname = TomahawkSettings::instance()->externalHostname(); + int externalPort = TomahawkSettings::instance()->externalPort(); + bool autodetectIp = TomahawkSettings::instance()->autoDetectExternalIp(); #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) - if ( !Servent::instance()->startListening( QHostAddress( QHostAddress::Any ), upnp, port ) ) + if ( !Servent::instance()->startListening( QHostAddress( QHostAddress::Any ), upnp, port, mode, defaultPort, autodetectIp, externalHostname, externalPort ) ) #else - if ( !Servent::instance()->startListening( QHostAddress( QHostAddress::AnyIPv6 ), upnp, port ) ) + if ( !Servent::instance()->startListening( QHostAddress( QHostAddress::AnyIPv6 ), upnp, port, mode, defaultPort, autodetectIp, externalHostname, externalPort ) ) #endif { tLog() << "Failed to start listening with servent";