From 8fa44842da7d806c56804158a7cee9e3cd0d8884 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Mon, 22 Oct 2018 23:07:44 +0200 Subject: [PATCH] Some improvements to OS::known_folder() function. --- src/OSSupport_Win32.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index 8d88d29..47b79c5 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -728,7 +728,7 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id) { if(g_known_folders_map->contains(folderId)) { - return g_known_folders_map->operator[](folderId); + return (*g_known_folders_map)[folderId]; } } @@ -741,7 +741,7 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id) { if(g_known_folders_map->contains(folderId)) { - return g_known_folders_map->operator[](folderId); + return (*g_known_folders_map)[folderId]; } } @@ -753,14 +753,14 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id) QString folderPath; - //Now try to get the folder path! + //Try SHGetKnownFolderPath() first! if(const SHGetKnownFolderPath_t known_folders_fpGetKnownFolderPath = MUtils::Win32Utils::resolve(QLatin1String("shell32"), QLatin1String("SHGetKnownFolderPath"))) { WCHAR *path = NULL; if(known_folders_fpGetKnownFolderPath(s_folders[folderId].guid, KF_FLAG_CREATE, NULL, &path) == S_OK) { //MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST); - QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path))); + const QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path))); if(folderTemp.exists()) { folderPath = folderTemp.canonicalPath(); @@ -768,23 +768,33 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id) CoTaskMemFree(path); } } - else if(const SHGetFolderPath_t known_folders_fpGetFolderPath = MUtils::Win32Utils::resolve(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW"))) + + //Fall back to SHGetFolderPathW() + if (folderPath.isEmpty()) { - QScopedArrayPointer path(new WCHAR[4096]); - if(known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK) + if (const SHGetFolderPath_t known_folders_fpGetFolderPath = MUtils::Win32Utils::resolve(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW"))) { - //MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST); - QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path.data()))); - if(folderTemp.exists()) + QScopedArrayPointer path(new WCHAR[4096]); + if (known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK) { - folderPath = folderTemp.canonicalPath(); + //MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST); + const QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path.data()))); + if (folderTemp.exists()) + { + folderPath = folderTemp.canonicalPath(); + } } } } //Update cache - g_known_folders_map->insert(folderId, folderPath); - return g_known_folders_map->operator[](folderId); + if (!folderPath.isEmpty()) + { + qWarning("g_known_folders_map->insert"); + g_known_folders_map->insert(folderId, folderPath); + } + + return folderPath; } /////////////////////////////////////////////////////////////////////////////// -- 2.11.0