~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to plugins/generic-player/rb-generic-player-source.c

Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  arch-tag: Implementation of generic audio player source object
 
3
 *
 
4
 *  Copyright (C) 2004 James Livingston  <jrl@ids.org.au>
 
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 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
19
 *
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
 
 
24
#include <gtk/gtktreeview.h>
 
25
#include <string.h>
 
26
#include "rhythmdb.h"
 
27
#include <libgnome/gnome-i18n.h>
 
28
#ifdef HAVE_HAL_0_5
 
29
#include <libhal.h>
 
30
#include <dbus/dbus.h>
 
31
#endif
 
32
#include <libgnomevfs/gnome-vfs-volume.h>
 
33
#include <libgnomevfs/gnome-vfs-volume-monitor.h>
 
34
#include <totem-pl-parser.h>
 
35
 
 
36
#include "eel-gconf-extensions.h"
 
37
#include "rb-static-playlist-source.h"
 
38
#include "rb-generic-player-source.h"
 
39
#include "rb-debug.h"
 
40
#include "rb-util.h"
 
41
#include "rb-file-helpers.h"
 
42
#include "rhythmdb.h"
 
43
#include "rb-dialog.h"
 
44
#include "rb-plugin.h"
 
45
 
 
46
static GObject *rb_generic_player_source_constructor (GType type, guint n_construct_properties,
 
47
                                                      GObjectConstructParam *construct_properties);
 
48
static void rb_generic_player_source_dispose (GObject *object);
 
49
 
 
50
static gboolean rb_generic_player_source_load_playlists (RBGenericPlayerSource *source);
 
51
static void rb_generic_player_source_load_songs (RBGenericPlayerSource *source);
 
52
 
 
53
static gboolean impl_show_popup (RBSource *source);
 
54
static void impl_delete_thyself (RBSource *source);
 
55
static gboolean impl_can_move_to_trash (RBSource *source);
 
56
 
 
57
static gchar *default_get_mount_path (RBGenericPlayerSource *source);
 
58
static void default_load_playlists (RBGenericPlayerSource *source);
 
59
static char * default_transform_playlist_uri (RBGenericPlayerSource *source,
 
60
                                              const char *uri);
 
61
 
 
62
typedef struct
 
63
{
 
64
        char *mount_path;
 
65
 
 
66
        RhythmDB *db;
 
67
 
 
68
        GList *playlists;
 
69
        gboolean read_only;
 
70
        gboolean handles_trash;
 
71
 
 
72
} RBGenericPlayerSourcePrivate;
 
73
 
 
74
 
 
75
RB_PLUGIN_DEFINE_TYPE(RBGenericPlayerSource, rb_generic_player_source, RB_TYPE_REMOVABLE_MEDIA_SOURCE)
 
76
#define GENERIC_PLAYER_SOURCE_GET_PRIVATE(o)   (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_GENERIC_PLAYER_SOURCE, RBGenericPlayerSourcePrivate))
 
77
 
 
78
static void
 
79
rb_generic_player_source_class_init (RBGenericPlayerSourceClass *klass)
 
80
{
 
81
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
82
        RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
 
83
 
 
84
        object_class->constructor = rb_generic_player_source_constructor;
 
85
        object_class->dispose = rb_generic_player_source_dispose;
 
86
 
 
87
        source_class->impl_show_popup = impl_show_popup;
 
88
        source_class->impl_delete_thyself = impl_delete_thyself;
 
89
        source_class->impl_can_move_to_trash = impl_can_move_to_trash;
 
90
 
 
91
        klass->impl_get_mount_path = default_get_mount_path;
 
92
        klass->impl_load_playlists = default_load_playlists;
 
93
        klass->impl_transform_playlist_uri = default_transform_playlist_uri;
 
94
 
 
95
        g_type_class_add_private (klass, sizeof (RBGenericPlayerSourcePrivate));
 
96
}
 
97
 
 
98
static void
 
99
rb_generic_player_source_init (RBGenericPlayerSource *source)
 
100
{
 
101
 
 
102
}
 
103
 
 
104
static GObject *
 
105
rb_generic_player_source_constructor (GType type, guint n_construct_properties,
 
106
                               GObjectConstructParam *construct_properties)
 
107
{
 
108
        RBGenericPlayerSource *source; 
 
109
        RBGenericPlayerSourcePrivate *priv;
 
110
        GnomeVFSVolume *volume;
 
111
        RBShell *shell;
 
112
 
 
113
        source = RB_GENERIC_PLAYER_SOURCE (G_OBJECT_CLASS (rb_generic_player_source_parent_class)->
 
114
                        constructor (type, n_construct_properties, construct_properties));
 
115
 
 
116
        priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
 
117
 
 
118
        g_object_get (G_OBJECT (source), "shell", &shell, NULL);
 
119
 
 
120
        g_object_get (G_OBJECT (shell), "db", &priv->db, NULL);
 
121
        g_object_unref (G_OBJECT (shell));
 
122
 
 
123
        g_object_get (G_OBJECT (source), "volume", &volume, NULL);
 
124
        priv->handles_trash = gnome_vfs_volume_handles_trash (volume);
 
125
        priv->read_only = gnome_vfs_volume_is_read_only (volume);
 
126
        g_object_unref (G_OBJECT (volume));
 
127
 
 
128
        rb_generic_player_source_load_songs (source);
 
129
        
 
130
        g_idle_add ((GSourceFunc)rb_generic_player_source_load_playlists, source);
 
131
 
 
132
        return G_OBJECT (source);
 
133
}
 
134
 
 
135
static void 
 
136
rb_generic_player_source_dispose (GObject *object)
 
137
{
 
138
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (object);
 
139
 
 
140
        if (priv->mount_path) {
 
141
                g_free (priv->mount_path);
 
142
                priv->mount_path = NULL;
 
143
        }
 
144
        if (priv->db) {
 
145
                g_object_unref (G_OBJECT (priv->db));
 
146
                priv->db = NULL;
 
147
        }
 
148
        
 
149
        G_OBJECT_CLASS (rb_generic_player_source_parent_class)->dispose (object);
 
150
}
 
151
 
 
152
RBRemovableMediaSource *
 
153
rb_generic_player_source_new (RBShell *shell, GnomeVFSVolume *volume)
 
154
{
 
155
        RBGenericPlayerSource *source;
 
156
        RhythmDBEntryType entry_type;
 
157
 
 
158
        g_assert (rb_generic_player_is_volume_player (volume));
 
159
 
 
160
        entry_type =  rhythmdb_entry_register_type (NULL);
 
161
 
 
162
        source = RB_GENERIC_PLAYER_SOURCE (g_object_new (RB_TYPE_GENERIC_PLAYER_SOURCE,
 
163
                                          "entry-type", entry_type,
 
164
                                          "volume", volume,
 
165
                                          "shell", shell,
 
166
                                          "sourcelist-group", RB_SOURCELIST_GROUP_REMOVABLE,
 
167
                                          NULL));
 
168
 
 
169
        rb_shell_register_entry_type_for_source (shell, RB_SOURCE (source), entry_type);
 
170
 
 
171
        return RB_REMOVABLE_MEDIA_SOURCE (source);
 
172
}
 
173
 
 
174
static void
 
175
impl_delete_thyself (RBSource *source)
 
176
{
 
177
        GList *p;
 
178
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
 
179
        
 
180
        for (p = priv->playlists; p != NULL; p = p->next) {
 
181
                RBSource *playlist = RB_SOURCE (p->data);
 
182
                rb_source_delete_thyself (playlist);
 
183
        }
 
184
        g_list_free (priv->playlists);
 
185
        priv->playlists = NULL;
 
186
 
 
187
        RB_SOURCE_CLASS (rb_generic_player_source_parent_class)->impl_delete_thyself (source);
 
188
}
 
189
 
 
190
static void
 
191
rb_generic_player_source_load_songs (RBGenericPlayerSource *source)
 
192
{
 
193
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
 
194
        RhythmDBEntryType entry_type;
 
195
 
 
196
        priv->mount_path = rb_generic_player_source_get_mount_path (source);
 
197
        g_object_get (G_OBJECT (source), "entry-type", &entry_type, NULL);
 
198
 
 
199
        rhythmdb_add_uri_with_type (priv->db, priv->mount_path, entry_type);
 
200
}
 
201
 
 
202
char *
 
203
rb_generic_player_source_get_mount_path (RBGenericPlayerSource *source)
 
204
{
 
205
        RBGenericPlayerSourceClass *klass = RB_GENERIC_PLAYER_SOURCE_GET_CLASS (source);
 
206
 
 
207
        return klass->impl_get_mount_path (source);
 
208
}
 
209
 
 
210
static gchar *
 
211
default_get_mount_path (RBGenericPlayerSource *source)
 
212
{
 
213
        gchar *uri;
 
214
        GnomeVFSVolume *volume;
 
215
 
 
216
        g_object_get (G_OBJECT (source), "volume", &volume, NULL);
 
217
        uri = gnome_vfs_volume_get_activation_uri (volume);
 
218
        g_object_unref (G_OBJECT (volume));
 
219
 
 
220
        return uri;
 
221
}
 
222
 
 
223
 
 
224
gboolean
 
225
rb_generic_player_is_volume_player (GnomeVFSVolume *volume)
 
226
{
 
227
        gboolean result = FALSE;
 
228
#ifdef HAVE_HAL_0_5
 
229
        gchar *udi = gnome_vfs_volume_get_hal_udi (volume);
 
230
 
 
231
        if (udi != NULL) {
 
232
                LibHalContext *ctx = NULL;
 
233
                DBusConnection *conn = NULL;
 
234
                DBusError error;
 
235
                char *prop = NULL;
 
236
 
 
237
                dbus_error_init (&error);
 
238
                ctx = libhal_ctx_new ();
 
239
                if (ctx == NULL)
 
240
                        goto end_hal;
 
241
 
 
242
                conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
 
243
                if (conn == NULL || dbus_error_is_set (&error))
 
244
                        goto end_hal;
 
245
                libhal_ctx_set_dbus_connection (ctx, conn);
 
246
                if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error))
 
247
                        goto end_hal;
 
248
 
 
249
                /* find the udi of the player itself */
 
250
                while (!libhal_device_query_capability (ctx, udi, "portable_audio_player", &error) &&
 
251
                       !dbus_error_is_set (&error)) {
 
252
                        char *new_udi = libhal_device_get_property_string (ctx, udi, "info.parent", &error);
 
253
                        if (dbus_error_is_set (&error))
 
254
                                goto end_hal;
 
255
 
 
256
                        if ((new_udi == NULL) || strcmp (new_udi, "/") == 0) {
 
257
                                rb_debug ("device is not audio player");
 
258
                                goto end_hal;
 
259
                        }
 
260
 
 
261
                        g_free (udi);
 
262
                        udi = g_strdup (new_udi);
 
263
                        libhal_free_string (new_udi);
 
264
                }
 
265
                if (dbus_error_is_set (&error))
 
266
                        goto end_hal;
 
267
 
 
268
                /* check that it can be accessed as mass-storage */
 
269
                prop = libhal_device_get_property_string (ctx, udi, "portable_audio_player.access_method", &error);
 
270
                if (prop == NULL || strcmp (prop, "storage") != 0 || dbus_error_is_set (&error)) {
 
271
                        rb_debug ("device cannot be accessed via storage");
 
272
                        goto end_hal;
 
273
                }
 
274
 
 
275
                /* the device has passed all tests, so it should be a usable player */
 
276
                result = TRUE;
 
277
end_hal:
 
278
                if (dbus_error_is_set (&error)) {
 
279
                        rb_debug ("Error: %s\n", error.message);
 
280
                        dbus_error_free (&error);
 
281
                        dbus_error_init (&error);
 
282
                }
 
283
 
 
284
                if (prop)
 
285
                        libhal_free_string (prop);
 
286
 
 
287
                if (ctx) {
 
288
                        libhal_ctx_shutdown (ctx, &error);
 
289
                        libhal_ctx_free (ctx);
 
290
                }
 
291
                dbus_error_free (&error);
 
292
 
 
293
                g_free (udi);
 
294
        }
 
295
#endif /* HAVE_HAL_0_5 */
 
296
 
 
297
        /* treat as audio player if ".is_audio_player" exists in the root of the volume  */
 
298
        if (!result) {
 
299
                char *path = gnome_vfs_volume_get_activation_uri (volume);
 
300
                char *file = g_build_filename (path, ".is_audio_player", NULL);
 
301
 
 
302
                if (rb_uri_is_local (file) && rb_uri_exists (file))
 
303
                        result = TRUE;
 
304
 
 
305
                g_free (file);
 
306
                g_free (path);
 
307
        }
 
308
 
 
309
        return result;
 
310
}
 
311
 
 
312
static gboolean
 
313
impl_show_popup (RBSource *source)
 
314
{
 
315
        _rb_source_show_popup (RB_SOURCE (source), "/GenericPlayerSourcePopup");
 
316
        return TRUE;
 
317
}
 
318
 
 
319
static gboolean
 
320
impl_can_move_to_trash (RBSource *source)
 
321
{
 
322
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
 
323
        return priv->handles_trash;
 
324
}
 
325
 
 
326
/* code for playlist loading */
 
327
 
 
328
void
 
329
rb_generic_player_source_add_playlist (RBGenericPlayerSource *source,
 
330
                                       RBShell *shell,
 
331
                                       RBSource *playlist)
 
332
{
 
333
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
 
334
        priv->playlists = g_list_prepend (priv->playlists, playlist);
 
335
 
 
336
        rb_shell_append_source (shell, playlist, RB_SOURCE (source));
 
337
}
 
338
 
 
339
static gboolean
 
340
rb_generic_player_source_load_playlists (RBGenericPlayerSource *source)
 
341
{
 
342
        RBGenericPlayerSourceClass *klass = RB_GENERIC_PLAYER_SOURCE_GET_CLASS (source);
 
343
 
 
344
        if (klass->impl_load_playlists)
 
345
                klass->impl_load_playlists (source);
 
346
 
 
347
        return FALSE;
 
348
}
 
349
 
 
350
static char *
 
351
rb_generic_player_source_transform_playlist_uri (RBGenericPlayerSource *source, const char *uri)
 
352
{
 
353
        RBGenericPlayerSourceClass *klass = RB_GENERIC_PLAYER_SOURCE_GET_CLASS (source);
 
354
 
 
355
        return klass->impl_transform_playlist_uri (source, uri);
 
356
}
 
357
 
 
358
 
 
359
typedef struct {
 
360
        RBGenericPlayerSource *player_source;
 
361
        RBStaticPlaylistSource *source;
 
362
} HandlePlaylistEntryData;
 
363
 
 
364
static void
 
365
handle_playlist_entry_cb (TotemPlParser *playlist, const char *uri,
 
366
                          const char *title,
 
367
                          const char *genre, HandlePlaylistEntryData *data)
 
368
{
 
369
        char *local_uri;
 
370
        const char *name;
 
371
 
 
372
        local_uri = rb_generic_player_source_transform_playlist_uri (data->player_source, uri);
 
373
        if (local_uri == NULL)
 
374
                return;
 
375
        
 
376
        g_object_get (G_OBJECT (data->source), "name", &name, NULL);
 
377
        rb_debug ("adding '%s' as '%s' to playlist '%s'", uri, local_uri, name);
 
378
        rb_static_playlist_source_add_location (data->source, local_uri, -1);
 
379
        g_free (local_uri);
 
380
}
 
381
 
 
382
static gboolean
 
383
visit_playlist_dirs (const gchar *rel_path,
 
384
                     GnomeVFSFileInfo *info,
 
385
                     gboolean recursing_will_loop,
 
386
                     RBGenericPlayerSource *source,
 
387
                     gboolean *recurse)
 
388
{
 
389
        RBShell *shell;
 
390
        RhythmDBEntryType entry_type;
 
391
        char *main_path;
 
392
        char *playlist_path;
 
393
        TotemPlParser *parser;
 
394
        RBStaticPlaylistSource *playlist;
 
395
        HandlePlaylistEntryData *data;
 
396
 
 
397
        *recurse = TRUE;
 
398
 
 
399
        /* add playlist */
 
400
        main_path = rb_generic_player_source_get_mount_path (source);
 
401
        playlist_path = rb_uri_append_path (main_path, rel_path);
 
402
        g_free (main_path);
 
403
 
 
404
        if (!g_str_has_suffix (playlist_path, ".m3u") &&
 
405
            !g_str_has_suffix (playlist_path, ".pls")) {
 
406
                g_free (playlist_path);
 
407
                return TRUE;
 
408
        }
 
409
 
 
410
        g_object_get (G_OBJECT (source), 
 
411
                      "shell", &shell, 
 
412
                      "entry-type", &entry_type,
 
413
                      NULL);
 
414
 
 
415
        playlist = RB_STATIC_PLAYLIST_SOURCE (
 
416
                        rb_static_playlist_source_new (shell, 
 
417
                                                      rel_path, 
 
418
                                                      FALSE,
 
419
                                                      entry_type));
 
420
 
 
421
        data = g_new0 (HandlePlaylistEntryData, 1);
 
422
        data->source = playlist;
 
423
        data->player_source = source;
 
424
        
 
425
        parser = totem_pl_parser_new ();
 
426
 
 
427
        g_signal_connect (parser,
 
428
                          "entry", G_CALLBACK (handle_playlist_entry_cb),
 
429
                          data);
 
430
        if (g_object_class_find_property (G_OBJECT_GET_CLASS (parser), "recurse"))
 
431
                g_object_set (G_OBJECT (parser), "recurse", FALSE, NULL);
 
432
 
 
433
        if (totem_pl_parser_parse (parser, playlist_path, TRUE) != TOTEM_PL_PARSER_RESULT_SUCCESS) {
 
434
                rb_debug ("unable to parse %s as playlist", playlist_path);
 
435
                g_object_unref (G_OBJECT (playlist));
 
436
        } else {
 
437
                rb_generic_player_source_add_playlist (source, shell, RB_SOURCE (playlist));
 
438
        }
 
439
 
 
440
        g_object_unref (G_OBJECT (parser));
 
441
        g_free (playlist_path);
 
442
        g_free (data);
 
443
 
 
444
        g_object_unref (G_OBJECT (shell));
 
445
 
 
446
        return TRUE;
 
447
}
 
448
 
 
449
static void
 
450
default_load_playlists (RBGenericPlayerSource *source)
 
451
{
 
452
        char *mount_path;
 
453
 
 
454
        mount_path = rb_generic_player_source_get_mount_path (source);
 
455
        gnome_vfs_directory_visit (mount_path,
 
456
                                   GNOME_VFS_FILE_INFO_DEFAULT,
 
457
                                   GNOME_VFS_DIRECTORY_VISIT_DEFAULT,
 
458
                                   (GnomeVFSDirectoryVisitFunc) visit_playlist_dirs,
 
459
                                   source);
 
460
        g_free (mount_path);
 
461
}
 
462
 
 
463
static char *
 
464
default_transform_playlist_uri (RBGenericPlayerSource *source, const char *uri)
 
465
{
 
466
        char *mount_uri;
 
467
        char *full_uri;
 
468
 
 
469
        mount_uri = rb_generic_player_source_get_mount_path (source);
 
470
        full_uri = rb_uri_append_uri (mount_uri, uri);
 
471
        g_free (mount_uri);
 
472
        return full_uri;
 
473
}