464399264e53a7e9254bd3b99c7d218c7396a8ad
[platform/upstream/glib.git] / gio / gunixdrive.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <glib.h>
28 #include "gunixdrive.h"
29 #include "gunixvolume.h"
30 #include "gdriveprivate.h"
31 #include "gthemedicon.h"
32 #include "gvolumemonitor.h"
33
34 #include "glibintl.h"
35
36 struct _GUnixDrive {
37   GObject parent;
38
39   GUnixVolume *volume; /* owned by volume monitor */
40   char *name;
41   char *icon;
42   char *mountpoint;
43   GUnixMountType guessed_type;
44 };
45
46 static void g_unix_volume_drive_iface_init (GDriveIface *iface);
47
48 G_DEFINE_TYPE_WITH_CODE (GUnixDrive, g_unix_drive, G_TYPE_OBJECT,
49                          G_IMPLEMENT_INTERFACE (G_TYPE_DRIVE,
50                                                 g_unix_volume_drive_iface_init))
51
52 static void
53 g_unix_drive_finalize (GObject *object)
54 {
55   GUnixDrive *drive;
56   
57   drive = G_UNIX_DRIVE (object);
58
59   if (drive->volume)
60     g_unix_volume_unset_drive (drive->volume, drive);
61   
62   g_free (drive->name);
63   g_free (drive->icon);
64   g_free (drive->mountpoint);
65
66   if (G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize)
67     (*G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize) (object);
68 }
69
70 static void
71 g_unix_drive_class_init (GUnixDriveClass *klass)
72 {
73   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
74
75   gobject_class->finalize = g_unix_drive_finalize;
76 }
77
78 static void
79 g_unix_drive_init (GUnixDrive *unix_drive)
80 {
81 }
82
83 static char *
84 type_to_icon (GUnixMountType type)
85 {
86   const char *icon_name = NULL;
87   
88   switch (type)
89     {
90     case G_UNIX_MOUNT_TYPE_HD:
91       icon_name = "drive-harddisk";
92       break;
93     case G_UNIX_MOUNT_TYPE_FLOPPY:
94     case G_UNIX_MOUNT_TYPE_ZIP:
95     case G_UNIX_MOUNT_TYPE_JAZ:
96     case G_UNIX_MOUNT_TYPE_MEMSTICK:
97       icon_name = "drive-removable-media";
98       break;
99     case G_UNIX_MOUNT_TYPE_CDROM:
100       icon_name = "drive-optical";
101       break;
102     case G_UNIX_MOUNT_TYPE_NFS:
103       /* TODO: Would like a better icon here... */
104       icon_name = "drive-removable-media";
105       break;
106     case G_UNIX_MOUNT_TYPE_CAMERA:
107       icon_name = "camera-photo";
108       break;
109     case G_UNIX_MOUNT_TYPE_IPOD:
110       icon_name = "multimedia-player";
111       break;
112     case G_UNIX_MOUNT_TYPE_UNKNOWN:
113     default:
114       icon_name = "drive-removable-media";
115       break;
116     }
117   return g_strdup (icon_name);
118 }
119
120 /**
121  * g_unix_drive_new:
122  * @volume_monitor: a #GVolumeMonitor.
123  * @mountpoint: a #GUnixMountPoint.
124  * 
125  * Returns: a #GUnixDrive for the given #GUnixMountPoint.
126  **/
127 GUnixDrive *
128 g_unix_drive_new (GVolumeMonitor *volume_monitor,
129                   GUnixMountPoint *mountpoint)
130 {
131   GUnixDrive *drive;
132   
133   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
134         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
135       g_unix_mount_point_is_loopback (mountpoint))
136     return NULL;
137   
138   drive = g_object_new (G_TYPE_UNIX_DRIVE, NULL);
139
140   drive->guessed_type = g_unix_mount_point_guess_type (mountpoint);
141   
142   /* TODO: */
143   drive->mountpoint = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
144   drive->icon = type_to_icon (drive->guessed_type);
145   drive->name = g_strdup (_("Unknown drive"));
146   
147   return drive;
148 }
149
150 /**
151  * g_unix_drive_disconnected:
152  * @drive:
153  * 
154  **/
155 void
156 g_unix_drive_disconnected (GUnixDrive *drive)
157 {
158   if (drive->volume)
159     {
160       g_unix_volume_unset_drive (drive->volume, drive);
161       drive->volume = NULL;
162     }
163 }
164
165 /**
166  * g_unix_drive_set_volume:
167  * @drive:
168  * @volume:
169  *  
170  **/
171 void
172 g_unix_drive_set_volume (GUnixDrive     *drive,
173                          GUnixVolume    *volume)
174 {
175   if (drive->volume == volume)
176     return;
177   
178   if (drive->volume)
179     g_unix_volume_unset_drive (drive->volume, drive);
180   
181   drive->volume = volume;
182   
183   /* TODO: Emit changed in idle to avoid locking issues */
184   g_signal_emit_by_name (drive, "changed");
185 }
186
187 /**
188  * g_unix_drive_unset_volume:
189  * @drive:
190  * @volume:
191  *
192  **/
193 void
194 g_unix_drive_unset_volume (GUnixDrive     *drive,
195                            GUnixVolume    *volume)
196 {
197   if (drive->volume == volume)
198     {
199       drive->volume = NULL;
200       /* TODO: Emit changed in idle to avoid locking issues */
201       g_signal_emit_by_name (drive, "changed");
202     }
203 }
204
205 static GIcon *
206 g_unix_drive_get_icon (GDrive *drive)
207 {
208   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
209
210   return g_themed_icon_new (unix_drive->icon);
211 }
212
213 static char *
214 g_unix_drive_get_name (GDrive *drive)
215 {
216   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
217   
218   return g_strdup (unix_drive->name);
219 }
220
221 static gboolean
222 g_unix_drive_is_automounted (GDrive *drive)
223 {
224   /* TODO */
225   return FALSE;
226 }
227
228 static gboolean
229 g_unix_drive_can_mount (GDrive *drive)
230 {
231   /* TODO */
232   return TRUE;
233 }
234
235 static gboolean
236 g_unix_drive_can_eject (GDrive *drive)
237 {
238   /* TODO */
239   return FALSE;
240 }
241
242 static GList *
243 g_unix_drive_get_volumes (GDrive *drive)
244 {
245   GList *l;
246   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
247
248   l = NULL;
249   if (unix_drive->volume)
250     l = g_list_prepend (l, g_object_ref (unix_drive->volume));
251
252   return l;
253 }
254
255 static gboolean
256 g_unix_drive_has_volumes (GDrive *drive)
257 {
258   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
259
260   return unix_drive->volume != NULL;
261 }
262
263
264 gboolean
265 g_unix_drive_has_mountpoint (GUnixDrive *drive,
266                              const char  *mountpoint)
267 {
268   return strcmp (drive->mountpoint, mountpoint) == 0;
269 }
270
271 static void
272 g_unix_drive_mount (GDrive         *drive,
273                     GMountOperation *mount_operation,
274                     GCancellable *cancellable,
275                     GAsyncReadyCallback callback,
276                     gpointer        user_data)
277 {
278   /* TODO */
279 }
280
281
282 static gboolean
283 g_unix_drive_mount_finish (GDrive *drive,
284                            GAsyncResult *result,
285                            GError **error)
286 {
287   return TRUE;
288 }
289
290 static void
291 g_unix_drive_eject (GDrive         *drive,
292                     GCancellable *cancellable,
293                     GAsyncReadyCallback callback,
294                     gpointer        user_data)
295 {
296   /* TODO */
297 }
298
299 static gboolean
300 g_unix_drive_eject_finish (GDrive *drive,
301                            GAsyncResult *result,
302                            GError **error)
303 {
304   return TRUE;
305 }
306
307 static void
308 g_unix_volume_drive_iface_init (GDriveIface *iface)
309 {
310   iface->get_name = g_unix_drive_get_name;
311   iface->get_icon = g_unix_drive_get_icon;
312   iface->has_volumes = g_unix_drive_has_volumes;
313   iface->get_volumes = g_unix_drive_get_volumes;
314   iface->is_automounted = g_unix_drive_is_automounted;
315   iface->can_mount = g_unix_drive_can_mount;
316   iface->can_eject = g_unix_drive_can_eject;
317   iface->mount = g_unix_drive_mount;
318   iface->mount_finish = g_unix_drive_mount_finish;
319   iface->eject = g_unix_drive_eject;
320   iface->eject_finish = g_unix_drive_eject_finish;
321 }