2006-11-10 James Livingston <doclivingston@gmail.com>
[rhythmbox.git] / plugins / generic-player / rb-nokia770-source.c
blob06896b8940c70fbf7b44c8d6ba8d7620f3c283ef
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * arch-tag: Implementation of Nokia 770 source object
5 * Copyright (C) 2006 James Livingston <jrl@ids.org.au>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 #define __EXTENSIONS__
25 #include "config.h"
27 #include <string.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <dbus/dbus.h>
32 #include <libhal.h>
33 #include <libgnomevfs/gnome-vfs-volume.h>
34 #include <libgnomevfs/gnome-vfs-volume-monitor.h>
36 #include "eel-gconf-extensions.h"
37 #include "rb-nokia770-source.h"
38 #include "rb-debug.h"
39 #include "rb-util.h"
40 #include "rb-file-helpers.h"
41 #include "rhythmdb.h"
42 #include "rb-plugin.h"
45 static char * impl_transform_playlist_uri (RBGenericPlayerSource *source, const char *uri);
48 typedef struct {
49 #ifdef __SUNPRO_C
50 int x; /* To build with Solaris forte compiler */
51 #endif
52 } RBNokia770SourcePrivate;
54 RB_PLUGIN_DEFINE_TYPE (RBNokia770Source, rb_nokia770_source, RB_TYPE_GENERIC_PLAYER_SOURCE)
55 #define NOKIA770_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RB_TYPE_NOKIA770_SOURCE, RBNokia770SourcePrivate))
58 #define NOKIA_INTERNAL_MOUNTPOINT "file:///media/mmc1/"
60 static void
61 rb_nokia770_source_class_init (RBNokia770SourceClass *klass)
63 RBGenericPlayerSourceClass *generic_class = RB_GENERIC_PLAYER_SOURCE_CLASS (klass);
65 generic_class->impl_transform_playlist_uri = impl_transform_playlist_uri;
67 g_type_class_add_private (klass, sizeof (RBNokia770SourcePrivate));
70 static void
71 rb_nokia770_source_init (RBNokia770Source *source)
76 RBRemovableMediaSource *
77 rb_nokia770_source_new (RBShell *shell, GnomeVFSVolume *volume)
79 RBNokia770Source *source;
80 RhythmDBEntryType entry_type;
81 RhythmDB *db;
83 g_assert (rb_nokia770_is_volume_player (volume));
85 g_object_get (shell, "db", &db, NULL);
86 entry_type = rhythmdb_entry_register_type (db, NULL);
87 g_object_unref (db);
89 source = RB_NOKIA770_SOURCE (g_object_new (RB_TYPE_NOKIA770_SOURCE,
90 "entry-type", entry_type,
91 "volume", volume,
92 "shell", shell,
93 "sourcelist-group", RB_SOURCELIST_GROUP_REMOVABLE,
94 NULL));
96 rb_shell_register_entry_type_for_source (shell, RB_SOURCE (source), entry_type);
98 return RB_REMOVABLE_MEDIA_SOURCE (source);
101 static char *
102 impl_transform_playlist_uri (RBGenericPlayerSource *source, const char *uri)
104 const char *path;
105 char *local_uri;
106 char *mount_uri;
108 if (!g_str_has_prefix (uri, NOKIA_INTERNAL_MOUNTPOINT)) {
109 rb_debug ("found playlist uri with unexpected mountpoint");
110 return NULL;
113 path = uri + strlen (NOKIA_INTERNAL_MOUNTPOINT);
114 mount_uri = rb_generic_player_source_get_mount_path (source);
115 local_uri = rb_uri_append_uri (mount_uri, path);
116 g_free (mount_uri);
117 return local_uri;
121 #ifdef HAVE_HAL_0_5
123 static gboolean
124 hal_udi_is_nokia770 (const char *udi)
126 LibHalContext *ctx;
127 DBusConnection *conn;
128 char *parent_udi;
129 char *parent_name;
130 gboolean result;
131 DBusError error;
132 gboolean inited = FALSE;
134 result = FALSE;
135 dbus_error_init (&error);
137 conn = NULL;
138 parent_udi = NULL;
139 parent_name = NULL;
141 ctx = libhal_ctx_new ();
142 if (ctx == NULL) {
143 rb_debug ("cannot connect to HAL");
144 goto end;
146 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
147 if (conn == NULL || dbus_error_is_set (&error))
148 goto end;
150 libhal_ctx_set_dbus_connection (ctx, conn);
151 if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error))
152 goto end;
154 inited = TRUE;
155 parent_udi = libhal_device_get_property_string (ctx, udi,
156 "info.parent", &error);
157 if (parent_udi == NULL || dbus_error_is_set (&error))
158 goto end;
161 rb_debug ("Nokia detection: info.parent=%s", parent_udi);
162 parent_name = libhal_device_get_property_string (ctx, parent_udi,
163 "info.vendor", &error);
164 rb_debug ("Nokia detection: info.vendor=%s", parent_name);
165 if (parent_name == NULL || dbus_error_is_set (&error))
166 goto end;
168 if (strcmp (parent_name, "Nokia") == 0) {
169 g_free (parent_name);
171 parent_name = libhal_device_get_property_string (ctx, parent_udi,
172 "info.product", &error);
173 rb_debug ("Nokia detection: info.product=%s", parent_name);
174 if (parent_name == NULL || dbus_error_is_set (&error))
175 goto end;
177 if (strcmp (parent_name, "770") == 0) {
178 result = TRUE;
182 end:
183 g_free (parent_name);
184 g_free (parent_udi);
186 if (dbus_error_is_set (&error)) {
187 rb_debug ("Error: %s\n", error.message);
188 dbus_error_free (&error);
189 dbus_error_init (&error);
192 if (ctx) {
193 if (inited)
194 libhal_ctx_shutdown (ctx, &error);
195 libhal_ctx_free(ctx);
198 dbus_error_free (&error);
200 return result;
203 #elif HAVE_HAL_0_2
205 static gboolean
206 hal_udi_is_nokia770 (const char *udi)
208 LibHalContext *ctx;
209 char *parent_udi;
210 char *parent_name;
211 gboolean result;
213 result = FALSE;
214 ctx = hal_initialize (NULL, FALSE);
215 if (ctx == NULL) {
216 return FALSE;
218 parent_udi = hal_device_get_property_string (ctx, udi,
219 "info.parent");
221 parent_name = hal_device_get_property_string (ctx, parent_udi,
222 "info.vendor");
223 if (parent_name != NULL && strcmp (parent_name, "Nokia") == 0) {
224 g_free (parent_udi);
225 parent_name = hal_device_get_property_string (ctx, parent_udi,
226 "info.product");
227 if (parent_name != NULL && strcmp (parent_name, "770") == 0) {
228 result = TRUE;
232 g_free (parent_name);
233 g_free (parent_udi);
235 hal_shutdown (ctx);
237 return result;
240 #endif
242 gboolean
243 rb_nokia770_is_volume_player (GnomeVFSVolume *volume)
245 gboolean result = FALSE;
246 gchar *str;
248 if (gnome_vfs_volume_get_volume_type (volume) != GNOME_VFS_VOLUME_TYPE_MOUNTPOINT) {
249 return FALSE;
252 str = gnome_vfs_volume_get_hal_udi (volume);
253 if (str != NULL) {
254 gboolean result;
256 result = hal_udi_is_nokia770 (str);
257 g_free (str);
258 return result;
261 return result;