1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
31 #include "gsubprocess.h"
33 #include "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gunixvolume.h"
37 #include "gmountprivate.h"
40 #include "gvolumemonitor.h"
41 #include "gthemedicon.h"
42 #include "gsimpleasyncresult.h"
52 GVolumeMonitor *volume_monitor;
54 GUnixVolume *volume; /* owned by volume monitor */
65 static void g_unix_mount_mount_iface_init (GMountIface *iface);
67 #define g_unix_mount_get_type _g_unix_mount_get_type
68 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
70 g_unix_mount_mount_iface_init))
74 g_unix_mount_finalize (GObject *object)
78 mount = G_UNIX_MOUNT (object);
80 if (mount->volume_monitor != NULL)
81 g_object_unref (mount->volume_monitor);
84 _g_unix_volume_unset_mount (mount->volume, mount);
86 /* TODO: g_warn_if_fail (volume->volume == NULL); */
87 g_object_unref (mount->icon);
88 g_object_unref (mount->symbolic_icon);
90 g_free (mount->device_path);
91 g_free (mount->mount_path);
93 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
97 g_unix_mount_class_init (GUnixMountClass *klass)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 gobject_class->finalize = g_unix_mount_finalize;
105 g_unix_mount_init (GUnixMount *unix_mount)
110 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
111 GUnixMountEntry *mount_entry,
116 /* No volume for mount: Ignore internal things */
117 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
120 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
121 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
122 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
123 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
124 mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
126 mount->name = g_unix_mount_guess_name (mount_entry);
127 mount->icon = g_unix_mount_guess_icon (mount_entry);
128 mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
130 /* need to do this last */
131 mount->volume = volume;
133 _g_unix_volume_set_mount (volume, mount);
139 _g_unix_mount_unmounted (GUnixMount *mount)
141 if (mount->volume != NULL)
143 _g_unix_volume_unset_mount (mount->volume, mount);
144 mount->volume = NULL;
145 g_signal_emit_by_name (mount, "changed");
146 /* there's really no need to emit mount_changed on the volume monitor
147 * as we're going to be deleted.. */
152 _g_unix_mount_unset_volume (GUnixMount *mount,
155 if (mount->volume == volume)
157 mount->volume = NULL;
158 /* TODO: Emit changed in idle to avoid locking issues */
159 g_signal_emit_by_name (mount, "changed");
160 if (mount->volume_monitor != NULL)
161 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
166 g_unix_mount_get_root (GMount *mount)
168 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
170 return g_file_new_for_path (unix_mount->mount_path);
174 g_unix_mount_get_icon (GMount *mount)
176 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
178 return g_object_ref (unix_mount->icon);
182 g_unix_mount_get_symbolic_icon (GMount *mount)
184 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
186 return g_object_ref (unix_mount->symbolic_icon);
190 g_unix_mount_get_uuid (GMount *mount)
196 g_unix_mount_get_name (GMount *mount)
198 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
200 return g_strdup (unix_mount->name);
204 _g_unix_mount_has_mount_path (GUnixMount *mount,
205 const char *mount_path)
207 return strcmp (mount->mount_path, mount_path) == 0;
211 g_unix_mount_get_drive (GMount *mount)
213 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
215 if (unix_mount->volume != NULL)
216 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
222 g_unix_mount_get_volume (GMount *mount)
224 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
226 if (unix_mount->volume)
227 return G_VOLUME (g_object_ref (unix_mount->volume));
233 g_unix_mount_can_unmount (GMount *mount)
239 g_unix_mount_can_eject (GMount *mount)
241 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
242 return unix_mount->can_eject;
246 eject_unmount_done (GObject *source,
247 GAsyncResult *result,
250 GSubprocess *subprocess = G_SUBPROCESS (source);
251 GTask *task = user_data;
252 GError *error = NULL;
255 if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
257 g_task_return_error (task, error);
258 g_error_free (error);
260 else /* successful communication */
262 if (!g_subprocess_get_successful (subprocess))
263 /* ...but bad exit code */
264 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str);
266 /* ...and successful exit code */
267 g_task_return_boolean (task, TRUE);
272 g_object_unref (task);
276 eject_unmount_do_cb (gpointer user_data)
278 GTask *task = user_data;
279 GError *error = NULL;
280 GSubprocess *subprocess;
283 argv = g_task_get_task_data (task);
285 if (g_task_return_error_if_cancelled (task))
286 return G_SOURCE_REMOVE;
288 subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
289 g_assert_no_error (error);
291 g_subprocess_communicate_utf8_async (subprocess, NULL,
292 g_task_get_cancellable (task),
293 eject_unmount_done, task);
295 return G_SOURCE_REMOVE;
299 eject_unmount_do (GMount *mount,
300 GCancellable *cancellable,
301 GAsyncReadyCallback callback,
305 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
309 task = g_task_new (mount, cancellable, callback, user_data);
310 g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev);
312 if (unix_mount->volume_monitor != NULL)
313 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
315 g_signal_emit_by_name (mount, "pre-unmount", 0);
317 timeout = g_timeout_source_new (500);
318 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
319 g_source_unref (timeout);
323 g_unix_mount_unmount (GMount *mount,
324 GMountUnmountFlags flags,
325 GCancellable *cancellable,
326 GAsyncReadyCallback callback,
329 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
330 char *argv[] = {"umount", NULL, NULL};
332 if (unix_mount->mount_path != NULL)
333 argv[1] = unix_mount->mount_path;
335 argv[1] = unix_mount->device_path;
337 eject_unmount_do (mount, cancellable, callback, user_data, argv);
341 g_unix_mount_unmount_finish (GMount *mount,
342 GAsyncResult *result,
345 return g_task_propagate_boolean (G_TASK (result), error);
349 g_unix_mount_eject (GMount *mount,
350 GMountUnmountFlags flags,
351 GCancellable *cancellable,
352 GAsyncReadyCallback callback,
355 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
356 char *argv[] = {"eject", NULL, NULL};
358 if (unix_mount->mount_path != NULL)
359 argv[1] = unix_mount->mount_path;
361 argv[1] = unix_mount->device_path;
363 eject_unmount_do (mount, cancellable, callback, user_data, argv);
367 g_unix_mount_eject_finish (GMount *mount,
368 GAsyncResult *result,
371 return g_task_propagate_boolean (G_TASK (result), error);
375 g_unix_mount_mount_iface_init (GMountIface *iface)
377 iface->get_root = g_unix_mount_get_root;
378 iface->get_name = g_unix_mount_get_name;
379 iface->get_icon = g_unix_mount_get_icon;
380 iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
381 iface->get_uuid = g_unix_mount_get_uuid;
382 iface->get_drive = g_unix_mount_get_drive;
383 iface->get_volume = g_unix_mount_get_volume;
384 iface->can_unmount = g_unix_mount_can_unmount;
385 iface->can_eject = g_unix_mount_can_eject;
386 iface->unmount = g_unix_mount_unmount;
387 iface->unmount_finish = g_unix_mount_unmount_finish;
388 iface->eject = g_unix_mount_eject;
389 iface->eject_finish = g_unix_mount_eject_finish;