diff --git a/data/js/tomahawk.js b/data/js/tomahawk.js index b9c016fd5..80e9168ac 100644 --- a/data/js/tomahawk.js +++ b/data/js/tomahawk.js @@ -196,6 +196,13 @@ var TomahawkResolver = { }, collection: function () { return {}; + }, + _testConfig: function (config) { + return Promise.resolve(this.testConfig(config)).then(function() { + return { result: Tomahawk.ConfigTestResultType.Success }; + }); + }, + testConfig: function () { } }; @@ -638,3 +645,15 @@ Tomahawk.PluginManager = { }); } }; + + +Tomahawk.ConfigTestResultType = { + Other: 0, + Success: 1, + Logout: 2, + CommunicationError: 3, + InvalidCredentials: 4, + InvalidAccount: 5, + PlayingElsewhere: 6, + AccountExpired: 7 +}; diff --git a/src/libtomahawk/accounts/Account.cpp b/src/libtomahawk/accounts/Account.cpp index 3377f7885..9b2ee15be 100644 --- a/src/libtomahawk/accounts/Account.cpp +++ b/src/libtomahawk/accounts/Account.cpp @@ -150,6 +150,13 @@ Account::removeFromConfig() } +void +Account::testConfig() +{ + emit configTestResult( ConfigTestResultSuccess ); +} + + void Account::setTypes( AccountTypes types ) { diff --git a/src/libtomahawk/accounts/Account.h b/src/libtomahawk/accounts/Account.h index e4d09d714..502a97b81 100644 --- a/src/libtomahawk/accounts/Account.h +++ b/src/libtomahawk/accounts/Account.h @@ -54,6 +54,19 @@ enum AccountType StatusPushType = 0x08 }; +// ATTENTION: keep in sync with tomahawk.js +enum ConfigTestResultType +{ + ConfigTestResultOther = 0, + ConfigTestResultSuccess = 1, + ConfigTestResultLogout = 2, + ConfigTestResultCommunicationError = 3, + ConfigTestResultInvalidCredentials = 4, + ConfigTestResultInvalidAccount = 5, + ConfigTestResultPlayingElsewhere = 6, + ConfigTestResultAccountExpired = 7 +}; + DLLEXPORT QString accountTypeToString( AccountType type ); Q_DECLARE_FLAGS( AccountTypes, AccountType ) @@ -143,6 +156,8 @@ public: */ virtual void removeFromConfig(); + virtual void testConfig(); + public slots: virtual void authenticate() = 0; virtual void deauthenticate() = 0; @@ -152,6 +167,7 @@ signals: void connectionStateChanged( Tomahawk::Accounts::Account::ConnectionState state ); void configurationChanged(); + void configTestResult( Tomahawk::Accounts::ConfigTestResultType ); protected: virtual void loadFromConfig( const QString &accountId ); diff --git a/src/libtomahawk/accounts/DelegateConfigWrapper.cpp b/src/libtomahawk/accounts/DelegateConfigWrapper.cpp index 18be51ad6..84ba787d7 100644 --- a/src/libtomahawk/accounts/DelegateConfigWrapper.cpp +++ b/src/libtomahawk/accounts/DelegateConfigWrapper.cpp @@ -19,6 +19,7 @@ #include "Account.h" #include "AccountConfigWidget.h" +#include "../utils/Logger.h" #include @@ -74,6 +75,8 @@ DelegateConfigWrapper::DelegateConfigWrapper( Tomahawk::Accounts::Account* accou if ( m_widget->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 ) connect( m_widget, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) ); + + connect( m_account, SIGNAL( configTestResult( Tomahawk::Accounts::ConfigTestResultType ) ), SLOT( onConfigTestResult( Tomahawk::Accounts::ConfigTestResultType ) ) ); } @@ -101,10 +104,9 @@ DelegateConfigWrapper::closed( QAbstractButton* b ) if ( buttons->standardButton( b ) == QDialogButtonBox::Help ) return; - int doneCode = 0; - if ( buttons->standardButton( b ) == QDialogButtonBox::Ok ) { + // TODO: probably should be hidden behind testConfig() in cpp accounts m_widget->resetErrors(); m_widget->checkForErrors(); if( !m_widget->settingsValid() ) @@ -117,7 +119,7 @@ DelegateConfigWrapper::closed( QAbstractButton* b ) return; } - doneCode = QDialog::Accepted; + m_account->testConfig(); } else if ( b == m_deleteButton ) { @@ -128,15 +130,8 @@ DelegateConfigWrapper::closed( QAbstractButton* b ) } else { - doneCode = QDialog::Rejected; + closeDialog( QDialog::Rejected ); } - - // let the config widget live to see another day - layout()->removeWidget( m_widget ); - m_widget->setParent( 0 ); - m_widget->setVisible( false ); - - done( doneCode ); } @@ -179,3 +174,31 @@ DelegateConfigWrapper::aboutClicked( bool ) } + +void +DelegateConfigWrapper::onConfigTestResult( Tomahawk::Accounts::ConfigTestResultType result ) +{ + tLog() << Q_FUNC_INFO << result; + + if( result == Tomahawk::Accounts::ConfigTestResultSuccess ) + { + closeDialog( QDialog::Accepted ); + } + else + { + // TODO: make this nicer + QMessageBox::critical( this, tr( "Error" ), tr( "Your config is invalid and can't be saved." ) ); + } +} + + +void +DelegateConfigWrapper::closeDialog( QDialog::DialogCode code ) +{ + // let the config widget live to see another day + layout()->removeWidget( m_widget ); + m_widget->setParent( 0 ); + m_widget->setVisible( false ); + + done( code ); +} diff --git a/src/libtomahawk/accounts/DelegateConfigWrapper.h b/src/libtomahawk/accounts/DelegateConfigWrapper.h index 7dae3a274..e24b0f60d 100644 --- a/src/libtomahawk/accounts/DelegateConfigWrapper.h +++ b/src/libtomahawk/accounts/DelegateConfigWrapper.h @@ -54,8 +54,11 @@ signals: private slots: void aboutClicked( bool ); + void onConfigTestResult( Tomahawk::Accounts::ConfigTestResultType ); private: + void closeDialog( QDialog::DialogCode code ); + Tomahawk::Accounts::Account* m_account; AccountConfigWidget* m_widget; QWidget* m_aboutW; diff --git a/src/libtomahawk/accounts/ResolverAccount.cpp b/src/libtomahawk/accounts/ResolverAccount.cpp index 4b83949f2..5865ee410 100644 --- a/src/libtomahawk/accounts/ResolverAccount.cpp +++ b/src/libtomahawk/accounts/ResolverAccount.cpp @@ -41,6 +41,12 @@ #include "TomahawkSettings.h" #include "TomahawkVersion.h" + +// HACK +#include "../resolvers/JSResolver.h" +#include "resolvers/ScriptObject.h" +#include "resolvers/ScriptJob.h" + #include #include #include @@ -511,6 +517,44 @@ ResolverAccount::removeBundle() } +void ResolverAccount::testConfig() +{ + // HACK: move to JSAccount once we have that properly + JSResolver* resolver = qobject_cast< Tomahawk::JSResolver* >( m_resolver ); + if ( resolver ) + { + QVariantMap data = resolver->loadDataFromWidgets(); + tLog() << "config data: " << data; + ScriptJob* job = resolver->scriptObject()->invoke( "_testConfig", data ); + connect( job, SIGNAL( done( QVariantMap ) ), SLOT( onTestConfig( QVariantMap ) ) ); + job->start(); + } + else + { + emit configTestResult( Accounts::ConfigTestResultSuccess ); + } +} + + +void +ResolverAccount::onTestConfig( const QVariantMap& result ) +{ + tLog() << Q_FUNC_INFO << result; + + int resultCode = result[ "result" ].toInt(); + if ( resultCode == 1 ) + { + emit configTestResult( Accounts::ConfigTestResultSuccess ); + } + else + { + emit configTestResult( Accounts::ConfigTestResultOther ); + } + + sender()->deleteLater(); +} + + /// AtticaResolverAccount AtticaResolverAccount::AtticaResolverAccount( const QString& accountId ) diff --git a/src/libtomahawk/accounts/ResolverAccount.h b/src/libtomahawk/accounts/ResolverAccount.h index f54621777..8dc79ae86 100644 --- a/src/libtomahawk/accounts/ResolverAccount.h +++ b/src/libtomahawk/accounts/ResolverAccount.h @@ -99,8 +99,11 @@ public: void removeBundle(); + void testConfig() override; + private slots: void resolverChanged(); + void onTestConfig( const QVariantMap& result ); protected: // Created by factory, when user installs a new resolver diff --git a/src/libtomahawk/resolvers/JSResolver.h b/src/libtomahawk/resolvers/JSResolver.h index 5c99eec6d..856d14215 100644 --- a/src/libtomahawk/resolvers/JSResolver.h +++ b/src/libtomahawk/resolvers/JSResolver.h @@ -71,6 +71,7 @@ public: bool canParseUrl( const QString& url, UrlType type ) override; + QVariantMap loadDataFromWidgets(); public slots: void resolve( const Tomahawk::query_ptr& query ) override; @@ -99,7 +100,6 @@ private: void loadUi(); void setWidgetData( const QVariant& value, QWidget* widget, const QString& property ); QVariant widgetData( QWidget* widget, const QString& property ); - QVariantMap loadDataFromWidgets(); void fillDataInWidgets( const QVariantMap& data ); void onCapabilitiesChanged( Capabilities capabilities ); void loadCollections();