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