a6e9867b25ff0a7d54db484d09f87b75af455943
[platform/upstream/glib.git] / gio / gunixvolume.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 "gunixvolumemonitor.h"
29 #include "gunixvolume.h"
30 #include "gunixdrive.h"
31 #include "gvolumeprivate.h"
32 #include "gvolumemonitor.h"
33 #include "gthemedicon.h"
34 #include "glibintl.h"
35
36 struct _GUnixVolume {
37   GObject parent;
38
39   GUnixDrive *drive; /* owned by volume monitor */
40   char *name;
41   char *icon;
42   char *mountpoint;
43 };
44
45 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
46
47 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
48                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
49                                                 g_unix_volume_volume_iface_init))
50
51
52 static void
53 g_unix_volume_finalize (GObject *object)
54 {
55   GUnixVolume *volume;
56   
57   volume = G_UNIX_VOLUME (object);
58
59   if (volume->drive)
60     g_unix_drive_unset_volume (volume->drive, volume);
61     
62   g_assert (volume->drive == NULL);
63   g_free (volume->name);
64   g_free (volume->icon);
65   g_free (volume->mountpoint);
66   
67   if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
68     (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
69 }
70
71 static void
72 g_unix_volume_class_init (GUnixVolumeClass *klass)
73 {
74   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
75
76   gobject_class->finalize = g_unix_volume_finalize;
77 }
78
79 static void
80 g_unix_volume_init (GUnixVolume *unix_volume)
81 {
82 }
83
84 static char *
85 get_filesystem_volume_name (const char *fs_type)
86 {
87   /* TODO: add translation table from gnome-vfs */
88   return g_strdup_printf (_("%s volume"), fs_type);
89 }
90
91 static char *
92 type_to_icon (GUnixMountType type)
93 {
94   const char *icon_name = NULL;
95   
96   switch (type)
97     {
98     case G_UNIX_MOUNT_TYPE_HD:
99       icon_name = "drive-harddisk";
100       break;
101     case G_UNIX_MOUNT_TYPE_FLOPPY:
102     case G_UNIX_MOUNT_TYPE_ZIP:
103     case G_UNIX_MOUNT_TYPE_JAZ:
104       icon_name = "media-floppy";
105       break;
106     case G_UNIX_MOUNT_TYPE_CDROM:
107       icon_name = "media-optical";
108       break;
109     case G_UNIX_MOUNT_TYPE_NFS:
110       /* TODO: Would like a better icon here... */
111       icon_name = "drive-harddisk";
112       break;
113     case G_UNIX_MOUNT_TYPE_MEMSTICK:
114       icon_name = "media-flash";
115       break;
116     case G_UNIX_MOUNT_TYPE_CAMERA:
117       icon_name = "camera-photo";
118       break;
119     case G_UNIX_MOUNT_TYPE_IPOD:
120       icon_name = "multimedia-player";
121       break;
122     case G_UNIX_MOUNT_TYPE_UNKNOWN:
123     default:
124       icon_name = "drive-harddisk";
125       break;
126     }
127   return g_strdup (icon_name);
128 }
129
130 /**
131  * g_unix_volume_new:
132  * @mount: 
133  * @drive:
134  * 
135  * Returns: a #GUnixVolume.
136  * 
137  **/
138 GUnixVolume *
139 g_unix_volume_new (GUnixMount *mount,
140                    GUnixDrive *drive)
141 {
142   GUnixVolume *volume;
143   GUnixMountType type;
144   const char *mount_path;
145   char *volume_name;
146   
147   mount_path = g_unix_mount_get_mount_path (mount);
148   
149   /* No drive for volume. Ignore internal things */
150   if (drive == NULL && g_unix_mount_is_system_internal (mount))
151     return NULL;
152   
153   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
154   volume->drive = drive;
155   if (drive)
156     g_unix_drive_set_volume (drive, volume);
157   volume->mountpoint = g_strdup (mount_path);
158
159   type = g_unix_mount_guess_type (mount);
160   
161   volume->icon = type_to_icon (type);
162                                           
163   volume_name = NULL;
164   if (mount_path)
165     {
166       if (strcmp (mount_path, "/") == 0)
167         volume_name = g_strdup (_("Filesystem root"));
168       else
169         volume_name = g_filename_display_basename (mount_path);
170     }
171       
172   if (volume_name == NULL)
173     {
174       if (g_unix_mount_get_fs_type (mount) != NULL)
175         volume_name = g_strdup (get_filesystem_volume_name (g_unix_mount_get_fs_type (mount)));
176     }
177   
178   if (volume_name == NULL)
179     {
180       /* TODO: Use volume size as name? */
181       volume_name = g_strdup (_("Unknown volume"));
182     }
183   
184   volume->name = volume_name;
185
186   return volume;
187 }
188
189 /**
190  * g_unix_volume_unmounted:
191  * @volume: 
192  * 
193  **/
194 void
195 g_unix_volume_unmounted (GUnixVolume *volume)
196 {
197   if (volume->drive)
198     {
199       g_unix_drive_unset_volume (volume->drive, volume);
200       volume->drive = NULL;
201       g_signal_emit_by_name (volume, "changed");
202     }
203 }
204
205 /**
206  * g_unix_volume_unset_drive:
207  * @volume:
208  * @drive:
209  *   
210  **/
211 void
212 g_unix_volume_unset_drive (GUnixVolume   *volume,
213                            GUnixDrive    *drive)
214 {
215   if (volume->drive == drive)
216     {
217       volume->drive = NULL;
218       /* TODO: Emit changed in idle to avoid locking issues */
219       g_signal_emit_by_name (volume, "changed");
220     }
221 }
222
223 static GFile *
224 g_unix_volume_get_root (GVolume *volume)
225 {
226   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
227
228   return g_file_new_for_path (unix_volume->mountpoint);
229 }
230
231 static GIcon *
232 g_unix_volume_get_icon (GVolume *volume)
233 {
234   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
235
236   return g_themed_icon_new (unix_volume->icon);
237 }
238
239 static char *
240 g_unix_volume_get_name (GVolume *volume)
241 {
242   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
243   
244   return g_strdup (unix_volume->name);
245 }
246
247 /**
248  * g_unix_volume_has_mountpoint:
249  * @volume:
250  * @mountpoint:
251  * 
252  * Returns: 
253  **/
254 gboolean
255 g_unix_volume_has_mountpoint (GUnixVolume *volume,
256                               const char  *mountpoint)
257 {
258   return strcmp (volume->mountpoint, mountpoint) == 0;
259 }
260
261 static GDrive *
262 g_unix_volume_get_drive (GVolume *volume)
263 {
264   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
265
266   if (unix_volume->drive)
267     return G_DRIVE (g_object_ref (unix_volume->drive));
268   
269   return NULL;
270 }
271
272 static gboolean
273 g_unix_volume_can_unmount (GVolume *volume)
274 {
275   /* TODO */
276   return FALSE;
277 }
278
279 static gboolean
280 g_unix_volume_can_eject (GVolume *volume)
281 {
282   /* TODO */
283   return FALSE;
284 }
285
286 static void
287 g_unix_volume_unmount (GVolume         *volume,
288                        GAsyncReadyCallback callback,
289                        gpointer         user_data)
290 {
291   /* TODO */
292 }
293
294 static gboolean
295 g_unix_volume_unmount_finish (GVolume *volume,
296                               GAsyncResult *result,
297                               GError **error)
298 {
299   return TRUE;
300 }
301
302 static void
303 g_unix_volume_eject (GVolume         *volume,
304                      GAsyncReadyCallback callback,
305                      gpointer         user_data)
306 {
307   /* TODO */
308 }
309
310 static gboolean
311 g_unix_volume_eject_finish (GVolume *volume,
312                             GAsyncResult *result,
313                             GError **error)
314 {
315   return TRUE;
316 }
317
318 static void
319 g_unix_volume_volume_iface_init (GVolumeIface *iface)
320 {
321   iface->get_root = g_unix_volume_get_root;
322   iface->get_name = g_unix_volume_get_name;
323   iface->get_icon = g_unix_volume_get_icon;
324   iface->get_drive = g_unix_volume_get_drive;
325   iface->can_unmount = g_unix_volume_can_unmount;
326   iface->can_eject = g_unix_volume_can_eject;
327   iface->unmount = g_unix_volume_unmount;
328   iface->unmount_finish = g_unix_volume_unmount_finish;
329   iface->eject = g_unix_volume_eject;
330   iface->eject_finish = g_unix_volume_eject_finish;
331 }