Some improvements to OS::known_folder() function.

This commit is contained in:
LoRd_MuldeR 2018-10-22 23:07:44 +02:00
parent 6d39b858d4
commit 8fa44842da

View File

@ -728,7 +728,7 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id)
{ {
if(g_known_folders_map->contains(folderId)) 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)) 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; QString folderPath;
//Now try to get the folder path! //Try SHGetKnownFolderPath() first!
if(const SHGetKnownFolderPath_t known_folders_fpGetKnownFolderPath = MUtils::Win32Utils::resolve<SHGetKnownFolderPath_t>(QLatin1String("shell32"), QLatin1String("SHGetKnownFolderPath"))) if(const SHGetKnownFolderPath_t known_folders_fpGetKnownFolderPath = MUtils::Win32Utils::resolve<SHGetKnownFolderPath_t>(QLatin1String("shell32"), QLatin1String("SHGetKnownFolderPath")))
{ {
WCHAR *path = NULL; WCHAR *path = NULL;
if(known_folders_fpGetKnownFolderPath(s_folders[folderId].guid, KF_FLAG_CREATE, NULL, &path) == S_OK) if(known_folders_fpGetKnownFolderPath(s_folders[folderId].guid, KF_FLAG_CREATE, NULL, &path) == S_OK)
{ {
//MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST); //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()) if(folderTemp.exists())
{ {
folderPath = folderTemp.canonicalPath(); folderPath = folderTemp.canonicalPath();
@ -768,23 +768,33 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id)
CoTaskMemFree(path); CoTaskMemFree(path);
} }
} }
else if(const SHGetFolderPath_t known_folders_fpGetFolderPath = MUtils::Win32Utils::resolve<SHGetFolderPath_t>(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW")))
//Fall back to SHGetFolderPathW()
if (folderPath.isEmpty())
{ {
QScopedArrayPointer<WCHAR> path(new WCHAR[4096]); if (const SHGetFolderPath_t known_folders_fpGetFolderPath = MUtils::Win32Utils::resolve<SHGetFolderPath_t>(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW")))
if(known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK)
{ {
//MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST); QScopedArrayPointer<WCHAR> path(new WCHAR[4096]);
QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path.data()))); if (known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK)
if(folderTemp.exists())
{ {
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 //Update cache
g_known_folders_map->insert(folderId, folderPath); if (!folderPath.isEmpty())
return g_known_folders_map->operator[](folderId); {
qWarning("g_known_folders_map->insert");
g_known_folders_map->insert(folderId, folderPath);
}
return folderPath;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////