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 "gunixdrive.h"
29 #include "gunixvolume.h"
30 #include "gdriveprivate.h"
31 #include "gthemedicon.h"
32 #include "gvolumemonitor.h"
39 GUnixVolume *volume; /* owned by volume monitor */
43 GUnixMountType guessed_type;
46 static void g_unix_volume_drive_iface_init (GDriveIface *iface);
48 G_DEFINE_TYPE_WITH_CODE (GUnixDrive, g_unix_drive, G_TYPE_OBJECT,
49 G_IMPLEMENT_INTERFACE (G_TYPE_DRIVE,
50 g_unix_volume_drive_iface_init))
53 g_unix_drive_finalize (GObject *object)
57 drive = G_UNIX_DRIVE (object);
60 g_unix_volume_unset_drive (drive->volume, drive);
64 g_free (drive->mountpoint);
66 if (G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize)
67 (*G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize) (object);
71 g_unix_drive_class_init (GUnixDriveClass *klass)
73 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
75 gobject_class->finalize = g_unix_drive_finalize;
79 g_unix_drive_init (GUnixDrive *unix_drive)
84 type_to_icon (GUnixMountType type)
86 const char *icon_name = NULL;
90 case G_UNIX_MOUNT_TYPE_HD:
91 icon_name = "drive-harddisk";
93 case G_UNIX_MOUNT_TYPE_FLOPPY:
94 case G_UNIX_MOUNT_TYPE_ZIP:
95 case G_UNIX_MOUNT_TYPE_JAZ:
96 case G_UNIX_MOUNT_TYPE_MEMSTICK:
97 icon_name = "drive-removable-media";
99 case G_UNIX_MOUNT_TYPE_CDROM:
100 icon_name = "drive-optical";
102 case G_UNIX_MOUNT_TYPE_NFS:
103 /* TODO: Would like a better icon here... */
104 icon_name = "drive-removable-media";
106 case G_UNIX_MOUNT_TYPE_CAMERA:
107 icon_name = "camera-photo";
109 case G_UNIX_MOUNT_TYPE_IPOD:
110 icon_name = "multimedia-player";
112 case G_UNIX_MOUNT_TYPE_UNKNOWN:
114 icon_name = "drive-removable-media";
117 return g_strdup (icon_name);
122 * @volume_monitor: a #GVolumeMonitor.
123 * @mountpoint: a #GUnixMountPoint.
125 * Returns: a #GUnixDrive for the given #GUnixMountPoint.
128 g_unix_drive_new (GVolumeMonitor *volume_monitor,
129 GUnixMountPoint *mountpoint)
133 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
134 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
135 g_unix_mount_point_is_loopback (mountpoint))
138 drive = g_object_new (G_TYPE_UNIX_DRIVE, NULL);
140 drive->guessed_type = g_unix_mount_point_guess_type (mountpoint);
143 drive->mountpoint = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
144 drive->icon = type_to_icon (drive->guessed_type);
145 drive->name = g_strdup (_("Unknown drive"));
151 * g_unix_drive_disconnected:
156 g_unix_drive_disconnected (GUnixDrive *drive)
160 g_unix_volume_unset_drive (drive->volume, drive);
161 drive->volume = NULL;
166 * g_unix_drive_set_volume:
172 g_unix_drive_set_volume (GUnixDrive *drive,
175 if (drive->volume == volume)
179 g_unix_volume_unset_drive (drive->volume, drive);
181 drive->volume = volume;
183 /* TODO: Emit changed in idle to avoid locking issues */
184 g_signal_emit_by_name (drive, "changed");
188 * g_unix_drive_unset_volume:
194 g_unix_drive_unset_volume (GUnixDrive *drive,
197 if (drive->volume == volume)
199 drive->volume = NULL;
200 /* TODO: Emit changed in idle to avoid locking issues */
201 g_signal_emit_by_name (drive, "changed");
206 g_unix_drive_get_icon (GDrive *drive)
208 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
210 return g_themed_icon_new (unix_drive->icon);
214 g_unix_drive_get_name (GDrive *drive)
216 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
218 return g_strdup (unix_drive->name);
222 g_unix_drive_is_automounted (GDrive *drive)
229 g_unix_drive_can_mount (GDrive *drive)
236 g_unix_drive_can_eject (GDrive *drive)
243 g_unix_drive_get_volumes (GDrive *drive)
246 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
249 if (unix_drive->volume)
250 l = g_list_prepend (l, g_object_ref (unix_drive->volume));
256 g_unix_drive_has_volumes (GDrive *drive)
258 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
260 return unix_drive->volume != NULL;
265 g_unix_drive_has_mountpoint (GUnixDrive *drive,
266 const char *mountpoint)
268 return strcmp (drive->mountpoint, mountpoint) == 0;
272 g_unix_drive_mount (GDrive *drive,
273 GMountOperation *mount_operation,
274 GCancellable *cancellable,
275 GAsyncReadyCallback callback,
283 g_unix_drive_mount_finish (GDrive *drive,
284 GAsyncResult *result,
291 g_unix_drive_eject (GDrive *drive,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
300 g_unix_drive_eject_finish (GDrive *drive,
301 GAsyncResult *result,
308 g_unix_volume_drive_iface_init (GDriveIface *iface)
310 iface->get_name = g_unix_drive_get_name;
311 iface->get_icon = g_unix_drive_get_icon;
312 iface->has_volumes = g_unix_drive_has_volumes;
313 iface->get_volumes = g_unix_drive_get_volumes;
314 iface->is_automounted = g_unix_drive_is_automounted;
315 iface->can_mount = g_unix_drive_can_mount;
316 iface->can_eject = g_unix_drive_can_eject;
317 iface->mount = g_unix_drive_mount;
318 iface->mount_finish = g_unix_drive_mount_finish;
319 iface->eject = g_unix_drive_eject;
320 iface->eject_finish = g_unix_drive_eject_finish;