QString QSpotifySession::getStoredLoginInformation() const { qDebug() << "QSpotifySession::getStoredLoginInformation"; QString username; char buffer[200]; int size = sp_session_remembered_user(m_sp_session, &buffer[0], 200); if (size > 0) { username = QString::fromUtf8(&buffer[0], size); } return username; }
int main(int argc, char **argv) { int listen_fd; sp_session *session; static sp_session_config config; static sp_session_callbacks callbacks = { .logged_in = &sess_callback_logged_in, .logged_out = &sess_callback_logged_out, .metadata_updated = &sess_callback_metadata_updated, .connection_error = NULL, .message_to_user = &sess_callback_message_to_user, .notify_main_thread = &sess_callback_notify, .music_delivery = &player_callback_frame_delivery, .play_token_lost = &player_callback_playtoken_lost, .log_message = &sess_callback_log_message, .end_of_track = &player_callback_end_of_track, .streaming_error = NULL, .userinfo_updated = NULL, .start_playback = &player_callback_start_playback, .stop_playback = &player_callback_stop_playback, .get_audio_buffer_stats = &player_callback_get_audio_buffer_stats, // libspotify 10 .offline_status_updated = &sess_callback_offline_status_updated, .offline_error = &sess_callback_offline_error, // libspotify 11 .credentials_blob_updated = sess_callback_credentials_blob_updated, // libspotify 12 .connectionstate_updated = &sess_callback_connectionstate_updated, .scrobble_error = NULL, .private_session_mode_changed = NULL, }; thread_main = pthread_self(); /* Setup logging to stderr */ openlog(LIBSPOTIFY_USERAGENT, LOG_PERROR, LOG_USER); /** * Filter logging with one of these * setlogmask(LOG_UPTO(LOG_WARNING)); setlogmask(LOG_UPTO(LOG_NOTICE)); setlogmask(LOG_UPTO(LOG_DEBUG)); */ setlogmask(LOG_UPTO(LOG_INFO)); config.api_version = SPOTIFY_API_VERSION; config.cache_location = LIBSPOTIFY_CACHE_DIR; config.settings_location = LIBSPOTIFY_CACHE_DIR; config.application_key = g_appkey; config.application_key_size = sizeof(g_appkey); config.user_agent = LIBSPOTIFY_USERAGENT; config.callbacks = &callbacks; config.userdata = app_create(); config.compress_playlists = 1; config.dont_save_metadata_for_playlists = 0; config.initially_unload_playlists = 0; config.device_id = NULL; syslog(LOG_DEBUG, "MAIN: Initializing libspotify"); if(sp_session_create(&config, &session) != SP_ERROR_OK) { syslog(LOG_ERR, "MAIN: Unable to initialize libspotify"); app_release(); return -1; } app_set_session(session); if(argc == 4) { sp_link *link = sp_link_create_from_string(argv[3]); app_set_link(link); } /* This program will be run on mobile internet connections */ sp_session_set_connection_type(session, SP_CONNECTION_TYPE_MOBILE_ROAMING); sp_session_set_connection_rules(session, SP_CONNECTION_RULE_NETWORK|SP_CONNECTION_RULE_NETWORK_IF_ROAMING|SP_CONNECTION_RULE_ALLOW_SYNC_OVER_MOBILE); sp_session_preferred_offline_bitrate(session, SP_BITRATE_160k, 0); if(argc < 2) { char username[256]; if(sp_session_remembered_user(session, username, sizeof(username)) > 0) syslog(LOG_DEBUG, "MAIN: Attempting to login using stored credentials for user '%s'", username); if(sp_session_relogin(session) == SP_ERROR_NO_CREDENTIALS) { syslog(LOG_ERR, "MAIN: No credentials stored. Please run: %s <username> <password>", argv[0]); app_release(); return -1; } } else { syslog(LOG_DEBUG, "MAIN: Attempting to login using command line credentials"); sp_session_login(session, argv[1], argc == 3? argv[2]: NULL, 1, get_auth_blob()); } if((listen_fd = net_create(CTRL_TCP_PORT)) < 0) { syslog(LOG_ERR, "MAIN: Failed to initialize external network"); app_release(); return -1; } mainloop(session, listen_fd); syslog(LOG_INFO, "MAIN: Outside main event loop, good bye!"); net_release(listen_fd); app_release(); return 0; }
/** 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." ); } }