f1cd0cb3ebfb5d5234c20ebcd106cb41577846d1
[platform/upstream/glib.git] / gio / gwin32mount.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  * Copyright (C) 2008 Hans Breuer
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Author: Alexander Larsson <alexl@redhat.com>
22  *         David Zeuthen <davidz@redhat.com>
23  *         Hans Breuer <hans@breuer.org>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29 #define WIN32_MEAN_AND_LEAN
30 #include <windows.h>
31
32 #include <glib.h>
33 #include "gwin32volumemonitor.h"
34 #include "gwin32mount.h"
35 #include "gmount.h"
36 #include "gfile.h"
37 #include "gmountprivate.h"
38 #include "gvolumemonitor.h"
39 #include "gthemedicon.h"
40 #include "gsimpleasyncresult.h"
41 #include "glibintl.h"
42
43 #include "gioalias.h"
44
45 struct _GWin32Mount {
46   GObject parent;
47
48   GVolumeMonitor   *volume_monitor;
49
50   GWin32Volume      *volume; /* owned by volume monitor */
51   int   drive_type;
52
53   /* why does all this stuff need to be duplicated? It is in volume already! */
54   char *name;
55   GIcon *icon;
56   char *mount_path;
57
58   gboolean can_eject;
59 };
60
61 static void g_win32_mount_mount_iface_init (GMountIface *iface);
62
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))
67
68
69 static void
70 g_win32_mount_finalize (GObject *object)
71 {
72   GWin32Mount *mount;
73   
74   mount = G_WIN32_MOUNT (object);
75
76   if (mount->volume_monitor != NULL)
77     g_object_unref (mount->volume_monitor);
78 #if 0
79   if (mount->volume)
80     _g_win32_volume_unset_mount (mount->volume, mount);
81 #endif
82   /* TODO: g_warn_if_fail (volume->volume == NULL); */
83   g_object_unref (mount->icon);
84   g_free (mount->name);
85   g_free (mount->mount_path);
86   
87   if (G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize)
88     (*G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize) (object);
89 }
90
91 static void
92 g_win32_mount_class_init (GWin32MountClass *klass)
93 {
94   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95
96   gobject_class->finalize = g_win32_mount_finalize;
97 }
98
99 static void
100 g_win32_mount_init (GWin32Mount *win32_mount)
101 {
102 }
103
104 gchar *
105 _win32_get_displayname (const char *drive)
106 {
107   gunichar2 *wdrive = g_utf8_to_utf16 (drive, -1, NULL, NULL, NULL);
108   gchar *name = NULL;
109   SHFILEINFOW sfi;
110   if (SHGetFileInfoW(wdrive, 0, &sfi, sizeof(sfi), SHGFI_DISPLAYNAME))
111     name = g_utf16_to_utf8 (sfi.szDisplayName, -1, NULL, NULL, NULL);
112
113   g_free (wdrive);
114   return name ? name : g_strdup (drive);
115 }
116
117 /**
118  * _g_win32_mount_new:
119  * @volume_monitor: a #GVolumeMonitor.
120  * @path: a win32 path.
121  * @volume: ususally NULL
122  * 
123  * Returns: a #GWin32Mount for the given win32 path.
124  **/
125 GWin32Mount *
126 _g_win32_mount_new (GVolumeMonitor  *volume_monitor,
127                     const char       *path,
128                     GWin32Volume     *volume)
129 {
130   GWin32Mount *mount;
131   const gchar *drive = path; //fixme
132
133 #if 0  
134   /* No volume for mount: Ignore internal things */
135   if (volume == NULL && !g_win32_mount_guess_should_display (mount_entry))
136     return NULL;
137 #endif
138
139   mount = g_object_new (G_TYPE_WIN32_MOUNT, NULL);
140   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
141   mount->mount_path = g_strdup (path);
142   mount->drive_type = GetDriveType (drive);
143   mount->can_eject = FALSE; /* TODO */
144   mount->name = _win32_get_displayname (drive);
145
146   /* need to do this last */
147   mount->volume = volume;
148 #if 0
149   if (volume != NULL)
150     _g_win32_volume_set_mount (volume, mount);
151 #endif
152   return mount;
153 }
154
155 void
156 _g_win32_mount_unmounted (GWin32Mount *mount)
157 {
158   if (mount->volume != NULL)
159     {
160 #if 0
161       _g_win32_volume_unset_mount (mount->volume, mount);
162 #endif
163       mount->volume = NULL;
164       g_signal_emit_by_name (mount, "changed");
165       /* there's really no need to emit mount_changed on the volume monitor 
166        * as we're going to be deleted.. */
167     }
168 }
169
170 void
171 _g_win32_mount_unset_volume (GWin32Mount *mount,
172                             GWin32Volume  *volume)
173 {
174   if (mount->volume == volume)
175     {
176       mount->volume = NULL;
177       /* TODO: Emit changed in idle to avoid locking issues */
178       g_signal_emit_by_name (mount, "changed");
179       if (mount->volume_monitor != NULL)
180         g_signal_emit_by_name (mount->volume_monitor, "mount_changed", mount);
181     }
182 }
183
184 static GFile *
185 g_win32_mount_get_root (GMount *mount)
186 {
187   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
188
189   return g_file_new_for_path (win32_mount->mount_path);
190 }
191
192 const char *
193 _win32_drive_type_to_icon (int type)
194 {
195   switch (type)
196   {
197   case DRIVE_REMOVABLE : return "gtk-floppy";
198   case DRIVE_FIXED : return "gtk-harddisk";
199   case DRIVE_REMOTE : return "gtk-network"; 
200   case DRIVE_CDROM : return "gtk-cdrom";
201   default : return "gtk-directory";
202   }
203 }
204
205 static GIcon *
206 g_win32_mount_get_icon (GMount *mount)
207 {
208   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
209
210   g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);
211
212   /* lazy creation */
213   if (!win32_mount->icon)
214     {
215       SHFILEINFOW shfi;
216       wchar_t *wfn = g_utf8_to_utf16 (win32_mount->mount_path, -1, NULL, NULL, NULL);
217
218       if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION))
219         {
220           gchar *name = g_utf16_to_utf8 (shfi.szDisplayName, -1, NULL, NULL, NULL);
221           gchar *id = g_strdup_printf ("%s,%i", name, shfi.iIcon);
222           win32_mount->icon = g_themed_icon_new (id);
223           g_free (name);
224           g_free (id);
225         }
226       else
227         {
228           win32_mount->icon = g_themed_icon_new_with_default_fallbacks (
229                                 _win32_drive_type_to_icon (win32_mount->drive_type));
230         }
231     }
232
233   return g_object_ref (win32_mount->icon);
234 }
235
236 static char *
237 g_win32_mount_get_uuid (GMount *mount)
238 {
239   return NULL;
240 }
241
242 static char *
243 g_win32_mount_get_name (GMount *mount)
244 {
245   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
246   
247   return g_strdup (win32_mount->name);
248 }
249
250 static GDrive *
251 g_win32_mount_get_drive (GMount *mount)
252 {
253   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
254
255   if (win32_mount->volume != NULL)
256     return g_volume_get_drive (G_VOLUME (win32_mount->volume));
257
258   return NULL;
259 }
260
261 static GVolume *
262 g_win32_mount_get_volume (GMount *mount)
263 {
264   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
265
266   if (win32_mount->volume)
267     return G_VOLUME (g_object_ref (win32_mount->volume));
268   
269   return NULL;
270 }
271
272 static gboolean
273 g_win32_mount_can_unmount (GMount *mount)
274 {
275   return FALSE;
276 }
277
278 static gboolean
279 g_win32_mount_can_eject (GMount *mount)
280 {
281   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
282   return win32_mount->can_eject;
283 }
284
285
286 typedef struct {
287   GWin32Mount *win32_mount;
288   GAsyncReadyCallback callback;
289   gpointer user_data;
290   GCancellable *cancellable;
291   int error_fd;
292   GIOChannel *error_channel;
293   guint error_channel_source_id;
294   GString *error_string;
295 } UnmountEjectOp;
296
297 static void 
298 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
299 {
300   UnmountEjectOp *data = user_data;
301   GSimpleAsyncResult *simple;
302
303 #if 0  
304   if (WEXITSTATUS (status) != 0)
305     {
306       GError *error;
307       error = g_error_new_literal (G_IO_ERROR, 
308                                    G_IO_ERROR_FAILED,
309                                    data->error_string->str);
310       simple = g_simple_async_result_new_from_error (G_OBJECT (data->win32_mount),
311                                                      data->callback,
312                                                      data->user_data,
313                                                      error);
314       g_error_free (error);
315     }
316   else
317     {
318       simple = g_simple_async_result_new (G_OBJECT (data->win32_mount),
319                                           data->callback,
320                                           data->user_data,
321                                           NULL);
322     }
323
324   g_simple_async_result_complete (simple);
325   g_object_unref (simple);
326
327   g_source_remove (data->error_channel_source_id);
328   g_io_channel_unref (data->error_channel);
329   g_string_free (data->error_string, TRUE);
330   close (data->error_fd);
331   g_spawn_close_pid (pid);
332   g_free (data);
333 #endif
334 }
335
336 static gboolean
337 eject_unmount_read_error (GIOChannel *channel,
338                     GIOCondition condition,
339                     gpointer user_data)
340 {
341   char *str;
342   gsize str_len;
343   UnmountEjectOp *data = user_data;
344
345   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
346   g_string_append (data->error_string, str);
347   g_free (str);
348   return TRUE;
349 }
350
351 static void
352 eject_unmount_do (GMount              *mount,
353                   GCancellable        *cancellable,
354                   GAsyncReadyCallback  callback,
355                   gpointer             user_data,
356                   char               **argv)
357 {
358   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
359 }
360
361 static void
362 g_win32_mount_unmount (GMount             *mount,
363                       GMountUnmountFlags flags,
364                       GCancellable        *cancellable,
365                       GAsyncReadyCallback  callback,
366                       gpointer             user_data)
367 {
368   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
369 }
370
371 static gboolean
372 g_win32_mount_unmount_finish (GMount       *mount,
373                              GAsyncResult  *result,
374                              GError       **error)
375 {
376   return FALSE;
377 }
378
379 static void
380 g_win32_mount_eject (GMount             *mount,
381                     GMountUnmountFlags flags,
382                     GCancellable        *cancellable,
383                     GAsyncReadyCallback  callback,
384                     gpointer             user_data)
385 {
386   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
387 }
388
389 static gboolean
390 g_win32_mount_eject_finish (GMount       *mount,
391                            GAsyncResult  *result,
392                            GError       **error)
393 {
394   return FALSE;
395 }
396
397 static void
398 g_win32_mount_mount_iface_init (GMountIface *iface)
399 {
400   iface->get_root = g_win32_mount_get_root;
401   iface->get_name = g_win32_mount_get_name;
402   iface->get_icon = g_win32_mount_get_icon;
403   iface->get_uuid = g_win32_mount_get_uuid;
404   iface->get_drive = g_win32_mount_get_drive;
405   iface->get_volume = g_win32_mount_get_volume;
406   iface->can_unmount = g_win32_mount_can_unmount;
407   iface->can_eject = g_win32_mount_can_eject;
408   iface->unmount = g_win32_mount_unmount;
409   iface->unmount_finish = g_win32_mount_unmount_finish;
410   iface->eject = g_win32_mount_eject;
411   iface->eject_finish = g_win32_mount_eject_finish;
412 }