Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gwin32volumemonitor.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
30 #include <glib.h>
31 #include "glibintl.h"
32
33 #include "gwin32volumemonitor.h"
34 #include "gwin32mount.h"
35 #include "gmount.h"
36 #include "giomodule.h"
37
38 #define _WIN32_WINNT 0x0500
39 #include <windows.h>
40
41 struct _GWin32VolumeMonitor {
42   GNativeVolumeMonitor parent;
43
44   GList *volumes;
45   GList *mounts;
46 };
47
48 #define g_win32_volume_monitor_get_type _g_win32_volume_monitor_get_type
49 G_DEFINE_TYPE_WITH_CODE (GWin32VolumeMonitor, g_win32_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
50                          g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
51                                                          g_define_type_id,
52                                                          "win32",
53                                                          0));
54                                                          
55 static void
56 g_win32_volume_monitor_finalize (GObject *object)
57 {
58   GWin32VolumeMonitor *monitor;
59   
60   monitor = G_WIN32_VOLUME_MONITOR (object);
61
62   if (G_OBJECT_CLASS (g_win32_volume_monitor_parent_class)->finalize)
63     (*G_OBJECT_CLASS (g_win32_volume_monitor_parent_class)->finalize) (object);
64 }
65
66 /*
67  * get_viewable_logical_drives:
68  *
69  * Returns the list of logical and viewable drives as defined by
70  * GetLogicalDrives() and the registry keys
71  * Software\Microsoft\Windows\CurrentVersion\Policies\Explorer under
72  * HKLM or HKCU. If neither key exists the result of
73  * GetLogicalDrives() is returned.
74  *
75  * Return value: bitmask with same meaning as returned by GetLogicalDrives()
76  */
77 static guint32 
78 get_viewable_logical_drives (void)
79 {
80   guint viewable_drives = GetLogicalDrives ();
81   HKEY key;
82
83   DWORD var_type = REG_DWORD; //the value's a REG_DWORD type
84   DWORD no_drives_size = 4;
85   DWORD no_drives;
86   gboolean hklm_present = FALSE;
87
88   if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
89                     "Software\\Microsoft\\Windows\\"
90                     "CurrentVersion\\Policies\\Explorer",
91                     0, KEY_READ, &key) == ERROR_SUCCESS)
92     {
93       if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
94                            (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
95         {
96           /* We need the bits that are set in viewable_drives, and
97            * unset in no_drives.
98            */
99           viewable_drives = viewable_drives & ~no_drives;
100           hklm_present = TRUE;
101         }
102       RegCloseKey (key);
103     }
104
105   /* If the key is present in HKLM then the one in HKCU should be ignored */
106   if (!hklm_present)
107     {
108       if (RegOpenKeyEx (HKEY_CURRENT_USER,
109                         "Software\\Microsoft\\Windows\\"
110                         "CurrentVersion\\Policies\\Explorer",
111                         0, KEY_READ, &key) == ERROR_SUCCESS)
112         {
113           if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
114                                (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
115             {
116               viewable_drives = viewable_drives & ~no_drives;
117             }
118           RegCloseKey (key);
119         }
120     }
121
122   return viewable_drives; 
123 }
124
125 /* deliver accesible (aka 'mounted') volumes */
126 static GList *
127 get_mounts (GVolumeMonitor *volume_monitor)
128 {
129   GWin32VolumeMonitor *monitor;
130   DWORD   drives;
131   gchar   drive[4] = "A:\\";
132   GList *list = NULL;
133   
134   monitor = G_WIN32_VOLUME_MONITOR (volume_monitor);
135
136   drives = get_viewable_logical_drives ();
137
138   if (!drives)
139     g_warning ("get_viewable_logical_drives failed.");
140
141   while (drives && drive[0] <= 'Z')
142     {
143       if (drives & 1)
144       {
145         list = g_list_prepend (list, _g_win32_mount_new (volume_monitor, drive, NULL));
146       }
147       drives >>= 1;
148       drive[0]++;
149     }
150   return list;
151 }
152
153 /* actually 'mounting' volumes is out of GIOs business on win32, so no volumes are delivered either */
154 static GList *
155 get_volumes (GVolumeMonitor *volume_monitor)
156 {
157   GWin32VolumeMonitor *monitor;
158   GList *l = NULL;
159   
160   monitor = G_WIN32_VOLUME_MONITOR (volume_monitor);
161
162   return l;
163 }
164
165 /* real hardware */
166 static GList *
167 get_connected_drives (GVolumeMonitor *volume_monitor)
168 {
169   GWin32VolumeMonitor *monitor;
170   HANDLE  find_handle;
171   BOOL    found;
172   wchar_t wc_name[MAX_PATH+1];
173   GList *list = NULL;
174   
175   monitor = G_WIN32_VOLUME_MONITOR (volume_monitor);
176
177 #if 0
178   find_handle = FindFirstVolumeW (wc_name, MAX_PATH);
179   found = (find_handle != INVALID_HANDLE_VALUE);
180   while (found)
181     {
182       /* I don't know what this code is supposed to do; clearly it now
183        * does nothing, the returned GList is always NULL. But what was
184        * this code supposed to be a start of? The volume names that
185        * the FindFirstVolume/FindNextVolume loop iterates over returns
186        * device names like
187        *
188        *   \Device\HarddiskVolume1
189        *   \Device\HarddiskVolume2
190        *   \Device\CdRom0
191        *
192        * No DOS devices there, so I don't see the point with the
193        * QueryDosDevice call below. Probably this code is confusing volumes
194        * with something else that does contain the mapping from DOS devices
195        * to volumes.
196        */
197       wchar_t wc_dev_name[MAX_PATH+1];
198       guint trailing = wcslen (wc_name) - 1;
199
200       /* remove trailing backslash and leading \\?\\ */
201       wc_name[trailing] = L'\0';
202       if (QueryDosDeviceW (&wc_name[4], wc_dev_name, MAX_PATH))
203         {
204           gchar *name = g_utf16_to_utf8 (wc_dev_name, -1, NULL, NULL, NULL);
205           g_print ("%s\n", name);
206           g_free (name);
207         }
208
209       found = FindNextVolumeW (find_handle, wc_name, MAX_PATH);
210     }
211   if (find_handle != INVALID_HANDLE_VALUE)
212     FindVolumeClose (find_handle);
213 #endif
214
215   return list;
216 }
217
218 static GVolume *
219 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
220 {
221   return NULL;
222 }
223
224 static GMount *
225 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
226 {
227   return NULL;
228 }
229
230 static gboolean
231 is_supported (void)
232 {
233   return TRUE;
234 }
235
236 static GMount *
237 get_mount_for_mount_path (const char *mount_path,
238                           GCancellable *cancellable)
239 {
240   GWin32Mount *mount;
241
242   /* TODO: Set mountable volume? */
243   mount = _g_win32_mount_new (NULL, mount_path, NULL);
244
245   return G_MOUNT (mount);
246 }
247
248 static void
249 g_win32_volume_monitor_class_init (GWin32VolumeMonitorClass *klass)
250 {
251   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
252   GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
253   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
254   
255   gobject_class->finalize = g_win32_volume_monitor_finalize;
256
257   monitor_class->get_mounts = get_mounts;
258   monitor_class->get_volumes = get_volumes;
259   monitor_class->get_connected_drives = get_connected_drives;
260   monitor_class->get_volume_for_uuid = get_volume_for_uuid;
261   monitor_class->get_mount_for_uuid = get_mount_for_uuid;
262   monitor_class->is_supported = is_supported;
263
264   native_class->get_mount_for_mount_path = get_mount_for_mount_path;
265 }
266
267 static void
268 g_win32_volume_monitor_init (GWin32VolumeMonitor *win32_monitor)
269 {
270   /* maybe we shoud setup a callback window to listern for WM_DEVICECHANGE ? */
271 #if 0
272   unix_monitor->mount_monitor = g_win32_mount_monitor_new ();
273
274   g_signal_connect (win32_monitor->mount_monitor,
275                     "mounts-changed", G_CALLBACK (mounts_changed),
276                     win32_monitor);
277   
278   g_signal_connect (win32_monitor->mount_monitor,
279                     "mountpoints-changed", G_CALLBACK (mountpoints_changed),
280                     win32_monitor);
281                     
282   update_volumes (win32_monitor);
283   update_mounts (win32_monitor);
284 #endif
285 }