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