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"
41 GUnixDrive *drive; /* owned by volume monitor */
47 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
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))
56 g_unix_volume_finalize (GObject *object)
60 volume = G_UNIX_VOLUME (object);
63 _g_unix_drive_unset_volume (volume->drive, volume);
65 g_warn_if_fail (volume->drive == NULL);
66 g_free (volume->name);
67 g_free (volume->icon);
68 g_free (volume->mountpoint);
70 if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
71 (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
75 g_unix_volume_class_init (GUnixVolumeClass *klass)
77 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79 gobject_class->finalize = g_unix_volume_finalize;
83 g_unix_volume_init (GUnixVolume *unix_volume)
88 get_filesystem_volume_name (const char *fs_type)
90 /* TODO: add translation table from gnome-vfs */
91 return g_strdup_printf (_("%s volume"), fs_type);
95 type_to_icon (GUnixMountType type)
97 const char *icon_name = NULL;
101 case G_UNIX_MOUNT_TYPE_HD:
102 icon_name = "drive-harddisk";
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";
109 case G_UNIX_MOUNT_TYPE_CDROM:
110 icon_name = "media-optical";
112 case G_UNIX_MOUNT_TYPE_NFS:
113 /* TODO: Would like a better icon here... */
114 icon_name = "drive-harddisk";
116 case G_UNIX_MOUNT_TYPE_MEMSTICK:
117 icon_name = "media-flash";
119 case G_UNIX_MOUNT_TYPE_CAMERA:
120 icon_name = "camera-photo";
122 case G_UNIX_MOUNT_TYPE_IPOD:
123 icon_name = "multimedia-player";
125 case G_UNIX_MOUNT_TYPE_UNKNOWN:
127 icon_name = "drive-harddisk";
130 return g_strdup (icon_name);
134 _g_unix_volume_new (GUnixMount *mount,
139 const char *mount_path;
142 mount_path = g_unix_mount_get_mount_path (mount);
144 /* No drive for volume. Ignore internal things */
145 if (drive == NULL && g_unix_mount_is_system_internal (mount))
148 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
149 volume->drive = drive;
151 _g_unix_drive_set_volume (drive, volume);
152 volume->mountpoint = g_strdup (mount_path);
154 type = g_unix_mount_guess_type (mount);
156 volume->icon = type_to_icon (type);
161 if (strcmp (mount_path, "/") == 0)
162 volume_name = g_strdup (_("Filesystem root"));
164 volume_name = g_filename_display_basename (mount_path);
167 if (volume_name == NULL)
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)));
173 if (volume_name == NULL)
174 /* TODO: Use volume size as name? */
175 volume_name = g_strdup (_("Unknown volume"));
177 volume->name = volume_name;
183 _g_unix_volume_unmounted (GUnixVolume *volume)
187 _g_unix_drive_unset_volume (volume->drive, volume);
188 volume->drive = NULL;
189 g_signal_emit_by_name (volume, "changed");
194 _g_unix_volume_unset_drive (GUnixVolume *volume,
197 if (volume->drive == drive)
199 volume->drive = NULL;
200 /* TODO: Emit changed in idle to avoid locking issues */
201 g_signal_emit_by_name (volume, "changed");
206 g_unix_volume_get_root (GVolume *volume)
208 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
210 return g_file_new_for_path (unix_volume->mountpoint);
214 g_unix_volume_get_icon (GVolume *volume)
216 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
218 return g_themed_icon_new (unix_volume->icon);
222 g_unix_volume_get_name (GVolume *volume)
224 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
226 return g_strdup (unix_volume->name);
230 _g_unix_volume_has_mountpoint (GUnixVolume *volume,
231 const char *mountpoint)
233 return strcmp (volume->mountpoint, mountpoint) == 0;
237 g_unix_volume_get_drive (GVolume *volume)
239 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
241 if (unix_volume->drive)
242 return G_DRIVE (g_object_ref (unix_volume->drive));
248 g_unix_volume_can_unmount (GVolume *volume)
255 g_unix_volume_can_eject (GVolume *volume)
262 g_unix_volume_unmount (GVolume *volume,
263 GCancellable *cancellable,
264 GAsyncReadyCallback callback,
271 g_unix_volume_unmount_finish (GVolume *volume,
272 GAsyncResult *result,
279 g_unix_volume_eject (GVolume *volume,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
288 g_unix_volume_eject_finish (GVolume *volume,
289 GAsyncResult *result,
296 g_unix_volume_volume_iface_init (GVolumeIface *iface)
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;