Add symbolic icon support to drive, volume, and mount
[platform/upstream/glib.git] / gio / gunixvolume.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 "gunixvolume.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gthemedicon.h"
37 #include "gvolume.h"
38 #include "gvolumemonitor.h"
39 #include "gsimpleasyncresult.h"
40 #include "gioerror.h"
41 #include "glibintl.h"
42 /* for BUFSIZ */
43 #include <stdio.h>
44
45
46 struct _GUnixVolume {
47   GObject parent;
48
49   GVolumeMonitor *volume_monitor;
50   GUnixMount     *mount; /* owned by volume monitor */
51   
52   char *device_path;
53   char *mount_path;
54   gboolean can_eject;
55
56   char *identifier;
57   char *identifier_type;
58   
59   char *name;
60   GIcon *icon;
61   GIcon *symbolic_icon;
62 };
63
64 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
65
66 #define g_unix_volume_get_type _g_unix_volume_get_type
67 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
68                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
69                                                 g_unix_volume_volume_iface_init))
70
71 static void
72 g_unix_volume_finalize (GObject *object)
73 {
74   GUnixVolume *volume;
75   
76   volume = G_UNIX_VOLUME (object);
77
78   if (volume->volume_monitor != NULL)
79     g_object_unref (volume->volume_monitor);
80
81   if (volume->mount)
82     _g_unix_mount_unset_volume (volume->mount, volume);
83   
84   g_object_unref (volume->icon);
85   g_object_unref (volume->symbolic_icon);
86   g_free (volume->name);
87   g_free (volume->mount_path);
88   g_free (volume->device_path);
89   g_free (volume->identifier);
90   g_free (volume->identifier_type);
91
92   G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
93 }
94
95 static void
96 g_unix_volume_class_init (GUnixVolumeClass *klass)
97 {
98   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99
100   gobject_class->finalize = g_unix_volume_finalize;
101 }
102
103 static void
104 g_unix_volume_init (GUnixVolume *unix_volume)
105 {
106 }
107
108 GUnixVolume *
109 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
110                     GUnixMountPoint *mountpoint)
111 {
112   GUnixVolume *volume;
113   
114   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
115         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
116       g_unix_mount_point_is_loopback (mountpoint))
117     return NULL;
118   
119   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
120   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
121   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
122   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
123   volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
124
125   volume->name = g_unix_mount_point_guess_name (mountpoint);
126   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
127   volume->symbolic_icon = g_unix_mount_point_guess_symbolic_icon (mountpoint);
128
129
130   if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
131     {
132       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
133       volume->identifier = g_strdup (volume->device_path);
134     }
135   else if (g_str_has_prefix (volume->device_path, "LABEL="))
136     {
137       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
138       volume->identifier = g_strdup (volume->device_path + 6);
139     }
140   else if (g_str_has_prefix (volume->device_path, "UUID="))
141     {
142       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
143       volume->identifier = g_strdup (volume->device_path + 5);
144     }
145   else if (g_path_is_absolute (volume->device_path))
146     {
147       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
148       volume->identifier = g_strdup (volume->device_path);
149     }
150   
151   return volume;
152 }
153
154 void
155 _g_unix_volume_disconnected (GUnixVolume *volume)
156 {
157   if (volume->mount)
158     {
159       _g_unix_mount_unset_volume (volume->mount, volume);
160       volume->mount = NULL;
161     }
162 }
163
164 void
165 _g_unix_volume_set_mount (GUnixVolume *volume,
166                           GUnixMount  *mount)
167 {
168   if (volume->mount == mount)
169     return;
170   
171   if (volume->mount)
172     _g_unix_mount_unset_volume (volume->mount, volume);
173   
174   volume->mount = mount;
175   
176   /* TODO: Emit changed in idle to avoid locking issues */
177   g_signal_emit_by_name (volume, "changed");
178   if (volume->volume_monitor != NULL)
179     g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
180 }
181
182 void
183 _g_unix_volume_unset_mount (GUnixVolume  *volume,
184                             GUnixMount *mount)
185 {
186   if (volume->mount == mount)
187     {
188       volume->mount = NULL;
189       /* TODO: Emit changed in idle to avoid locking issues */
190       g_signal_emit_by_name (volume, "changed");
191       if (volume->volume_monitor != NULL)
192         g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
193     }
194 }
195
196 static GIcon *
197 g_unix_volume_get_icon (GVolume *volume)
198 {
199   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
200   return g_object_ref (unix_volume->icon);
201 }
202
203 static GIcon *
204 g_unix_volume_get_symbolic_icon (GVolume *volume)
205 {
206   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
207   return g_object_ref (unix_volume->symbolic_icon);
208 }
209
210 static char *
211 g_unix_volume_get_name (GVolume *volume)
212 {
213   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
214   return g_strdup (unix_volume->name);
215 }
216
217 static char *
218 g_unix_volume_get_uuid (GVolume *volume)
219 {
220   return NULL;
221 }
222
223 static gboolean
224 g_unix_volume_can_mount (GVolume *volume)
225 {
226   return TRUE;
227 }
228
229 static gboolean
230 g_unix_volume_can_eject (GVolume *volume)
231 {
232   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
233   return unix_volume->can_eject;
234 }
235
236 static gboolean
237 g_unix_volume_should_automount (GVolume *volume)
238 {
239   /* We automount all local volumes because we don't even
240    * make the internal stuff visible
241    */
242   return TRUE;
243 }
244
245 static GDrive *
246 g_unix_volume_get_drive (GVolume *volume)
247 {
248   return NULL;
249 }
250
251 static GMount *
252 g_unix_volume_get_mount (GVolume *volume)
253 {
254   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
255
256   if (unix_volume->mount != NULL)
257     return g_object_ref (unix_volume->mount);
258
259   return NULL;
260 }
261
262
263 gboolean
264 _g_unix_volume_has_mount_path (GUnixVolume *volume,
265                                const char  *mount_path)
266 {
267   return strcmp (volume->mount_path, mount_path) == 0;
268 }
269
270
271 typedef struct {
272   GUnixVolume *unix_volume;
273   GAsyncReadyCallback callback;
274   gpointer user_data;
275   GCancellable *cancellable;
276   int error_fd;
277   GIOChannel *error_channel;
278   GSource *error_channel_source;
279   GString *error_string;
280 } EjectMountOp;
281
282 static void
283 eject_mount_cb (GPid     pid,
284                 gint     status,
285                 gpointer user_data)
286 {
287   EjectMountOp *data = user_data;
288   GSimpleAsyncResult *simple;
289   
290   if (WEXITSTATUS (status) != 0)
291     {
292       GError *error;
293       error = g_error_new_literal (G_IO_ERROR, 
294                                    G_IO_ERROR_FAILED,
295                                    data->error_string->str);
296       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
297                                                      data->callback,
298                                                      data->user_data,
299                                                      error);
300       g_error_free (error);
301     }
302   else
303     {
304       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
305                                           data->callback,
306                                           data->user_data,
307                                           NULL);
308     }
309
310   g_simple_async_result_complete (simple);
311   g_object_unref (simple);
312
313   if (data->error_channel_source)
314     {
315       g_source_destroy (data->error_channel_source);
316       g_source_unref (data->error_channel_source);
317     }
318   g_io_channel_unref (data->error_channel);
319   g_string_free (data->error_string, TRUE);
320   close (data->error_fd);
321   g_spawn_close_pid (pid);
322   g_free (data);
323 }
324
325 static gboolean
326 eject_mount_read_error (GIOChannel   *channel,
327                         GIOCondition  condition,
328                         gpointer      user_data)
329 {
330   EjectMountOp *data = user_data;
331   char buf[BUFSIZ];
332   gsize bytes_read;
333   GError *error;
334   GIOStatus status;
335
336   error = NULL;
337 read:
338   status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
339   if (status == G_IO_STATUS_NORMAL)
340    {
341      g_string_append_len (data->error_string, buf, bytes_read);
342      if (bytes_read == sizeof (buf))
343         goto read;
344    }
345   else if (status == G_IO_STATUS_EOF)
346     g_string_append_len (data->error_string, buf, bytes_read);
347   else if (status == G_IO_STATUS_ERROR)
348     {
349       if (data->error_string->len > 0)
350         g_string_append (data->error_string, "\n");
351
352       g_string_append (data->error_string, error->message);
353       g_error_free (error);
354
355       if (data->error_channel_source)
356         {
357           g_source_unref (data->error_channel_source);
358           data->error_channel_source = NULL;
359         }
360       return FALSE;
361     }
362
363   return TRUE;
364 }
365
366 static void
367 eject_mount_do (GVolume             *volume,
368                 GCancellable        *cancellable,
369                 GAsyncReadyCallback  callback,
370                 gpointer             user_data,
371                 char               **argv)
372 {
373   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
374   EjectMountOp *data;
375   GPid child_pid;
376   GSource *child_watch;
377   GError *error;
378   
379   data = g_new0 (EjectMountOp, 1);
380   data->unix_volume = unix_volume;
381   data->callback = callback;
382   data->user_data = user_data;
383   data->cancellable = cancellable;
384   
385   error = NULL;
386   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
387                                  argv,
388                                  NULL,         /* envp */
389                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
390                                  NULL,         /* child_setup */
391                                  NULL,         /* user_data for child_setup */
392                                  &child_pid,
393                                  NULL,           /* standard_input */
394                                  NULL,           /* standard_output */
395                                  &(data->error_fd),
396                                  &error))
397     {
398       g_assert (error != NULL);
399       goto handle_error;
400     }
401
402   data->error_string = g_string_new ("");
403
404   data->error_channel = g_io_channel_unix_new (data->error_fd);
405   g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
406   if (error != NULL)
407     goto handle_error;
408
409   data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
410   g_source_set_callback (data->error_channel_source,
411                          (GSourceFunc) eject_mount_read_error, data, NULL);
412   g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
413
414   child_watch = g_child_watch_source_new (child_pid);
415   g_source_set_callback (child_watch, (GSourceFunc) eject_mount_cb, data, NULL);
416   g_source_attach (child_watch, g_main_context_get_thread_default ());
417   g_source_unref (child_watch);
418
419 handle_error:
420   if (error != NULL)
421     {
422       GSimpleAsyncResult *simple;
423       simple = g_simple_async_result_new_take_error (G_OBJECT (data->unix_volume),
424                                                      data->callback,
425                                                      data->user_data,
426                                                      error);
427       g_simple_async_result_complete (simple);
428       g_object_unref (simple);
429
430       if (data->error_string != NULL)
431         g_string_free (data->error_string, TRUE);
432
433       if (data->error_channel != NULL)
434         g_io_channel_unref (data->error_channel);
435
436       g_free (data);
437     }
438 }
439
440
441 static void
442 g_unix_volume_mount (GVolume            *volume,
443                      GMountMountFlags    flags,
444                      GMountOperation     *mount_operation,
445                      GCancellable        *cancellable,
446                      GAsyncReadyCallback  callback,
447                      gpointer             user_data)
448 {
449   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
450   char *argv[] = { "mount", NULL, NULL };
451
452   if (unix_volume->mount_path != NULL)
453     argv[1] = unix_volume->mount_path;
454   else
455     argv[1] = unix_volume->device_path;
456
457   eject_mount_do (volume, cancellable, callback, user_data, argv);
458 }
459
460 static gboolean
461 g_unix_volume_mount_finish (GVolume        *volume,
462                             GAsyncResult  *result,
463                             GError       **error)
464 {
465   return TRUE;
466 }
467
468 static void
469 g_unix_volume_eject (GVolume             *volume,
470                      GMountUnmountFlags   flags,
471                      GCancellable        *cancellable,
472                      GAsyncReadyCallback  callback,
473                      gpointer             user_data)
474 {
475   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
476   char *argv[] = { "eject", NULL, NULL };
477
478   argv[1] = unix_volume->device_path;
479
480   eject_mount_do (volume, cancellable, callback, user_data, argv);
481 }
482
483 static gboolean
484 g_unix_volume_eject_finish (GVolume       *volume,
485                             GAsyncResult  *result,
486                             GError       **error)
487 {
488   return TRUE;
489 }
490
491 static gchar *
492 g_unix_volume_get_identifier (GVolume     *volume,
493                               const gchar *kind)
494 {
495   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
496
497   if (unix_volume->identifier_type != NULL &&
498       strcmp (kind, unix_volume->identifier_type) == 0)
499     return g_strdup (unix_volume->identifier);
500
501   return NULL;
502 }
503
504 static gchar **
505 g_unix_volume_enumerate_identifiers (GVolume *volume)
506 {
507   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
508   gchar **res;
509
510   if (unix_volume->identifier_type)
511     {
512       res = g_new (gchar *, 2);
513       res[0] = g_strdup (unix_volume->identifier_type);
514       res[1] = NULL;
515     }
516   else
517     {
518       res = g_new (gchar *, 1);
519       res[0] = NULL;
520     }
521
522   return res;
523 }
524
525 static void
526 g_unix_volume_volume_iface_init (GVolumeIface *iface)
527 {
528   iface->get_name = g_unix_volume_get_name;
529   iface->get_icon = g_unix_volume_get_icon;
530   iface->get_symbolic_icon = g_unix_volume_get_symbolic_icon;
531   iface->get_uuid = g_unix_volume_get_uuid;
532   iface->get_drive = g_unix_volume_get_drive;
533   iface->get_mount = g_unix_volume_get_mount;
534   iface->can_mount = g_unix_volume_can_mount;
535   iface->can_eject = g_unix_volume_can_eject;
536   iface->should_automount = g_unix_volume_should_automount;
537   iface->mount_fn = g_unix_volume_mount;
538   iface->mount_finish = g_unix_volume_mount_finish;
539   iface->eject = g_unix_volume_eject;
540   iface->eject_finish = g_unix_volume_eject_finish;
541   iface->get_identifier = g_unix_volume_get_identifier;
542   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
543 }