void SpotifyClient::LoggedInCallback(sp_session* session, sp_error error) {
  SpotifyClient* me = reinterpret_cast<SpotifyClient*>(sp_session_userdata(session));
  const bool success = error == SP_ERROR_OK;
  spotify_pb::LoginResponse_Error error_code = spotify_pb::LoginResponse_Error_Other;

  if (!success) {
    qLog(Warning) << "Failed to login" << sp_error_message(error);
  }

  switch (error) {
  case SP_ERROR_BAD_USERNAME_OR_PASSWORD:
    error_code = spotify_pb::LoginResponse_Error_BadUsernameOrPassword;
    break;
  case SP_ERROR_USER_BANNED:
    error_code = spotify_pb::LoginResponse_Error_UserBanned;
    break;
  case SP_ERROR_USER_NEEDS_PREMIUM :
    error_code = spotify_pb::LoginResponse_Error_UserNeedsPremium;
    break;
  }

  me->SendLoginCompleted(success, sp_error_message(error), error_code);

  if (success) {
    sp_playlistcontainer_add_callbacks(
          sp_session_playlistcontainer(session),
          &me->playlistcontainer_callbacks_, me);
  }
}
示例#2
0
static void logged_in(sp_session *session, sp_error error) {
	pthread_mutex_lock(&g_mutex);
	if (SP_ERROR_OK != error) {
		fprintf(stderr, "Could not perform login: %s\n", sp_error_message(error));
		exit(1);
	}
	pthread_cond_signal(&g_cond);
	pthread_mutex_unlock(&g_mutex);

	printf("Logged in!\n");

	sp_playlistcontainer* list_container = sp_session_playlistcontainer(session);
	if (list_container == NULL) {
		fprintf(stderr, "Cannot get main playlist container, aborting");
		exit(1);
	}


	sp_playlistcontainer_add_callbacks(
        list_container,
        &pc_callbacks,
        NULL);



}
示例#3
0
/**
 * This callback is called when an attempt to login has succeeded or failed.
 *
 * @sa sp_session_callbacks#logged_in
 */
static void logged_in(sp_session *sess, sp_error error)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(sess);
	int i;

	if (SP_ERROR_OK != error) {
		fprintf(stderr, "jukebox: Login failed: %s\n",
			sp_error_message(error));
		exit(2);
	}

	sp_playlistcontainer_add_callbacks(
		pc,
		&pc_callbacks,
		NULL);

	printf("jukebox: Looking at %d playlists\n", sp_playlistcontainer_num_playlists(pc));

	for (i = 0; i < sp_playlistcontainer_num_playlists(pc); ++i) {
		sp_playlist *pl = sp_playlistcontainer_playlist(pc, i);

		sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);

		if (!strcasecmp(sp_playlist_name(pl), g_listname)) {
			g_jukeboxlist = pl;
			try_jukebox_start();
		}
	}

	if (!g_jukeboxlist) {
		printf("jukebox: No such playlist. Waiting for one to pop up...\n");
		fflush(stdout);
	}
}
bool PlayListContainer::Load(sp_playlistcontainer *container) {
    container_ = container;
    sp_playlistcontainer_callbacks callbacks;
    GetCallbacks(&callbacks);
    sp_playlistcontainer_add_callbacks(container_, &callbacks, this);
    loading_ = true;
    return true;
}
void Spotify::loggedInCb(sp_session *sp, sp_error err)
{
    if (err == SP_ERROR_OK) {
        sp_playlistcontainer_add_callbacks(sp_session_playlistcontainer(sp),
                                           Spotify_Wrapper::playlistcontainerCallbacks(),
                                           this);
        eq.put(EVENT_LOGGED_IN);
    } else {
        fprintf(stderr, "Failed to login: %s\n",
                sp_error_message(err));
        eq.put(EVENT_LOGGED_OUT);
    }
}
示例#6
0
static struct playlistcontainer_handler *register_playlistcontainer_callbacks(
    sp_playlistcontainer *pc,
    struct evhttp_request *request,
    handle_playlistcontainer_fn callback,
    sp_playlistcontainer_callbacks *playlistcontainer_callbacks,
    void *userdata) {
  struct playlistcontainer_handler *handler = malloc(sizeof (struct playlistcontainer_handler));
  handler->request = request;
  handler->callback = callback;
  handler->playlistcontainer_callbacks = playlistcontainer_callbacks;
  handler->userdata = userdata;
  sp_playlistcontainer_add_callbacks(pc, handler->playlistcontainer_callbacks,
                                     handler);
  return handler;
}
SpotifyPlaylistContainer::SpotifyPlaylistContainer(sp_session* session,
		SpotifySession* spotifySession) {
	this->session = session;
	this->spotifySession = spotifySession;
	playlistContainer = sp_session_playlistcontainer(session);

	sp_playlistcontainer_callbacks playlistCallbacks;
	playlistCallbacks.container_loaded = &container_loaded;
	playlistCallbacks.playlist_added = &playlist_added;
	playlistCallbacks.playlist_removed = &playlist_removed;
	playlistCallbacks.playlist_moved = &playlist_moved;

	sp_playlistcontainer_add_callbacks(playlistContainer, &playlistCallbacks,
	nullptr);
}
示例#8
0
/* -- Session callbacks -- */
static void logged_in(sp_session *session, sp_error error)
{
    debug("Callback on_login");
    if (error != SP_ERROR_OK) 
    {
        fprintf(stderr, "Error: unable to log in: %s\n", sp_error_message(error));
        exit(1);
    }

    pc = sp_session_playlistcontainer(session);
    sp_playlistcontainer_add_callbacks( pc, &pc_callbacks, NULL);
    g_logged_in = 1;
   
    is_logged_in = TRUE;
    printf("logged in\n");
}
QSpotifyPlaylistContainer::QSpotifyPlaylistContainer(sp_playlistcontainer *container)
    : QSpotifyObject(true)
    , m_updateEventPosted(false)
{
    m_container = container;
    g_containerObjects.insert(container, this);
    m_callbacks = new sp_playlistcontainer_callbacks;
    m_callbacks->container_loaded = callback_container_loaded;
    m_callbacks->playlist_added = callback_playlist_added;
    m_callbacks->playlist_moved = callback_playlist_moved;
    m_callbacks->playlist_removed = callback_playlist_removed;
    sp_playlistcontainer_add_callbacks(m_container, m_callbacks, 0);
    connect(QSpotifySession::instance(), SIGNAL(offlineModeChanged()), this, SLOT(updatePlaylists()));

    metadataUpdated();
}
示例#10
0
static void logged_in(sp_session *session, sp_error error) {
  if (error != SP_ERROR_OK) {
    fprintf(stderr, "%s\n", sp_error_message(error));
    exit_status = EXIT_FAILURE;
    logged_out(session);
    return;
  }

  struct state *state = sp_session_userdata(session);
  state->session = session;
  evsignal_add(state->sigint, NULL);

  sp_playlistcontainer *pc = sp_session_playlistcontainer(session);
  sp_playlistcontainer_add_callbacks(pc, &playlistcontainer_callbacks,
                                     session);
}
示例#11
0
PHP_METHOD(Spotify, initPlaylistContainer)
{
	int timeout = 0;

	spotify_object *p = (spotify_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
	if (p->playlistcontainer != NULL) {
		RETURN_TRUE;
	}

	sp_playlistcontainer *tempcontainer = sp_session_playlistcontainer(p->session);
	sp_playlistcontainer_add_callbacks(tempcontainer, &playlistcontainer_callbacks, p);

	while (p->playlistcontainer == NULL) {
		sp_session_process_events(p->session, &timeout);
	}

	RETURN_TRUE;
}
示例#12
0
void logged_in(sp_session *session, sp_error error) {
  struct state *state = sp_session_userdata(session);

  if (error != SP_ERROR_OK) {
    syslog(LOG_CRIT, "Error logging in to Spotify: %s",
           sp_error_message(error));
    state->exit_status = EXIT_FAILURE;
    logged_out(session);
    return;
  }

  state->session = session;
  evsignal_add(state->sigint, NULL);

  sp_playlistcontainer *pc = sp_session_playlistcontainer(session);
  sp_playlistcontainer_add_callbacks(pc, &playlistcontainer_callbacks,
                                     session);
}
/**
  loggedin
  callback from spotify
  also initilizes the playlistcontainer and callbacks
  **/
void SpotifySession::loggedIn(sp_session *session, sp_error error)
{
   SpotifySession* _session = reinterpret_cast<SpotifySession*>(sp_session_userdata(session));
    if (error == SP_ERROR_OK) {

        qDebug() << "Logged in successfully!!";

        _session->setSession(session);
        _session->setLoggedIn(true);
        _session->setPlaylistContainer( sp_session_playlistcontainer(session) );

        sp_playlistcontainer_add_ref( _session->PlaylistContainer() );
        sp_playlistcontainer_add_callbacks(_session->PlaylistContainer(), &SpotifyCallbacks::containerCallbacks, _session);
    }

    qDebug() << Q_FUNC_INFO << "==== " << sp_error_message( error ) << " ====";
    const QString msg = QString::fromUtf8( sp_error_message( error ) );
    emit _session->loginResponse( error == SP_ERROR_OK, msg );
}
示例#14
0
void SpotWorker::emitLoggedInSignal(sp_session *session, sp_error error)
{
    if (SP_ERROR_OK != error) {
        fprintf(stderr, "SpotWorker: Login failed: %s\n",
                sp_error_message(error));
    }
    else{
        printf("SpotWorker: Successfully logged in\n");

        emit loggedIn(session, error);

        sp_playlistcontainer *playlists = sp_session_playlistcontainer(session);
        void * userdata = NULL;
        sp_playlistcontainer_add_callbacks(playlists,
                                           &pc_callbacks,
                                           userdata);

        int listCount = sp_playlistcontainer_num_playlists(playlists);
        printf("%d playlists discovered\n", sp_playlistcontainer_num_playlists(playlists));

        if(listCount > 0)
            emit playlistAdded(playlists);

        for (int i = 0; i < listCount && i < 10; ++i) {
            sp_playlist *pl = sp_playlistcontainer_playlist(playlists, i);

            //TODO: register playback callback
            //sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);

            if (sp_playlist_is_loaded(pl)){
                DEBUG printf("Playlist found: %s (%i tracks)\n",
                             sp_playlist_name(pl), sp_playlist_num_tracks(pl));
            }
        }
	DEBUG {
	    if (listCount > 10)
		printf("...and %d more.\n", listCount-10);
	}
    }
}
示例#15
0
static void SP_CALLCONV logged_in(sp_session *session, sp_error error) {
	sp_playlistcontainer *pc;
	static sp_playlistcontainer_callbacks callbacks;

	DSFYDEBUG("SESSION CALLBACK\n");
	if (SP_ERROR_OK != error) {
		fprintf(stderr, "failed to log in to Spotify: %s\n",
		                sp_error_message(error));

		return;
	}


	/* Snoop on added playlists */
	memset(&callbacks, 0, sizeof(callbacks));
	callbacks.playlist_added = pc_playlist_added;
	callbacks.playlist_moved = NULL;
	callbacks.playlist_removed = NULL;

	pc = sp_session_playlistcontainer(session);
	sp_playlistcontainer_add_callbacks(pc, &callbacks, NULL);
}
示例#16
0
/**
 * Added note: The callbacks for the playlist-handling are set
 *             by logged_in_playlist(), for each playlist in the
 *             playlist container.
 * 
 *             The callbacks for the playlist-container-handling are set
 *             by main().
 * 
 */
int spshell_init(const char *username, const char *password)
{
	sp_session_config config;
	sp_error error;
	sp_session *session;

        /// The application key is specific to each project, and allows Spotify
        /// to produce statistics on how our service is used.
	extern const char g_appkey[];
        /// The size of the application key.
	extern const size_t g_appkey_size;

	// Always do this. It allows libspotify to check for
	// header/library inconsistencies.
	config.api_version = SPOTIFY_API_VERSION;

	// The path of the directory to store the cache. This must be specified.
	// Please read the documentation on preferred values.
	config.cache_location = "tmp";

	// The path of the directory to store the settings. 
	// This must be specified.
	// Please read the documentation on preferred values.
	config.settings_location = "tmp";

	// The key of the application. They are generated by Spotify,
	// and are specific to each application using libspotify.
	config.application_key = g_appkey;
	config.application_key_size = g_appkey_size;

	// This identifies the application using some
	// free-text string [1, 255] characters.
	config.user_agent = "listify";

	// Register the callbacks.
	config.callbacks = &callbacks;

	error = sp_session_init(&config, &session);
	if (SP_ERROR_OK != error) {
		fprintf(stderr, "failed to create session: %s\n",
		                sp_error_message(error));
		return 2;
	}

	// Added Code: Adding the callbacks for the playlist-container
	sp_playlistcontainer_add_callbacks(
		sp_session_playlistcontainer(session),
		&pc_callbacks,
		NULL);

	// Login using the credentials given on the command line.
	error = sp_session_login(session, username, password);

	if (SP_ERROR_OK != error) {
		fprintf(stderr, "failed to login: %s\n",
		                sp_error_message(error));
		return 3;
	}

	g_session = session;
	return 0;
}
示例#17
0
int main(int argc, char **argv)
{
	sp_session *sp;
	sp_error err;
	int next_timeout = 0;
	const char *username = NULL;
	const char *password = NULL;
	int opt;

	while ((opt = getopt(argc, argv, "u:p:l:d")) != EOF) {
		switch (opt) {
		case 'u':
			username = optarg;
			break;

		case 'p':
			password = optarg;
			break;

		case 'l':
			g_listname = optarg;
			break;

		case 'd':
			g_remove_tracks = 1;
			break;

		default:
			exit(1);
		}
	}

	if (!username || !password || !g_listname) {
		usage(basename(argv[0]));
		exit(1);
	}

	audio_init(&g_audiofifo);

	/* Create session */
	spconfig.application_key_size = g_appkey_size;

	err = sp_session_create(&spconfig, &sp);

	if (SP_ERROR_OK != err) {
		fprintf(stderr, "Unable to create session: %s\n",
			sp_error_message(err));
		exit(1);
	}

	g_sess = sp;

	pthread_mutex_init(&g_notify_mutex, NULL);
	pthread_cond_init(&g_notify_cond, NULL);

	sp_playlistcontainer_add_callbacks(
		sp_session_playlistcontainer(g_sess),
		&pc_callbacks,
		NULL);

	sp_session_login(sp, username, password, 0);
	pthread_mutex_lock(&g_notify_mutex);

	for (;;) {
		if (next_timeout == 0) {
			while(!g_notify_do && !g_playback_done)
				pthread_cond_wait(&g_notify_cond, &g_notify_mutex);
		} else {
			struct timespec ts;

#if _POSIX_TIMERS > 0
			clock_gettime(CLOCK_REALTIME, &ts);
#else
			struct timeval tv;
			gettimeofday(&tv, NULL);
			TIMEVAL_TO_TIMESPEC(&tv, &ts);
#endif
			ts.tv_sec += next_timeout / 1000;
			ts.tv_nsec += (next_timeout % 1000) * 1000000;

			pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts);
		}

		g_notify_do = 0;
		pthread_mutex_unlock(&g_notify_mutex);

		if (g_playback_done) {
			track_ended();
			g_playback_done = 0;
		}

		do {
			sp_session_process_events(sp, &next_timeout);
		} while (next_timeout == 0);

		pthread_mutex_lock(&g_notify_mutex);
	}

	return 0;
}
示例#18
0
int main(int argc, char **argv)
{
    init_session_config();
    init_session_callbacks();
    init_playlist_callbacks();
    init_playlist_container_callbacks();

    sp_session *sp;
    sp_error err;
    int next_timeout = 0;
    const char *username = NULL;
    const char *password = NULL;
    int opt;

    while ((opt = getopt(argc, argv, "u:p:l:d")) != EOF) {
        switch (opt) {
        case 'u':
            username = optarg;
            break;

        case 'p':
            password = optarg;
            break;

        case 'l':
            g_listname = optarg;
            break;

        case 'd':
            g_remove_tracks = 1;
            break;

        default:
            exit(1);
        }
    }

    if (!username || !password || !g_listname) {
        usage(argv[0]);
        exit(1);
    }

    audio_open();

    err = sp_session_create(&spconfig, &sp);

    if (SP_ERROR_OK != err) {
        fprintf(stderr, "Unable to create session: %s\n",
            sp_error_message(err));
        exit(1);
    }

    g_sess = sp;

    g_notify_mutex = SDL_CreateMutex();
    g_notify_cond = SDL_CreateCond();


    sp_playlistcontainer_add_callbacks(
        sp_session_playlistcontainer(g_sess),
        &pc_callbacks,
        NULL);

    sp_session_login(sp, username, password);
    SDL_mutexP(g_notify_mutex);

    for (;;) {
        if (next_timeout == 0) {
            while(!g_notify_do && !g_playback_done)
                SDL_CondWait(g_notify_cond, g_notify_mutex);
        } else {
            SDL_CondWaitTimeout(g_notify_cond, g_notify_mutex, next_timeout);
        }

        g_notify_do = 0;
        SDL_mutexV(g_notify_mutex);

        if (g_playback_done) {
            track_ended();
            g_playback_done = 0;
        }

        do {
            sp_session_process_events(sp, &next_timeout);
        } while (next_timeout == 0);

        SDL_mutexP(g_notify_mutex);
    }

    return 0;
}
示例#19
0
JNIEXPORT int JNICALL Java_jahspotify_impl_JahSpotifyImpl_initialize ( JNIEnv *env, jobject obj, jstring username, jstring password )
{
    sp_session *sp;
    sp_error err;
    uint8_t *nativePassword = NULL;
    uint8_t *nativeUsername = NULL;
    int next_timeout = 0;

    if ( !username || !password )
    {
        fprintf ( stderr, "Username or password not specified\n" );
        return 1;
    }

    /* Create session */
    spconfig.application_key_size = g_appkey_size;
    err = sp_session_create ( &spconfig, &sp );

    if ( SP_ERROR_OK != err )
    {
        fprintf ( stderr, "Unable to create session: %s\n",sp_error_message ( err ) );
        return 1;
    }

    fprintf ( stderr, "Session created %x\n",(int)sp);

    nativeUsername = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, username, NULL );
    nativePassword = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, password, NULL );

    g_sess = sp;

    pthread_mutex_init ( &g_notify_mutex, NULL );
    pthread_cond_init ( &g_notify_cond, NULL );

    sp_playlistcontainer_add_callbacks ( sp_session_playlistcontainer ( g_sess ),&pc_callbacks,NULL );

    fprintf ( stderr, "Initiating login: %s\n",nativeUsername );

    sp_session_login ( sp, nativeUsername, nativePassword );
    pthread_mutex_lock ( &g_notify_mutex );


    for ( ;; )
    {
        if ( next_timeout == 0 )
        {
            while ( !g_notify_do && !g_playback_done )
                pthread_cond_wait ( &g_notify_cond, &g_notify_mutex );
        }
        else
        {
            struct timespec ts;

#if _POSIX_TIMERS > 0
            clock_gettime ( CLOCK_REALTIME, &ts );
#else
            struct timeval tv;
            gettimeofday ( &tv, NULL );
            TIMEVAL_TO_TIMESPEC ( &tv, &ts );
#endif
            ts.tv_sec += next_timeout / 1000;
            ts.tv_nsec += ( next_timeout % 1000 ) * 1000000;

            pthread_cond_timedwait ( &g_notify_cond, &g_notify_mutex, &ts );
        }

        g_notify_do = 0;
        pthread_mutex_unlock ( &g_notify_mutex );

        if ( g_playback_done )
        {
            track_ended();
            g_playback_done = 0;
        }

        sp_connectionstate conn_state = sp_session_connectionstate(sp);
        switch (conn_state)
        {
        case SP_CONNECTION_STATE_UNDEFINED:
        case SP_CONNECTION_STATE_LOGGED_OUT:
        case SP_CONNECTION_STATE_LOGGED_IN:
            break;
        case SP_CONNECTION_STATE_DISCONNECTED:
            fprintf ( stderr, "Disconnected!\n");
            break;
        }


        do
        {
            sp_session_process_events ( sp, &next_timeout );
        }
        while ( next_timeout == 0 );

        pthread_mutex_lock ( &g_notify_mutex );
    }

    // FIXME: Release the username/password allocated?

    return 0;

}