Example #1
0
void QSpotifySession::logout(bool keepLoginInfo)
{
    qDebug() << "QSpotifySession::logout";
    if (!m_isLoggedIn || m_pending_connectionRequest)
        return;

    stop();
    m_playQueue->clear();

    if (!keepLoginInfo) {
        setOfflineMode(false);
        sp_session_forget_me(m_sp_session);
    }

    m_explicitLogout = true;

    m_pending_connectionRequest = true;
    emit pendingConnectionRequestChanged();
    emit loggingOut();

    if(m_user) {
        m_user->deleteLater();
        m_user = nullptr;
    }
    sp_session_logout(m_sp_session);
}
/**
  login
  takes username, password
  tries to login with previous remembered user, thus, password can be empty
  will  also try and utilize blob
  **/
void SpotifySession::login( const QString& username, const QString& password, const QByteArray& blob )
{

    if ( m_loggedIn && m_username == username && m_password == password )
    {
        /// Always relogin when ever credentials change
        qDebug() << "Asked to log in with same username and pw that we are already logged in with, ignoring login";
        /// Send response, and notifyAllreadyLoggedin, this will make config gui to stop "loading", and refetch all playlists
        emit loginResponse( true, "Logged in" );
        emit notifyAllreadyLoggedin();
        return;
    }


    if( m_username != username && m_loggedIn )
    {
//        qDebug() << "We were previously logged in with a different user, so notify client of difference!";
        emit userChanged();
    }

    m_username = username;
    m_password = password;
    char reloginname[256];
    sp_error error;
    int ok = sp_session_remembered_user( m_session, reloginname, sizeof(reloginname) );

    if( ok != -1 && QString::fromUtf8( reloginname, strlen(reloginname) ) == m_username.toUtf8() )
    {
        if ( sp_session_relogin(m_session) == SP_ERROR_NO_CREDENTIALS)
        {
            qDebug() << "No stored credentials tryin blob";
        }
        else
        {
            qDebug() << "Logging in as remembered user";
            return;
        }
    }
    else
    {
        // Forget last user
        if( ok == -1 )
            qDebug() << " Relogin username was truncated or an error occured... Logging in with credentials";
        else
            qDebug() << "Forgetting last user!" << QString::fromUtf8( reloginname, strlen( reloginname ) ) << m_username;

        sp_session_forget_me(m_session);
    }

    if( !m_username.isEmpty() && ( !m_password.isEmpty() || !blob.isEmpty() )  )
    {
        /// @note:  If current state is not logged out, logout this session
        ///         and relogin in callback
        /// @note2: We can be logged out, but the session is still connected to accesspoint
        ///         Wait for that to.
        if( sp_session_connectionstate(m_session) != SP_CONNECTION_STATE_LOGGED_OUT || m_loggedIn)
        {
            qDebug() << Q_FUNC_INFO << "SpotifySession asked to relog in! Logging out";
            m_relogin = true;
            m_blob = blob;
            logout( true );
            return;
        }

        qDebug() << Q_FUNC_INFO << "Logging in with username:"******" and is " << ( blob.isEmpty() ? "not" : "" ) << "using blob";
#if SPOTIFY_API_VERSION >= 12
        error = sp_session_login(m_session, m_username.toUtf8(), m_password.toUtf8(), 1, blob.isEmpty() ? NULL : blob.constData()  );
        if( error != SP_ERROR_OK )
            emit loginResponse( false, sp_error_message( error ) );
#elif SPOTIFY_API_VERSION >= 11
        sp_session_login(m_session, m_username.toUtf8(), m_password.toUtf8(), 1, blob.isEmpty() ? NULL : blob.constData() );
#else
        sp_session_login(m_session, m_username.toUtf8(), m_password.toUtf8(), 1);
#endif


    }
    else
    {
        qDebug() << "No username, password or blob provided!";
        /// TODO: need a way to prompt for password if fail to login as remebered on startup
        /// If auth widget is not visual, we should promt user for re-entering creds
        emit loginResponse( false, "Failed to authenticate credentials." );
    }


}