File: autodetection.c

package info (click to toggle)
gtkpod 0.99.14-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,716 kB
  • ctags: 3,266
  • sloc: ansic: 43,120; sh: 4,837; xml: 1,316; lex: 637; perl: 264; makefile: 236; awk: 73; python: 35
file content (455 lines) | stat: -rw-r--r-- 10,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
|  Copyright (C) 2007 Jorg Schuler <jcsjcs at users.sourceforge.net>
|  Part of the gtkpod project.
|
|  URL: http://gtkpod.sourceforge.net/
|  URL: http://www.gtkpod.org
|
|  This program is free software; you can redistribute it and/or modify
|  it under the terms of the GNU General Public License as published by
|  the Free Software Foundation; either version 2 of the License, or
|  (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id: autodetection.c 1681 2007-08-18 06:16:04Z jcsjcs $
*/


#include "autodetection.h"
#include "config.h"
#include "misc.h"
#include "prefs.h"
#include <stdio.h>
#include <string.h>



#ifdef HAVE_GNOME_VFS
#include <libgnomevfs/gnome-vfs.h>
#endif
#ifdef HAVE_HAL
#include <libhal.h>
#include <dbus/dbus.h>
#endif

#undef DEBUG_AUTO
#ifdef DEBUG_AUTO
#   define _TO_STR(x) #x
#   define TO_STR(x) _TO_STR(x)
#   define debug(...) do { fprintf(stderr,  __FILE__ ":" TO_STR(__LINE__) ":" __VA_ARGS__); } while(0)
#else
#   define debug(...)
#endif



/* Find out if an itdb uses the mountpoint @mountpoint and return that
   itdb */
static iTunesDB *ad_find_repository_with_mountpoint (const gchar *mountpoint)
{
    GList *gl;
    gchar *mp;
    gint lenmp;
    iTunesDB *result = NULL;
    struct itdbs_head *itdbs;

    g_return_val_if_fail (mountpoint, NULL);

    itdbs = gp_get_itdbs_head (gtkpod_window);
    g_return_val_if_fail (itdbs, NULL);

    /* eliminate trailing dir separators ('/') */
    mp = g_strdup (mountpoint);
    lenmp = strlen (mountpoint);
    if ((lenmp > 0) && (mp[lenmp-1] == G_DIR_SEPARATOR))
    {
	mp[lenmp-1] = 0;
    }

    for (gl=itdbs->itdbs; gl; gl=gl->next)
    {
	iTunesDB *itdb = gl->data;
	g_return_val_if_fail (itdb, NULL);

	if (itdb->usertype & GP_ITDB_TYPE_IPOD)
	{
	    gchar *imp = get_itdb_prefs_string (itdb, KEY_MOUNTPOINT);
	    if (imp)
	    {
		gint comp;
		gint lenimp = strlen (imp);

		/* eliminate trailing dir separators ('/') */
		if ((lenimp > 0) && (imp[lenimp-1] == G_DIR_SEPARATOR))
		{
		    imp[lenmp-1] = 0;
		}

		comp = strcmp (mp, imp);

		g_free (imp);

		if (comp == 0)
		{
		    result = itdb;
		    break;
		}
	    }
	}
    }

    g_free (mp);

    return result;
}




#ifdef HAVE_GNOME_VFS
typedef struct _AutoDetect AutoDetect;

static gboolean ad_timeout_cb (gpointer data);


struct _AutoDetect
{
    GMutex *mutex;              /* shared lock */
    GList *new_ipod_uris;       /* list of new mounts */
    guint timeout_id;
};    

static AutoDetect *autodetect;


#ifdef HAVE_HAL
/* from rb-ipod-source.c (rhythmbox ipod plugin) */
static gboolean
hal_udi_is_ipod (const char *udi)
{
	LibHalContext *ctx;
	DBusConnection *conn;
	char *parent_udi;
	char *parent_name;
	gboolean result;
	DBusError error;
	gboolean inited = FALSE;

	result = FALSE;
	dbus_error_init (&error);

	conn = NULL;
	parent_udi = NULL;
	parent_name = NULL;

	ctx = libhal_ctx_new ();
	if (ctx == NULL) {
		/* FIXME: should we return an error somehow so that we can
		 * fall back to a check for iTunesDB presence instead ?
		 */
		debug ("cannot connect to HAL");
		goto end;
	}
	conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (conn == NULL || dbus_error_is_set (&error))
		goto end;

	libhal_ctx_set_dbus_connection (ctx, conn);
	if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error))
		goto end;

	inited = TRUE;
	parent_udi = libhal_device_get_property_string (ctx, udi,
			"info.parent", &error);
	if (parent_udi == NULL || dbus_error_is_set (&error))
		goto end;

	parent_name = libhal_device_get_property_string (ctx, parent_udi,
			"storage.model", &error);
	if (parent_name == NULL || dbus_error_is_set (&error))
		goto end;

	if (strcmp (parent_name, "iPod") == 0)
		result = TRUE;

end:
	g_free (parent_udi);
	g_free (parent_name);

	if (dbus_error_is_set (&error)) {
		debug ("Error: %s\n", error.message);
		dbus_error_free (&error);
		dbus_error_init (&error);
	}

	if (ctx) {
		if (inited)
			libhal_ctx_shutdown (ctx, &error);
		libhal_ctx_free(ctx);
	}

	dbus_error_free (&error);

	return result;
}
#endif


/* adapted from rb-ipod-source.c (rhythmbox ipod plugin) */
static gchar *ad_get_itunes_dir (GnomeVFSVolume *volume)
{
    gchar *mount_point_uri;
    gchar *result = NULL;

    mount_point_uri = gnome_vfs_volume_get_activation_uri (volume);
    if (mount_point_uri)
    {
	gchar *mount_point;
	mount_point = g_filename_from_uri (mount_point_uri, NULL, NULL);
	if (mount_point)
	{
	    result = itdb_get_itunes_dir (mount_point);
	    g_free (mount_point);
	}
	g_free (mount_point_uri);
    }
    return result;
}


/* adapted from rb-ipod-source.c (rhythmbox ipod plugin) */
static gboolean ad_volume_has_ipod_dir (GnomeVFSVolume *volume)
{
    gchar *itunes_dir;
    gboolean result = FALSE;

    itunes_dir = ad_get_itunes_dir (volume);

    if (itunes_dir)
    {
	result = g_file_test (itunes_dir, G_FILE_TEST_EXISTS);
    }

    g_free (itunes_dir);

    return result;
}

/* adapted from rb-ipod-source.c (rhythmbox ipod plugin) */
static gboolean ad_volume_is_ipod (GnomeVFSVolume *volume)
{
#ifdef HAVE_HAL
    gchar *udi;
#endif
    if (gnome_vfs_volume_get_volume_type (volume) != GNOME_VFS_VOLUME_TYPE_MOUNTPOINT)
    {
	return FALSE;
    }

#ifdef HAVE_HAL
    udi = gnome_vfs_volume_get_hal_udi (volume);
    if (udi != NULL)
    {
	gboolean result;

	result = hal_udi_is_ipod (udi);
	g_free (udi);
	if (result == FALSE)
	{
	    return FALSE;
	}
    }
#endif

    return ad_volume_has_ipod_dir (volume);
}



static void ad_volume_mounted_cb (GnomeVFSVolumeMonitor *vfsvolumemonitor,
				  GnomeVFSVolume *volume,
				  AutoDetect *ad)
{
    g_return_if_fail (volume && ad);

    if (ad_volume_is_ipod (volume))
    {
	gchar *uri;

	uri = gnome_vfs_volume_get_activation_uri (volume);

	debug ("mounted iPod: '%s'\n", uri);

	g_mutex_lock (ad->mutex);
	ad->new_ipod_uris = g_list_prepend (ad->new_ipod_uris, uri);
	g_mutex_unlock (ad->mutex);
    }
}


void autodetection_init ()
{
    if (autodetect == NULL)
    {
	GList *volumes, *gl;

	if (!gnome_vfs_init ())
	{
	    gtkpod_warning (_("Could not initialize GnomeVFS\n"));
	    g_return_if_reached ();
	}

	autodetect = g_new0 (AutoDetect, 1);
	autodetect->mutex = g_mutex_new ();

	/* Check if an iPod is already mounted and add it to the list */
	volumes = gnome_vfs_volume_monitor_get_mounted_volumes (
	    gnome_vfs_get_volume_monitor ());

	for (gl=volumes; gl; gl=gl->next)
	{
	    GnomeVFSVolume *volume = gl->data;
	    g_return_if_fail (volume);
	    ad_volume_mounted_cb (NULL, volume, autodetect);
	    gnome_vfs_volume_unref (volume);
	}
	g_list_free (volumes);

	g_signal_connect (G_OBJECT (gnome_vfs_get_volume_monitor ()),
			  "volume-mounted",
			  G_CALLBACK (ad_volume_mounted_cb),
			  autodetect);

	/* start timeout function for the monitor */
	autodetect->timeout_id = g_timeout_add (100,   /* every 100 ms */
						ad_timeout_cb,
						autodetect);
    }
}


static gboolean ad_timeout_cb (gpointer data)
{
    AutoDetect *ad = data;
    g_return_val_if_fail (ad, FALSE);


    /* Don't interfere with a blocked display -- try again later */
    if (!widgets_blocked)
    {
	gdk_threads_enter ();
	g_mutex_lock (ad->mutex);

	while (ad->new_ipod_uris)
	{
	    iTunesDB *itdb, *loaded_itdb = NULL;
	    gchar *mountpoint;
	    struct itdbs_head *itdbs;
	    GList *gl = ad->new_ipod_uris;
	    gchar *mount_uri = gl->data;

	    ad->new_ipod_uris = g_list_delete_link (ad->new_ipod_uris, gl);

	    g_mutex_unlock (ad->mutex);

	    g_return_val_if_fail (mount_uri, (gdk_threads_leave(), release_widgets(), TRUE));

	    mountpoint = g_filename_from_uri (mount_uri, NULL, NULL);
	    g_free (mount_uri);
	    debug ("Mounted iPod at '%s'\n", mountpoint);

	    itdb = ad_find_repository_with_mountpoint (mountpoint);

	    itdbs = gp_get_itdbs_head (gtkpod_window);
	    g_return_val_if_fail (itdbs, (gdk_threads_leave(), release_widgets(), TRUE));

	    block_widgets ();

	    if (itdb)
	    {
		ExtraiTunesDBData *eitdb = itdb->userdata;
		g_return_val_if_fail (eitdb,(gdk_threads_leave(), release_widgets(), TRUE));

		debug ("...used by itdb %p\n", itdb);

		if (!eitdb->itdb_imported)
		{
		    loaded_itdb = gp_load_ipod (itdb);
		    if (loaded_itdb)
		    {
			loaded_itdb->usertype |= GP_ITDB_TYPE_AUTOMATIC;
			set_itdb_prefs_int (loaded_itdb, "type", loaded_itdb->usertype);
		    }
		    else
		    {
			gtkpod_warning (_("Newly mounted iPod at '%s' could not be loaded into gtkpod.\n\n"),
					mountpoint);
		    }
		}
		else
		{
		    gtkpod_warning (_("Newly mounted iPod at '%s' appears to be already loaded!\n\n"),
                                    mountpoint);
		}
		debug ("...OK (used)\n");
	    }
	    else
	    {   /* Set up new itdb (which we'll add to the end of the list) */
		iTunesDB *new_itdb;
		gint index = g_list_length (itdbs->itdbs);
		debug ("...not used by any itdb.\n");
		set_itdb_index_prefs_string (index,
					     KEY_MOUNTPOINT, mountpoint);
		set_itdb_index_prefs_string (index,
					     "name", _("New iPod"));
		set_itdb_index_prefs_int (index,
					  "type", GP_ITDB_TYPE_IPOD |
					          GP_ITDB_TYPE_AUTOMATIC);
		new_itdb = setup_itdb_n (index);
		g_return_val_if_fail (new_itdb,
				      (gdk_threads_leave(), release_widgets(), TRUE));
		/* add to display */
		gp_itdb_add (new_itdb, -1);
		/* load prefs from iPod */
		loaded_itdb = gp_load_ipod (new_itdb);
		if (!loaded_itdb)
		{   /* remove itdb and all associated keys again */
		    remove_itdb_prefs (itdb);
		    gp_itdb_remove (new_itdb);
		    gp_itdb_free (new_itdb);
		}
		debug ("...OK (new)\n");
	    }

	    release_widgets ();

	    g_free (mountpoint);

	    g_mutex_lock (ad->mutex);
	}
	g_mutex_unlock (ad->mutex);
	gdk_threads_leave();
    }

    return TRUE;
}

#else
/* No GNOME_VFS support */

void autodetection_init ()
{
}
#endif