~ubuntu-branches/ubuntu/oneiric/rhythmbox/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2006-06-26 19:06:10 UTC
  • mto: (2.1.1 lenny) (1.1.37 upstream)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20060626190610-08x8lgvvfs0gr619
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "eel-gconf-extensions.h"
35
 
#include "rb-generic-player-source.h"
36
 
#include "rb-debug.h"
37
 
#include "rb-util.h"
38
 
#include "rb-file-helpers.h"
39
 
#include "rhythmdb.h"
40
 
 
41
 
static GObject *rb_generic_player_source_constructor (GType type, guint n_construct_properties,
42
 
                                                      GObjectConstructParam *construct_properties);
43
 
static void rb_generic_player_source_dispose (GObject *object);
44
 
 
45
 
static gboolean impl_show_popup (RBSource *source);
46
 
static void rb_generic_player_source_load_songs (RBGenericPlayerSource *source);
47
 
static gchar *default_get_mount_path (RBGenericPlayerSource *source);
48
 
 
49
 
typedef struct
50
 
{
51
 
        char *mount_path;
52
 
} RBGenericPlayerSourcePrivate;
53
 
 
54
 
 
55
 
G_DEFINE_TYPE (RBGenericPlayerSource, rb_generic_player_source, RB_TYPE_REMOVABLE_MEDIA_SOURCE)
56
 
#define GENERIC_PLAYER_SOURCE_GET_PRIVATE(o)   (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_GENERIC_PLAYER_SOURCE, RBGenericPlayerSourcePrivate))
57
 
 
58
 
 
59
 
static void
60
 
rb_generic_player_source_class_init (RBGenericPlayerSourceClass *klass)
61
 
{
62
 
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
63
 
        RBSourceClass *source_class = RB_SOURCE_CLASS (klass);
64
 
 
65
 
        object_class->constructor = rb_generic_player_source_constructor;
66
 
        object_class->dispose = rb_generic_player_source_dispose;
67
 
 
68
 
        source_class->impl_show_popup = impl_show_popup;
69
 
 
70
 
        klass->impl_get_mount_path = default_get_mount_path;
71
 
 
72
 
        g_type_class_add_private (klass, sizeof (RBGenericPlayerSourcePrivate));
73
 
}
74
 
 
75
 
static void
76
 
rb_generic_player_source_init (RBGenericPlayerSource *source)
77
 
{
78
 
 
79
 
}
80
 
 
81
 
static GObject *
82
 
rb_generic_player_source_constructor (GType type, guint n_construct_properties,
83
 
                               GObjectConstructParam *construct_properties)
84
 
{
85
 
        GObjectClass *klass, *parent_class; 
86
 
        RBGenericPlayerSource *source; 
87
 
 
88
 
        klass = G_OBJECT_CLASS (g_type_class_peek (type));
89
 
        parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
90
 
        source = RB_GENERIC_PLAYER_SOURCE (parent_class->constructor (type, n_construct_properties, construct_properties));
91
 
 
92
 
        rb_generic_player_source_load_songs (source);
93
 
 
94
 
        return G_OBJECT (source);
95
 
}
96
 
 
97
 
static void 
98
 
rb_generic_player_source_dispose (GObject *object)
99
 
{
100
 
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (object);
101
 
 
102
 
        if (priv->mount_path) {
103
 
                g_free (priv->mount_path);
104
 
                priv->mount_path = NULL;
105
 
        }
106
 
        
107
 
        G_OBJECT_CLASS (rb_generic_player_source_parent_class)->dispose (object);
108
 
}
109
 
 
110
 
RBRemovableMediaSource *
111
 
rb_generic_player_source_new (RBShell *shell, GnomeVFSVolume *volume)
112
 
{
113
 
        RBGenericPlayerSource *source;
114
 
        RhythmDBEntryType entry_type;
115
 
 
116
 
        g_assert (rb_generic_player_is_volume_player (volume));
117
 
 
118
 
        entry_type =  rhythmdb_entry_register_type ();
119
 
 
120
 
        source = RB_GENERIC_PLAYER_SOURCE (g_object_new (RB_TYPE_GENERIC_PLAYER_SOURCE,
121
 
                                          "entry-type", entry_type,
122
 
                                          "volume", volume,
123
 
                                          "shell", shell,
124
 
                                          NULL));
125
 
 
126
 
        rb_shell_register_entry_type_for_source (shell, RB_SOURCE (source), entry_type);
127
 
 
128
 
        return RB_REMOVABLE_MEDIA_SOURCE (source);
129
 
}
130
 
 
131
 
static void
132
 
rb_generic_player_source_load_songs (RBGenericPlayerSource *source)
133
 
{
134
 
        RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source);
135
 
        RBShell *shell;
136
 
        RhythmDB *db;
137
 
        RhythmDBEntryType entry_type;
138
 
 
139
 
        priv->mount_path = rb_generic_player_source_get_mount_path (source);
140
 
        g_object_get (G_OBJECT (source), "entry-type", &entry_type, NULL);
141
 
        g_object_get (G_OBJECT (source), "shell", &shell, NULL);
142
 
        g_object_get (G_OBJECT (shell), "db", &db, NULL);
143
 
        g_object_unref (G_OBJECT (shell));
144
 
 
145
 
        rhythmdb_add_uri_with_type (db, priv->mount_path, entry_type);
146
 
}
147
 
 
148
 
char *
149
 
rb_generic_player_source_get_mount_path (RBGenericPlayerSource *source)
150
 
{
151
 
        RBGenericPlayerSourceClass *klass = RB_GENERIC_PLAYER_SOURCE_GET_CLASS (source);
152
 
 
153
 
        return klass->impl_get_mount_path (source);
154
 
}
155
 
 
156
 
static gchar *
157
 
default_get_mount_path (RBGenericPlayerSource *source)
158
 
{
159
 
        gchar *uri;
160
 
        GnomeVFSVolume *volume;
161
 
 
162
 
        g_object_get (G_OBJECT (source), "volume", &volume, NULL);
163
 
        uri = gnome_vfs_volume_get_activation_uri (volume);
164
 
        g_object_unref (G_OBJECT (volume));
165
 
 
166
 
        return uri;
167
 
}
168
 
 
169
 
 
170
 
gboolean
171
 
rb_generic_player_is_volume_player (GnomeVFSVolume *volume)
172
 
{
173
 
        gboolean result = FALSE;
174
 
#ifdef HAVE_HAL_0_5
175
 
        gchar *udi = gnome_vfs_volume_get_hal_udi (volume);
176
 
 
177
 
        if (udi != NULL) {
178
 
                LibHalContext *ctx = NULL;
179
 
                DBusConnection *conn = NULL;
180
 
                DBusError error;
181
 
                char *prop = NULL;
182
 
 
183
 
                dbus_error_init (&error);
184
 
                ctx = libhal_ctx_new ();
185
 
                if (ctx == NULL)
186
 
                        goto end_hal;
187
 
 
188
 
                conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
189
 
                if (conn == NULL || dbus_error_is_set (&error))
190
 
                        goto end_hal;
191
 
                libhal_ctx_set_dbus_connection (ctx, conn);
192
 
                if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error))
193
 
                        goto end_hal;
194
 
 
195
 
                /* find the udi of the player itself */
196
 
                while (!libhal_device_query_capability (ctx, udi, "portable_audio_player", &error) &&
197
 
                       !dbus_error_is_set (&error)) {
198
 
                        char *new_udi = libhal_device_get_property_string (ctx, udi, "info.parent", &error);
199
 
                        if (dbus_error_is_set (&error))
200
 
                                goto end_hal;
201
 
 
202
 
                        if ((new_udi == NULL) || strcmp (new_udi, "/") == 0) {
203
 
                                rb_debug ("device is not audio player");
204
 
                                goto end_hal;
205
 
                        }
206
 
 
207
 
                        g_free (udi);
208
 
                        udi = g_strdup (new_udi);
209
 
                        libhal_free_string (new_udi);
210
 
                }
211
 
                if (dbus_error_is_set (&error))
212
 
                        goto end_hal;
213
 
 
214
 
                /* check that it can be accessed as mass-storage */
215
 
                prop = libhal_device_get_property_string (ctx, udi, "portable_audio_player.access_method", &error);
216
 
                if (prop == NULL || strcmp (prop, "storage") != 0 || dbus_error_is_set (&error)) {
217
 
                        rb_debug ("device cannot be accessed via storage");
218
 
                        goto end_hal;
219
 
                }
220
 
 
221
 
                /* the device has passed all tests, so it should be a usable player */
222
 
                result = TRUE;
223
 
end_hal:
224
 
                if (dbus_error_is_set (&error)) {
225
 
                        rb_debug ("Error: %s\n", error.message);
226
 
                        dbus_error_free (&error);
227
 
                        dbus_error_init (&error);
228
 
                }
229
 
 
230
 
                if (prop)
231
 
                        libhal_free_string (prop);
232
 
 
233
 
                if (ctx) {
234
 
                        libhal_ctx_shutdown (ctx, &error);
235
 
                        libhal_ctx_free (ctx);
236
 
                }
237
 
                dbus_error_free (&error);
238
 
 
239
 
                g_free (udi);
240
 
        }
241
 
#endif /* HAVE_HAL_0_5 */
242
 
 
243
 
        /* treat as audio player if ".is_audio_player" exists in the root of the volume  */
244
 
        if (!result) {
245
 
                char *path = gnome_vfs_volume_get_activation_uri (volume);
246
 
                char *file = g_build_filename (path, ".is_audio_player", NULL);
247
 
 
248
 
                if (rb_uri_is_local (file) && rb_uri_exists (file))
249
 
                        result = TRUE;
250
 
 
251
 
                g_free (file);
252
 
                g_free (path);
253
 
        }
254
 
 
255
 
        return result;
256
 
}
257
 
 
258
 
static gboolean
259
 
impl_show_popup (RBSource *source)
260
 
{
261
 
        _rb_source_show_popup (RB_SOURCE (source), "/GenericPlayerSourcePopup");
262
 
        return TRUE;
263
 
}