1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
28 #include "gunixvolumemonitor.h"
29 #include "gunixvolume.h"
30 #include "gunixdrive.h"
31 #include "gvolumeprivate.h"
32 #include "gvolumemonitor.h"
33 #include "gthemedicon.h"
40 GUnixDrive *drive; /* owned by volume monitor */
46 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
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))
54 g_unix_volume_finalize (GObject *object)
58 volume = G_UNIX_VOLUME (object);
61 g_unix_drive_unset_volume (volume->drive, volume);
63 g_assert (volume->drive == NULL);
64 g_free (volume->name);
65 g_free (volume->icon);
66 g_free (volume->mountpoint);
68 if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
69 (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
73 g_unix_volume_class_init (GUnixVolumeClass *klass)
75 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
77 gobject_class->finalize = g_unix_volume_finalize;
81 g_unix_volume_init (GUnixVolume *unix_volume)
86 get_filesystem_volume_name (const char *fs_type)
88 /* TODO: add translation table from gnome-vfs */
89 return g_strdup_printf (_("%s volume"), fs_type);
93 type_to_icon (GUnixMountType type)
95 const char *icon_name = NULL;
99 case G_UNIX_MOUNT_TYPE_HD:
100 icon_name = "drive-harddisk";
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";
107 case G_UNIX_MOUNT_TYPE_CDROM:
108 icon_name = "media-optical";
110 case G_UNIX_MOUNT_TYPE_NFS:
111 /* TODO: Would like a better icon here... */
112 icon_name = "drive-harddisk";
114 case G_UNIX_MOUNT_TYPE_MEMSTICK:
115 icon_name = "media-flash";
117 case G_UNIX_MOUNT_TYPE_CAMERA:
118 icon_name = "camera-photo";
120 case G_UNIX_MOUNT_TYPE_IPOD:
121 icon_name = "multimedia-player";
123 case G_UNIX_MOUNT_TYPE_UNKNOWN:
125 icon_name = "drive-harddisk";
128 return g_strdup (icon_name);
136 * Returns: a #GUnixVolume.
140 g_unix_volume_new (GUnixMount *mount,
145 const char *mount_path;
148 mount_path = g_unix_mount_get_mount_path (mount);
150 /* No drive for volume. Ignore internal things */
151 if (drive == NULL && g_unix_mount_is_system_internal (mount))
154 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
155 volume->drive = drive;
157 g_unix_drive_set_volume (drive, volume);
158 volume->mountpoint = g_strdup (mount_path);
160 type = g_unix_mount_guess_type (mount);
162 volume->icon = type_to_icon (type);
167 if (strcmp (mount_path, "/") == 0)
168 volume_name = g_strdup (_("Filesystem root"));
170 volume_name = g_filename_display_basename (mount_path);
173 if (volume_name == NULL)
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)));
179 if (volume_name == NULL)
181 /* TODO: Use volume size as name? */
182 volume_name = g_strdup (_("Unknown volume"));
185 volume->name = volume_name;
191 * g_unix_volume_unmounted:
196 g_unix_volume_unmounted (GUnixVolume *volume)
200 g_unix_drive_unset_volume (volume->drive, volume);
201 volume->drive = NULL;
202 g_signal_emit_by_name (volume, "changed");
207 * g_unix_volume_unset_drive:
213 g_unix_volume_unset_drive (GUnixVolume *volume,
216 if (volume->drive == drive)
218 volume->drive = NULL;
219 /* TODO: Emit changed in idle to avoid locking issues */
220 g_signal_emit_by_name (volume, "changed");
225 g_unix_volume_get_root (GVolume *volume)
227 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
229 return g_file_new_for_path (unix_volume->mountpoint);
233 g_unix_volume_get_icon (GVolume *volume)
235 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
237 return g_themed_icon_new (unix_volume->icon);
241 g_unix_volume_get_name (GVolume *volume)
243 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
245 return g_strdup (unix_volume->name);
249 * g_unix_volume_has_mountpoint:
256 g_unix_volume_has_mountpoint (GUnixVolume *volume,
257 const char *mountpoint)
259 return strcmp (volume->mountpoint, mountpoint) == 0;
263 g_unix_volume_get_drive (GVolume *volume)
265 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
267 if (unix_volume->drive)
268 return G_DRIVE (g_object_ref (unix_volume->drive));
274 g_unix_volume_can_unmount (GVolume *volume)
281 g_unix_volume_can_eject (GVolume *volume)
288 g_unix_volume_unmount (GVolume *volume,
289 GAsyncReadyCallback callback,
296 g_unix_volume_unmount_finish (GVolume *volume,
297 GAsyncResult *result,
304 g_unix_volume_eject (GVolume *volume,
305 GAsyncReadyCallback callback,
312 g_unix_volume_eject_finish (GVolume *volume,
313 GAsyncResult *result,
320 g_unix_volume_volume_iface_init (GVolumeIface *iface)
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;