Beispiel #1
0
void QSpotifySession::init()
{
    memset(&m_sp_callbacks, 0, sizeof(m_sp_callbacks));
    m_sp_callbacks.logged_in = callback_logged_in;
    m_sp_callbacks.logged_out = callback_logged_out;
    m_sp_callbacks.metadata_updated = callback_metadata_updated;
    m_sp_callbacks.connection_error = callback_connection_error;
    m_sp_callbacks.notify_main_thread = callback_notify_main_thread;
    m_sp_callbacks.music_delivery = callback_music_delivery;
    m_sp_callbacks.play_token_lost = callback_play_token_lost;
    m_sp_callbacks.log_message = callback_log_message;
    m_sp_callbacks.end_of_track = callback_end_of_track;
    m_sp_callbacks.userinfo_updated = callback_userinfo_updated;
    m_sp_callbacks.offline_error = callback_offline_error;
    m_sp_callbacks.connectionstate_updated = callback_connectionstate_updated;
    m_sp_callbacks.scrobble_error = callback_scrobble_error;

    QString dpString = settings.value("dataPath").toString();
    dataPath = (char *) calloc(strlen(dpString.toLatin1()) + 1, sizeof(char));
    strcpy(dataPath, dpString.toLatin1());

    memset(&m_sp_config, 0, sizeof(m_sp_config));
    m_sp_config.api_version = SPOTIFY_API_VERSION;
    m_sp_config.cache_location = dataPath;
    m_sp_config.settings_location = dataPath;
    m_sp_config.application_key = g_appkey;
    m_sp_config.application_key_size = g_appkey_size;
    m_sp_config.user_agent = "CuteSpot";
    m_sp_config.callbacks = &m_sp_callbacks;
    sp_error error = sp_session_create(&m_sp_config, &m_sp_session);

    if (error != SP_ERROR_OK)
    {
        fprintf(stderr, "failed to create session: %s\n",
                sp_error_message(error));

        m_sp_session = nullptr;
        return;
    }
    Q_ASSERT(m_sp_session);

    sp_session_set_cache_size(m_sp_session, 0);

    // Remove stored login information from older version of MeeSpot
    if (settings.contains("username")) {
        settings.remove("username");
        settings.remove("password");
    }
    // Remove old volume information
    if (settings.contains("volume")) {
        settings.remove("volume");
    }

    m_offlineMode = settings.value("offlineMode", false).toBool();

    checkNetworkAccess();

    StreamingQuality quality = StreamingQuality(settings.value("streamingQuality", int(LowQuality)).toInt());
    setStreamingQuality(quality);

    StreamingQuality syncQuality = StreamingQuality(settings.value("syncQuality", int(HighQuality)).toInt());
    setSyncQuality(syncQuality);

    bool syncMobile = settings.value("syncOverMobile", false).toBool();
    setSyncOverMobile(syncMobile);

    QString storedLogin = getStoredLoginInformation();
    if (!storedLogin.isEmpty()) {
        login(storedLogin);
    }

    bool shuffle = settings.value("shuffle", false).toBool();
    setShuffle(shuffle);

    bool repeat = settings.value("repeat", false).toBool();
    setRepeat(repeat);

    bool repeatOne = settings.value("repeatOne", false).toBool();
    setRepeatOne(repeatOne);

    bool volumeNormalizeSet = settings.value("volumeNormalize", true).toBool();
    setVolumeNormalize(volumeNormalizeSet);

    m_lfmLoggedIn = false;

    // Flush cache regularly as we might get killed by life cycle manager / OOM killer
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(flush()));
    timer->start(60000);

    connect(this, SIGNAL(offlineModeChanged()), m_playQueue, SLOT(onOfflineModeChanged()));

    new MPRISMediaPlayer(this);
    new MPRISMediaPlayerPlayer(this);

    QDBusConnection::sessionBus().registerObject(QString("/org/mpris/MediaPlayer2"), this, QDBusConnection::ExportAdaptors);
    QDBusConnection::sessionBus().registerService("org.mpris.MediaPlayer2.CuteSpot");
}
Beispiel #2
0
/**********************
 *** Init functions ***
 **********************/
void session_init() {
    sp_error error;
    gchar* cache_path;

    g_debug("Creating session...");

    /* Cache path */
    cache_path = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), NULL);

    /* libspotify session config */
    sp_session_config config = {
        .api_version = SPOTIFY_API_VERSION,
        .cache_location = cache_path,
        .settings_location = cache_path,
        .application_key = g_appkey,
        .application_key_size = g_appkey_size,
        .user_agent = "spop " SPOP_VERSION,
        .callbacks = &g_sp_session_callbacks,
        .userdata = NULL,
        .compress_playlists = FALSE,
        .dont_save_metadata_for_playlists = FALSE,
        .initially_unload_playlists = FALSE,
        NULL,
    };

    error = sp_session_create(&config, &g_session);
    if (error != SP_ERROR_OK)
        g_error("Failed to create session: %s", sp_error_message(error));

    /* Set bitrate */
    if (config_get_bool_opt("high_bitrate", TRUE)) {
        g_debug("Setting preferred bitrate to high.");
        sp_session_preferred_bitrate(g_session, SP_BITRATE_320k);
    }
    else {
        g_debug("Setting preferred bitrate to low.");
        sp_session_preferred_bitrate(g_session, SP_BITRATE_160k);
    }
    if (config_get_bool_opt("offline_high_bitrate", TRUE)) {
        g_debug("Setting preferred offline bitrate to high.");
        sp_session_preferred_offline_bitrate(g_session, SP_BITRATE_320k, FALSE);
    }
    else {
        g_debug("Setting preferred offline bitrate to low.");
        sp_session_preferred_offline_bitrate(g_session, SP_BITRATE_160k, FALSE);
    }

    size_t cache_size = config_get_int_opt("cache_size", 0);
    g_debug("Setting cache size to %zu.", cache_size);
    sp_session_set_cache_size(g_session, cache_size);

    g_debug("Session created.");
}

void session_login(const char* username, const char* password) {
    g_debug("Logging in...");
    if (!g_session)
        g_error("Session is not ready.");

    sp_session_login(g_session, username, password, TRUE, NULL);
}