hythmbox-2.98/plugins/audioscrobbler/rb-audioscrobbler-plugin.c

No issues found

  1 /*
  2  * rb-audioscrobbler-plugin.c
  3  *
  4  * Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as published by
  8  * the Free Software Foundation; either version 2, or (at your option)
  9  * any later version.
 10  *
 11  * The Rhythmbox authors hereby grant permission for non-GPL compatible
 12  * GStreamer plugins to be used and distributed together with GStreamer
 13  * and Rhythmbox. This permission is above and beyond the permissions granted
 14  * by the GPL license by which Rhythmbox is covered. If you modify this code
 15  * you may extend this exception to your version of the code, but you are not
 16  * obligated to do so. If you do not wish to do so, delete this exception
 17  * statement from your version.
 18  *
 19  * This program is distributed in the hope that it will be useful,
 20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22  * GNU General Public License for more details.
 23  *
 24  * You should have received a copy of the GNU General Public License
 25  * along with this program; if not, write to the Free Software
 26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 27  */
 28 
 29 #ifdef HAVE_CONFIG_H
 30 #include <config.h>
 31 #endif
 32 
 33 #include <glib/gi18n-lib.h>
 34 #include <gtk/gtk.h>
 35 #include <glib.h>
 36 #include <glib-object.h>
 37 
 38 #include <libpeas-gtk/peas-gtk.h>
 39 
 40 #include <lib/rb-builder-helpers.h>
 41 #include <lib/rb-debug.h>
 42 #include <lib/rb-file-helpers.h>
 43 #include <sources/rb-display-page-group.h>
 44 #include <plugins/rb-plugin-macros.h>
 45 #include <shell/rb-shell.h>
 46 
 47 #include "rb-audioscrobbler-account.h"
 48 #include "rb-audioscrobbler.h"
 49 #include "rb-audioscrobbler-play-order.h"
 50 #include "rb-audioscrobbler-profile-page.h"
 51 #include "rb-audioscrobbler-radio-source.h"
 52 #include "rb-audioscrobbler-radio-track-entry-type.h"
 53 #include "rb-audioscrobbler-service.h"
 54 #include "rb-audioscrobbler-user.h"
 55 
 56 #define RB_TYPE_AUDIOSCROBBLER_PLUGIN		(rb_audioscrobbler_plugin_get_type ())
 57 #define RB_AUDIOSCROBBLER_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPlugin))
 58 #define RB_AUDIOSCROBBLER_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPluginClass))
 59 #define RB_IS_AUDIOSCROBBLER_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN))
 60 #define RB_IS_AUDIOSCROBBLER_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_AUDIOSCROBBLER_PLUGIN))
 61 #define RB_AUDIOSCROBBLER_PLUGIN_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPluginClass))
 62 
 63 typedef struct
 64 {
 65 	PeasExtensionBase parent;
 66 
 67 	GtkWidget *config_dialog;
 68 
 69 	/* Last.fm */
 70 	GSettings *lastfm_settings;
 71 	GtkWidget *lastfm_enabled_check;
 72 	RBDisplayPage *lastfm_page;
 73 
 74 	/* Libre.fm */
 75 	GSettings *librefm_settings;
 76 	GtkWidget *librefm_enabled_check;
 77 	RBDisplayPage *librefm_page;
 78 } RBAudioscrobblerPlugin;
 79 
 80 typedef struct
 81 {
 82 	PeasExtensionBaseClass parent_class;
 83 } RBAudioscrobblerPluginClass;
 84 
 85 G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
 86 
 87 static GtkWidget *impl_create_configure_widget (PeasGtkConfigurable *bplugin);
 88 static void peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface);
 89 
 90 static void lastfm_settings_changed_cb (GSettings *settings,
 91 					const char *key,
 92 					RBAudioscrobblerPlugin *plugin);
 93 static void librefm_settings_changed_cb (GSettings *settings,
 94 					 const char *key,
 95 					 RBAudioscrobblerPlugin *plugin);
 96 
 97 RB_DEFINE_PLUGIN(RB_TYPE_AUDIOSCROBBLER_PLUGIN,
 98 		 RBAudioscrobblerPlugin,
 99 		 rb_audioscrobbler_plugin,
100 		 (G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_GTK_TYPE_CONFIGURABLE,
101 						peas_gtk_configurable_iface_init)))
102 
103 static void
104 rb_audioscrobbler_plugin_init (RBAudioscrobblerPlugin *plugin)
105 {
106 	rb_debug ("RBAudioscrobblerPlugin initialising");
107 
108 	plugin->lastfm_settings = g_settings_new_with_path (AUDIOSCROBBLER_SETTINGS_SCHEMA,
109 							    AUDIOSCROBBLER_SETTINGS_PATH "/Last.fm");
110 	plugin->librefm_settings = g_settings_new_with_path (AUDIOSCROBBLER_SETTINGS_SCHEMA,
111 							     AUDIOSCROBBLER_SETTINGS_PATH "/Libre.fm");
112 }
113 
114 static void
115 impl_activate (PeasActivatable *bplugin)
116 {
117 	RBAudioscrobblerPlugin *plugin;
118 
119 	plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
120 
121 	g_signal_connect_object (plugin->lastfm_settings,
122 				 "changed",
123 				 G_CALLBACK (lastfm_settings_changed_cb),
124 				 plugin, 0);
125 	lastfm_settings_changed_cb (plugin->lastfm_settings, AUDIOSCROBBLER_SERVICE_ENABLED_KEY, plugin);
126 
127 	g_signal_connect_object (plugin->librefm_settings,
128 				 "changed",
129 				 G_CALLBACK (librefm_settings_changed_cb),
130 				 plugin, 0);
131 	librefm_settings_changed_cb (plugin->librefm_settings, AUDIOSCROBBLER_SERVICE_ENABLED_KEY, plugin);
132 }
133 
134 static void
135 impl_deactivate	(PeasActivatable *bplugin)
136 {
137 	RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
138 
139 	if (plugin->config_dialog != NULL) {
140 		gtk_widget_destroy (plugin->config_dialog);
141 		plugin->config_dialog = NULL;
142 	}
143 
144 	if (plugin->lastfm_settings != NULL) {
145 		g_object_unref (plugin->lastfm_settings);
146 		plugin->lastfm_settings = NULL;
147 	}
148 
149 	if (plugin->lastfm_page != NULL) {
150 		rb_display_page_delete_thyself (plugin->lastfm_page);
151 		plugin->lastfm_page = NULL;
152 	}
153 
154 	if (plugin->librefm_settings != NULL) {
155 		g_object_unref (plugin->librefm_settings);
156 		plugin->librefm_settings = NULL;
157 	}
158 
159 	if (plugin->librefm_page != NULL) {
160 		rb_display_page_delete_thyself (plugin->librefm_page);
161 		plugin->librefm_page = NULL;
162 	}
163 }
164 
165 static GtkWidget *
166 impl_create_configure_widget (PeasGtkConfigurable *bplugin)
167 {
168 	RBAudioscrobblerPlugin *plugin;
169 	char *builderfile;
170 	GtkBuilder *builder;
171 	GtkWidget *widget;
172 
173 	plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
174 
175 	builderfile = rb_find_plugin_data_file (G_OBJECT (plugin), "audioscrobbler-preferences.ui");
176 	if (builderfile == NULL) {
177 		g_warning ("can't find audioscrobbler-preferences.ui");
178 		return NULL;
179 	}
180 
181 	builder = rb_builder_load (builderfile, plugin);
182 	g_free (builderfile);
183 
184 	widget = GTK_WIDGET (gtk_builder_get_object (builder, "config"));
185 	g_object_ref_sink (widget);
186 
187 	plugin->lastfm_enabled_check = GTK_WIDGET (gtk_builder_get_object (builder, "lastfm_enabled_check"));
188 	g_settings_bind (plugin->lastfm_settings, AUDIOSCROBBLER_SERVICE_ENABLED_KEY, plugin->lastfm_enabled_check, "active", G_SETTINGS_BIND_DEFAULT);
189 	plugin->librefm_enabled_check = GTK_WIDGET (gtk_builder_get_object (builder, "librefm_enabled_check"));
190 	g_settings_bind (plugin->librefm_settings, AUDIOSCROBBLER_SERVICE_ENABLED_KEY, plugin->librefm_enabled_check, "active", G_SETTINGS_BIND_DEFAULT);
191 
192 	g_object_unref (builder);
193 	return widget;
194 }
195 
196 static void
197 peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface)
198 {
199 	iface->create_configure_widget = impl_create_configure_widget;
200 }
201 
202 static void
203 lastfm_settings_changed_cb (GSettings *settings,
204 			    const char *key,
205 			    RBAudioscrobblerPlugin *plugin)
206 {
207 	gboolean enabled;
208 	if (g_strcmp0 (key, AUDIOSCROBBLER_SERVICE_ENABLED_KEY) != 0) {
209 		return;
210 	}
211 
212 	enabled = g_settings_get_boolean (settings, key);
213 	if (enabled == TRUE && plugin->lastfm_page == NULL) {
214 		RBAudioscrobblerService *lastfm;
215 		RBShell *shell;
216 
217 		lastfm = rb_audioscrobbler_service_new_lastfm ();
218 		g_object_get (plugin, "object", &shell, NULL);
219 		plugin->lastfm_page = rb_audioscrobbler_profile_page_new (shell,
220 		                                                          G_OBJECT (plugin),
221 		                                                          lastfm);
222 		g_object_unref (shell);
223 		g_object_unref (lastfm);
224 	} else if (enabled == FALSE && plugin->lastfm_page != NULL) {
225 		rb_display_page_delete_thyself (plugin->lastfm_page);
226 		plugin->lastfm_page = NULL;
227 	}
228 }
229 
230 static void
231 librefm_settings_changed_cb (GSettings *settings,
232 			     const char *key,
233 			     RBAudioscrobblerPlugin *plugin)
234 {
235 	gboolean enabled;
236 	if (g_strcmp0 (key, AUDIOSCROBBLER_SERVICE_ENABLED_KEY) != 0) {
237 		return;
238 	}
239 
240 	enabled = g_settings_get_boolean (settings, key);
241 	if (enabled == TRUE && plugin->librefm_page == NULL) {
242 		RBAudioscrobblerService *librefm;
243 		RBShell *shell;
244 
245 		librefm = rb_audioscrobbler_service_new_librefm ();
246 		g_object_get (plugin, "object", &shell, NULL);
247 		plugin->librefm_page = rb_audioscrobbler_profile_page_new (shell,
248 		                                                           G_OBJECT (plugin),
249 		                                                           librefm);
250 		g_object_unref (librefm);
251 		g_object_unref (shell);
252 	} else if (enabled == FALSE && plugin->librefm_page != NULL) {
253 		rb_display_page_delete_thyself (plugin->librefm_page);
254 		plugin->librefm_page = NULL;
255 	}
256 }
257 
258 G_MODULE_EXPORT void
259 peas_register_types (PeasObjectModule *module)
260 {
261 	rb_audioscrobbler_plugin_register_type (G_TYPE_MODULE (module));
262 	_rb_audioscrobbler_account_register_type (G_TYPE_MODULE (module));
263 	_rb_audioscrobbler_register_type (G_TYPE_MODULE (module));
264 	_rb_audioscrobbler_play_order_register_type (G_TYPE_MODULE (module));
265 	_rb_audioscrobbler_profile_page_register_type (G_TYPE_MODULE (module));
266 	_rb_audioscrobbler_radio_source_register_type (G_TYPE_MODULE (module));
267 	_rb_audioscrobbler_radio_track_entry_type_register_type (G_TYPE_MODULE (module));
268 	_rb_audioscrobbler_service_register_type (G_TYPE_MODULE (module));
269 	_rb_audioscrobbler_user_register_type (G_TYPE_MODULE (module));
270 
271 	peas_object_module_register_extension_type (module,
272 						    PEAS_TYPE_ACTIVATABLE,
273 						    RB_TYPE_AUDIOSCROBBLER_PLUGIN);
274 	peas_object_module_register_extension_type (module,
275 						    PEAS_GTK_TYPE_CONFIGURABLE,
276 						    RB_TYPE_AUDIOSCROBBLER_PLUGIN);
277 }