gunixvolume.c revision d9594f5709313d6e7a8a4f3e5f3b23fc72017417
1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 * Author: Alexander Larsson <alexl@redhat.com>
21 */
22
23#include <config.h>
24
25#include <string.h>
26
27#include <glib.h>
28#include "gunixvolumemonitor.h"
29#include "gunixvolume.h"
30#include "gunixdrive.h"
31#include "gvolumeprivate.h"
32#include "gvolumemonitor.h"
33#include "gthemedicon.h"
34#include "glibintl.h"
35
36#include "gioalias.h"
37
38struct _GUnixVolume {
39  GObject parent;
40
41  GUnixDrive *drive; /* owned by volume monitor */
42  char *name;
43  char *icon;
44  char *mountpoint;
45};
46
47static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
48
49#define g_unix_volume_get_type _g_unix_volume_get_type
50G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
51			 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
52						g_unix_volume_volume_iface_init))
53
54
55static void
56g_unix_volume_finalize (GObject *object)
57{
58  GUnixVolume *volume;
59
60  volume = G_UNIX_VOLUME (object);
61
62  if (volume->drive)
63    _g_unix_drive_unset_volume (volume->drive, volume);
64
65  g_assert (volume->drive == NULL);
66  g_free (volume->name);
67  g_free (volume->icon);
68  g_free (volume->mountpoint);
69
70  if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
71    (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
72}
73
74static void
75g_unix_volume_class_init (GUnixVolumeClass *klass)
76{
77  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78
79  gobject_class->finalize = g_unix_volume_finalize;
80}
81
82static void
83g_unix_volume_init (GUnixVolume *unix_volume)
84{
85}
86
87static char *
88get_filesystem_volume_name (const char *fs_type)
89{
90  /* TODO: add translation table from gnome-vfs */
91  return g_strdup_printf (_("%s volume"), fs_type);
92}
93
94static char *
95type_to_icon (GUnixMountType type)
96{
97  const char *icon_name = NULL;
98
99  switch (type)
100    {
101    case G_UNIX_MOUNT_TYPE_HD:
102      icon_name = "drive-harddisk";
103      break;
104    case G_UNIX_MOUNT_TYPE_FLOPPY:
105    case G_UNIX_MOUNT_TYPE_ZIP:
106    case G_UNIX_MOUNT_TYPE_JAZ:
107      icon_name = "media-floppy";
108      break;
109    case G_UNIX_MOUNT_TYPE_CDROM:
110      icon_name = "media-optical";
111      break;
112    case G_UNIX_MOUNT_TYPE_NFS:
113      /* TODO: Would like a better icon here... */
114      icon_name = "drive-harddisk";
115      break;
116    case G_UNIX_MOUNT_TYPE_MEMSTICK:
117      icon_name = "media-flash";
118      break;
119    case G_UNIX_MOUNT_TYPE_CAMERA:
120      icon_name = "camera-photo";
121      break;
122    case G_UNIX_MOUNT_TYPE_IPOD:
123      icon_name = "multimedia-player";
124      break;
125    case G_UNIX_MOUNT_TYPE_UNKNOWN:
126    default:
127      icon_name = "drive-harddisk";
128      break;
129    }
130  return g_strdup (icon_name);
131}
132
133/**
134 * g_unix_volume_new:
135 * @mount:
136 * @drive:
137 *
138 * Returns: a #GUnixVolume.
139 *
140 **/
141GUnixVolume *
142_g_unix_volume_new (GUnixMount *mount,
143		    GUnixDrive *drive)
144{
145  GUnixVolume *volume;
146  GUnixMountType type;
147  const char *mount_path;
148  char *volume_name;
149
150  mount_path = g_unix_mount_get_mount_path (mount);
151
152  /* No drive for volume. Ignore internal things */
153  if (drive == NULL && g_unix_mount_is_system_internal (mount))
154    return NULL;
155
156  volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
157  volume->drive = drive;
158  if (drive)
159    _g_unix_drive_set_volume (drive, volume);
160  volume->mountpoint = g_strdup (mount_path);
161
162  type = g_unix_mount_guess_type (mount);
163
164  volume->icon = type_to_icon (type);
165
166  volume_name = NULL;
167  if (mount_path)
168    {
169      if (strcmp (mount_path, "/") == 0)
170	volume_name = g_strdup (_("Filesystem root"));
171      else
172	volume_name = g_filename_display_basename (mount_path);
173    }
174
175  if (volume_name == NULL)
176    {
177      if (g_unix_mount_get_fs_type (mount) != NULL)
178	volume_name = g_strdup (get_filesystem_volume_name (g_unix_mount_get_fs_type (mount)));
179    }
180
181  if (volume_name == NULL)
182    {
183      /* TODO: Use volume size as name? */
184      volume_name = g_strdup (_("Unknown volume"));
185    }
186
187  volume->name = volume_name;
188
189  return volume;
190}
191
192/**
193 * g_unix_volume_unmounted:
194 * @volume:
195 *
196 **/
197void
198_g_unix_volume_unmounted (GUnixVolume *volume)
199{
200  if (volume->drive)
201    {
202      _g_unix_drive_unset_volume (volume->drive, volume);
203      volume->drive = NULL;
204      g_signal_emit_by_name (volume, "changed");
205    }
206}
207
208/**
209 * g_unix_volume_unset_drive:
210 * @volume:
211 * @drive:
212 *
213 **/
214void
215_g_unix_volume_unset_drive (GUnixVolume   *volume,
216			    GUnixDrive    *drive)
217{
218  if (volume->drive == drive)
219    {
220      volume->drive = NULL;
221      /* TODO: Emit changed in idle to avoid locking issues */
222      g_signal_emit_by_name (volume, "changed");
223    }
224}
225
226static GFile *
227g_unix_volume_get_root (GVolume *volume)
228{
229  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
230
231  return g_file_new_for_path (unix_volume->mountpoint);
232}
233
234static GIcon *
235g_unix_volume_get_icon (GVolume *volume)
236{
237  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
238
239  return g_themed_icon_new (unix_volume->icon);
240}
241
242static char *
243g_unix_volume_get_name (GVolume *volume)
244{
245  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246
247  return g_strdup (unix_volume->name);
248}
249
250/**
251 * g_unix_volume_has_mountpoint:
252 * @volume:
253 * @mountpoint:
254 *
255 * Returns:
256 **/
257gboolean
258_g_unix_volume_has_mountpoint (GUnixVolume *volume,
259			       const char  *mountpoint)
260{
261  return strcmp (volume->mountpoint, mountpoint) == 0;
262}
263
264static GDrive *
265g_unix_volume_get_drive (GVolume *volume)
266{
267  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
268
269  if (unix_volume->drive)
270    return G_DRIVE (g_object_ref (unix_volume->drive));
271
272  return NULL;
273}
274
275static gboolean
276g_unix_volume_can_unmount (GVolume *volume)
277{
278  /* TODO */
279  return FALSE;
280}
281
282static gboolean
283g_unix_volume_can_eject (GVolume *volume)
284{
285  /* TODO */
286  return FALSE;
287}
288
289static void
290g_unix_volume_unmount (GVolume         *volume,
291		       GCancellable    *cancellable,
292		       GAsyncReadyCallback callback,
293		       gpointer         user_data)
294{
295  /* TODO */
296}
297
298static gboolean
299g_unix_volume_unmount_finish (GVolume *volume,
300			      GAsyncResult *result,
301			      GError **error)
302{
303  return TRUE;
304}
305
306static void
307g_unix_volume_eject (GVolume         *volume,
308		     GCancellable    *cancellable,
309		     GAsyncReadyCallback callback,
310		     gpointer         user_data)
311{
312  /* TODO */
313}
314
315static gboolean
316g_unix_volume_eject_finish (GVolume *volume,
317			    GAsyncResult *result,
318			    GError **error)
319{
320  return TRUE;
321}
322
323static void
324g_unix_volume_volume_iface_init (GVolumeIface *iface)
325{
326  iface->get_root = g_unix_volume_get_root;
327  iface->get_name = g_unix_volume_get_name;
328  iface->get_icon = g_unix_volume_get_icon;
329  iface->get_drive = g_unix_volume_get_drive;
330  iface->can_unmount = g_unix_volume_can_unmount;
331  iface->can_eject = g_unix_volume_can_eject;
332  iface->unmount = g_unix_volume_unmount;
333  iface->unmount_finish = g_unix_volume_unmount_finish;
334  iface->eject = g_unix_volume_eject;
335  iface->eject_finish = g_unix_volume_eject_finish;
336}
337