1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-29 08:40:40 +02:00

Decouple Servent from TomahawkSettings

This commit is contained in:
Uwe L. Korn
2013-07-06 18:13:38 +02:00
parent e480f18b68
commit 0c81889010
7 changed files with 76 additions and 32 deletions

View File

@@ -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 );
}

View File

@@ -21,11 +21,13 @@
#ifndef TOMAHAWK_SETTINGS_H
#define TOMAHAWK_SETTINGS_H
#include "network/Enums.h"
#include "DllMacro.h"
#include "Typedefs.h"
#include <QSettings>
#include <QtNetwork/QNetworkProxy>
#include <QNetworkProxy>
#include <QStringList>
#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 );

View File

@@ -0,0 +1,39 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef ENUMS_H
#define ENUMS_H
namespace Tomahawk
{
namespace Network
{
namespace ExternalAddress
{
enum Mode { Lan, Upnp, Static };
}
}
}
#endif // ENUMS_H

View File

@@ -44,7 +44,6 @@
#include "Source.h"
#include "SourceList.h"
#include "StreamConnection.h"
#include "TomahawkSettings.h"
#include <QCoreApplication>
#include <QMutexLocker>
@@ -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<QHostAddress>();
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

View File

@@ -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 <QObject>
#include <QMap>
#include <QNetworkReply>
#include <QSharedPointer>
#include <QTcpServer>
#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 );

View File

@@ -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() );

View File

@@ -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";