No issues found
1 /*
2 * rb-generic-player-plugin.c
3 *
4 * Copyright (C) 2006 Jonathan Matthew
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 #define __EXTENSIONS__
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <string.h> /* For strlen */
36 #include <glib/gi18n-lib.h>
37 #include <gmodule.h>
38 #include <gtk/gtk.h>
39 #include <glib.h>
40 #include <glib-object.h>
41
42 #include "rb-plugin-macros.h"
43 #include "rb-debug.h"
44 #include "rb-shell.h"
45 #include "rb-dialog.h"
46 #include "rb-removable-media-manager.h"
47 #include "rb-generic-player-source.h"
48 #include "rb-generic-player-playlist-source.h"
49 #include "rb-file-helpers.h"
50 #include "rb-stock-icons.h"
51 #include "rb-nokia770-source.h"
52 #include "rb-psp-source.h"
53 #include "rb-display-page-tree.h"
54
55
56 #define RB_TYPE_GENERIC_PLAYER_PLUGIN (rb_generic_player_plugin_get_type ())
57 #define RB_GENERIC_PLAYER_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_GENERIC_PLAYER_PLUGIN, RBGenericPlayerPlugin))
58 #define RB_GENERIC_PLAYER_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_GENERIC_PLAYER_PLUGIN, RBGenericPlayerPluginClass))
59 #define RB_IS_GENERIC_PLAYER_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_GENERIC_PLAYER_PLUGIN))
60 #define RB_IS_GENERIC_PLAYER_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_GENERIC_PLAYER_PLUGIN))
61 #define RB_GENERIC_PLAYER_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_GENERIC_PLAYER_PLUGIN, RBGenericPlayerPluginClass))
62
63 typedef struct
64 {
65 PeasExtensionBase parent;
66
67 guint ui_merge_id;
68
69 GList *player_sources;
70 GtkActionGroup *actions;
71 } RBGenericPlayerPlugin;
72
73 typedef struct
74 {
75 PeasExtensionBaseClass parent_class;
76 } RBGenericPlayerPluginClass;
77
78
79 G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);
80
81 static void rb_generic_player_plugin_init (RBGenericPlayerPlugin *plugin);
82
83 static void rb_generic_player_plugin_new_playlist (GtkAction *action, RBSource *source);
84 static void rb_generic_player_plugin_delete_playlist (GtkAction *action, RBSource *source);
85 static void rb_generic_player_plugin_properties (GtkAction *action, RBSource *source);
86
87 RB_DEFINE_PLUGIN(RB_TYPE_GENERIC_PLAYER_PLUGIN, RBGenericPlayerPlugin, rb_generic_player_plugin,)
88
89 static GtkActionEntry rb_generic_player_plugin_actions[] = {
90 { "GenericPlayerSourceNewPlaylist", RB_STOCK_PLAYLIST_NEW, N_("New Playlist"), NULL,
91 N_("Create a new playlist on this device"),
92 G_CALLBACK (rb_generic_player_plugin_new_playlist) },
93 { "GenericPlayerPlaylistDelete", GTK_STOCK_DELETE, N_("Delete Playlist"), NULL,
94 N_("Delete this playlist"),
95 G_CALLBACK (rb_generic_player_plugin_delete_playlist) },
96 { "GenericPlayerSourceProperties", GTK_STOCK_PROPERTIES, N_("_Properties"), NULL,
97 N_("Display device properties"),
98 G_CALLBACK (rb_generic_player_plugin_properties) }
99 };
100
101 static void
102 rb_generic_player_plugin_init (RBGenericPlayerPlugin *plugin)
103 {
104 rb_debug ("RBGenericPlayerPlugin initialising");
105 }
106
107 static void
108 rb_generic_player_plugin_source_deleted (RBGenericPlayerSource *source, RBGenericPlayerPlugin *plugin)
109 {
110 plugin->player_sources = g_list_remove (plugin->player_sources, source);
111 }
112
113 static void
114 rb_generic_player_plugin_new_playlist (GtkAction *action, RBSource *source)
115 {
116 RBShell *shell;
117 RBSource *playlist;
118 RBDisplayPageTree *page_tree;
119 RhythmDBEntryType *entry_type;
120
121 g_return_if_fail (RB_IS_GENERIC_PLAYER_SOURCE (source));
122 g_object_get (source,
123 "shell", &shell,
124 "entry-type", &entry_type,
125 NULL);
126
127 playlist = rb_generic_player_playlist_source_new (shell, RB_GENERIC_PLAYER_SOURCE (source), NULL, NULL, entry_type);
128 g_object_unref (entry_type);
129
130 rb_generic_player_source_add_playlist (RB_GENERIC_PLAYER_SOURCE (source),
131 shell,
132 playlist);
133
134 g_object_get (shell, "display-page-tree", &page_tree, NULL);
135 rb_display_page_tree_edit_source_name (page_tree, playlist);
136 g_object_unref (page_tree);
137
138 g_object_unref (shell);
139 }
140
141 static void
142 rb_generic_player_plugin_delete_playlist (GtkAction *action, RBSource *source)
143 {
144 g_return_if_fail (RB_IS_GENERIC_PLAYER_PLAYLIST_SOURCE (source));
145
146 rb_generic_player_playlist_delete_from_player (RB_GENERIC_PLAYER_PLAYLIST_SOURCE (source));
147 rb_display_page_delete_thyself (RB_DISPLAY_PAGE (source));
148 }
149
150 static RBSource *
151 create_source_cb (RBRemovableMediaManager *rmm, GMount *mount, MPIDDevice *device_info, RBGenericPlayerPlugin *plugin)
152 {
153 RBSource *source = NULL;
154 RBShell *shell;
155
156 g_object_get (plugin, "object", &shell, NULL);
157
158 if (rb_psp_is_mount_player (mount, device_info))
159 source = rb_psp_source_new (G_OBJECT (plugin), shell, mount, device_info);
160 if (source == NULL && rb_nokia770_is_mount_player (mount, device_info))
161 source = rb_nokia770_source_new (G_OBJECT (plugin), shell, mount, device_info);
162 if (source == NULL && rb_generic_player_is_mount_player (mount, device_info))
163 source = rb_generic_player_source_new (G_OBJECT (plugin), shell, mount, device_info);
164
165 if (plugin->actions == NULL) {
166 plugin->actions = gtk_action_group_new ("GenericPlayerActions");
167 gtk_action_group_set_translation_domain (plugin->actions, GETTEXT_PACKAGE);
168
169 _rb_action_group_add_display_page_actions (plugin->actions,
170 G_OBJECT (shell),
171 rb_generic_player_plugin_actions,
172 G_N_ELEMENTS (rb_generic_player_plugin_actions));
173 }
174
175 if (source) {
176 if (plugin->ui_merge_id == 0) {
177 GtkUIManager *uimanager = NULL;
178 char *file = NULL;
179
180 g_object_get (shell, "ui-manager", &uimanager, NULL);
181
182 gtk_ui_manager_insert_action_group (uimanager, plugin->actions, 0);
183
184 file = rb_find_plugin_data_file (G_OBJECT (plugin), "generic-player-ui.xml");
185 plugin->ui_merge_id = gtk_ui_manager_add_ui_from_file (uimanager,
186 file,
187 NULL);
188 g_free (file);
189 g_object_unref (uimanager);
190 }
191
192 plugin->player_sources = g_list_prepend (plugin->player_sources, source);
193 g_signal_connect_object (G_OBJECT (source),
194 "deleted", G_CALLBACK (rb_generic_player_plugin_source_deleted),
195 plugin, 0);
196 }
197
198 g_object_unref (shell);
199 return source;
200 }
201
202
203
204 static void
205 impl_activate (PeasActivatable *plugin)
206 {
207 RBGenericPlayerPlugin *pi = RB_GENERIC_PLAYER_PLUGIN (plugin);
208 RBRemovableMediaManager *rmm;
209 RBShell *shell;
210 gboolean scanned;
211
212 g_object_get (plugin, "object", &shell, NULL);
213 g_object_get (shell, "removable-media-manager", &rmm, NULL);
214
215 /* watch for new removable media. use connect_after so
216 * plugins for more specific device types can get in first.
217 */
218 g_signal_connect_after (G_OBJECT (rmm),
219 "create-source-mount", G_CALLBACK (create_source_cb),
220 pi);
221
222 /* only scan if we're being loaded after the initial scan has been done */
223 g_object_get (rmm, "scanned", &scanned, NULL);
224 if (scanned)
225 rb_removable_media_manager_scan (rmm);
226
227 g_object_unref (rmm);
228 g_object_unref (shell);
229 }
230
231 static void
232 impl_deactivate (PeasActivatable *bplugin)
233 {
234 RBGenericPlayerPlugin *plugin = RB_GENERIC_PLAYER_PLUGIN (bplugin);
235 RBRemovableMediaManager *rmm;
236 GtkUIManager *uimanager;
237 RBShell *shell;
238
239 g_object_get (plugin, "object", &shell, NULL);
240 g_object_get (shell,
241 "removable-media-manager", &rmm,
242 "ui-manager", &uimanager,
243 NULL);
244
245 g_signal_handlers_disconnect_by_func (G_OBJECT (rmm), create_source_cb, plugin);
246
247 g_list_foreach (plugin->player_sources, (GFunc)rb_display_page_delete_thyself, NULL);
248 g_list_free (plugin->player_sources);
249 plugin->player_sources = NULL;
250
251 if (plugin->ui_merge_id) {
252 gtk_ui_manager_remove_ui (uimanager, plugin->ui_merge_id);
253 plugin->ui_merge_id = 0;
254 }
255
256 g_object_unref (uimanager);
257 g_object_unref (rmm);
258 g_object_unref (shell);
259 }
260
261 static void
262 rb_generic_player_plugin_properties (GtkAction *action, RBSource *source)
263 {
264 g_return_if_fail (RB_IS_GENERIC_PLAYER_SOURCE (source));
265 rb_media_player_source_show_properties (RB_MEDIA_PLAYER_SOURCE (source));
266 }
267
268 G_MODULE_EXPORT void
269 peas_register_types (PeasObjectModule *module)
270 {
271 rb_generic_player_plugin_register_type (G_TYPE_MODULE (module));
272 _rb_generic_player_source_register_type (G_TYPE_MODULE (module));
273 _rb_generic_player_playlist_source_register_type (G_TYPE_MODULE (module));
274 _rb_nokia770_source_register_type (G_TYPE_MODULE (module));
275 _rb_psp_source_register_type (G_TYPE_MODULE (module));
276
277 peas_object_module_register_extension_type (module,
278 PEAS_TYPE_ACTIVATABLE,
279 RB_TYPE_GENERIC_PLAYER_PLUGIN);
280 }