Blender V5.3
fsmenu_system_win32.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10/* Need to include windows.h so _WIN32_IE is defined. */
11#ifdef WIN32_LEAN_AND_MEAN
12# undef WIN32_LEAN_AND_MEAN
13#endif
14#include <windows.h>
15/* For SHGetSpecialFolderPath, has to be done before BLI_winstuff
16 * because 'near' is disabled through BLI_windstuff. */
17#include "BLI_winstuff.hh"
18#include <comdef.h>
19#include <shlobj.h>
20#include <shlwapi.h>
21#include <winnetwk.h>
22#include <wrl.h>
23
24#include "utfconv.hh"
25
26#include "MEM_guardedalloc.h"
27
28#include "BLI_path_utils.hh"
29#include "BLI_string.hh"
30
31#include "BLT_translation.hh"
32
33#include "UI_resources.hh"
34
35#include "fsmenu.hh"
36
37namespace blender {
38
39struct FSMenu;
40
41/* Add Windows Quick Access items to the System list. */
43 FSMenuCategory category,
44 FSMenuInsert flag)
45{
46 Microsoft::WRL::ComPtr<IShellDispatch> shell;
47 if (CoCreateInstance(CLSID_Shell, nullptr, CLSCTX_ALL, IID_PPV_ARGS(shell.GetAddressOf())) !=
48 S_OK)
49 {
50 return;
51 }
52
53 /* Open Quick Access folder. */
54 Microsoft::WRL::ComPtr<Folder> dir;
55 if (shell->NameSpace(_variant_t(L"shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}"),
56 dir.GetAddressOf()) != S_OK)
57 {
58 return;
59 }
60
61 /* Get FolderItems. */
62 Microsoft::WRL::ComPtr<FolderItems> items;
63 if (dir->Items(items.GetAddressOf()) != S_OK) {
64 return;
65 }
66
67 long count = 0;
68 if (items->get_Count(&count) != S_OK) {
69 return;
70 }
71
72 /* Iterate through the folder. */
73 for (long i = 0; i < count; i++) {
74 Microsoft::WRL::ComPtr<FolderItem> item;
75
76 if (items->Item(_variant_t(i), item.GetAddressOf()) != S_OK) {
77 continue;
78 }
79
80 VARIANT_BOOL isFolder;
81 /* Skip if it's not a folder. */
82 if (item->get_IsFolder(&isFolder) != S_OK || isFolder == VARIANT_FALSE) {
83 continue;
84 }
85
86 _bstr_t path;
87 if (item->get_Path(path.GetAddress()) != S_OK) {
88 continue;
89 }
90
91 char utf_path[FILE_MAXDIR];
92 conv_utf_16_to_8(path, utf_path, FILE_MAXDIR);
93
94 /* Despite the above IsFolder check, Windows considers libraries and archives to be folders.
95 * However, as Blender does not support opening them, they must be filtered out. #138863. */
96 const char *ext_folderlike[] = {
97 ".library-ms",
98 ".zip",
99 ".rar",
100 ".7z",
101 ".tar",
102 ".gz",
103 ".bz2",
104 ".zst",
105 ".xz",
106 ".cab",
107 ".iso",
108 nullptr,
109 };
110 if (!BLI_path_extension_check_array(utf_path, ext_folderlike)) {
111 /* Add folder to the fsmenu. */
112 fsmenu_insert_entry(fsmenu, category, utf_path, NULL, ICON_FILE_FOLDER, flag);
113 }
114 }
115}
116
117/* Add a Windows known folder path to the System list. */
119 FSMenuCategory category,
120 REFKNOWNFOLDERID rfid,
121 const char *name,
122 const int icon,
123 FSMenuInsert flag)
124{
125 LPWSTR pPath;
126 char line[FILE_MAXDIR];
127 if (SHGetKnownFolderPath(rfid, 0, nullptr, &pPath) == S_OK) {
128 conv_utf_16_to_8(pPath, line, FILE_MAXDIR);
129 fsmenu_insert_entry(fsmenu, category, line, name, icon, flag);
130 }
131 CoTaskMemFree(pPath);
132}
133
134static int fsmenu_external_drive_icon(char drive_letter)
135{
136 bool is_removable = false; /* ZIP, JAZ, CDROM, MO, etc. instead of a HDD. */
137 bool is_hotplug = false; /* 1394, USB, etc. */
138 bool is_usb = false; /* USB bus. */
139 char volumeName[8] = "";
140 SNPRINTF(volumeName, "\\\\.\\%c:", drive_letter);
141 HANDLE volume = ::CreateFile(volumeName, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
142
143 if (volume != INVALID_HANDLE_VALUE) {
144 STORAGE_HOTPLUG_INFO Info = {0};
145 DWORD bytesReturned = 0;
146 if (::DeviceIoControl(volume,
147 IOCTL_STORAGE_GET_HOTPLUG_INFO,
148 0,
149 0,
150 &Info,
151 sizeof(Info),
152 &bytesReturned,
153 NULL))
154 {
155 is_removable = Info.MediaRemovable != 0;
156 is_hotplug = Info.DeviceHotplug != 0;
157 }
158
159 STORAGE_PROPERTY_QUERY Prop;
160 Prop.PropertyId = StorageDeviceProperty;
161 Prop.QueryType = PropertyStandardQuery;
162 Prop.AdditionalParameters[0] = 0;
163 STORAGE_DEVICE_DESCRIPTOR DevInfo = {0};
164 if (::DeviceIoControl(volume,
165 IOCTL_STORAGE_QUERY_PROPERTY,
166 &Prop,
167 sizeof(Prop),
168 &DevInfo,
169 sizeof(DevInfo),
170 &bytesReturned,
171 NULL))
172 {
173 is_usb = (DevInfo.BusType == BusTypeUsb);
174 }
175 ::CloseHandle(volume);
176 }
177
178 return (is_removable && is_hotplug && is_usb) ? ICON_USB_DRIVE : ICON_EXTERNAL_DRIVE;
179}
180
181void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
182{
183 char line[FILE_MAXDIR];
184 /* Add the drive names to the listing */
185 wchar_t wline[FILE_MAXDIR];
186 __int64 tmp;
187 char tmps[4], *name;
188
189 tmp = GetLogicalDrives();
190
191 for (int i = 0; i < 26; i++) {
192 if ((tmp >> i) & 1) {
193 tmps[0] = 'A' + i;
194 tmps[1] = ':';
195 tmps[2] = '\\';
196 tmps[3] = '\0';
197 name = nullptr;
198
199 /* Skip over floppy disks A & B. */
200 if (i > 1) {
201 /* Friendly volume descriptions without using SHGetFileInfoW (#85689). */
202 conv_utf_8_to_16(tmps, wline, 4);
203 IShellFolder *desktop;
204 if (SHGetDesktopFolder(&desktop) == S_OK) {
205 PIDLIST_RELATIVE volume;
206 if (desktop->ParseDisplayName(nullptr, nullptr, wline, nullptr, &volume, nullptr) ==
207 S_OK)
208 {
209 STRRET volume_name;
210 volume_name.uType = STRRET_WSTR;
211 if (desktop->GetDisplayNameOf(volume, SHGDN_FORADDRESSBAR, &volume_name) == S_OK) {
212 wchar_t *volume_name_wchar;
213 if (StrRetToStrW(&volume_name, volume, &volume_name_wchar) == S_OK) {
214 conv_utf_16_to_8(volume_name_wchar, line, FILE_MAXDIR);
215 name = line;
216 CoTaskMemFree(volume_name_wchar);
217 }
218 }
219 CoTaskMemFree(volume);
220 }
221 desktop->Release();
222 }
223 }
224 if (name == nullptr) {
225 name = tmps;
226 }
227
228 int icon = ICON_DISK_DRIVE;
229 switch (GetDriveType(tmps)) {
230 case DRIVE_REMOVABLE:
231 icon = fsmenu_external_drive_icon('A' + i);
232 break;
233 case DRIVE_CDROM:
234 icon = ICON_DISC;
235 break;
236 case DRIVE_FIXED: {
237 DWORD fileSystemFlags;
238 const bool is_cloud =
239 (GetVolumeInformation(
240 tmps, nullptr, 0, nullptr, nullptr, &fileSystemFlags, nullptr, 0) &&
241 (fileSystemFlags & FILE_SUPPORTS_REMOTE_STORAGE));
242 icon = is_cloud ? ICON_INTERNET : ICON_DISK_DRIVE;
243 break;
244 }
245 case DRIVE_RAMDISK:
246 icon = ICON_DISK_DRIVE;
247 break;
248 case DRIVE_REMOTE: {
249 char lpLocalName[] = {static_cast<char>('A' + i), ':', 0};
250 char lpRemoteName[MAX_PATH];
251 DWORD lpnLength = sizeof(lpRemoteName);
252 const bool is_cloud = (WNetGetConnection(lpLocalName, lpRemoteName, &lpnLength) ==
253 NO_ERROR &&
254 STRPREFIX(lpRemoteName, "https:"));
255 icon = is_cloud ? ICON_INTERNET : ICON_NETWORK_DRIVE;
256 break;
257 }
258 default:
259 icon = ICON_DISK_DRIVE;
260 break;
261 }
262
264 fsmenu, FS_CATEGORY_SYSTEM, tmps, name, icon, FSMenuInsert(FS_INSERT_SORTED));
265 }
266 }
267
268 /* Get Special Folder Locations. */
269 if (read_bookmarks) {
270
271 /* These items are shown in System List. */
274 FOLDERID_Profile,
275 N_("Home"),
276 ICON_HOME,
280 FOLDERID_Desktop,
281 N_("Desktop"),
282 ICON_DESKTOP,
286 FOLDERID_Documents,
287 N_("Documents"),
288 ICON_DOCUMENTS,
292 FOLDERID_Downloads,
293 N_("Downloads"),
294 ICON_IMPORT,
298 FOLDERID_Music,
299 N_("Music"),
300 ICON_FILE_SOUND,
304 FOLDERID_Pictures,
305 N_("Pictures"),
306 ICON_FILE_IMAGE,
310 FOLDERID_Videos,
311 N_("Videos"),
312 ICON_FILE_MOVIE,
316 FOLDERID_Fonts,
317 N_("Fonts"),
318 ICON_FILE_FONT,
322 FOLDERID_SkyDrive,
323 N_("OneDrive"),
324 ICON_INTERNET,
326
327 /* These items are just put in path cache for thumbnail views and if bookmarked. */
329 fsmenu, FS_CATEGORY_OTHER, FOLDERID_UserProfiles, nullptr, ICON_COMMUNITY, FS_INSERT_LAST);
330
331 /* Last add Quick Access items to avoid duplicates and use icons if available. */
333 }
334}
335
336} // namespace blender
#define FILE_MAXDIR
#define SNPRINTF(dst, format,...)
Compatibility-like things for windows.
Read Blender memory allocation and freeing.
#define STRPREFIX(a, b)
#define L
BMesh Iterators.
static void fsmenu_add_windows_folder(FSMenu *fsmenu, FSMenuCategory category, REFKNOWNFOLDERID rfid, const char *name, const int icon, FSMenuInsert flag)
static int fsmenu_external_drive_icon(char drive_letter)
static void fsmenu_add_windows_quick_access(FSMenu *fsmenu, FSMenuCategory category, FSMenuInsert flag)
void fsmenu_insert_entry(FSMenu *fsmenu, FSMenuCategory category, const char *path, const char *name, int icon, FSMenuInsert flag)
Definition fsmenu.cc:229
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
@ FS_CATEGORY_SYSTEM_BOOKMARKS
void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition utfconv.cc:182
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116
#define N_(msgid)