/* TODO: use a command_finalize_func for that too */ void interface_notify() { GString* str = g_string_sized_new(1024); JsonBuilder* jb = json_builder_new(); command_context ctx = { jb, NULL, NULL }; json_builder_begin_object(jb); status(&ctx); json_builder_end_object(jb); JsonGenerator *gen = json_generator_new(); g_object_set(gen, "pretty", config_get_bool_opt("pretty_json", FALSE), NULL); json_generator_set_root(gen, json_builder_get_root(jb)); gchar *tmp = json_generator_to_data(gen, NULL); g_string_assign(str, tmp); g_string_append(str, "\n"); g_object_unref(gen); g_object_unref(jb); g_free(tmp); /* First notify idle channels */ g_list_foreach(g_idle_channels, interface_notify_chan, str); g_list_free(g_idle_channels); g_idle_channels = NULL; /* Then call callbacks from plugins */ g_list_foreach(g_notification_callbacks, interface_notify_callback, str); g_string_free(str, TRUE); }
/********************** *** 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); }