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 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))
55 g_unix_volume_finalize (GObject *object)
59 volume = G_UNIX_VOLUME (object);
62 g_unix_drive_unset_volume (volume->drive, volume);
64 g_assert (volume->drive == NULL);
65 g_free (volume->name);
66 g_free (volume->icon);
67 g_free (volume->mountpoint);
69 if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
70 (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
74 g_unix_volume_class_init (GUnixVolumeClass *klass)
76 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78 gobject_class->finalize = g_unix_volume_finalize;
82 g_unix_volume_init (GUnixVolume *unix_volume)
87 get_filesystem_volume_name (const char *fs_type)
89 /* TODO: add translation table from gnome-vfs */
90 return g_strdup_printf (_("%s volume"), fs_type);
94 type_to_icon (GUnixMountType type)
96 const char *icon_name = NULL;
100 case G_UNIX_MOUNT_TYPE_HD:
101 icon_name = "drive-harddisk";
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";
108 case G_UNIX_MOUNT_TYPE_CDROM:
109 icon_name = "media-optical";
111 case G_UNIX_MOUNT_TYPE_NFS:
112 /* TODO: Would like a better icon here... */
113 icon_name = "drive-harddisk";
115 case G_UNIX_MOUNT_TYPE_MEMSTICK:
116 icon_name = "media-flash";
118 case G_UNIX_MOUNT_TYPE_CAMERA:
119 icon_name = "camera-photo";
121 case G_UNIX_MOUNT_TYPE_IPOD:
122 icon_name = "multimedia-player";
124 case G_UNIX_MOUNT_TYPE_UNKNOWN:
126 icon_name = "drive-harddisk";
129 return g_strdup (icon_name);
137 * Returns: a #GUnixVolume.
141 g_unix_volume_new (GUnixMount *mount,
146 const char *mount_path;
149 mount_path = g_unix_mount_get_mount_path (mount);
151 /* No drive for volume. Ignore internal things */
152 if (drive == NULL && g_unix_mount_is_system_internal (mount))
155 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
156 volume->drive = drive;
158 g_unix_drive_set_volume (drive, volume);
159 volume->mountpoint = g_strdup (mount_path);
161 type = g_unix_mount_guess_type (mount);
163 volume->icon = type_to_icon (type);
168 if (strcmp (mount_path, "/") == 0)
169 volume_name = g_strdup (_("Filesystem root"));
171 volume_name = g_filename_display_basename (mount_path);
174 if (volume_name == NULL)
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)));
180 if (volume_name == NULL)
182 /* TODO: Use volume size as name? */
183 volume_name = g_strdup (_("Unknown volume"));
186 volume->name = volume_name;
192 * g_unix_volume_unmounted:
197 g_unix_volume_unmounted (GUnixVolume *volume)
201 g_unix_drive_unset_volume (volume->drive, volume);
202 volume->drive = NULL;
203 g_signal_emit_by_name (volume, "changed");
208 * g_unix_volume_unset_drive:
214 g_unix_volume_unset_drive (GUnixVolume *volume,
217 if (volume->drive == drive)
219 volume->drive = NULL;
220 /* TODO: Emit changed in idle to avoid locking issues */
221 g_signal_emit_by_name (volume, "changed");
226 g_unix_volume_get_root (GVolume *volume)
228 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
230 return g_file_new_for_path (unix_volume->mountpoint);
234 g_unix_volume_get_icon (GVolume *volume)
236 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
238 return g_themed_icon_new (unix_volume->icon);
242 g_unix_volume_get_name (GVolume *volume)
244 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246 return g_strdup (unix_volume->name);
250 * g_unix_volume_has_mountpoint:
257 g_unix_volume_has_mountpoint (GUnixVolume *volume,
258 const char *mountpoint)
260 return strcmp (volume->mountpoint, mountpoint) == 0;
264 g_unix_volume_get_drive (GVolume *volume)
266 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
268 if (unix_volume->drive)
269 return G_DRIVE (g_object_ref (unix_volume->drive));
275 g_unix_volume_can_unmount (GVolume *volume)
282 g_unix_volume_can_eject (GVolume *volume)
289 g_unix_volume_unmount (GVolume *volume,
290 GAsyncReadyCallback callback,
297 g_unix_volume_unmount_finish (GVolume *volume,
298 GAsyncResult *result,
305 g_unix_volume_eject (GVolume *volume,
306 GAsyncReadyCallback callback,
313 g_unix_volume_eject_finish (GVolume *volume,
314 GAsyncResult *result,
321 g_unix_volume_volume_iface_init (GVolumeIface *iface)
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;