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"
51 GVolumeMonitor
*volume_monitor
;
53 GUnixVolume
*volume
; /* owned by volume monitor */
64 static void g_unix_mount_mount_iface_init (GMountIface
*iface
);
66 #define g_unix_mount_get_type _g_unix_mount_get_type
67 G_DEFINE_TYPE_WITH_CODE (GUnixMount
, g_unix_mount
, G_TYPE_OBJECT
,
68 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT
,
69 g_unix_mount_mount_iface_init
))
73 g_unix_mount_finalize (GObject
*object
)
77 mount
= G_UNIX_MOUNT (object
);
79 if (mount
->volume_monitor
!= NULL
)
80 g_object_unref (mount
->volume_monitor
);
83 _g_unix_volume_unset_mount (mount
->volume
, mount
);
85 /* TODO: g_warn_if_fail (volume->volume == NULL); */
86 g_object_unref (mount
->icon
);
87 g_object_unref (mount
->symbolic_icon
);
89 g_free (mount
->device_path
);
90 g_free (mount
->mount_path
);
92 G_OBJECT_CLASS (g_unix_mount_parent_class
)->finalize (object
);
96 g_unix_mount_class_init (GUnixMountClass
*klass
)
98 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
100 gobject_class
->finalize
= g_unix_mount_finalize
;
104 g_unix_mount_init (GUnixMount
*unix_mount
)
109 _g_unix_mount_new (GVolumeMonitor
*volume_monitor
,
110 GUnixMountEntry
*mount_entry
,
115 /* No volume for mount: Ignore internal things */
116 if (volume
== NULL
&& !g_unix_mount_guess_should_display (mount_entry
))
119 mount
= g_object_new (G_TYPE_UNIX_MOUNT
, NULL
);
120 mount
->volume_monitor
= volume_monitor
!= NULL
? g_object_ref (volume_monitor
) : NULL
;
121 mount
->device_path
= g_strdup (g_unix_mount_get_device_path (mount_entry
));
122 mount
->mount_path
= g_strdup (g_unix_mount_get_mount_path (mount_entry
));
123 mount
->can_eject
= g_unix_mount_guess_can_eject (mount_entry
);
125 mount
->name
= g_unix_mount_guess_name (mount_entry
);
126 mount
->icon
= g_unix_mount_guess_icon (mount_entry
);
127 mount
->symbolic_icon
= g_unix_mount_guess_symbolic_icon (mount_entry
);
129 /* need to do this last */
130 mount
->volume
= volume
;
132 _g_unix_volume_set_mount (volume
, mount
);
138 _g_unix_mount_unmounted (GUnixMount
*mount
)
140 if (mount
->volume
!= NULL
)
142 _g_unix_volume_unset_mount (mount
->volume
, mount
);
143 mount
->volume
= NULL
;
144 g_signal_emit_by_name (mount
, "changed");
145 /* there's really no need to emit mount_changed on the volume monitor
146 * as we're going to be deleted.. */
151 _g_unix_mount_unset_volume (GUnixMount
*mount
,
154 if (mount
->volume
== volume
)
156 mount
->volume
= NULL
;
157 /* TODO: Emit changed in idle to avoid locking issues */
158 g_signal_emit_by_name (mount
, "changed");
159 if (mount
->volume_monitor
!= NULL
)
160 g_signal_emit_by_name (mount
->volume_monitor
, "mount-changed", mount
);
165 g_unix_mount_get_root (GMount
*mount
)
167 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
169 return g_file_new_for_path (unix_mount
->mount_path
);
173 g_unix_mount_get_icon (GMount
*mount
)
175 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
177 return g_object_ref (unix_mount
->icon
);
181 g_unix_mount_get_symbolic_icon (GMount
*mount
)
183 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
185 return g_object_ref (unix_mount
->symbolic_icon
);
189 g_unix_mount_get_uuid (GMount
*mount
)
195 g_unix_mount_get_name (GMount
*mount
)
197 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
199 return g_strdup (unix_mount
->name
);
203 _g_unix_mount_has_mount_path (GUnixMount
*mount
,
204 const char *mount_path
)
206 return strcmp (mount
->mount_path
, mount_path
) == 0;
210 g_unix_mount_get_drive (GMount
*mount
)
212 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
214 if (unix_mount
->volume
!= NULL
)
215 return g_volume_get_drive (G_VOLUME (unix_mount
->volume
));
221 g_unix_mount_get_volume (GMount
*mount
)
223 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
225 if (unix_mount
->volume
)
226 return G_VOLUME (g_object_ref (unix_mount
->volume
));
232 g_unix_mount_can_unmount (GMount
*mount
)
238 g_unix_mount_can_eject (GMount
*mount
)
240 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
241 return unix_mount
->can_eject
;
245 eject_unmount_done (GObject
*source
,
246 GAsyncResult
*result
,
249 GSubprocess
*subprocess
= G_SUBPROCESS (source
);
250 GTask
*task
= user_data
;
251 GError
*error
= NULL
;
254 if (!g_subprocess_communicate_utf8_finish (subprocess
, result
, NULL
, &stderr_str
, &error
))
256 g_task_return_error (task
, error
);
257 g_error_free (error
);
259 else /* successful communication */
261 if (!g_subprocess_get_successful (subprocess
))
262 /* ...but bad exit code */
263 g_task_return_new_error (task
, G_IO_ERROR
, G_IO_ERROR_FAILED
, "%s", stderr_str
);
265 /* ...and successful exit code */
266 g_task_return_boolean (task
, TRUE
);
271 g_object_unref (task
);
275 eject_unmount_do_cb (gpointer user_data
)
277 GTask
*task
= user_data
;
278 GError
*error
= NULL
;
279 GSubprocess
*subprocess
;
282 argv
= g_task_get_task_data (task
);
284 if (g_task_return_error_if_cancelled (task
))
286 g_object_unref (task
);
287 return G_SOURCE_REMOVE
;
290 subprocess
= g_subprocess_newv (argv
, G_SUBPROCESS_FLAGS_STDOUT_SILENCE
| G_SUBPROCESS_FLAGS_STDERR_PIPE
, &error
);
291 g_assert_no_error (error
);
293 g_subprocess_communicate_utf8_async (subprocess
, NULL
,
294 g_task_get_cancellable (task
),
295 eject_unmount_done
, task
);
297 return G_SOURCE_REMOVE
;
301 eject_unmount_do (GMount
*mount
,
302 GCancellable
*cancellable
,
303 GAsyncReadyCallback callback
,
307 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
311 task
= g_task_new (mount
, cancellable
, callback
, user_data
);
312 g_task_set_source_tag (task
, eject_unmount_do
);
313 g_task_set_task_data (task
, g_strdupv (argv
), (GDestroyNotify
) g_strfreev
);
315 if (unix_mount
->volume_monitor
!= NULL
)
316 g_signal_emit_by_name (unix_mount
->volume_monitor
, "mount-pre-unmount", mount
);
318 g_signal_emit_by_name (mount
, "pre-unmount", 0);
320 timeout
= g_timeout_source_new (500);
321 g_task_attach_source (task
, timeout
, (GSourceFunc
) eject_unmount_do_cb
);
322 g_source_unref (timeout
);
326 g_unix_mount_unmount (GMount
*mount
,
327 GMountUnmountFlags flags
,
328 GCancellable
*cancellable
,
329 GAsyncReadyCallback callback
,
332 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
333 char *argv
[] = {"umount", NULL
, NULL
};
335 if (unix_mount
->mount_path
!= NULL
)
336 argv
[1] = unix_mount
->mount_path
;
338 argv
[1] = unix_mount
->device_path
;
340 eject_unmount_do (mount
, cancellable
, callback
, user_data
, argv
);
344 g_unix_mount_unmount_finish (GMount
*mount
,
345 GAsyncResult
*result
,
348 return g_task_propagate_boolean (G_TASK (result
), error
);
352 g_unix_mount_eject (GMount
*mount
,
353 GMountUnmountFlags flags
,
354 GCancellable
*cancellable
,
355 GAsyncReadyCallback callback
,
358 GUnixMount
*unix_mount
= G_UNIX_MOUNT (mount
);
359 char *argv
[] = {"eject", NULL
, NULL
};
361 if (unix_mount
->mount_path
!= NULL
)
362 argv
[1] = unix_mount
->mount_path
;
364 argv
[1] = unix_mount
->device_path
;
366 eject_unmount_do (mount
, cancellable
, callback
, user_data
, argv
);
370 g_unix_mount_eject_finish (GMount
*mount
,
371 GAsyncResult
*result
,
374 return g_task_propagate_boolean (G_TASK (result
), error
);
378 g_unix_mount_mount_iface_init (GMountIface
*iface
)
380 iface
->get_root
= g_unix_mount_get_root
;
381 iface
->get_name
= g_unix_mount_get_name
;
382 iface
->get_icon
= g_unix_mount_get_icon
;
383 iface
->get_symbolic_icon
= g_unix_mount_get_symbolic_icon
;
384 iface
->get_uuid
= g_unix_mount_get_uuid
;
385 iface
->get_drive
= g_unix_mount_get_drive
;
386 iface
->get_volume
= g_unix_mount_get_volume
;
387 iface
->can_unmount
= g_unix_mount_can_unmount
;
388 iface
->can_eject
= g_unix_mount_can_eject
;
389 iface
->unmount
= g_unix_mount_unmount
;
390 iface
->unmount_finish
= g_unix_mount_unmount_finish
;
391 iface
->eject
= g_unix_mount_eject
;
392 iface
->eject_finish
= g_unix_mount_eject_finish
;