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 "gthemedicon.h"
31 #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 #define g_unix_drive_get_type _g_unix_drive_get_type
49 G_DEFINE_TYPE_WITH_CODE (GUnixDrive, g_unix_drive, G_TYPE_OBJECT,
50 G_IMPLEMENT_INTERFACE (G_TYPE_DRIVE,
51 g_unix_volume_drive_iface_init))
54 g_unix_drive_finalize (GObject *object)
58 drive = G_UNIX_DRIVE (object);
61 _g_unix_volume_unset_drive (drive->volume, drive);
65 g_free (drive->mountpoint);
67 if (G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize)
68 (*G_OBJECT_CLASS (g_unix_drive_parent_class)->finalize) (object);
72 g_unix_drive_class_init (GUnixDriveClass *klass)
74 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
76 gobject_class->finalize = g_unix_drive_finalize;
80 g_unix_drive_init (GUnixDrive *unix_drive)
85 type_to_icon (GUnixMountType type)
87 const char *icon_name = NULL;
91 case G_UNIX_MOUNT_TYPE_HD:
92 icon_name = "drive-harddisk";
94 case G_UNIX_MOUNT_TYPE_FLOPPY:
95 case G_UNIX_MOUNT_TYPE_ZIP:
96 case G_UNIX_MOUNT_TYPE_JAZ:
97 case G_UNIX_MOUNT_TYPE_MEMSTICK:
98 icon_name = "drive-removable-media";
100 case G_UNIX_MOUNT_TYPE_CDROM:
101 icon_name = "drive-optical";
103 case G_UNIX_MOUNT_TYPE_NFS:
104 /* TODO: Would like a better icon here... */
105 icon_name = "drive-removable-media";
107 case G_UNIX_MOUNT_TYPE_CAMERA:
108 icon_name = "camera-photo";
110 case G_UNIX_MOUNT_TYPE_IPOD:
111 icon_name = "multimedia-player";
113 case G_UNIX_MOUNT_TYPE_UNKNOWN:
115 icon_name = "drive-removable-media";
118 return g_strdup (icon_name);
123 * @volume_monitor: a #GVolumeMonitor.
124 * @mountpoint: a #GUnixMountPoint.
126 * Returns: a #GUnixDrive for the given #GUnixMountPoint.
129 _g_unix_drive_new (GVolumeMonitor *volume_monitor,
130 GUnixMountPoint *mountpoint)
134 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
135 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
136 g_unix_mount_point_is_loopback (mountpoint))
139 drive = g_object_new (G_TYPE_UNIX_DRIVE, NULL);
141 drive->guessed_type = g_unix_mount_point_guess_type (mountpoint);
144 drive->mountpoint = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
145 drive->icon = type_to_icon (drive->guessed_type);
146 drive->name = g_strdup (_("Unknown drive"));
152 * g_unix_drive_disconnected:
157 _g_unix_drive_disconnected (GUnixDrive *drive)
161 _g_unix_volume_unset_drive (drive->volume, drive);
162 drive->volume = NULL;
167 * g_unix_drive_set_volume:
173 _g_unix_drive_set_volume (GUnixDrive *drive,
176 if (drive->volume == volume)
180 _g_unix_volume_unset_drive (drive->volume, drive);
182 drive->volume = volume;
184 /* TODO: Emit changed in idle to avoid locking issues */
185 g_signal_emit_by_name (drive, "changed");
189 * g_unix_drive_unset_volume:
195 _g_unix_drive_unset_volume (GUnixDrive *drive,
198 if (drive->volume == volume)
200 drive->volume = NULL;
201 /* TODO: Emit changed in idle to avoid locking issues */
202 g_signal_emit_by_name (drive, "changed");
207 g_unix_drive_get_icon (GDrive *drive)
209 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
211 return g_themed_icon_new (unix_drive->icon);
215 g_unix_drive_get_name (GDrive *drive)
217 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
219 return g_strdup (unix_drive->name);
223 g_unix_drive_is_automounted (GDrive *drive)
230 g_unix_drive_can_mount (GDrive *drive)
237 g_unix_drive_can_eject (GDrive *drive)
244 g_unix_drive_get_volumes (GDrive *drive)
247 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
250 if (unix_drive->volume)
251 l = g_list_prepend (l, g_object_ref (unix_drive->volume));
257 g_unix_drive_has_volumes (GDrive *drive)
259 GUnixDrive *unix_drive = G_UNIX_DRIVE (drive);
261 return unix_drive->volume != NULL;
266 _g_unix_drive_has_mountpoint (GUnixDrive *drive,
267 const char *mountpoint)
269 return strcmp (drive->mountpoint, mountpoint) == 0;
273 g_unix_drive_mount (GDrive *drive,
274 GMountOperation *mount_operation,
275 GCancellable *cancellable,
276 GAsyncReadyCallback callback,
284 g_unix_drive_mount_finish (GDrive *drive,
285 GAsyncResult *result,
292 g_unix_drive_eject (GDrive *drive,
293 GCancellable *cancellable,
294 GAsyncReadyCallback callback,
301 g_unix_drive_eject_finish (GDrive *drive,
302 GAsyncResult *result,
309 g_unix_volume_drive_iface_init (GDriveIface *iface)
311 iface->get_name = g_unix_drive_get_name;
312 iface->get_icon = g_unix_drive_get_icon;
313 iface->has_volumes = g_unix_drive_has_volumes;
314 iface->get_volumes = g_unix_drive_get_volumes;
315 iface->is_automounted = g_unix_drive_is_automounted;
316 iface->can_mount = g_unix_drive_can_mount;
317 iface->can_eject = g_unix_drive_can_eject;
318 iface->mount = g_unix_drive_mount;
319 iface->mount_finish = g_unix_drive_mount_finish;
320 iface->eject = g_unix_drive_eject;
321 iface->eject_finish = g_unix_drive_eject_finish;