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_assert (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);
138 * Returns: a #GUnixVolume.
142 _g_unix_volume_new (GUnixMount *mount,
147 const char *mount_path;
150 mount_path = g_unix_mount_get_mount_path (mount);
152 /* No drive for volume. Ignore internal things */
153 if (drive == NULL && g_unix_mount_is_system_internal (mount))
156 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
157 volume->drive = drive;
159 _g_unix_drive_set_volume (drive, volume);
160 volume->mountpoint = g_strdup (mount_path);
162 type = g_unix_mount_guess_type (mount);
164 volume->icon = type_to_icon (type);
169 if (strcmp (mount_path, "/") == 0)
170 volume_name = g_strdup (_("Filesystem root"));
172 volume_name = g_filename_display_basename (mount_path);
175 if (volume_name == NULL)
177 if (g_unix_mount_get_fs_type (mount) != NULL)
178 volume_name = g_strdup (get_filesystem_volume_name (g_unix_mount_get_fs_type (mount)));
181 if (volume_name == NULL)
183 /* TODO: Use volume size as name? */
184 volume_name = g_strdup (_("Unknown volume"));
187 volume->name = volume_name;
193 * g_unix_volume_unmounted:
198 _g_unix_volume_unmounted (GUnixVolume *volume)
202 _g_unix_drive_unset_volume (volume->drive, volume);
203 volume->drive = NULL;
204 g_signal_emit_by_name (volume, "changed");
209 * g_unix_volume_unset_drive:
215 _g_unix_volume_unset_drive (GUnixVolume *volume,
218 if (volume->drive == drive)
220 volume->drive = NULL;
221 /* TODO: Emit changed in idle to avoid locking issues */
222 g_signal_emit_by_name (volume, "changed");
227 g_unix_volume_get_root (GVolume *volume)
229 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
231 return g_file_new_for_path (unix_volume->mountpoint);
235 g_unix_volume_get_icon (GVolume *volume)
237 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
239 return g_themed_icon_new (unix_volume->icon);
243 g_unix_volume_get_name (GVolume *volume)
245 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
247 return g_strdup (unix_volume->name);
251 * g_unix_volume_has_mountpoint:
258 _g_unix_volume_has_mountpoint (GUnixVolume *volume,
259 const char *mountpoint)
261 return strcmp (volume->mountpoint, mountpoint) == 0;
265 g_unix_volume_get_drive (GVolume *volume)
267 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
269 if (unix_volume->drive)
270 return G_DRIVE (g_object_ref (unix_volume->drive));
276 g_unix_volume_can_unmount (GVolume *volume)
283 g_unix_volume_can_eject (GVolume *volume)
290 g_unix_volume_unmount (GVolume *volume,
291 GCancellable *cancellable,
292 GAsyncReadyCallback callback,
299 g_unix_volume_unmount_finish (GVolume *volume,
300 GAsyncResult *result,
307 g_unix_volume_eject (GVolume *volume,
308 GCancellable *cancellable,
309 GAsyncReadyCallback callback,
316 g_unix_volume_eject_finish (GVolume *volume,
317 GAsyncResult *result,
324 g_unix_volume_volume_iface_init (GVolumeIface *iface)
326 iface->get_root = g_unix_volume_get_root;
327 iface->get_name = g_unix_volume_get_name;
328 iface->get_icon = g_unix_volume_get_icon;
329 iface->get_drive = g_unix_volume_get_drive;
330 iface->can_unmount = g_unix_volume_can_unmount;
331 iface->can_eject = g_unix_volume_can_eject;
332 iface->unmount = g_unix_volume_unmount;
333 iface->unmount_finish = g_unix_volume_unmount_finish;
334 iface->eject = g_unix_volume_eject;
335 iface->eject_finish = g_unix_volume_eject_finish;