Add new error codes for when compilation fails and make compilation error
[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     /* TODO: Use volume size as name? */
175     volume_name = g_strdup (_("Unknown volume"));
176   
177   volume->name = volume_name;
178
179   return volume;
180 }
181
182 void
183 _g_unix_volume_unmounted (GUnixVolume *volume)
184 {
185   if (volume->drive)
186     {
187       _g_unix_drive_unset_volume (volume->drive, volume);
188       volume->drive = NULL;
189       g_signal_emit_by_name (volume, "changed");
190     }
191 }
192
193 void
194 _g_unix_volume_unset_drive (GUnixVolume *volume,
195                             GUnixDrive  *drive)
196 {
197   if (volume->drive == drive)
198     {
199       volume->drive = NULL;
200       /* TODO: Emit changed in idle to avoid locking issues */
201       g_signal_emit_by_name (volume, "changed");
202     }
203 }
204
205 static GFile *
206 g_unix_volume_get_root (GVolume *volume)
207 {
208   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
209
210   return g_file_new_for_path (unix_volume->mountpoint);
211 }
212
213 static GIcon *
214 g_unix_volume_get_icon (GVolume *volume)
215 {
216   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
217
218   return g_themed_icon_new (unix_volume->icon);
219 }
220
221 static char *
222 g_unix_volume_get_name (GVolume *volume)
223 {
224   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
225   
226   return g_strdup (unix_volume->name);
227 }
228
229 gboolean
230 _g_unix_volume_has_mountpoint (GUnixVolume *volume,
231                                const char  *mountpoint)
232 {
233   return strcmp (volume->mountpoint, mountpoint) == 0;
234 }
235
236 static GDrive *
237 g_unix_volume_get_drive (GVolume *volume)
238 {
239   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
240
241   if (unix_volume->drive)
242     return G_DRIVE (g_object_ref (unix_volume->drive));
243   
244   return NULL;
245 }
246
247 static gboolean
248 g_unix_volume_can_unmount (GVolume *volume)
249 {
250   /* TODO */
251   return FALSE;
252 }
253
254 static gboolean
255 g_unix_volume_can_eject (GVolume *volume)
256 {
257   /* TODO */
258   return FALSE;
259 }
260
261 static void
262 g_unix_volume_unmount (GVolume             *volume,
263                        GCancellable        *cancellable,
264                        GAsyncReadyCallback  callback,
265                        gpointer             user_data)
266 {
267   /* TODO */
268 }
269
270 static gboolean
271 g_unix_volume_unmount_finish (GVolume       *volume,
272                               GAsyncResult  *result,
273                               GError       **error)
274 {
275   return TRUE;
276 }
277
278 static void
279 g_unix_volume_eject (GVolume         *volume,
280                      GCancellable    *cancellable,
281                      GAsyncReadyCallback callback,
282                      gpointer         user_data)
283 {
284   /* TODO */
285 }
286
287 static gboolean
288 g_unix_volume_eject_finish (GVolume *volume,
289                             GAsyncResult *result,
290                             GError **error)
291 {
292   return TRUE;
293 }
294
295 static void
296 g_unix_volume_volume_iface_init (GVolumeIface *iface)
297 {
298   iface->get_root = g_unix_volume_get_root;
299   iface->get_name = g_unix_volume_get_name;
300   iface->get_icon = g_unix_volume_get_icon;
301   iface->get_drive = g_unix_volume_get_drive;
302   iface->can_unmount = g_unix_volume_can_unmount;
303   iface->can_eject = g_unix_volume_can_eject;
304   iface->unmount = g_unix_volume_unmount;
305   iface->unmount_finish = g_unix_volume_unmount_finish;
306   iface->eject = g_unix_volume_eject;
307   iface->eject_finish = g_unix_volume_eject_finish;
308 }