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

No issues found

  1 /*
  2  * rb-ipod-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 <gmodule.h>
 35 #include <gtk/gtk.h>
 36 #include <glib.h>
 37 #include <glib-object.h>
 38 
 39 #include "mediaplayerid.h"
 40 
 41 #include "rb-ipod-helpers.h"
 42 #include "rb-removable-media-manager.h"
 43 #include "rb-media-player-source.h"
 44 #include "rb-source.h"
 45 #include "rb-ipod-source.h"
 46 #include "rb-ipod-static-playlist-source.h"
 47 #include "rb-plugin-macros.h"
 48 #include "rb-debug.h"
 49 #include "rb-file-helpers.h"
 50 #include "rb-util.h"
 51 #include "rb-shell.h"
 52 #include "rb-stock-icons.h"
 53 #include "rb-display-page-tree.h"
 54 
 55 
 56 #define RB_TYPE_IPOD_PLUGIN		(rb_ipod_plugin_get_type ())
 57 #define RB_IPOD_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_IPOD_PLUGIN, RBIpodPlugin))
 58 #define RB_IPOD_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_IPOD_PLUGIN, RBIpodPluginClass))
 59 #define RB_IS_IPOD_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_IPOD_PLUGIN))
 60 #define RB_IS_IPOD_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_IPOD_PLUGIN))
 61 #define RB_IPOD_PLUGIN_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_IPOD_PLUGIN, RBIpodPluginClass))
 62 
 63 typedef struct
 64 {
 65 	PeasExtensionBase parent;
 66 
 67 	RBShell *shell;
 68 	GtkActionGroup *action_group;
 69 	guint ui_merge_id;
 70 
 71 	GList *ipod_sources;
 72 } RBIpodPlugin;
 73 
 74 typedef struct
 75 {
 76 	PeasExtensionBaseClass parent_class;
 77 } RBIpodPluginClass;
 78 
 79 
 80 G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
 81 
 82 static void rb_ipod_plugin_init (RBIpodPlugin *plugin);
 83 
 84 static RBSource * create_source_cb (RBRemovableMediaManager *rmm,
 85 				    GMount *mount,
 86 				    MPIDDevice *device_info,
 87 				    RBIpodPlugin *plugin);
 88 static void  rb_ipod_plugin_cmd_rename (GtkAction *action, RBSource *source);
 89 static void  rb_ipod_plugin_cmd_playlist_new (GtkAction *action, RBSource *source);
 90 static void  rb_ipod_plugin_cmd_playlist_rename (GtkAction *action, RBSource *source);
 91 static void  rb_ipod_plugin_cmd_playlist_delete (GtkAction *action, RBSource *source);
 92 static void  rb_ipod_plugin_cmd_properties (GtkAction *action, RBSource *source);
 93 
 94 RB_DEFINE_PLUGIN(RB_TYPE_IPOD_PLUGIN, RBIpodPlugin, rb_ipod_plugin,)
 95 
 96 
 97 static GtkActionEntry rb_ipod_plugin_actions [] =
 98 {
 99 	{ "iPodSourceRename", NULL, N_("_Rename"), NULL,
100 	  N_("Rename iPod"),
101 	  G_CALLBACK (rb_ipod_plugin_cmd_rename) },
102 	{ "iPodProperties", GTK_STOCK_PROPERTIES, N_("_Properties"), NULL,
103 	  N_("Display iPod properties"),
104 	  G_CALLBACK (rb_ipod_plugin_cmd_properties) },
105 	{ "iPodSourcePlaylistNew", RB_STOCK_PLAYLIST_NEW, N_("_New Playlist"), NULL,
106 	  N_("Add new playlist to iPod"),
107 	  G_CALLBACK (rb_ipod_plugin_cmd_playlist_new) },
108 	{ "iPodPlaylistSourceRename", NULL, N_("_Rename"), NULL,
109 	  N_("Rename playlist"),
110 	  G_CALLBACK (rb_ipod_plugin_cmd_playlist_rename) },
111 	{ "iPodPlaylistSourceDelete", GTK_STOCK_REMOVE, N_("_Delete"), NULL,
112 	  N_("Delete playlist"),
113 	  G_CALLBACK (rb_ipod_plugin_cmd_playlist_delete) },
114 };
115 
116 static void
117 rb_ipod_plugin_init (RBIpodPlugin *plugin)
118 {
119 	rb_debug ("RBIpodPlugin initialising");
120 }
121 
122 static void
123 impl_activate (PeasActivatable *bplugin)
124 {
125 	RBIpodPlugin *plugin = RB_IPOD_PLUGIN (bplugin);
126 	RBRemovableMediaManager *rmm = NULL;
127 	GtkUIManager *uimanager = NULL;
128 	RBShell *shell;
129 	gboolean scanned;
130 	char *file;
131 
132 	g_object_get (plugin, "object", &shell, NULL);
133 
134 	g_object_get (G_OBJECT (shell),
135 		      "removable-media-manager", &rmm,
136 		      "ui-manager", &uimanager,
137 		      NULL);
138 
139 	rb_media_player_source_init_actions (shell);
140 
141 	/* add ipod UI */
142 	plugin->action_group = gtk_action_group_new ("iPodActions");
143 	gtk_action_group_set_translation_domain (plugin->action_group,
144 						 GETTEXT_PACKAGE);
145 	_rb_action_group_add_display_page_actions (plugin->action_group,
146 						   G_OBJECT (shell),
147 						   rb_ipod_plugin_actions,
148 						   G_N_ELEMENTS (rb_ipod_plugin_actions));
149 	gtk_ui_manager_insert_action_group (uimanager, plugin->action_group, 0);
150 	file = rb_find_plugin_data_file (G_OBJECT (bplugin), "ipod-ui.xml");
151 	plugin->ui_merge_id = gtk_ui_manager_add_ui_from_file (uimanager,
152 							       file,
153 							       NULL);
154 	g_free (file);
155 
156 	/* watch for new removable media, and cause a rescan */
157 	g_signal_connect (G_OBJECT (rmm),
158 			  "create-source-mount", G_CALLBACK (create_source_cb),
159 			  plugin);
160 
161 	/* only scan if we're being loaded after the initial scan has been done */
162 	g_object_get (G_OBJECT (rmm), "scanned", &scanned, NULL);
163 	if (scanned)
164 		rb_removable_media_manager_scan (rmm);
165 
166 	g_object_unref (rmm);
167 	g_object_unref (uimanager);
168 	g_object_unref (shell);
169 }
170 
171 static void
172 impl_deactivate	(PeasActivatable *bplugin)
173 {
174 	RBIpodPlugin *plugin = RB_IPOD_PLUGIN (bplugin);
175 	RBRemovableMediaManager *rmm = NULL;
176 	GtkUIManager *uimanager = NULL;
177 	RBShell *shell;
178 
179 	g_object_get (plugin, "object", &shell, NULL);
180 
181 	g_object_get (shell,
182 		      "removable-media-manager", &rmm,
183 		      "ui-manager", &uimanager,
184 		      NULL);
185 
186 	gtk_ui_manager_remove_ui (uimanager, plugin->ui_merge_id);
187 	gtk_ui_manager_remove_action_group (uimanager, plugin->action_group);
188 
189 	g_signal_handlers_disconnect_by_func (G_OBJECT (rmm), create_source_cb, plugin);
190 
191 	g_list_foreach (plugin->ipod_sources, (GFunc)rb_display_page_delete_thyself, NULL);
192 	g_list_free (plugin->ipod_sources);
193 	plugin->ipod_sources = NULL;
194 
195 	g_object_unref (uimanager);
196 	g_object_unref (rmm);
197 	g_object_unref (shell);
198 }
199 
200 static void
201 rb_ipod_plugin_source_deleted (RBiPodSource *source, RBIpodPlugin *plugin)
202 {
203 	plugin->ipod_sources = g_list_remove (plugin->ipod_sources, source);
204 }
205 
206 static RBSource *
207 create_source_cb (RBRemovableMediaManager *rmm, GMount *mount, MPIDDevice *device_info, RBIpodPlugin *plugin)
208 {
209 	RBSource *src;
210 	RBShell *shell;
211 
212 	if (!rb_ipod_helpers_is_ipod (mount, device_info)) {
213 		return NULL;
214 	}
215 
216 	g_object_get (plugin, "object", &shell, NULL);
217 	src = RB_SOURCE (rb_ipod_source_new (G_OBJECT (plugin),
218 					     shell,
219 					     mount,
220 					     device_info));
221 	g_object_unref (shell);
222 
223 	plugin->ipod_sources = g_list_prepend (plugin->ipod_sources, src);
224 	g_signal_connect_object (G_OBJECT (src),
225 				 "deleted", G_CALLBACK (rb_ipod_plugin_source_deleted),
226 				 plugin, 0);
227 	return src;
228 }
229 
230 static void
231 rb_ipod_plugin_cmd_properties (GtkAction *action, RBSource *source)
232 {
233 	g_return_if_fail (RB_IS_IPOD_SOURCE (source));
234 	rb_media_player_source_show_properties (RB_MEDIA_PLAYER_SOURCE (source));
235 }
236  
237 static void
238 rb_ipod_plugin_cmd_rename (GtkAction *action, RBSource *source)
239 {
240 	RBDisplayPageTree *page_tree = NULL;
241 	RBShell *shell;
242 
243 	g_return_if_fail (RB_IS_IPOD_SOURCE (source));
244 
245 	/* FIXME: this is pretty ugly, the sourcelist should automatically add
246 	 * a "rename" menu item for sources that have can_rename == TRUE.
247 	 * This is a bit trickier to handle though, since playlists want
248 	 * to make rename sensitive/unsensitive instead of showing/hiding it
249 	 */
250 
251 	g_object_get (source, "shell", &shell, NULL);
252 	g_object_get (shell, "display-page-tree", &page_tree, NULL);
253 
254 	rb_display_page_tree_edit_source_name (page_tree, source);
255 	/* Once editing is done, notify::name will be fired on the source, and
256 	 * we'll catch that in our rename callback
257 	 */
258 	g_object_unref (page_tree);
259 	g_object_unref (shell);
260 }
261 
262 static void
263 rb_ipod_plugin_cmd_playlist_rename (GtkAction *action, RBSource *source)
264 {
265 	RBDisplayPageTree *page_tree = NULL;
266 	RBShell *shell;
267 
268 	g_return_if_fail (RB_IS_IPOD_STATIC_PLAYLIST_SOURCE (source));
269 
270 	g_object_get (source, "shell", &shell, NULL);
271 	g_object_get (shell, "display-page-tree", &page_tree, NULL);
272 	rb_display_page_tree_edit_source_name (page_tree, source);
273 	g_object_unref (page_tree);
274 	g_object_unref (shell);
275 }
276 
277 static void
278 rb_ipod_plugin_cmd_playlist_delete (GtkAction *action, RBSource *source)
279 {
280 	RBiPodSource *ipod_source;
281 
282 	g_return_if_fail (RB_IS_IPOD_STATIC_PLAYLIST_SOURCE (source));
283 
284 	g_object_get (source, "ipod-source", &ipod_source, NULL);
285 	rb_ipod_source_remove_playlist (ipod_source, source);
286 	g_object_unref (ipod_source);
287 }
288 
289 static void
290 rb_ipod_plugin_cmd_playlist_new (GtkAction *action, RBSource *source)
291 {
292 	g_return_if_fail (RB_IS_IPOD_SOURCE (source));
293 	rb_ipod_source_new_playlist (RB_IPOD_SOURCE (source));
294 }
295 
296 G_MODULE_EXPORT void
297 peas_register_types (PeasObjectModule *module)
298 {
299 	rb_ipod_plugin_register_type (G_TYPE_MODULE (module));
300 	_rb_ipod_source_register_type (G_TYPE_MODULE (module));
301 	_rb_ipod_static_playlist_source_register_type (G_TYPE_MODULE (module));
302 	_rb_ipod_db_register_type (G_TYPE_MODULE (module));
303 	peas_object_module_register_extension_type (module,
304 						    PEAS_TYPE_ACTIVATABLE,
305 						    RB_TYPE_IPOD_PLUGIN);
306 }