From ac605db1efeafc8c6591613a4e6dd8c9a062658d Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 25 Jun 2013 15:08:18 -0300 Subject: [PATCH] Port lastfm_support and lastfm_user to PraghaPreferences. TODO: * Port lastfm password without adding the property to PraghaPreferences. * So, not save it in memory. Only search when necessary. * Maybe save the hexadecimal characters to hide the password. --- src/init.c | 28 ------- src/pragha-glyr.c | 2 +- src/pragha-lastfm.c | 24 +++--- src/pragha-preferences-dialog.c | 43 +++------- src/pragha-preferences.c | 137 +++++++++++++++++++++++++++++++- src/pragha-preferences.h | 15 ++++ src/pragha.c | 1 - src/pragha.h | 2 - 8 files changed, 176 insertions(+), 76 deletions(-) diff --git a/src/init.c b/src/init.c index 108b2c6b..b25512cc 100644 --- a/src/init.c +++ b/src/init.c @@ -99,9 +99,6 @@ gint init_config(struct con_win *cwin) CDEBUG(DBG_INFO, "Initializing configuration"); start_mode_f = window_size_f = window_position_f = album_f = FALSE; - #ifdef HAVE_LIBCLASTFM - gboolean lastfm_f = FALSE; - #endif all_f = FALSE; @@ -174,27 +171,6 @@ gint init_config(struct con_win *cwin) /* Retrieve Services Internet preferences */ #ifdef HAVE_LIBCLASTFM - cwin->cpref->lastfm_support = - g_key_file_get_boolean(cwin->cpref->configrc_keyfile, - GROUP_SERVICES, - KEY_LASTFM, - &error); - if (error) { - g_error_free(error); - error = NULL; - lastfm_f = TRUE; - } - - cwin->cpref->lastfm_user = - g_key_file_get_string(cwin->cpref->configrc_keyfile, - GROUP_SERVICES, - KEY_LASTFM_USER, - &error); - if (error) { - g_error_free(error); - error = NULL; - } - cwin->cpref->lastfm_pass = g_key_file_get_string(cwin->cpref->configrc_keyfile, GROUP_SERVICES, @@ -222,10 +198,6 @@ gint init_config(struct con_win *cwin) } if (all_f || start_mode_f) cwin->cpref->start_mode = g_strdup(NORMAL_STATE); - #ifdef HAVE_LIBCLASTFM - if (all_f || lastfm_f) - cwin->cpref->lastfm_support = FALSE; - #endif if (err) return -1; diff --git a/src/pragha-glyr.c b/src/pragha-glyr.c index 7f28f172..7e9cfeed 100644 --- a/src/pragha-glyr.c +++ b/src/pragha-glyr.c @@ -441,7 +441,7 @@ update_related_handler (gpointer data) CDEBUG(DBG_INFO, "Updating Lastm and getting the cover art depending preferences"); #ifdef HAVE_LIBCLASTFM - if (cwin->cpref->lastfm_support) + if (pragha_preferences_get_lastfm_support (cwin->preferences)) lastfm_now_playing_handler(cwin); #endif #ifdef HAVE_LIBGLYR diff --git a/src/pragha-lastfm.c b/src/pragha-lastfm.c index 1ec5792f..c431f910 100644 --- a/src/pragha-lastfm.c +++ b/src/pragha-lastfm.c @@ -77,7 +77,7 @@ update_menubar_lastfm_state (struct con_win *cwin) gboolean playing = pragha_backend_get_state (cwin->backend) != ST_STOPPED; gboolean logged = cwin->clastfm->status == LASTFM_STATUS_OK; gboolean lfm_inited = cwin->clastfm->session_id != NULL; - gboolean has_user = lfm_inited && string_is_not_empty(cwin->cpref->lastfm_user); + gboolean has_user = lfm_inited && string_is_not_empty(pragha_preferences_get_lastfm_user(cwin->preferences)); action = gtk_ui_manager_get_action(cwin->bar_context_menu, "/Menubar/ToolsMenu/Lastfm/Love track"); gtk_action_set_sensitive (GTK_ACTION (action), playing && logged); @@ -587,9 +587,9 @@ do_lastfm_add_favorites_action (gpointer user_data) do { rpages = LASTFM_user_get_loved_tracks(cwin->clastfm->session_id, - cwin->cpref->lastfm_user, - cpage, - &results); + pragha_preferences_get_lastfm_user(cwin->preferences), + cpage, + &results); for(li=results; li; li=li->next) { track = li->data; @@ -615,7 +615,7 @@ lastfm_add_favorites_action (GtkAction *action, struct con_win *cwin) CDEBUG(DBG_LASTFM, "Add Favorites action"); if ((cwin->clastfm->session_id == NULL) || - string_is_empty(cwin->cpref->lastfm_user)) { + string_is_empty(pragha_preferences_get_lastfm_user(cwin->preferences))) { pragha_statusbar_set_misc_text(cwin->statusbar, _("No connection Last.fm has been established.")); return; } @@ -921,7 +921,7 @@ lastfm_now_playing_handler (struct con_win *cwin) if(pragha_backend_get_state (cwin->backend) == ST_STOPPED) return; - if(string_is_empty(cwin->cpref->lastfm_user) || + if(string_is_empty(pragha_preferences_get_lastfm_user(cwin->preferences)) || string_is_empty(cwin->cpref->lastfm_pass)) return; @@ -975,10 +975,10 @@ do_just_init_lastfm(gpointer data) cwin->clastfm->session_id = LASTFM_init(LASTFM_API_KEY, LASTFM_SECRET); if (cwin->clastfm->session_id != NULL) { - if(string_is_not_empty(cwin->cpref->lastfm_user) && + if(string_is_not_empty(pragha_preferences_get_lastfm_user(cwin->preferences)) && string_is_not_empty(cwin->cpref->lastfm_pass)) { cwin->clastfm->status = LASTFM_login (cwin->clastfm->session_id, - cwin->cpref->lastfm_user, + pragha_preferences_get_lastfm_user(cwin->preferences), cwin->cpref->lastfm_pass); if(cwin->clastfm->status != LASTFM_STATUS_OK) { @@ -999,7 +999,7 @@ do_just_init_lastfm(gpointer data) gint just_init_lastfm (struct con_win *cwin) { - if (cwin->cpref->lastfm_support) { + if (pragha_preferences_get_lastfm_support (cwin->preferences)) { CDEBUG(DBG_INFO, "Initializing LASTFM"); g_idle_add (do_just_init_lastfm, cwin); } @@ -1017,10 +1017,10 @@ do_init_lastfm_idle(gpointer data) cwin->clastfm->session_id = LASTFM_init(LASTFM_API_KEY, LASTFM_SECRET); if (cwin->clastfm->session_id != NULL) { - if(string_is_not_empty(cwin->cpref->lastfm_user) && + if(string_is_not_empty(pragha_preferences_get_lastfm_user(cwin->preferences)) && string_is_not_empty(cwin->cpref->lastfm_pass)) { cwin->clastfm->status = LASTFM_login (cwin->clastfm->session_id, - cwin->cpref->lastfm_user, + pragha_preferences_get_lastfm_user(cwin->preferences), cwin->cpref->lastfm_pass); if(cwin->clastfm->status != LASTFM_STATUS_OK) @@ -1049,7 +1049,7 @@ init_lastfm(struct con_win *cwin) /* Test internet and launch threads.*/ - if (cwin->cpref->lastfm_support) { + if (pragha_preferences_get_lastfm_support (cwin->preferences)) { CDEBUG(DBG_INFO, "Initializing LASTFM"); #if GLIB_CHECK_VERSION(2,32,0) diff --git a/src/pragha-preferences-dialog.c b/src/pragha-preferences-dialog.c index 3e4f0406..4bfc1acd 100644 --- a/src/pragha-preferences-dialog.c +++ b/src/pragha-preferences-dialog.c @@ -332,23 +332,18 @@ static void pref_dialog_cb(GtkDialog *dialog, gint response_id, /* Services internet preferences */ #ifdef HAVE_LIBCLASTFM - cwin->cpref->lastfm_support = - gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( - cwin->preferences_w->lastfm_w)); + pragha_preferences_set_lastfm_support (cwin->preferences, + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cwin->preferences_w->lastfm_w))); - if (cwin->cpref->lastfm_user) { - g_free(cwin->cpref->lastfm_user); - cwin->cpref->lastfm_user = NULL; - } if (cwin->cpref->lastfm_pass) { g_free(cwin->cpref->lastfm_pass); cwin->cpref->lastfm_pass = NULL; } - if (cwin->cpref->lastfm_support) { - cwin->cpref->lastfm_user = - g_strdup(gtk_entry_get_text(GTK_ENTRY( - cwin->preferences_w->lastfm_uname_w))); + if (pragha_preferences_get_lastfm_support (cwin->preferences)) { + pragha_preferences_set_lastfm_user (cwin->preferences, + gtk_entry_get_text(GTK_ENTRY(cwin->preferences_w->lastfm_uname_w))); + cwin->cpref->lastfm_pass = g_strdup(gtk_entry_get_text(GTK_ENTRY( cwin->preferences_w->lastfm_pass_w))); @@ -794,14 +789,13 @@ static void update_preferences(struct con_win *cwin) /* Service Internet Option */ #ifdef HAVE_LIBCLASTFM - if (cwin->cpref->lastfm_support) { - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON( - cwin->preferences_w->lastfm_w), - TRUE); + if (pragha_preferences_get_lastfm_support (cwin->preferences)) { + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cwin->preferences_w->lastfm_w), TRUE); + gtk_entry_set_text(GTK_ENTRY(cwin->preferences_w->lastfm_uname_w), - cwin->cpref->lastfm_user); + pragha_preferences_get_lastfm_user (cwin->preferences)); gtk_entry_set_text(GTK_ENTRY(cwin->preferences_w->lastfm_pass_w), - cwin->cpref->lastfm_pass); + cwin->cpref->lastfm_pass); } #endif #ifdef HAVE_LIBGLYR @@ -917,25 +911,12 @@ void save_preferences(struct con_win *cwin) /* Services internet */ /* Save last.fm option */ #ifdef HAVE_LIBCLASTFM - g_key_file_set_boolean(cwin->cpref->configrc_keyfile, - GROUP_SERVICES, - KEY_LASTFM, - cwin->cpref->lastfm_support); - - if (!cwin->cpref->lastfm_support) { - pragha_preferences_remove_key(cwin->preferences, - GROUP_SERVICES, - KEY_LASTFM_USER); + if (!pragha_preferences_get_lastfm_support (cwin->preferences)) { pragha_preferences_remove_key(cwin->preferences, GROUP_SERVICES, KEY_LASTFM_PASS); } else { - if (cwin->cpref->lastfm_user) - g_key_file_set_string(cwin->cpref->configrc_keyfile, - GROUP_SERVICES, - KEY_LASTFM_USER, - cwin->cpref->lastfm_user); if (cwin->cpref->lastfm_pass) g_key_file_set_string(cwin->cpref->configrc_keyfile, GROUP_SERVICES, diff --git a/src/pragha-preferences.c b/src/pragha-preferences.c index de71cf60..0e878d9e 100644 --- a/src/pragha-preferences.c +++ b/src/pragha-preferences.c @@ -75,6 +75,8 @@ struct _PraghaPreferencesPrivate gboolean use_cddb; gboolean download_album_art; gboolean use_mpris2; + gboolean lastfm_support; + gchar *lastfm_user; }; enum @@ -113,6 +115,8 @@ enum PROP_USE_CDDB, PROP_DOWNLOAD_ALBUM_ART, PROP_USE_MPRIS2, + PROP_LASTFM_SUPPORT, + PROP_LASTFM_USER, LAST_PROP }; @@ -1274,6 +1278,61 @@ pragha_preferences_set_use_mpris2 (PraghaPreferences *preferences, g_object_notify_by_pspec(G_OBJECT(preferences), gParamSpecs[PROP_USE_MPRIS2]); } +/** + * pragha_preferences_get_lastfm_support: + * + */ +gboolean +pragha_preferences_get_lastfm_support (PraghaPreferences *preferences) +{ + g_return_val_if_fail(PRAGHA_IS_PREFERENCES(preferences), TRUE); + + return preferences->priv->lastfm_support; +} + +/** + * pragha_preferences_set_lastfm_support: + * + */ +void +pragha_preferences_set_lastfm_support (PraghaPreferences *preferences, + gboolean lastfm_support) +{ + g_return_if_fail(PRAGHA_IS_PREFERENCES(preferences)); + + preferences->priv->lastfm_support = lastfm_support; + + g_object_notify_by_pspec(G_OBJECT(preferences), gParamSpecs[PROP_LASTFM_SUPPORT]); +} + +/** + * pragha_preferences_get_lastfm_user: + * + */ +const gchar * +pragha_preferences_get_lastfm_user (PraghaPreferences *preferences) +{ + g_return_val_if_fail(PRAGHA_IS_PREFERENCES(preferences), NULL); + + return preferences->priv->lastfm_user; +} + +/** + * pragha_preferences_set_lastfm_user: + * + */ +void +pragha_preferences_set_lastfm_user (PraghaPreferences *preferences, + const gchar *lastfm_user) +{ + g_return_if_fail(PRAGHA_IS_PREFERENCES(preferences)); + + g_free(preferences->priv->lastfm_user); + preferences->priv->lastfm_user = g_strdup(lastfm_user); + + g_object_notify_by_pspec(G_OBJECT(preferences), gParamSpecs[PROP_LASTFM_USER]); +} + static void pragha_preferences_load_from_file(PraghaPreferences *preferences) { @@ -1283,7 +1342,8 @@ pragha_preferences_load_from_file(PraghaPreferences *preferences) gchar *album_art_pattern; gchar *last_folder, *last_folder_converted = NULL; gboolean add_recursively, timer_remaining_mode, show_osd, album_art_in_osd, actions_in_osd, hide_instead_close; - gboolean use_cddb, download_album_art, use_mpris2; + gboolean use_cddb, download_album_art, use_mpris2, lastfm_support; + gchar *lastfm_user; gchar *audio_sink, *audio_device, *audio_cd_device; gdouble software_volume; gint library_style, sidebar_size, album_art_size; @@ -1741,10 +1801,35 @@ pragha_preferences_load_from_file(PraghaPreferences *preferences) pragha_preferences_set_use_mpris2(preferences, use_mpris2); } + lastfm_support = g_key_file_get_boolean(priv->rc_keyfile, + GROUP_SERVICES, + KEY_LASTFM, + &error); + if (error) { + g_error_free(error); + error = NULL; + } + else { + pragha_preferences_set_lastfm_support(preferences, lastfm_support); + } + + lastfm_user = g_key_file_get_string(priv->rc_keyfile, + GROUP_SERVICES, + KEY_LASTFM_USER, + &error); + if (error) { + g_error_free(error); + error = NULL; + } + else { + pragha_preferences_set_lastfm_user(preferences, lastfm_user); + } + g_free(audio_sink); g_free(audio_device); g_free(audio_cd_device); g_free(album_art_pattern); + g_free(lastfm_user); g_free(last_folder); g_free(last_folder_converted); } @@ -1922,6 +2007,21 @@ pragha_preferences_finalize (GObject *object) KEY_ALLOW_MPRIS2, priv->use_mpris2); + g_key_file_set_boolean(priv->rc_keyfile, + GROUP_SERVICES, + KEY_LASTFM, + priv->lastfm_support); + + if (string_is_not_empty(priv->lastfm_user)) + g_key_file_set_string(priv->rc_keyfile, + GROUP_SERVICES, + KEY_LASTFM_USER, + priv->lastfm_user); + else + pragha_preferences_remove_key(preferences, + GROUP_SERVICES, + KEY_LASTFM_USER); + /* Save to key file */ data = g_key_file_to_data(priv->rc_keyfile, &length, NULL); @@ -2048,6 +2148,12 @@ pragha_preferences_get_property (GObject *object, case PROP_USE_MPRIS2: g_value_set_boolean (value, pragha_preferences_get_use_mpris2(preferences)); break; + case PROP_LASTFM_SUPPORT: + g_value_set_boolean (value, pragha_preferences_get_lastfm_support(preferences)); + break; + case PROP_LASTFM_USER: + g_value_set_string (value, pragha_preferences_get_lastfm_user(preferences)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } @@ -2161,6 +2267,12 @@ pragha_preferences_set_property (GObject *object, case PROP_USE_MPRIS2: pragha_preferences_set_use_mpris2(preferences, g_value_get_boolean(value)); break; + case PROP_LASTFM_SUPPORT: + pragha_preferences_set_lastfm_support(preferences, g_value_get_boolean(value)); + break; + case PROP_LASTFM_USER: + pragha_preferences_set_lastfm_user(preferences, g_value_get_string(value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); } @@ -2563,6 +2675,29 @@ pragha_preferences_class_init (PraghaPreferencesClass *klass) TRUE, PRAGHA_PREF_PARAMS); + /** + * PraghaPreferences:lastfm_support: + * + */ + gParamSpecs[PROP_LASTFM_SUPPORT] = + g_param_spec_boolean("lastfm-support", + "LastfmSupport", + "Lastfm Support Preference", + FALSE, + PRAGHA_PREF_PARAMS); + + /** + * PraghaPreferences:lastfm_user: + * + */ + gParamSpecs[PROP_LASTFM_USER] = + g_param_spec_string("lastfm-user", + "LastfmUser", + "Lastfm User Preference", + "", + PRAGHA_PREF_PARAMS); + + g_object_class_install_properties(object_class, LAST_PROP, gParamSpecs); } diff --git a/src/pragha-preferences.h b/src/pragha-preferences.h index 8590f0cc..723d8a1d 100644 --- a/src/pragha-preferences.h +++ b/src/pragha-preferences.h @@ -420,6 +420,21 @@ pragha_preferences_get_use_mpris2 (PraghaPreferences *preferences); void pragha_preferences_set_use_mpris2 (PraghaPreferences *preferences, gboolean use_mpris2); + +gboolean +pragha_preferences_get_lastfm_support (PraghaPreferences *preferences); + +void +pragha_preferences_set_lastfm_support (PraghaPreferences *preferences, + gboolean lastfm_support); + +const gchar * +pragha_preferences_get_lastfm_user (PraghaPreferences *preferences); + +void +pragha_preferences_set_lastfm_user (PraghaPreferences *preferences, + const gchar *lastfm_user); + G_END_DECLS #endif /* PRAGHA_PREFERENCES_H */ diff --git a/src/pragha.c b/src/pragha.c index b54060d9..16f10ac4 100644 --- a/src/pragha.c +++ b/src/pragha.c @@ -44,7 +44,6 @@ GThread *pragha_main_thread = NULL; static void preferences_free (struct con_pref *cpref) { #ifdef HAVE_LIBCLASTFM - g_free(cpref->lastfm_user); g_free(cpref->lastfm_pass); #endif g_free(cpref->installed_version); diff --git a/src/pragha.h b/src/pragha.h index 10a3a5a9..968f4376 100644 --- a/src/pragha.h +++ b/src/pragha.h @@ -103,8 +103,6 @@ struct con_pref { gint window_y; GKeyFile *configrc_keyfile; #ifdef HAVE_LIBCLASTFM - gboolean lastfm_support; - gchar *lastfm_user; gchar *lastfm_pass; #endif };