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