1 /*
2  * rb-android-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 <gudev/gudev.h>
43 
44 #include "rb-plugin-macros.h"
45 #include "rb-debug.h"
46 #include "rb-shell.h"
47 #include "rb-dialog.h"
48 #include "rb-removable-media-manager.h"
49 #include "rb-file-helpers.h"
50 #include "rb-display-page-tree.h"
51 #include "rb-builder-helpers.h"
52 #include "rb-application.h"
53 #include "rb-android-source.h"
54 
55 
56 #define RB_TYPE_ANDROID_PLUGIN		(rb_android_plugin_get_type ())
57 #define RB_ANDROID_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_ANDROID_PLUGIN, RBAndroidPlugin))
58 #define RB_ANDROID_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_ANDROID_PLUGIN, RBAndroidPluginClass))
59 #define RB_IS_ANDROID_PLUGIN(o)		(G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_ANDROID_PLUGIN))
60 #define RB_IS_ANDROID_PLUGIN_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_ANDROID_PLUGIN))
61 #define RB_ANDROID_PLUGIN_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_ANDROID_PLUGIN, RBAndroidPluginClass))
62 
63 typedef struct
64 {
65 	PeasExtensionBase parent;
66 
67 	GList *sources;
68 } RBAndroidPlugin;
69 
70 typedef struct
71 {
72 	PeasExtensionBaseClass parent_class;
73 } RBAndroidPluginClass;
74 
75 
76 G_MODULE_EXPORT void peas_register_types (PeasObjectModule  *module);
77 
78 static void rb_android_plugin_init (RBAndroidPlugin *plugin);
79 
80 RB_DEFINE_PLUGIN(RB_TYPE_ANDROID_PLUGIN, RBAndroidPlugin, rb_android_plugin,)
81 
82 static void
rb_android_plugin_init(RBAndroidPlugin * plugin)83 rb_android_plugin_init (RBAndroidPlugin *plugin)
84 {
85 	rb_debug ("RBAndroidPlugin initialising");
86 }
87 
88 static void
source_deleted_cb(RBAndroidSource * source,RBAndroidPlugin * plugin)89 source_deleted_cb (RBAndroidSource *source, RBAndroidPlugin *plugin)
90 {
91 	plugin->sources = g_list_remove (plugin->sources, source);
92 }
93 
94 static RBSource *
create_source_cb(RBRemovableMediaManager * rmm,GVolume * volume,RBAndroidPlugin * plugin)95 create_source_cb (RBRemovableMediaManager *rmm, GVolume *volume, RBAndroidPlugin *plugin)
96 {
97 	RBSource *source = NULL;
98 	RBShell *shell;
99 	RhythmDB *db;
100 	RhythmDBEntryType *entry_type;
101 	RhythmDBEntryType *error_type;
102 	RhythmDBEntryType *ignore_type;
103 	GObject *dev;
104 	GUdevDevice *gudev_device;
105 	GtkBuilder *builder;
106 	GMenu *toolbar;
107 	GSettings *settings;
108 	GFile *root;
109 	MPIDDevice *device_info;
110 	const char *device_serial;
111 	const char *mpi_file;
112 	char *uri_prefix;
113 	char *name;
114 	char *path;
115 
116 	dev = rb_removable_media_manager_get_gudev_device (rmm, volume);
117 	if (dev == NULL) {
118 		return NULL;
119 	}
120 	gudev_device = G_UDEV_DEVICE (dev);
121 
122 	if (rb_removable_media_manager_device_is_android (rmm, G_OBJECT (gudev_device)) == FALSE) {
123 		g_object_unref (gudev_device);
124 		return NULL;
125 	}
126 
127 #if defined(USE_UNINSTALLED_DIRS)
128 	mpi_file = rb_find_plugin_data_file (G_OBJECT (plugin), "android.mpi");
129 #else
130 	mpi_file = "/org/gnome/Rhythmbox/android/android.mpi";
131 #endif
132 	device_info = mpid_device_new_from_mpi_file (mpi_file);
133 
134 	path = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
135 
136 	g_object_get (plugin, "object", &shell, NULL);
137 	g_object_get (shell, "db", &db, NULL);
138 
139 	device_serial = g_udev_device_get_property (gudev_device, "ID_SERIAL");
140 
141 	root = g_volume_get_activation_root (volume);
142 	uri_prefix = g_file_get_uri (root);
143 
144 	rb_debug ("metadata cache mapping: %s <=> %s", uri_prefix, device_serial);
145 
146 	name = g_strdup_printf ("android: %s", path);
147 	entry_type = g_object_new (RB_TYPE_MEDIA_PLAYER_ENTRY_TYPE,
148 				   "db", db,
149 				   "name", name,
150 				   "save-to-disk", FALSE,
151 				   "category", RHYTHMDB_ENTRY_NORMAL,
152 				   "cache-name", "android-mtp",
153 				   "key-prefix", device_serial,
154 				   "uri-prefix", uri_prefix,
155 				   NULL);
156 	rhythmdb_register_entry_type (db, entry_type);
157 	g_free (name);
158 
159 	name = g_strdup_printf ("android (ignore): %s", path);
160 	ignore_type = g_object_new (RB_TYPE_MEDIA_PLAYER_ENTRY_TYPE,
161 				    "db", db,
162 				    "name", name,
163 				    "save-to-disk", FALSE,
164 				    "category", RHYTHMDB_ENTRY_VIRTUAL,
165 				    "cache-name", "android-mtp",
166 				    "key-prefix", device_serial,
167 				    "uri-prefix", uri_prefix,
168 				    NULL);
169 	rhythmdb_register_entry_type (db, ignore_type);
170 	g_free (name);
171 
172 	name = g_strdup_printf ("android (errors): %s", path);
173 	error_type = g_object_new (RHYTHMDB_TYPE_ENTRY_TYPE,
174 				   "db", db,
175 				   "name", name,
176 				   "save-to-disk", FALSE,
177 				   "category", RHYTHMDB_ENTRY_VIRTUAL,
178 				   NULL);
179 	rhythmdb_register_entry_type (db, error_type);
180 	g_free (name);
181 
182 	g_free (uri_prefix);
183 	g_object_unref (db);
184 
185 	builder = rb_builder_load_plugin_file (G_OBJECT (plugin), "android-toolbar.ui", NULL);
186 	toolbar = G_MENU (gtk_builder_get_object (builder, "android-toolbar"));
187 	rb_application_link_shared_menus (RB_APPLICATION (g_application_get_default ()), toolbar);
188 
189 	settings = g_settings_new ("org.gnome.rhythmbox.plugins.android");
190 
191 	source = RB_SOURCE (g_object_new (RB_TYPE_ANDROID_SOURCE,
192 					  "plugin", plugin,
193 					  "entry-type", entry_type,
194 					  "ignore-entry-type", ignore_type,
195 					  "error-entry-type", error_type,
196 					  "volume", volume,
197 					  "mount-root", root,
198 					  "shell", shell,
199 					  "device-info", device_info,
200 					  "load-status", RB_SOURCE_LOAD_STATUS_LOADING,
201 					  "settings", g_settings_get_child (settings, "source"),
202 					  "encoding-settings", g_settings_get_child (settings, "encoding"),
203 					  "toolbar-menu", toolbar,
204 					  "gudev-device", gudev_device,
205 					  NULL));
206 
207 	g_object_unref (settings);
208 	g_object_unref (builder);
209 	g_object_unref (gudev_device);
210 
211 	rb_shell_register_entry_type_for_source (shell, RB_SOURCE (source), entry_type);
212 
213 	plugin->sources = g_list_prepend (plugin->sources, source);
214 	g_signal_connect_object (G_OBJECT (source),
215 				 "deleted", G_CALLBACK (source_deleted_cb),
216 				 plugin, 0);
217 
218 	g_object_unref (shell);
219 	return source;
220 }
221 
222 static void
impl_activate(PeasActivatable * plugin)223 impl_activate (PeasActivatable *plugin)
224 {
225 	RBAndroidPlugin *pi = RB_ANDROID_PLUGIN (plugin);
226 	RBRemovableMediaManager *rmm;
227 	RBShell *shell;
228 	gboolean scanned;
229 
230 	g_object_get (plugin, "object", &shell, NULL);
231 	g_object_get (shell, "removable-media-manager", &rmm, NULL);
232 
233 	g_signal_connect_object (rmm, "create-source-volume", G_CALLBACK (create_source_cb), pi, 0);
234 
235 	g_object_get (rmm, "scanned", &scanned, NULL);
236 	if (scanned)
237 		rb_removable_media_manager_scan (rmm);
238 
239 	g_object_unref (rmm);
240 	g_object_unref (shell);
241 }
242 
243 static void
impl_deactivate(PeasActivatable * bplugin)244 impl_deactivate	(PeasActivatable *bplugin)
245 {
246 	RBAndroidPlugin *plugin = RB_ANDROID_PLUGIN (bplugin);
247 	RBRemovableMediaManager *rmm;
248 	RBShell *shell;
249 
250 	g_object_get (plugin, "object", &shell, NULL);
251 	g_object_get (shell,
252 		      "removable-media-manager", &rmm,
253 		      NULL);
254 
255 	g_signal_handlers_disconnect_by_func (G_OBJECT (rmm), create_source_cb, plugin);
256 
257 	g_list_foreach (plugin->sources, (GFunc)rb_display_page_delete_thyself, NULL);
258 	g_list_free (plugin->sources);
259 	plugin->sources = NULL;
260 
261 	g_object_unref (rmm);
262 	g_object_unref (shell);
263 }
264 
265 G_MODULE_EXPORT void
peas_register_types(PeasObjectModule * module)266 peas_register_types (PeasObjectModule *module)
267 {
268 	rb_android_plugin_register_type (G_TYPE_MODULE (module));
269 	_rb_android_source_register_type (G_TYPE_MODULE (module));
270 
271 	peas_object_module_register_extension_type (module,
272 						    PEAS_TYPE_ACTIVATABLE,
273 						    RB_TYPE_ANDROID_PLUGIN);
274 }
275