gsocket: Add missing preconditions to g_socket_send_message()
[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, see <http://www.gnu.org/licenses/>.
18  *
19  * Author: Alexander Larsson <alexl@redhat.com>
20  *         David Zeuthen <davidz@redhat.com>
21  *         Hans Breuer <hans@breuer.org>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #define WIN32_MEAN_AND_LEAN
28 #include <windows.h>
29
30 #include <glib.h>
31 #include "gwin32volumemonitor.h"
32 #include "gwin32mount.h"
33 #include "gmount.h"
34 #include "gfile.h"
35 #include "gmountprivate.h"
36 #include "gvolumemonitor.h"
37 #include "gthemedicon.h"
38 #include "gsimpleasyncresult.h"
39 #include "glibintl.h"
40
41
42 struct _GWin32Mount {
43   GObject parent;
44
45   GVolumeMonitor   *volume_monitor;
46
47   GWin32Volume      *volume; /* owned by volume monitor */
48   int   drive_type;
49
50   /* why does all this stuff need to be duplicated? It is in volume already! */
51   char *name;
52   GIcon *icon;
53   GIcon *symbolic_icon;
54   char *mount_path;
55
56   gboolean can_eject;
57 };
58
59 static void g_win32_mount_mount_iface_init (GMountIface *iface);
60
61 #define g_win32_mount_get_type _g_win32_mount_get_type
62 G_DEFINE_TYPE_WITH_CODE (GWin32Mount, g_win32_mount, G_TYPE_OBJECT,
63                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
64                                                 g_win32_mount_mount_iface_init))
65
66
67 static void
68 g_win32_mount_finalize (GObject *object)
69 {
70   GWin32Mount *mount;
71   
72   mount = G_WIN32_MOUNT (object);
73
74   if (mount->volume_monitor != NULL)
75     g_object_unref (mount->volume_monitor);
76 #if 0
77   if (mount->volume)
78     _g_win32_volume_unset_mount (mount->volume, mount);
79 #endif
80   /* TODO: g_warn_if_fail (volume->volume == NULL); */
81
82   if (mount->icon != NULL)
83     g_object_unref (mount->icon);
84   if (mount->symbolic_icon != NULL)
85     g_object_unref (mount->symbolic_icon);
86
87   g_free (mount->name);
88   g_free (mount->mount_path);
89   
90   if (G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize)
91     (*G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize) (object);
92 }
93
94 static void
95 g_win32_mount_class_init (GWin32MountClass *klass)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98
99   gobject_class->finalize = g_win32_mount_finalize;
100 }
101
102 static void
103 g_win32_mount_init (GWin32Mount *win32_mount)
104 {
105 }
106
107 static gchar *
108 _win32_get_displayname (const char *drive)
109 {
110   gunichar2 *wdrive = g_utf8_to_utf16 (drive, -1, NULL, NULL, NULL);
111   gchar *name = NULL;
112   SHFILEINFOW sfi;
113   if (SHGetFileInfoW(wdrive, 0, &sfi, sizeof(sfi), SHGFI_DISPLAYNAME))
114     name = g_utf16_to_utf8 (sfi.szDisplayName, -1, NULL, NULL, NULL);
115
116   g_free (wdrive);
117   return name ? name : g_strdup (drive);
118 }
119
120 /*
121  * _g_win32_mount_new:
122  * @volume_monitor: a #GVolumeMonitor.
123  * @path: a win32 path.
124  * @volume: usually NULL
125  * 
126  * Returns: a #GWin32Mount for the given win32 path.
127  **/
128 GWin32Mount *
129 _g_win32_mount_new (GVolumeMonitor  *volume_monitor,
130                     const char      *path,
131                     GWin32Volume    *volume)
132 {
133   GWin32Mount *mount;
134   const gchar *drive = path; //fixme
135
136 #if 0  
137   /* No volume for mount: Ignore internal things */
138   if (volume == NULL && !g_win32_mount_guess_should_display (mount_entry))
139     return NULL;
140 #endif
141
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);
148
149   /* need to do this last */
150   mount->volume = volume;
151 #if 0
152   if (volume != NULL)
153     _g_win32_volume_set_mount (volume, mount);
154 #endif
155   return mount;
156 }
157
158 void
159 _g_win32_mount_unmounted (GWin32Mount *mount)
160 {
161   if (mount->volume != NULL)
162     {
163 #if 0
164       _g_win32_volume_unset_mount (mount->volume, mount);
165 #endif
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.. */
170     }
171 }
172
173 void
174 _g_win32_mount_unset_volume (GWin32Mount  *mount,
175                              GWin32Volume *volume)
176 {
177   if (mount->volume == volume)
178     {
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);
184     }
185 }
186
187 static GFile *
188 g_win32_mount_get_root (GMount *mount)
189 {
190   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
191
192   return g_file_new_for_path (win32_mount->mount_path);
193 }
194
195 static const char *
196 _win32_drive_type_to_icon (int type, gboolean use_symbolic)
197 {
198   switch (type)
199   {
200   case DRIVE_REMOVABLE : return use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
201   case DRIVE_FIXED : return use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
202   case DRIVE_REMOTE : return use_symbolic ? "folder-remote-symbolic" : "folder-remote";
203   case DRIVE_CDROM : return use_symbolic ? "drive-optical-symbolic" : "drive-optical";
204   default : return use_symbolic ? "folder-symbolic" : "folder";
205   }
206 }
207
208 static GIcon *
209 g_win32_mount_get_icon (GMount *mount)
210 {
211   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
212
213   g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);
214
215   /* lazy creation */
216   if (!win32_mount->icon)
217     {
218       SHFILEINFOW shfi;
219       wchar_t *wfn = g_utf8_to_utf16 (win32_mount->mount_path, -1, NULL, NULL, NULL);
220
221       if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION))
222         {
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);
226           g_free (name);
227           g_free (id);
228         }
229       else
230         {
231           win32_mount->icon = g_themed_icon_new_with_default_fallbacks (_win32_drive_type_to_icon (win32_mount->drive_type, FALSE));
232         }
233     }
234
235   return g_object_ref (win32_mount->icon);
236 }
237
238 static GIcon *
239 g_win32_mount_get_symbolic_icon (GMount *mount)
240 {
241   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
242
243   g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);
244
245   /* lazy creation */
246   if (!win32_mount->symbolic_icon)
247     {
248       win32_mount->symbolic_icon = g_themed_icon_new_with_default_fallbacks (_win32_drive_type_to_icon (win32_mount->drive_type, TRUE));
249     }
250
251   return g_object_ref (win32_mount->symbolic_icon);
252 }
253
254 static char *
255 g_win32_mount_get_uuid (GMount *mount)
256 {
257   return NULL;
258 }
259
260 static char *
261 g_win32_mount_get_name (GMount *mount)
262 {
263   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
264   
265   return g_strdup (win32_mount->name);
266 }
267
268 static GDrive *
269 g_win32_mount_get_drive (GMount *mount)
270 {
271   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
272
273   if (win32_mount->volume != NULL)
274     return g_volume_get_drive (G_VOLUME (win32_mount->volume));
275
276   return NULL;
277 }
278
279 static GVolume *
280 g_win32_mount_get_volume (GMount *mount)
281 {
282   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
283
284   if (win32_mount->volume)
285     return G_VOLUME (g_object_ref (win32_mount->volume));
286   
287   return NULL;
288 }
289
290 static gboolean
291 g_win32_mount_can_unmount (GMount *mount)
292 {
293   return FALSE;
294 }
295
296 static gboolean
297 g_win32_mount_can_eject (GMount *mount)
298 {
299   GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
300   return win32_mount->can_eject;
301 }
302
303
304 typedef struct {
305   GWin32Mount *win32_mount;
306   GAsyncReadyCallback callback;
307   gpointer user_data;
308   GCancellable *cancellable;
309   int error_fd;
310   GIOChannel *error_channel;
311   guint error_channel_source_id;
312   GString *error_string;
313 } UnmountEjectOp;
314
315 static void
316 g_win32_mount_unmount (GMount              *mount,
317                        GMountUnmountFlags   flags,
318                        GCancellable        *cancellable,
319                        GAsyncReadyCallback  callback,
320                        gpointer             user_data)
321 {
322 }
323
324 static gboolean
325 g_win32_mount_unmount_finish (GMount        *mount,
326                               GAsyncResult  *result,
327                               GError       **error)
328 {
329   return FALSE;
330 }
331
332 static void
333 g_win32_mount_eject (GMount              *mount,
334                      GMountUnmountFlags   flags,
335                      GCancellable        *cancellable,
336                      GAsyncReadyCallback  callback,
337                      gpointer             user_data)
338 {
339 }
340
341 static gboolean
342 g_win32_mount_eject_finish (GMount        *mount,
343                             GAsyncResult  *result,
344                             GError       **error)
345 {
346   return FALSE;
347 }
348
349 static void
350 g_win32_mount_mount_iface_init (GMountIface *iface)
351 {
352   iface->get_root = g_win32_mount_get_root;
353   iface->get_name = g_win32_mount_get_name;
354   iface->get_icon = g_win32_mount_get_icon;
355   iface->get_symbolic_icon = g_win32_mount_get_symbolic_icon;
356   iface->get_uuid = g_win32_mount_get_uuid;
357   iface->get_drive = g_win32_mount_get_drive;
358   iface->get_volume = g_win32_mount_get_volume;
359   iface->can_unmount = g_win32_mount_can_unmount;
360   iface->can_eject = g_win32_mount_can_eject;
361   iface->unmount = g_win32_mount_unmount;
362   iface->unmount_finish = g_win32_mount_unmount_finish;
363   iface->eject = g_win32_mount_eject;
364   iface->eject_finish = g_win32_mount_eject_finish;
365 }