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"
39 GUnixDrive *drive; /* owned by volume monitor */
45 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
47 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
48 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
49 g_unix_volume_volume_iface_init))
53 g_unix_volume_finalize (GObject *object)
57 volume = G_UNIX_VOLUME (object);
60 g_unix_drive_unset_volume (volume->drive, volume);
62 g_assert (volume->drive == NULL);
63 g_free (volume->name);
64 g_free (volume->icon);
65 g_free (volume->mountpoint);
67 if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
68 (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
72 g_unix_volume_class_init (GUnixVolumeClass *klass)
74 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
76 gobject_class->finalize = g_unix_volume_finalize;
80 g_unix_volume_init (GUnixVolume *unix_volume)
85 get_filesystem_volume_name (const char *fs_type)
87 /* TODO: add translation table from gnome-vfs */
88 return g_strdup_printf (_("%s volume"), fs_type);
92 type_to_icon (GUnixMountType type)
94 const char *icon_name = NULL;
98 case G_UNIX_MOUNT_TYPE_HD:
99 icon_name = "drive-harddisk";
101 case G_UNIX_MOUNT_TYPE_FLOPPY:
102 case G_UNIX_MOUNT_TYPE_ZIP:
103 case G_UNIX_MOUNT_TYPE_JAZ:
104 icon_name = "media-floppy";
106 case G_UNIX_MOUNT_TYPE_CDROM:
107 icon_name = "media-optical";
109 case G_UNIX_MOUNT_TYPE_NFS:
110 /* TODO: Would like a better icon here... */
111 icon_name = "drive-harddisk";
113 case G_UNIX_MOUNT_TYPE_MEMSTICK:
114 icon_name = "media-flash";
116 case G_UNIX_MOUNT_TYPE_CAMERA:
117 icon_name = "camera-photo";
119 case G_UNIX_MOUNT_TYPE_IPOD:
120 icon_name = "multimedia-player";
122 case G_UNIX_MOUNT_TYPE_UNKNOWN:
124 icon_name = "drive-harddisk";
127 return g_strdup (icon_name);
135 * Returns: a #GUnixVolume.
139 g_unix_volume_new (GUnixMount *mount,
144 const char *mount_path;
147 mount_path = g_unix_mount_get_mount_path (mount);
149 /* No drive for volume. Ignore internal things */
150 if (drive == NULL && g_unix_mount_is_system_internal (mount))
153 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
154 volume->drive = drive;
156 g_unix_drive_set_volume (drive, volume);
157 volume->mountpoint = g_strdup (mount_path);
159 type = g_unix_mount_guess_type (mount);
161 volume->icon = type_to_icon (type);
166 if (strcmp (mount_path, "/") == 0)
167 volume_name = g_strdup (_("Filesystem root"));
169 volume_name = g_filename_display_basename (mount_path);
172 if (volume_name == NULL)
174 if (g_unix_mount_get_fs_type (mount) != NULL)
175 volume_name = g_strdup (get_filesystem_volume_name (g_unix_mount_get_fs_type (mount)));
178 if (volume_name == NULL)
180 /* TODO: Use volume size as name? */
181 volume_name = g_strdup (_("Unknown volume"));
184 volume->name = volume_name;
190 * g_unix_volume_unmounted:
195 g_unix_volume_unmounted (GUnixVolume *volume)
199 g_unix_drive_unset_volume (volume->drive, volume);
200 volume->drive = NULL;
201 g_signal_emit_by_name (volume, "changed");
206 * g_unix_volume_unset_drive:
212 g_unix_volume_unset_drive (GUnixVolume *volume,
215 if (volume->drive == drive)
217 volume->drive = NULL;
218 /* TODO: Emit changed in idle to avoid locking issues */
219 g_signal_emit_by_name (volume, "changed");
224 g_unix_volume_get_root (GVolume *volume)
226 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
228 return g_file_new_for_path (unix_volume->mountpoint);
232 g_unix_volume_get_icon (GVolume *volume)
234 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
236 return g_themed_icon_new (unix_volume->icon);
240 g_unix_volume_get_name (GVolume *volume)
242 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
244 return g_strdup (unix_volume->name);
248 * g_unix_volume_has_mountpoint:
255 g_unix_volume_has_mountpoint (GUnixVolume *volume,
256 const char *mountpoint)
258 return strcmp (volume->mountpoint, mountpoint) == 0;
262 g_unix_volume_get_drive (GVolume *volume)
264 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
266 if (unix_volume->drive)
267 return G_DRIVE (g_object_ref (unix_volume->drive));
273 g_unix_volume_can_unmount (GVolume *volume)
280 g_unix_volume_can_eject (GVolume *volume)
287 g_unix_volume_unmount (GVolume *volume,
288 GAsyncReadyCallback callback,
295 g_unix_volume_unmount_finish (GVolume *volume,
296 GAsyncResult *result,
303 g_unix_volume_eject (GVolume *volume,
304 GAsyncReadyCallback callback,
311 g_unix_volume_eject_finish (GVolume *volume,
312 GAsyncResult *result,
319 g_unix_volume_volume_iface_init (GVolumeIface *iface)
321 iface->get_root = g_unix_volume_get_root;
322 iface->get_name = g_unix_volume_get_name;
323 iface->get_icon = g_unix_volume_get_icon;
324 iface->get_drive = g_unix_volume_get_drive;
325 iface->can_unmount = g_unix_volume_can_unmount;
326 iface->can_eject = g_unix_volume_can_eject;
327 iface->unmount = g_unix_volume_unmount;
328 iface->unmount_finish = g_unix_volume_unmount_finish;
329 iface->eject = g_unix_volume_eject;
330 iface->eject_finish = g_unix_volume_eject_finish;