2a5e40c8c6879dc2cf051188b9492cf53f9325d7
[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 "gthemedicon.h"
31 #include "gvolumemonitor.h"
32 #include "glibintl.h"
33
34 #include "gioalias.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 #define g_unix_drive_get_type _g_unix_drive_get_type
49 G_DEFINE_TYPE_WITH_CODE (GUnixDrive, g_unix_drive, G_TYPE_OBJECT,
50                          G_IMPLEMENT_INTERFACE (G_TYPE_DRIVE,
51                                                 g_unix_volume_drive_iface_init))
52
53 static void
54 g_unix_drive_finalize (GObject *object)
55 {
56   GUnixDrive *drive;
57   
58   drive = G_UNIX_DRIVE (object);
59
60   if (drive->volume)
61     _g_unix_volume_unset_drive (drive->volume, drive);
62   
63   g_free (drive->name);
64   g_free (drive->icon);
65   g_free (drive->mountpoint);
66
67   if (G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize)
68     (*G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize) (object);
69 }
70
71 static void
72 g_unix_drive_class_init (GUnixDriveClass *klass)
73 {
74   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
75
76   gobject_class->finalize = g_unix_drive_finalize;
77 }
78
79 static void
80 g_unix_drive_init (GUnixDrive *unix_drive)
81 {
82 }
83
84 static char *
85 type_to_icon (GUnixMountType type)
86 {
87   const char *icon_name = NULL;
88   
89   switch (type)
90     {
91     case G_UNIX_MOUNT_TYPE_HD:
92       icon_name = "drive-harddisk";
93       break;
94     case G_UNIX_MOUNT_TYPE_FLOPPY:
95     case G_UNIX_MOUNT_TYPE_ZIP:
96     case G_UNIX_MOUNT_TYPE_JAZ:
97     case G_UNIX_MOUNT_TYPE_MEMSTICK:
98       icon_name = "drive-removable-media";
99       break;
100     case G_UNIX_MOUNT_TYPE_CDROM:
101       icon_name = "drive-optical";
102       break;
103     case G_UNIX_MOUNT_TYPE_NFS:
104       /* TODO: Would like a better icon here... */
105       icon_name = "drive-removable-media";
106       break;
107     case G_UNIX_MOUNT_TYPE_CAMERA:
108       icon_name = "camera-photo";
109       break;
110     case G_UNIX_MOUNT_TYPE_IPOD:
111       icon_name = "multimedia-player";
112       break;
113     case G_UNIX_MOUNT_TYPE_UNKNOWN:
114     default:
115       icon_name = "drive-removable-media";
116       break;
117     }
118   return g_strdup (icon_name);
119 }
120
121 /**
122  * g_unix_drive_new:
123  * @volume_monitor: a #GVolumeMonitor.
124  * @mountpoint: a #GUnixMountPoint.
125  * 
126  * Returns: a #GUnixDrive for the given #GUnixMountPoint.
127  **/
128 GUnixDrive *
129 _g_unix_drive_new (GVolumeMonitor  *volume_monitor,
130                    GUnixMountPoint *mountpoint)
131 {
132   GUnixDrive *drive;
133   
134   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
135         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
136       g_unix_mount_point_is_loopback (mountpoint))
137     return NULL;
138   
139   drive = g_object_new (G_TYPE_UNIX_DRIVE, NULL);
140
141   drive->guessed_type = g_unix_mount_point_guess_type (mountpoint);
142   
143   /* TODO: */
144   drive->mountpoint = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
145   drive->icon = type_to_icon (drive->guessed_type);
146   drive->name = g_strdup (_("Unknown drive"));
147   
148   return drive;
149 }
150
151 /**
152  * g_unix_drive_disconnected:
153  * @drive:
154  * 
155  **/
156 void
157 _g_unix_drive_disconnected (GUnixDrive *drive)
158 {
159   if (drive->volume)
160     {
161       _g_unix_volume_unset_drive (drive->volume, drive);
162       drive->volume = NULL;
163     }
164 }
165
166 /**
167  * g_unix_drive_set_volume:
168  * @drive:
169  * @volume:
170  *  
171  **/
172 void
173 _g_unix_drive_set_volume (GUnixDrive  *drive,
174                           GUnixVolume *volume)
175 {
176   if (drive->volume == volume)
177     return;
178   
179   if (drive->volume)
180     _g_unix_volume_unset_drive (drive->volume, drive);
181   
182   drive->volume = volume;
183   
184   /* TODO: Emit changed in idle to avoid locking issues */
185   g_signal_emit_by_name (drive, "changed");
186 }
187
188 /**
189  * g_unix_drive_unset_volume:
190  * @drive:
191  * @volume:
192  *
193  **/
194 void
195 _g_unix_drive_unset_volume (GUnixDrive  *drive,
196                             GUnixVolume *volume)
197 {
198   if (drive->volume == volume)
199     {
200       drive->volume = NULL;
201       /* TODO: Emit changed in idle to avoid locking issues */
202       g_signal_emit_by_name (drive, "changed");
203     }
204 }
205
206 static GIcon *
207 g_unix_drive_get_icon (GDrive *drive)
208 {
209   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
210
211   return g_themed_icon_new (unix_drive->icon);
212 }
213
214 static char *
215 g_unix_drive_get_name (GDrive *drive)
216 {
217   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
218   
219   return g_strdup (unix_drive->name);
220 }
221
222 static gboolean
223 g_unix_drive_is_automounted (GDrive *drive)
224 {
225   /* TODO */
226   return FALSE;
227 }
228
229 static gboolean
230 g_unix_drive_can_mount (GDrive *drive)
231 {
232   /* TODO */
233   return TRUE;
234 }
235
236 static gboolean
237 g_unix_drive_can_eject (GDrive *drive)
238 {
239   /* TODO */
240   return FALSE;
241 }
242
243 static GList *
244 g_unix_drive_get_volumes (GDrive *drive)
245 {
246   GList *l;
247   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
248
249   l = NULL;
250   if (unix_drive->volume)
251     l = g_list_prepend (l, g_object_ref (unix_drive->volume));
252
253   return l;
254 }
255
256 static gboolean
257 g_unix_drive_has_volumes (GDrive *drive)
258 {
259   GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
260
261   return unix_drive->volume != NULL;
262 }
263
264
265 gboolean
266 _g_unix_drive_has_mountpoint (GUnixDrive *drive,
267                              const char  *mountpoint)
268 {
269   return strcmp (drive->mountpoint, mountpoint) == 0;
270 }
271
272 static void
273 g_unix_drive_mount (GDrive              *drive,
274                     GMountOperation     *mount_operation,
275                     GCancellable        *cancellable,
276                     GAsyncReadyCallback  callback,
277                     gpointer             user_data)
278 {
279   /* TODO */
280 }
281
282
283 static gboolean
284 g_unix_drive_mount_finish (GDrive        *drive,
285                            GAsyncResult  *result,
286                            GError       **error)
287 {
288   return TRUE;
289 }
290
291 static void
292 g_unix_drive_eject (GDrive              *drive,
293                     GCancellable        *cancellable,
294                     GAsyncReadyCallback  callback,
295                     gpointer             user_data)
296 {
297   /* TODO */
298 }
299
300 static gboolean
301 g_unix_drive_eject_finish (GDrive        *drive,
302                            GAsyncResult  *result,
303                            GError       **error)
304 {
305   return TRUE;
306 }
307
308 static void
309 g_unix_volume_drive_iface_init (GDriveIface *iface)
310 {
311   iface->get_name = g_unix_drive_get_name;
312   iface->get_icon = g_unix_drive_get_icon;
313   iface->has_volumes = g_unix_drive_has_volumes;
314   iface->get_volumes = g_unix_drive_get_volumes;
315   iface->is_automounted = g_unix_drive_is_automounted;
316   iface->can_mount = g_unix_drive_can_mount;
317   iface->can_eject = g_unix_drive_can_eject;
318   iface->mount = g_unix_drive_mount;
319   iface->mount_finish = g_unix_drive_mount_finish;
320   iface->eject = g_unix_drive_eject;
321   iface->eject_finish = g_unix_drive_eject_finish;
322 }