rb-audioscrobbler-plugin.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <string.h> /* For strlen */
  32. #include <glib/gi18n-lib.h>
  33. #include <gmodule.h>
  34. #include <gtk/gtk.h>
  35. #include <glib.h>
  36. #include <glib-object.h>
  37. #include "rb-audioscrobbler.h"
  38. #include "rb-lastfm-source.h"
  39. #include "rb-plugin.h"
  40. #include "rb-debug.h"
  41. #include "rb-shell.h"
  42. #include "rb-dialog.h"
  43. #define RB_TYPE_AUDIOSCROBBLER_PLUGIN (rb_audioscrobbler_plugin_get_type ())
  44. #define RB_AUDIOSCROBBLER_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPlugin))
  45. #define RB_AUDIOSCROBBLER_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPluginClass))
  46. #define RB_IS_AUDIOSCROBBLER_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN))
  47. #define RB_IS_AUDIOSCROBBLER_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_AUDIOSCROBBLER_PLUGIN))
  48. #define RB_AUDIOSCROBBLER_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_AUDIOSCROBBLER_PLUGIN, RBAudioscrobblerPluginClass))
  49. typedef struct
  50. {
  51. RBPlugin parent;
  52. RBAudioscrobbler *audioscrobbler;
  53. GtkWidget *preferences;
  54. guint ui_merge_id;
  55. RBSource *lastfm_source;
  56. } RBAudioscrobblerPlugin;
  57. typedef struct
  58. {
  59. RBPluginClass parent_class;
  60. } RBAudioscrobblerPluginClass;
  61. G_MODULE_EXPORT GType register_rb_plugin (GTypeModule *module);
  62. GType rb_audioscrobbler_plugin_get_type (void) G_GNUC_CONST;
  63. static void rb_audioscrobbler_plugin_init (RBAudioscrobblerPlugin *plugin);
  64. static void rb_audioscrobbler_plugin_finalize (GObject *object);
  65. static void impl_activate (RBPlugin *plugin, RBShell *shell);
  66. static void impl_deactivate (RBPlugin *plugin, RBShell *shell);
  67. static GtkWidget* impl_create_configure_dialog (RBPlugin *plugin);
  68. RB_PLUGIN_REGISTER(RBAudioscrobblerPlugin, rb_audioscrobbler_plugin)
  69. static void
  70. rb_audioscrobbler_plugin_class_init (RBAudioscrobblerPluginClass *klass)
  71. {
  72. GObjectClass *object_class = G_OBJECT_CLASS (klass);
  73. RBPluginClass *plugin_class = RB_PLUGIN_CLASS (klass);
  74. object_class->finalize = rb_audioscrobbler_plugin_finalize;
  75. plugin_class->activate = impl_activate;
  76. plugin_class->deactivate = impl_deactivate;
  77. plugin_class->create_configure_dialog = impl_create_configure_dialog;
  78. }
  79. static void
  80. rb_audioscrobbler_plugin_init (RBAudioscrobblerPlugin *plugin)
  81. {
  82. rb_debug ("RBAudioscrobblerPlugin initialising");
  83. }
  84. static void
  85. rb_audioscrobbler_plugin_finalize (GObject *object)
  86. {
  87. RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (object);
  88. rb_debug ("RBAudioscrobblerPlugin finalising");
  89. g_assert (plugin->audioscrobbler == NULL);
  90. if (plugin->preferences)
  91. gtk_widget_destroy (plugin->preferences);
  92. G_OBJECT_CLASS (rb_audioscrobbler_plugin_parent_class)->finalize (object);
  93. }
  94. static void
  95. impl_activate (RBPlugin *bplugin,
  96. RBShell *shell)
  97. {
  98. RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
  99. RBProxyConfig *proxy_config;
  100. GtkUIManager *uimanager = NULL;
  101. gboolean no_registration;
  102. char *file;
  103. /* Source icon data */
  104. gchar *icon_filename;
  105. gint icon_size;
  106. GdkPixbuf *icon_pixbuf;
  107. g_assert (plugin->audioscrobbler == NULL);
  108. g_object_get (G_OBJECT (shell),
  109. "proxy-config", &proxy_config,
  110. "no-registration", &no_registration,
  111. "ui-manager", &uimanager,
  112. NULL);
  113. /*
  114. * Don't use audioscrobbler when the no-registration flag is set.
  115. * This flag is only used to run multiple instances at the same time, and
  116. * last.fm only allows one active client per user.
  117. */
  118. if (!no_registration) {
  119. plugin->audioscrobbler = rb_audioscrobbler_new (RB_SHELL_PLAYER (rb_shell_get_player (shell)),
  120. proxy_config);
  121. }
  122. g_object_unref (G_OBJECT (proxy_config));
  123. file = rb_plugin_find_file (bplugin, "audioscrobbler-ui.xml");
  124. plugin->ui_merge_id = gtk_ui_manager_add_ui_from_file (uimanager,
  125. file,
  126. NULL);
  127. g_free (file);
  128. plugin->lastfm_source = rb_lastfm_source_new (bplugin, shell);
  129. icon_filename = rb_plugin_find_file (bplugin, "as-icon.png");
  130. gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &icon_size, NULL);
  131. icon_pixbuf = gdk_pixbuf_new_from_file_at_size (icon_filename,
  132. icon_size, icon_size,
  133. NULL);
  134. g_free (icon_filename);
  135. rb_source_set_pixbuf (plugin->lastfm_source, icon_pixbuf);
  136. g_object_unref (icon_pixbuf);
  137. rb_shell_append_source (shell, plugin->lastfm_source, NULL);
  138. g_object_unref (G_OBJECT (uimanager));
  139. }
  140. static void
  141. impl_deactivate (RBPlugin *bplugin,
  142. RBShell *shell)
  143. {
  144. RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
  145. GtkUIManager *uimanager = NULL;
  146. g_object_get (G_OBJECT (shell),
  147. "ui-manager", &uimanager,
  148. NULL);
  149. rb_source_delete_thyself (plugin->lastfm_source);
  150. plugin->lastfm_source = NULL;
  151. gtk_ui_manager_remove_ui (uimanager, plugin->ui_merge_id);
  152. g_object_unref (G_OBJECT (uimanager));
  153. if (plugin->audioscrobbler) {
  154. g_object_unref (plugin->audioscrobbler);
  155. plugin->audioscrobbler = NULL;
  156. }
  157. }
  158. static void
  159. preferences_response_cb (GtkWidget *dialog, gint response, RBPlugin *plugin)
  160. {
  161. gtk_widget_hide (dialog);
  162. }
  163. static GtkWidget*
  164. impl_create_configure_dialog (RBPlugin *bplugin)
  165. {
  166. RBAudioscrobblerPlugin *plugin = RB_AUDIOSCROBBLER_PLUGIN (bplugin);
  167. if (plugin->audioscrobbler == NULL)
  168. return NULL;
  169. if (plugin->preferences == NULL) {
  170. GtkWidget *widget;
  171. widget = rb_audioscrobbler_get_config_widget (plugin->audioscrobbler, bplugin);
  172. plugin->preferences = gtk_dialog_new_with_buttons (_("Last.fm Preferences"),
  173. NULL,
  174. GTK_DIALOG_DESTROY_WITH_PARENT,
  175. GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
  176. NULL);
  177. gtk_dialog_set_has_separator (GTK_DIALOG (plugin->preferences), FALSE);
  178. gtk_container_set_border_width (GTK_CONTAINER (plugin->preferences), 5);
  179. gtk_window_set_resizable (GTK_WINDOW (plugin->preferences), FALSE);
  180. g_signal_connect (G_OBJECT (plugin->preferences),
  181. "response",
  182. G_CALLBACK (preferences_response_cb),
  183. plugin);
  184. gtk_widget_hide_on_delete (plugin->preferences);
  185. gtk_container_add (GTK_CONTAINER (GTK_DIALOG (plugin->preferences)->vbox), widget);
  186. }
  187. gtk_widget_show_all (plugin->preferences);
  188. return plugin->preferences;
  189. }