Add Friulian translation
[glib.git] / gio / gunixmount.c
blob278d1f4b425ff15cf2b5fcf274955f6744940a73
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
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>
24 #include "config.h"
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
30 #include <glib.h>
31 #include "gsubprocess.h"
32 #include "gioenums.h"
33 #include "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gunixvolume.h"
37 #include "gmountprivate.h"
38 #include "gmount.h"
39 #include "gfile.h"
40 #include "gvolumemonitor.h"
41 #include "gthemedicon.h"
42 #include "gioerror.h"
43 #include "glibintl.h"
44 /* for BUFSIZ */
45 #include <stdio.h>
48 struct _GUnixMount {
49 GObject parent;
51 GVolumeMonitor *volume_monitor;
53 GUnixVolume *volume; /* owned by volume monitor */
55 char *name;
56 GIcon *icon;
57 GIcon *symbolic_icon;
58 char *device_path;
59 char *mount_path;
61 gboolean can_eject;
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))
72 static void
73 g_unix_mount_finalize (GObject *object)
75 GUnixMount *mount;
77 mount = G_UNIX_MOUNT (object);
79 if (mount->volume_monitor != NULL)
80 g_object_unref (mount->volume_monitor);
82 if (mount->volume)
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);
88 g_free (mount->name);
89 g_free (mount->device_path);
90 g_free (mount->mount_path);
92 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
95 static void
96 g_unix_mount_class_init (GUnixMountClass *klass)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 gobject_class->finalize = g_unix_mount_finalize;
103 static void
104 g_unix_mount_init (GUnixMount *unix_mount)
108 GUnixMount *
109 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
110 GUnixMountEntry *mount_entry,
111 GUnixVolume *volume)
113 GUnixMount *mount;
115 /* No volume for mount: Ignore internal things */
116 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
117 return NULL;
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;
131 if (volume != NULL)
132 _g_unix_volume_set_mount (volume, mount);
134 return mount;
137 void
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.. */
150 void
151 _g_unix_mount_unset_volume (GUnixMount *mount,
152 GUnixVolume *volume)
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);
164 static GFile *
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);
172 static GIcon *
173 g_unix_mount_get_icon (GMount *mount)
175 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
177 return g_object_ref (unix_mount->icon);
180 static GIcon *
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);
188 static char *
189 g_unix_mount_get_uuid (GMount *mount)
191 return NULL;
194 static char *
195 g_unix_mount_get_name (GMount *mount)
197 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
199 return g_strdup (unix_mount->name);
202 gboolean
203 _g_unix_mount_has_mount_path (GUnixMount *mount,
204 const char *mount_path)
206 return strcmp (mount->mount_path, mount_path) == 0;
209 static GDrive *
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));
217 return NULL;
220 static GVolume *
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));
228 return NULL;
231 static gboolean
232 g_unix_mount_can_unmount (GMount *mount)
234 return TRUE;
237 static gboolean
238 g_unix_mount_can_eject (GMount *mount)
240 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
241 return unix_mount->can_eject;
244 static void
245 eject_unmount_done (GObject *source,
246 GAsyncResult *result,
247 gpointer user_data)
249 GSubprocess *subprocess = G_SUBPROCESS (source);
250 GTask *task = user_data;
251 GError *error = NULL;
252 gchar *stderr_str;
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);
264 else
265 /* ...and successful exit code */
266 g_task_return_boolean (task, TRUE);
268 g_free (stderr_str);
271 g_object_unref (task);
274 static gboolean
275 eject_unmount_do_cb (gpointer user_data)
277 GTask *task = user_data;
278 GError *error = NULL;
279 GSubprocess *subprocess;
280 const gchar **argv;
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;
300 static void
301 eject_unmount_do (GMount *mount,
302 GCancellable *cancellable,
303 GAsyncReadyCallback callback,
304 gpointer user_data,
305 char **argv)
307 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
308 GTask *task;
309 GSource *timeout;
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);
325 static void
326 g_unix_mount_unmount (GMount *mount,
327 GMountUnmountFlags flags,
328 GCancellable *cancellable,
329 GAsyncReadyCallback callback,
330 gpointer user_data)
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;
337 else
338 argv[1] = unix_mount->device_path;
340 eject_unmount_do (mount, cancellable, callback, user_data, argv);
343 static gboolean
344 g_unix_mount_unmount_finish (GMount *mount,
345 GAsyncResult *result,
346 GError **error)
348 return g_task_propagate_boolean (G_TASK (result), error);
351 static void
352 g_unix_mount_eject (GMount *mount,
353 GMountUnmountFlags flags,
354 GCancellable *cancellable,
355 GAsyncReadyCallback callback,
356 gpointer user_data)
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;
363 else
364 argv[1] = unix_mount->device_path;
366 eject_unmount_do (mount, cancellable, callback, user_data, argv);
369 static gboolean
370 g_unix_mount_eject_finish (GMount *mount,
371 GAsyncResult *result,
372 GError **error)
374 return g_task_propagate_boolean (G_TASK (result), error);
377 static void
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;