diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp index 6ac2b661b..e8734f8d9 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.cpp @@ -543,6 +543,13 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg creds[ "highQuality" ] = msg.value( "highQuality" ); setCredentials( creds ); + const bool loggedIn = msg.value( "loggedIn", false ).toBool(); + if ( loggedIn ) + { + configurationWidget(); + m_configWidget.data()->loginResponse( true, QString(), creds[ "username" ].toString() ); + } + qDebug() << "Set creds:" << creds.value( "username" ) << creds.value( "password" ) << msg.value( "username" ) << msg.value( "password" ); QVariantHash config = configuration(); @@ -714,7 +721,7 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg if ( m_configWidget.data() ) { const QString message = msg.value( "message" ).toString(); - m_configWidget.data()->loginResponse( success, message ); + m_configWidget.data()->loginResponse( success, message, creds[ "username" ].toString() ); } } else if ( msgType == "playlistDeleted" ) @@ -773,6 +780,7 @@ SpotifyAccount::configurationWidget() { m_configWidget = QWeakPointer< SpotifyAccountConfig >( new SpotifyAccountConfig( this ) ); connect( m_configWidget.data(), SIGNAL( login( QString,QString ) ), this, SLOT( login( QString,QString ) ) ); + connect( m_configWidget.data(), SIGNAL( logout() ), this, SLOT( logout() ) ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); } @@ -855,7 +863,6 @@ SpotifyAccount::saveConfig() void SpotifyAccount::login( const QString& username, const QString& password ) { - // Send the result to the resolver QVariantMap msg; msg[ "_msgtype" ] = "login"; msg[ "username" ] = username; @@ -867,6 +874,15 @@ SpotifyAccount::login( const QString& username, const QString& password ) } +void +SpotifyAccount::logout() +{ + QVariantMap msg; + msg[ "_msgtype" ] = "logout"; + m_spotifyResolver.data()->sendMessage( msg ); +} + + void SpotifyAccount::startPlaylistSync( SpotifyPlaylistInfo* playlist ) { diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccount.h b/src/libtomahawk/accounts/spotify/SpotifyAccount.h index 94e3f547f..d734f6b66 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccount.h +++ b/src/libtomahawk/accounts/spotify/SpotifyAccount.h @@ -123,6 +123,8 @@ private slots: void resolverMessage( const QString& msgType, const QVariantMap& msg ); void login( const QString& username, const QString& password ); + void logout(); + // SpotifyResolver message handlers, all take msgtype, msg as argument // void ( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); void startPlaylistSyncWithPlaylist( const QString& msgType, const QVariantMap& msg, const QVariant& extraData ); diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.cpp b/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.cpp index 5be0d52c5..7bc36806b 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.cpp @@ -25,6 +25,7 @@ #include #include #include +#include using namespace Tomahawk; using namespace Accounts; @@ -32,9 +33,11 @@ using namespace Accounts; SpotifyAccountConfig::SpotifyAccountConfig( SpotifyAccount *account ) : QWidget( 0 ) , m_ui( new Ui::SpotifyConfig ) + , m_loggedInUser( 0 ) , m_account( account ) , m_playlistsLoading( 0 ) , m_loggedInManually( false ) + , m_isLoggedIn( false ) { m_ui->setupUi( this ); @@ -134,28 +137,42 @@ SpotifyAccountConfig::setPlaylists( const QList& playlist void SpotifyAccountConfig::doLogin() { - m_ui->loginButton->setText( tr( "Logging in..." ) ); - m_ui->loginButton->setEnabled( false ); + if ( !m_isLoggedIn ) + { + m_ui->loginButton->setText( tr( "Logging in..." ) ); + m_ui->loginButton->setEnabled( false ); - m_playlistsLoading->fadeIn(); - m_loggedInManually = true; + m_playlistsLoading->fadeIn(); + m_loggedInManually = true; - emit login( username(), password() ); + emit login( username(), password() ); + } + else + { + // Log out + m_isLoggedIn = false; + m_loggedInManually = false; + m_verifiedUsername.clear(); + emit logout(); + showLoggedOut(); + } } void -SpotifyAccountConfig::loginResponse( bool success, const QString& msg ) +SpotifyAccountConfig::loginResponse( bool success, const QString& msg, const QString& username ) { if ( success ) { - m_ui->loginButton->setText( tr( "Logged in!" ) ); - m_ui->loginButton->setEnabled( false ); + m_verifiedUsername = username; + m_isLoggedIn = true; + showLoggedIn(); } else { setPlaylists( QList< SpotifyPlaylistInfo* >() ); m_playlistsLoading->fadeOut(); + m_ui->loginButton->setText( tr( "Failed: %1" ).arg( msg ) ); m_ui->loginButton->setEnabled( true ); } @@ -163,6 +180,42 @@ SpotifyAccountConfig::loginResponse( bool success, const QString& msg ) } +void +SpotifyAccountConfig::showLoggedIn() +{ + m_ui->passwordEdit->hide(); + m_ui->passwordLabel->hide(); + m_ui->usernameEdit->hide(); + m_ui->usernameLabel->hide(); + + if ( !m_loggedInUser ) + { + m_loggedInUser = new QLabel( this ); + m_ui->verticalLayout->insertWidget( 1, m_loggedInUser, 0, Qt::AlignCenter ); + } + + m_loggedInUser->setText( tr( "Logged in as %1" ).arg( m_verifiedUsername ) ); + + m_ui->loginButton->setText( tr( "Log Out" ) ); + m_ui->loginButton->setEnabled( true ); +} + + +void +SpotifyAccountConfig::showLoggedOut() +{ + m_ui->passwordEdit->show(); + m_ui->passwordLabel->show(); + m_ui->usernameEdit->show(); + m_ui->usernameLabel->show(); + + m_loggedInUser->hide(); + + m_ui->loginButton->setText( tr( "Log In" ) ); + m_ui->loginButton->setEnabled( true ); +} + + void SpotifyAccountConfig::resetLoginButton() { diff --git a/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.h b/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.h index 79c398e04..712e139ad 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.h +++ b/src/libtomahawk/accounts/spotify/SpotifyAccountConfig.h @@ -23,6 +23,7 @@ #include #include +class QLabel; class AnimatedSpinner; class QShowEvent; @@ -55,14 +56,13 @@ public: void loadFromConfig(); void saveSettings(); - void loginResponse( bool success, const QString& msg ); + void loginResponse( bool success, const QString& msg, const QString& username ); bool loggedInManually() const { return m_loggedInManually; } + signals: void login( const QString& username, const QString& pw ); - -public slots: -// void verifyResult( const QString& msgType, const QVariantMap& msg ); + void logout(); protected: void showEvent( QShowEvent* event ); @@ -72,10 +72,15 @@ private slots: void resetLoginButton(); private: + void showLoggedIn(); + void showLoggedOut(); + Ui::SpotifyConfig* m_ui; + QLabel* m_loggedInUser; + QString m_verifiedUsername; SpotifyAccount* m_account; AnimatedSpinner* m_playlistsLoading; - bool m_loggedInManually; + bool m_loggedInManually, m_isLoggedIn; }; } diff --git a/src/libtomahawk/resolvers/ScriptResolver.cpp b/src/libtomahawk/resolvers/ScriptResolver.cpp index 23c615b6e..47b0b3c30 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.cpp +++ b/src/libtomahawk/resolvers/ScriptResolver.cpp @@ -389,6 +389,7 @@ ScriptResolver::doSetup( const QVariantMap& m ) m_ready = true; m_configSent = false; + m_num_restarts = 0; if ( !m_stopped ) Tomahawk::Pipeline::instance()->addResolver( this );