1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-13 20:39:57 +01:00

Add an option to automatically detect the external IP(v4)

This commit is contained in:
Uwe L. Korn 2013-06-30 12:29:00 +02:00
parent dd4e2916c8
commit 7a25b55220
7 changed files with 114 additions and 14 deletions

View File

@ -782,6 +782,20 @@ TomahawkSettings::setSongChangeNotificationEnabled(bool enable)
}
bool
TomahawkSettings::autoDetectExternalIp() const
{
return value( "network/auto-detect-external-ip" ).toBool();
}
void
TomahawkSettings::setAutoDetectExternalIp( bool autoDetect )
{
setValue( "network/auto-detect-external-ip", autoDetect );
}
unsigned int
TomahawkSettings::volume() const
{

View File

@ -147,6 +147,9 @@ public:
bool songChangeNotificationEnabled() const; /// true by default
void setSongChangeNotificationEnabled( bool enable );
bool autoDetectExternalIp() const;
void setAutoDetectExternalIp( bool autoDetect );
QString externalHostname() const;
void setExternalHostname( const QString& externalHostname );

View File

@ -127,6 +127,8 @@ Servent::~Servent()
bool
Servent::startListening( QHostAddress ha, bool upnp, int port )
{
Q_D( Servent );
d_func()->externalAddresses = QList<QHostAddress>();
d_func()->port = port;
int defPort = TomahawkSettings::instance()->defaultPort();
@ -181,11 +183,20 @@ Servent::startListening( QHostAddress ha, bool upnp, int port )
switch ( mode )
{
case TomahawkSettings::Static:
d_func()->externalHostname = TomahawkSettings::instance()->externalHostname();
d_func()->externalPort = TomahawkSettings::instance()->externalPort();
d_func()->ready = true;
// All setup is made, were done.
emit ready();
d->externalPort = TomahawkSettings::instance()->externalPort();
if ( TomahawkSettings::instance()->autoDetectExternalIp() )
{
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( "http://toma.hk/?stat=1" ) ) );
connect( reply, SIGNAL( finished() ), SLOT( ipDetected() ) );
// Not emitting ready here as we are not done.
}
else
{
d->externalHostname = TomahawkSettings::instance()->externalHostname();
d->ready = true;
// All setup is made, were done.
emit ready();
}
break;
case TomahawkSettings::Lan:
@ -950,6 +961,39 @@ Servent::checkACLResult( const QString& nodeid, const QString& username, Tomahaw
d_func()->queuedForACLResult[username].remove( nodeid );
}
void
Servent::ipDetected()
{
Q_D( Servent );
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
if ( reply->error() == QNetworkReply::NoError )
{
QJson::Parser p;
bool ok;
const QVariantMap res = p.parse( reply, &ok ).toMap();
if ( !ok )
{
tLog() << Q_FUNC_INFO << "Failed parsing ip-autodetection response";
d->externalPort = -1;
}
else
{
QString externalIP = res.value( "ip" ).toString();
tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Found external IP:" << externalIP;
d->externalHostname = externalIP;
}
}
else
{
d->externalPort = -1;
tLog() << Q_FUNC_INFO << "ip-autodetection returned an error:" << reply->errorString();
}
d->ready = true;
emit ready();
}
void
Servent::reverseOfferRequest( ControlConnection* orig_conn, const QString& theirdbid, const QString& key, const QString& theirkey )

View File

@ -162,6 +162,7 @@ private slots:
void readyRead();
void socketError( QAbstractSocket::SocketError e );
void checkACLResult( const QString &nodeid, const QString &username, Tomahawk::ACLStatus::Type peerStatus );
void ipDetected();
Connection* claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer = QHostAddress::Any );

View File

@ -108,7 +108,7 @@ SettingsDialog::SettingsDialog(QObject *parent )
m_advancedWidgetUi->upnpRadioButton->setChecked( true );
m_advancedWidgetUi->staticHostNamePortLabel->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() );
m_advancedWidgetUi->staticHostName->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() );
m_advancedWidgetUi->staticHostName->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() && !s->autoDetectExternalIp() );
m_advancedWidgetUi->staticPort->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() );
m_advancedWidgetUi->staticHostNameLabel->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() );
m_advancedWidgetUi->staticPortLabel->setEnabled( m_advancedWidgetUi->staticIpRadioButton->isChecked() );
@ -173,6 +173,7 @@ SettingsDialog::SettingsDialog(QObject *parent )
m_advancedWidgetUi->staticHostName->setText( s->externalHostname() );
m_advancedWidgetUi->staticPort->setValue( s->externalPort() );
m_advancedWidgetUi->proxyButton->setVisible( true );
m_advancedWidgetUi->autoDetectIpCheckBox->setChecked( s->autoDetectExternalIp() );
m_collectionWidgetUi->checkBoxWatchForChanges->setChecked( s->watchForChanges() );
m_collectionWidgetUi->scannerTimeSpinBox->setValue( s->scannerTime() );
@ -248,6 +249,7 @@ SettingsDialog::SettingsDialog(QObject *parent )
connect( m_advancedWidgetUi->lanOnlyRadioButton, SIGNAL( toggled(bool) ), SLOT( toggleRemoteMode() ) );
connect( m_advancedWidgetUi->staticIpRadioButton, SIGNAL( toggled(bool) ), SLOT( toggleRemoteMode() ) );
connect( m_advancedWidgetUi->upnpRadioButton, SIGNAL( toggled(bool) ), SLOT( toggleRemoteMode() ) );
connect( m_advancedWidgetUi->autoDetectIpCheckBox, SIGNAL( toggled( bool ) ), SLOT( toggleAutoDetectIp( bool ) ) );
connect( m_advancedWidgetUi->enableProxyCheckBox, SIGNAL( toggled(bool) ), SLOT( toggleProxyEnabled() ) );
connect( m_advancedWidgetUi->enableProxyCheckBox, SIGNAL( toggled(bool) ), SLOT( requiresRestart() ) );
@ -268,6 +270,7 @@ SettingsDialog::saveSettings()
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->setAutoDetectExternalIp( m_advancedWidgetUi->autoDetectIpCheckBox->isChecked() );
s->setExternalHostname( m_advancedWidgetUi->staticHostName->text() );
s->setExternalPort( m_advancedWidgetUi->staticPort->value() );
@ -385,6 +388,13 @@ SettingsDialog::toggleProxyEnabled()
}
void
SettingsDialog::toggleAutoDetectIp( bool checked )
{
m_advancedWidgetUi->staticHostName->setEnabled( !checked );
}
void
SettingsDialog::updateScanOptionsView()
{

View File

@ -39,16 +39,16 @@ class QToolbarTabDialog;
namespace Ui
{
class ProxyDialog;
class ProxyDialog;
}
namespace Tomahawk
{
namespace Accounts
{
class AccountModel;
class Account;
class AccountFactory;
namespace Accounts
{
class AccountModel;
class Account;
class AccountFactory;
class Account;
class AccountModelFilterProxy;
}
@ -60,7 +60,7 @@ Q_OBJECT
public:
explicit ProxyDialog( QWidget* parent = 0 );
~ProxyDialog() {};
~ProxyDialog() {}
void saveSettings();
@ -85,6 +85,7 @@ private slots:
void toggleRemoteMode();
void toggleProxyEnabled();
void toggleAutoDetectIp( bool checked );
void showProxySettings();
void accountsFilterChanged( int );

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<width>473</width>
<height>475</height>
</rect>
</property>
@ -109,6 +109,33 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="autoDetectIpCheckBox">
<property name="text">
<string>Automatically detect external IP address</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>