1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2008 Hans Breuer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
22 * David Zeuthen <davidz@redhat.com>
23 * Hans Breuer <hans@breuer.org>
29 #define WIN32_MEAN_AND_LEAN
33 #include "gwin32volumemonitor.h"
34 #include "gwin32mount.h"
37 #include "gmountprivate.h"
38 #include "gvolumemonitor.h"
39 #include "gthemedicon.h"
40 #include "gsimpleasyncresult.h"
48 GVolumeMonitor *volume_monitor;
50 GWin32Volume *volume; /* owned by volume monitor */
53 /* why does all this stuff need to be duplicated? It is in volume already! */
61 static void g_win32_mount_mount_iface_init (GMountIface *iface);
63 #define g_win32_mount_get_type _g_win32_mount_get_type
64 G_DEFINE_TYPE_WITH_CODE (GWin32Mount, g_win32_mount, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
66 g_win32_mount_mount_iface_init))
70 g_win32_mount_finalize (GObject *object)
74 mount = G_WIN32_MOUNT (object);
76 if (mount->volume_monitor != NULL)
77 g_object_unref (mount->volume_monitor);
80 _g_win32_volume_unset_mount (mount->volume, mount);
82 /* TODO: g_warn_if_fail (volume->volume == NULL); */
84 if (mount->icon != NULL)
85 g_object_unref (mount->icon);
88 g_free (mount->mount_path);
90 if (G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize)
91 (*G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize) (object);
95 g_win32_mount_class_init (GWin32MountClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = g_win32_mount_finalize;
103 g_win32_mount_init (GWin32Mount *win32_mount)
108 _win32_get_displayname (const char *drive)
110 gunichar2 *wdrive = g_utf8_to_utf16 (drive, -1, NULL, NULL, NULL);
113 if (SHGetFileInfoW(wdrive, 0, &sfi, sizeof(sfi), SHGFI_DISPLAYNAME))
114 name = g_utf16_to_utf8 (sfi.szDisplayName, -1, NULL, NULL, NULL);
117 return name ? name : g_strdup (drive);
121 * _g_win32_mount_new:
122 * @volume_monitor: a #GVolumeMonitor.
123 * @path: a win32 path.
124 * @volume: ususally NULL
126 * Returns: a #GWin32Mount for the given win32 path.
129 _g_win32_mount_new (GVolumeMonitor *volume_monitor,
131 GWin32Volume *volume)
134 const gchar *drive = path; //fixme
137 /* No volume for mount: Ignore internal things */
138 if (volume == NULL && !g_win32_mount_guess_should_display (mount_entry))
142 mount = g_object_new (G_TYPE_WIN32_MOUNT, NULL);
143 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
144 mount->mount_path = g_strdup (path);
145 mount->drive_type = GetDriveType (drive);
146 mount->can_eject = FALSE; /* TODO */
147 mount->name = _win32_get_displayname (drive);
149 /* need to do this last */
150 mount->volume = volume;
153 _g_win32_volume_set_mount (volume, mount);
159 _g_win32_mount_unmounted (GWin32Mount *mount)
161 if (mount->volume != NULL)
164 _g_win32_volume_unset_mount (mount->volume, mount);
166 mount->volume = NULL;
167 g_signal_emit_by_name (mount, "changed");
168 /* there's really no need to emit mount_changed on the volume monitor
169 * as we're going to be deleted.. */
174 _g_win32_mount_unset_volume (GWin32Mount *mount,
175 GWin32Volume *volume)
177 if (mount->volume == volume)
179 mount->volume = NULL;
180 /* TODO: Emit changed in idle to avoid locking issues */
181 g_signal_emit_by_name (mount, "changed");
182 if (mount->volume_monitor != NULL)
183 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
188 g_win32_mount_get_root (GMount *mount)
190 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
192 return g_file_new_for_path (win32_mount->mount_path);
196 _win32_drive_type_to_icon (int type)
200 case DRIVE_REMOVABLE : return "gtk-floppy";
201 case DRIVE_FIXED : return "gtk-harddisk";
202 case DRIVE_REMOTE : return "gtk-network";
203 case DRIVE_CDROM : return "gtk-cdrom";
204 default : return "gtk-directory";
209 g_win32_mount_get_icon (GMount *mount)
211 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
213 g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);
216 if (!win32_mount->icon)
219 wchar_t *wfn = g_utf8_to_utf16 (win32_mount->mount_path, -1, NULL, NULL, NULL);
221 if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION))
223 gchar *name = g_utf16_to_utf8 (shfi.szDisplayName, -1, NULL, NULL, NULL);
224 gchar *id = g_strdup_printf ("%s,%i", name, shfi.iIcon);
225 win32_mount->icon = g_themed_icon_new (id);
231 win32_mount->icon = g_themed_icon_new_with_default_fallbacks (
232 _win32_drive_type_to_icon (win32_mount->drive_type));
236 return g_object_ref (win32_mount->icon);
240 g_win32_mount_get_uuid (GMount *mount)
246 g_win32_mount_get_name (GMount *mount)
248 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
250 return g_strdup (win32_mount->name);
254 g_win32_mount_get_drive (GMount *mount)
256 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
258 if (win32_mount->volume != NULL)
259 return g_volume_get_drive (G_VOLUME (win32_mount->volume));
265 g_win32_mount_get_volume (GMount *mount)
267 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
269 if (win32_mount->volume)
270 return G_VOLUME (g_object_ref (win32_mount->volume));
276 g_win32_mount_can_unmount (GMount *mount)
282 g_win32_mount_can_eject (GMount *mount)
284 GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
285 return win32_mount->can_eject;
290 GWin32Mount *win32_mount;
291 GAsyncReadyCallback callback;
293 GCancellable *cancellable;
295 GIOChannel *error_channel;
296 guint error_channel_source_id;
297 GString *error_string;
301 g_win32_mount_unmount (GMount *mount,
302 GMountUnmountFlags flags,
303 GCancellable *cancellable,
304 GAsyncReadyCallback callback,
310 g_win32_mount_unmount_finish (GMount *mount,
311 GAsyncResult *result,
318 g_win32_mount_eject (GMount *mount,
319 GMountUnmountFlags flags,
320 GCancellable *cancellable,
321 GAsyncReadyCallback callback,
327 g_win32_mount_eject_finish (GMount *mount,
328 GAsyncResult *result,
335 g_win32_mount_mount_iface_init (GMountIface *iface)
337 iface->get_root = g_win32_mount_get_root;
338 iface->get_name = g_win32_mount_get_name;
339 iface->get_icon = g_win32_mount_get_icon;
340 iface->get_uuid = g_win32_mount_get_uuid;
341 iface->get_drive = g_win32_mount_get_drive;
342 iface->get_volume = g_win32_mount_get_volume;
343 iface->can_unmount = g_win32_mount_can_unmount;
344 iface->can_eject = g_win32_mount_can_eject;
345 iface->unmount = g_win32_mount_unmount;
346 iface->unmount_finish = g_win32_mount_unmount_finish;
347 iface->eject = g_win32_mount_eject;
348 iface->eject_finish = g_win32_mount_eject_finish;