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