Add symbolic icon support to drive, volume, and mount
[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 "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 "gsimpleasyncresult.h"
43 #include "gioerror.h"
44 #include "glibintl.h"
45 /* for BUFSIZ */
46 #include <stdio.h>
47
48
49 struct _GUnixMount {
50   GObject parent;
51
52   GVolumeMonitor   *volume_monitor;
53
54   GUnixVolume      *volume; /* owned by volume monitor */
55
56   char *name;
57   GIcon *icon;
58   GIcon *symbolic_icon;
59   char *device_path;
60   char *mount_path;
61
62   gboolean can_eject;
63 };
64
65 static void g_unix_mount_mount_iface_init (GMountIface *iface);
66
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))
71
72
73 static void
74 g_unix_mount_finalize (GObject *object)
75 {
76   GUnixMount *mount;
77   
78   mount = G_UNIX_MOUNT (object);
79
80   if (mount->volume_monitor != NULL)
81     g_object_unref (mount->volume_monitor);
82
83   if (mount->volume)
84     _g_unix_volume_unset_mount (mount->volume, mount);
85     
86   /* TODO: g_warn_if_fail (volume->volume == NULL); */
87   g_object_unref (mount->icon);
88   g_object_unref (mount->symbolic_icon);
89   g_free (mount->name);
90   g_free (mount->device_path);
91   g_free (mount->mount_path);
92
93   G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
94 }
95
96 static void
97 g_unix_mount_class_init (GUnixMountClass *klass)
98 {
99   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100
101   gobject_class->finalize = g_unix_mount_finalize;
102 }
103
104 static void
105 g_unix_mount_init (GUnixMount *unix_mount)
106 {
107 }
108
109 GUnixMount *
110 _g_unix_mount_new (GVolumeMonitor  *volume_monitor,
111                    GUnixMountEntry *mount_entry,
112                    GUnixVolume     *volume)
113 {
114   GUnixMount *mount;
115   
116   /* No volume for mount: Ignore internal things */
117   if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
118     return NULL;
119
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);
125
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);
129
130   /* need to do this last */
131   mount->volume = volume;
132   if (volume != NULL)
133     _g_unix_volume_set_mount (volume, mount);
134
135   return mount;
136 }
137
138 void
139 _g_unix_mount_unmounted (GUnixMount *mount)
140 {
141   if (mount->volume != NULL)
142     {
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.. */
148     }
149 }
150
151 void
152 _g_unix_mount_unset_volume (GUnixMount *mount,
153                             GUnixVolume  *volume)
154 {
155   if (mount->volume == volume)
156     {
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);
162     }
163 }
164
165 static GFile *
166 g_unix_mount_get_root (GMount *mount)
167 {
168   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
169
170   return g_file_new_for_path (unix_mount->mount_path);
171 }
172
173 static GIcon *
174 g_unix_mount_get_icon (GMount *mount)
175 {
176   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
177
178   return g_object_ref (unix_mount->icon);
179 }
180
181 static GIcon *
182 g_unix_mount_get_symbolic_icon (GMount *mount)
183 {
184   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
185
186   return g_object_ref (unix_mount->symbolic_icon);
187 }
188
189 static char *
190 g_unix_mount_get_uuid (GMount *mount)
191 {
192   return NULL;
193 }
194
195 static char *
196 g_unix_mount_get_name (GMount *mount)
197 {
198   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
199   
200   return g_strdup (unix_mount->name);
201 }
202
203 gboolean
204 _g_unix_mount_has_mount_path (GUnixMount *mount,
205                               const char  *mount_path)
206 {
207   return strcmp (mount->mount_path, mount_path) == 0;
208 }
209
210 static GDrive *
211 g_unix_mount_get_drive (GMount *mount)
212 {
213   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
214
215   if (unix_mount->volume != NULL)
216     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
217
218   return NULL;
219 }
220
221 static GVolume *
222 g_unix_mount_get_volume (GMount *mount)
223 {
224   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
225
226   if (unix_mount->volume)
227     return G_VOLUME (g_object_ref (unix_mount->volume));
228   
229   return NULL;
230 }
231
232 static gboolean
233 g_unix_mount_can_unmount (GMount *mount)
234 {
235   return TRUE;
236 }
237
238 static gboolean
239 g_unix_mount_can_eject (GMount *mount)
240 {
241   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
242   return unix_mount->can_eject;
243 }
244
245
246 typedef struct {
247   GUnixMount *unix_mount;
248   GAsyncReadyCallback callback;
249   gpointer user_data;
250   GCancellable *cancellable;
251   int error_fd;
252   GIOChannel *error_channel;
253   GSource *error_channel_source;
254   GString *error_string;
255   gchar **argv;
256 } UnmountEjectOp;
257
258 static void 
259 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
260 {
261   UnmountEjectOp *data = user_data;
262   GSimpleAsyncResult *simple;
263   
264   if (WEXITSTATUS (status) != 0)
265     {
266       GError *error;
267       error = g_error_new_literal (G_IO_ERROR, 
268                                    G_IO_ERROR_FAILED,
269                                    data->error_string->str);
270       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
271                                                      data->callback,
272                                                      data->user_data,
273                                                      error);
274       g_error_free (error);
275     }
276   else
277     {
278       simple = g_simple_async_result_new (G_OBJECT (data->unix_mount),
279                                           data->callback,
280                                           data->user_data,
281                                           NULL);
282     }
283
284   g_simple_async_result_complete (simple);
285   g_object_unref (simple);
286
287   if (data->error_channel_source)
288     {
289       g_source_destroy (data->error_channel_source);
290       g_source_unref (data->error_channel_source);
291     }
292   g_io_channel_unref (data->error_channel);
293   g_string_free (data->error_string, TRUE);
294   g_strfreev (data->argv);
295   close (data->error_fd);
296   g_spawn_close_pid (pid);
297   g_free (data);
298 }
299
300 static gboolean
301 eject_unmount_read_error (GIOChannel *channel,
302                     GIOCondition condition,
303                     gpointer user_data)
304 {
305   UnmountEjectOp *data = user_data;
306   char buf[BUFSIZ];
307   gsize bytes_read;
308   GError *error;
309   GIOStatus status;
310
311   error = NULL;
312 read:
313   status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
314   if (status == G_IO_STATUS_NORMAL)
315    {
316      g_string_append_len (data->error_string, buf, bytes_read);
317      if (bytes_read == sizeof (buf))
318         goto read;
319    }
320   else if (status == G_IO_STATUS_EOF)
321     g_string_append_len (data->error_string, buf, bytes_read);
322   else if (status == G_IO_STATUS_ERROR)
323     {
324       if (data->error_string->len > 0)
325         g_string_append (data->error_string, "\n");
326
327       g_string_append (data->error_string, error->message);
328       g_error_free (error);
329
330       if (data->error_channel_source)
331         {
332           g_source_unref (data->error_channel_source);
333           data->error_channel_source = NULL;
334         }
335       return FALSE;
336     }
337
338   return TRUE;
339 }
340
341 static gboolean
342 eject_unmount_do_cb (gpointer user_data)
343 {
344   UnmountEjectOp *data = (UnmountEjectOp *) user_data;
345   GPid child_pid;
346   GSource *child_watch;
347   GError *error = NULL;
348
349   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
350                                  data->argv,
351                                  NULL,         /* envp */
352                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
353                                  NULL,         /* child_setup */
354                                  NULL,         /* user_data for child_setup */
355                                  &child_pid,
356                                  NULL,           /* standard_input */
357                                  NULL,           /* standard_output */
358                                  &(data->error_fd),
359                                  &error)) {
360     g_assert (error != NULL);
361     goto handle_error;
362   }
363
364   data->error_string = g_string_new ("");
365
366   data->error_channel = g_io_channel_unix_new (data->error_fd);
367   g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
368   if (error != NULL)
369     goto handle_error;
370
371   data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
372   g_source_set_callback (data->error_channel_source,
373                          (GSourceFunc) eject_unmount_read_error, data, NULL);
374   g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
375
376   child_watch = g_child_watch_source_new (child_pid);
377   g_source_set_callback (child_watch, (GSourceFunc) eject_unmount_cb, data, NULL);
378   g_source_attach (child_watch, g_main_context_get_thread_default ());
379   g_source_unref (child_watch);
380
381 handle_error:
382   if (error != NULL) {
383     GSimpleAsyncResult *simple;
384     simple = g_simple_async_result_new_take_error (G_OBJECT (data->unix_mount),
385                                                    data->callback,
386                                                    data->user_data,
387                                                    error);
388     g_simple_async_result_complete (simple);
389     g_object_unref (simple);
390
391     if (data->error_string != NULL)
392       g_string_free (data->error_string, TRUE);
393
394     if (data->error_channel != NULL)
395       g_io_channel_unref (data->error_channel);
396
397     g_strfreev (data->argv);
398     g_free (data);
399   }
400
401   return G_SOURCE_REMOVE;
402 }
403
404 static void
405 eject_unmount_do (GMount              *mount,
406                   GCancellable        *cancellable,
407                   GAsyncReadyCallback  callback,
408                   gpointer             user_data,
409                   char               **argv)
410 {
411   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
412   UnmountEjectOp *data;
413
414   data = g_new0 (UnmountEjectOp, 1);
415   data->unix_mount = unix_mount;
416   data->callback = callback;
417   data->user_data = user_data;
418   data->cancellable = cancellable;
419   data->argv = g_strdupv (argv);
420
421   if (unix_mount->volume_monitor != NULL)
422     g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
423
424   g_signal_emit_by_name (mount, "pre-unmount", 0);
425
426   g_timeout_add (500, (GSourceFunc) eject_unmount_do_cb, data);
427 }
428
429 static void
430 g_unix_mount_unmount (GMount             *mount,
431                       GMountUnmountFlags flags,
432                       GCancellable        *cancellable,
433                       GAsyncReadyCallback  callback,
434                       gpointer             user_data)
435 {
436   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
437   char *argv[] = {"umount", NULL, NULL};
438
439   if (unix_mount->mount_path != NULL)
440     argv[1] = unix_mount->mount_path;
441   else
442     argv[1] = unix_mount->device_path;
443
444   eject_unmount_do (mount, cancellable, callback, user_data, argv);
445 }
446
447 static gboolean
448 g_unix_mount_unmount_finish (GMount       *mount,
449                              GAsyncResult  *result,
450                              GError       **error)
451 {
452   return TRUE;
453 }
454
455 static void
456 g_unix_mount_eject (GMount             *mount,
457                     GMountUnmountFlags flags,
458                     GCancellable        *cancellable,
459                     GAsyncReadyCallback  callback,
460                     gpointer             user_data)
461 {
462   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
463   char *argv[] = {"eject", NULL, NULL};
464
465   if (unix_mount->mount_path != NULL)
466     argv[1] = unix_mount->mount_path;
467   else
468     argv[1] = unix_mount->device_path;
469
470   eject_unmount_do (mount, cancellable, callback, user_data, argv);
471 }
472
473 static gboolean
474 g_unix_mount_eject_finish (GMount       *mount,
475                            GAsyncResult  *result,
476                            GError       **error)
477 {
478   return TRUE;
479 }
480
481 static void
482 g_unix_mount_mount_iface_init (GMountIface *iface)
483 {
484   iface->get_root = g_unix_mount_get_root;
485   iface->get_name = g_unix_mount_get_name;
486   iface->get_icon = g_unix_mount_get_icon;
487   iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
488   iface->get_uuid = g_unix_mount_get_uuid;
489   iface->get_drive = g_unix_mount_get_drive;
490   iface->get_volume = g_unix_mount_get_volume;
491   iface->can_unmount = g_unix_mount_can_unmount;
492   iface->can_eject = g_unix_mount_can_eject;
493   iface->unmount = g_unix_mount_unmount;
494   iface->unmount_finish = g_unix_mount_unmount_finish;
495   iface->eject = g_unix_mount_eject;
496   iface->eject_finish = g_unix_mount_eject_finish;
497 }