Add g_unix_mount_guess_should_display and use for unix volume monitor
[platform/upstream/glib.git] / gio / gunixmount.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  *         David Zeuthen <davidz@redhat.com>
24  */
25
26 #include <config.h>
27
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixvolume.h"
36 #include "gmountprivate.h"
37 #include "gvolumemonitor.h"
38 #include "gthemedicon.h"
39 #include "gsimpleasyncresult.h"
40 #include "glibintl.h"
41
42 #include "gioalias.h"
43
44 struct _GUnixMount {
45   GObject parent;
46
47   GVolumeMonitor   *volume_monitor;
48
49   GUnixVolume      *volume; /* owned by volume monitor */
50
51   char *name;
52   GIcon *icon;
53   char *device_path;
54   char *mount_path;
55
56   gboolean can_eject;
57 };
58
59 static void g_unix_mount_mount_iface_init (GMountIface *iface);
60
61 #define g_unix_mount_get_type _g_unix_mount_get_type
62 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
63                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
64                                                 g_unix_mount_mount_iface_init))
65
66
67 static void
68 g_unix_mount_finalize (GObject *object)
69 {
70   GUnixMount *mount;
71   
72   mount = G_UNIX_MOUNT (object);
73
74   if (mount->volume_monitor != NULL)
75     g_object_unref (mount->volume_monitor);
76
77   if (mount->volume)
78     _g_unix_volume_unset_mount (mount->volume, mount);
79     
80   /* TODO: g_warn_if_fail (volume->volume == NULL); */
81   g_object_unref (mount->icon);
82   g_free (mount->name);
83   g_free (mount->device_path);
84   g_free (mount->mount_path);
85   
86   if (G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize)
87     (*G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize) (object);
88 }
89
90 static void
91 g_unix_mount_class_init (GUnixMountClass *klass)
92 {
93   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94
95   gobject_class->finalize = g_unix_mount_finalize;
96 }
97
98 static void
99 g_unix_mount_init (GUnixMount *unix_mount)
100 {
101 }
102
103 GUnixMount *
104 _g_unix_mount_new (GVolumeMonitor  *volume_monitor,
105                    GUnixMountEntry *mount_entry,
106                    GUnixVolume     *volume)
107 {
108   GUnixMount *mount;
109   
110   /* No volume for mount: Ignore internal things */
111   if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
112     return NULL;
113
114   mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
115   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
116   mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
117   mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
118   mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
119
120   mount->name = g_unix_mount_guess_name (mount_entry);
121   mount->icon = g_unix_mount_guess_icon (mount_entry);
122
123   /* need to do this last */
124   mount->volume = volume;
125   if (volume != NULL)
126     _g_unix_volume_set_mount (volume, mount);
127
128   return mount;
129 }
130
131 void
132 _g_unix_mount_unmounted (GUnixMount *mount)
133 {
134   if (mount->volume != NULL)
135     {
136       _g_unix_volume_unset_mount (mount->volume, mount);
137       mount->volume = NULL;
138       g_signal_emit_by_name (mount, "changed");
139       /* there's really no need to emit mount_changed on the volume monitor 
140        * as we're going to be deleted.. */
141     }
142 }
143
144 void
145 _g_unix_mount_unset_volume (GUnixMount *mount,
146                             GUnixVolume  *volume)
147 {
148   if (mount->volume == volume)
149     {
150       mount->volume = NULL;
151       /* TODO: Emit changed in idle to avoid locking issues */
152       g_signal_emit_by_name (mount, "changed");
153       if (mount->volume_monitor != NULL)
154         g_signal_emit_by_name (mount->volume_monitor, "mount_changed", mount);
155     }
156 }
157
158 static GFile *
159 g_unix_mount_get_root (GMount *mount)
160 {
161   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
162
163   return g_file_new_for_path (unix_mount->mount_path);
164 }
165
166 static GIcon *
167 g_unix_mount_get_icon (GMount *mount)
168 {
169   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
170
171   return g_object_ref (unix_mount->icon);
172 }
173
174 static char *
175 g_unix_mount_get_uuid (GMount *mount)
176 {
177   return NULL;
178 }
179
180 static char *
181 g_unix_mount_get_name (GMount *mount)
182 {
183   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
184   
185   return g_strdup (unix_mount->name);
186 }
187
188 gboolean
189 _g_unix_mount_has_mount_path (GUnixMount *mount,
190                               const char  *mount_path)
191 {
192   return strcmp (mount->mount_path, mount_path) == 0;
193 }
194
195 static GDrive *
196 g_unix_mount_get_drive (GMount *mount)
197 {
198   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
199
200   if (unix_mount->volume != NULL)
201     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
202
203   return NULL;
204 }
205
206 static GVolume *
207 g_unix_mount_get_volume (GMount *mount)
208 {
209   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
210
211   if (unix_mount->volume)
212     return G_VOLUME (g_object_ref (unix_mount->volume));
213   
214   return NULL;
215 }
216
217 static gboolean
218 g_unix_mount_can_unmount (GMount *mount)
219 {
220   return TRUE;
221 }
222
223 static gboolean
224 g_unix_mount_can_eject (GMount *mount)
225 {
226   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
227   return unix_mount->can_eject;
228 }
229
230
231 typedef struct {
232   GUnixMount *unix_mount;
233   GAsyncReadyCallback callback;
234   gpointer user_data;
235   GCancellable *cancellable;
236   int error_fd;
237   GIOChannel *error_channel;
238   guint error_channel_source_id;
239   GString *error_string;
240 } UnmountEjectOp;
241
242 static void 
243 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
244 {
245   UnmountEjectOp *data = user_data;
246   GSimpleAsyncResult *simple;
247   
248   if (WEXITSTATUS (status) != 0)
249     {
250       GError *error;
251       error = g_error_new_literal (G_IO_ERROR, 
252                                    G_IO_ERROR_FAILED,
253                                    data->error_string->str);
254       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
255                                                      data->callback,
256                                                      data->user_data,
257                                                      error);
258       g_error_free (error);
259     }
260   else
261     {
262       simple = g_simple_async_result_new (G_OBJECT (data->unix_mount),
263                                           data->callback,
264                                           data->user_data,
265                                           NULL);
266     }
267
268   g_simple_async_result_complete (simple);
269   g_object_unref (simple);
270
271   g_source_remove (data->error_channel_source_id);
272   g_io_channel_unref (data->error_channel);
273   g_string_free (data->error_string, TRUE);
274   close (data->error_fd);
275   g_spawn_close_pid (pid);
276   g_free (data);
277 }
278
279 static gboolean
280 eject_unmount_read_error (GIOChannel *channel,
281                     GIOCondition condition,
282                     gpointer user_data)
283 {
284   char *str;
285   gsize str_len;
286   UnmountEjectOp *data = user_data;
287
288   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
289   g_string_append (data->error_string, str);
290   g_free (str);
291   return TRUE;
292 }
293
294 static void
295 eject_unmount_do (GMount              *mount,
296                   GCancellable        *cancellable,
297                   GAsyncReadyCallback  callback,
298                   gpointer             user_data,
299                   char               **argv)
300 {
301   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
302   UnmountEjectOp *data;
303   GPid child_pid;
304   GError *error;
305   
306   data = g_new0 (UnmountEjectOp, 1);
307   data->unix_mount = unix_mount;
308   data->callback = callback;
309   data->user_data = user_data;
310   data->cancellable = cancellable;
311   
312   error = NULL;
313   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
314                                  argv,
315                                  NULL,         /* envp */
316                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
317                                  NULL,         /* child_setup */
318                                  NULL,         /* user_data for child_setup */
319                                  &child_pid,
320                                  NULL,           /* standard_input */
321                                  NULL,           /* standard_output */
322                                  &(data->error_fd),
323                                  &error)) {
324     GSimpleAsyncResult *simple;
325     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
326                                                    data->callback,
327                                                    data->user_data,
328                                                    error);
329     g_simple_async_result_complete (simple);
330     g_object_unref (simple);
331     g_error_free (error);
332     g_free (data);
333     return;
334   }
335   data->error_string = g_string_new ("");
336   data->error_channel = g_io_channel_unix_new (data->error_fd);
337   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_unmount_read_error, data);
338   g_child_watch_add (child_pid, eject_unmount_cb, data);
339 }
340
341 static void
342 g_unix_mount_unmount (GMount             *mount,
343                       GMountUnmountFlags flags,
344                       GCancellable        *cancellable,
345                       GAsyncReadyCallback  callback,
346                       gpointer             user_data)
347 {
348   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
349   char *argv[] = {"umount", NULL, NULL};
350
351   if (unix_mount->mount_path != NULL)
352     argv[1] = unix_mount->mount_path;
353   else
354     argv[1] = unix_mount->device_path;
355
356   eject_unmount_do (mount, cancellable, callback, user_data, argv);
357 }
358
359 static gboolean
360 g_unix_mount_unmount_finish (GMount       *mount,
361                              GAsyncResult  *result,
362                              GError       **error)
363 {
364   return TRUE;
365 }
366
367 static void
368 g_unix_mount_eject (GMount             *mount,
369                     GMountUnmountFlags flags,
370                     GCancellable        *cancellable,
371                     GAsyncReadyCallback  callback,
372                     gpointer             user_data)
373 {
374   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
375   char *argv[] = {"eject", NULL, NULL};
376
377   if (unix_mount->mount_path != NULL)
378     argv[1] = unix_mount->mount_path;
379   else
380     argv[1] = unix_mount->device_path;
381
382   eject_unmount_do (mount, cancellable, callback, user_data, argv);
383 }
384
385 static gboolean
386 g_unix_mount_eject_finish (GMount       *mount,
387                            GAsyncResult  *result,
388                            GError       **error)
389 {
390   return TRUE;
391 }
392
393 static void
394 g_unix_mount_mount_iface_init (GMountIface *iface)
395 {
396   iface->get_root = g_unix_mount_get_root;
397   iface->get_name = g_unix_mount_get_name;
398   iface->get_icon = g_unix_mount_get_icon;
399   iface->get_uuid = g_unix_mount_get_uuid;
400   iface->get_drive = g_unix_mount_get_drive;
401   iface->get_volume = g_unix_mount_get_volume;
402   iface->can_unmount = g_unix_mount_can_unmount;
403   iface->can_eject = g_unix_mount_can_eject;
404   iface->unmount = g_unix_mount_unmount;
405   iface->unmount_finish = g_unix_mount_unmount_finish;
406   iface->eject = g_unix_mount_eject;
407   iface->eject_finish = g_unix_mount_eject_finish;
408 }