Various improvements to known_folder() function, again.

This commit is contained in:
LoRd_MuldeR 2019-05-18 18:32:58 +02:00
parent 1ea1c23d12
commit aa434dd678
5 changed files with 65 additions and 38 deletions

View File

@ -105,12 +105,24 @@ namespace MUtils
*/ */
typedef enum typedef enum
{ {
FOLDER_ROAMING_DATA = 0, ///< Application-specific data FOLDER_PROFILE_USER = 1, ///< The user's profile folder
FOLDER_LOCALAPPDATA = 1, ///< Local application data (non-roaming) FOLDER_PROFILE_PUBL = 2, ///< The "all users" profile folder
FOLDER_USER_PROFILE = 2, ///< The user's profile folder FOLDER_APPDATA_ROAM = 3, ///< Application-specific data
FOLDER_PROGRAMFILES = 3, ///< Program files FOLDER_APPDATA_LOCA = 4, ///< Local application data (non-roaming)
FOLDER_SYSTEMFOLDER = 4, ///< System directory FOLDER_DOCS_USER = 5, ///< The user's Documents directory
FOLDER_SYSTROOT_DIR = 5 ///< System "root" directory FOLDER_DOCS_PUBL = 6, ///< The "all users" Documents directory
FOLDER_DESKTOP_USER = 7, ///< The user's Desktop directory
FOLDER_DESKTOP_PUBL = 8, ///< The "all users" Desktop directory
FOLDER_PICTURES_USER = 9, ///< The user's Music directory
FOLDER_PICTURES_PUBL = 10, ///< The user's Music directory
FOLDER_MUSIC_USER = 11, ///< The user's Music directory
FOLDER_MUSIC_PUBL = 12, ///< The "all users" Music directory
FOLDER_VIDEO_USER = 13, ///< The user's Video directory
FOLDER_VIDEO_PUBL = 14, ///< The "all users" Video directory
FOLDER_PROGRAMFILES = 15, ///< Program files
FOLDER_SYSROOT = 16, ///< System "root" directory
FOLDER_SYSTEM_DEF = 17, ///< System directory
FOLDER_SYSTEM_X86 = 18, ///< System directory for x86 (32-Bit)
} }
known_folder_t; known_folder_t;

View File

@ -74,7 +74,7 @@ namespace MUtils
#error Compiler version is not supported yet! #error Compiler version is not supported yet!
#endif #endif
#elif (_MSC_VER == 1916) #elif (_MSC_VER == 1916)
#if((_MSC_FULL_VER >= 191627024) && (_MSC_FULL_VER <= 191627030)) #if((_MSC_FULL_VER >= 191627024) && (_MSC_FULL_VER <= 191627031))
"MSVC 2017.9"; "MSVC 2017.9";
#else #else
#error Compiler version is not supported yet! #error Compiler version is not supported yet!

View File

@ -420,7 +420,7 @@ const QString &MUtils::temp_folder(void)
} }
qWarning("%%TEMP%% directory not found -> trying fallback mode now!"); qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR }; static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_APPDATA_LOCA, OS::FOLDER_SYSROOT };
for(size_t id = 0; id < 2; id++) for(size_t id = 0; id < 2; id++)
{ {
const QString &knownFolder = OS::known_folder(FOLDER_ID[id]); const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
@ -572,7 +572,7 @@ void MUtils::init_process(QProcess &process, const QString &wokringDir, const bo
} }
//Set up system root directory //Set up system root directory
const QString sysRoot = QDir::toNativeSeparators(OS::known_folder(OS::FOLDER_SYSTROOT_DIR)); const QString sysRoot = QDir::toNativeSeparators(OS::known_folder(OS::FOLDER_SYSROOT));
if (!sysRoot.isEmpty()) if (!sysRoot.isEmpty())
{ {
for (const char *const *ptr = ENVVAR_NAMES_SYS; *ptr; ++ptr) for (const char *const *ptr = ENVVAR_NAMES_SYS; *ptr; ++ptr)

View File

@ -695,21 +695,36 @@ typedef HRESULT (WINAPI *SHGetFolderPathProc)(HWND hwndOwner, int nFolder, HANDL
static const struct static const struct
{ {
MUtils::OS::known_folder_t id;
INT32 csidl; INT32 csidl;
GUID kfuid; GUID known_folder_id;
} }
s_known_folders_lut[] = s_known_folders_lut[] =
{ {
{ 0x001A, { 0x3EB685DB, 0x65F9, 0x4CF6, { 0xA0, 0x3A, 0xE3, 0xEF, 0x65, 0x72, 0x9F, 0x3D } } }, //CSIDL_APPDATA { MUtils::OS::FOLDER_PROFILE_USER, 0x0028, { 0x5E6C858F, 0x0E22, 0x4760, { 0x9A, 0xFE, 0xEA, 0x33, 0x17, 0xB6, 0x71, 0x73 } } }, //CSIDL_PROFILE
{ 0x001C, { 0xF1B32785, 0x6FBA, 0x4FCF, { 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91 } } }, //CSIDL_LOCAL_APPDATA { MUtils::OS::FOLDER_PROFILE_PUBL, 0x0000, { 0XDFDF76A2, 0XC82A, 0X4D63, { 0X90, 0X6A, 0X56, 0X44, 0XAC, 0X45, 0X73, 0X85 } } }, //FOLDERID_Public
{ 0x0028, { 0x5E6C858F, 0x0E22, 0x4760, { 0x9A, 0xFE, 0xEA, 0x33, 0x17, 0xB6, 0x71, 0x73 } } }, //CSIDL_PROFILE { MUtils::OS::FOLDER_APPDATA_ROAM, 0x001A, { 0x3EB685DB, 0x65F9, 0x4CF6, { 0xA0, 0x3A, 0xE3, 0xEF, 0x65, 0x72, 0x9F, 0x3D } } }, //CSIDL_APPDATA
{ 0x0026, { 0x905E63B6, 0xC1BF, 0x494E, { 0xB2, 0x9C, 0x65, 0xB7, 0x32, 0xD3, 0xD2, 0x1A } } }, //CSIDL_PROGRAM_FILES { MUtils::OS::FOLDER_APPDATA_LOCA, 0x001C, { 0xF1B32785, 0x6FBA, 0x4FCF, { 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91 } } }, //CSIDL_LOCAL_APPDATA
{ 0x0025, { 0x1AC14E77, 0x02E7, 0x4E5D, { 0xB7, 0x44, 0x2E, 0xB1, 0xAE, 0x51, 0x98, 0xB7 } } }, //CSIDL_SYSTEM_FOLDER { MUtils::OS::FOLDER_DOCS_USER, 0x0005, { 0xFDD39AD0, 0x238F, 0x46AF, { 0xAD, 0xB4, 0x6C, 0x85, 0x48, 0x03, 0x69, 0xC7 } } }, //CSIDL_MYDOCUMENTS
{ 0x0024, { 0xF38BF404, 0x1D43, 0x42F2, { 0x93, 0x05, 0x67, 0xDE, 0x0B, 0x28, 0xFC, 0x23 } } }, //CSIDL_WINDOWS_FOLDER { MUtils::OS::FOLDER_DOCS_PUBL, 0x002E, { 0xED4824AF, 0xDCE4, 0x45A8, { 0x81, 0xE2, 0xFC, 0x79, 0x65, 0x08, 0x36, 0x34 } } }, //CSIDL_COMMON_DOCUMENTS
{ MUtils::OS::FOLDER_DESKTOP_USER, 0x0010, { 0xB4BFCC3A, 0xDB2C, 0x424C, { 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41 } } }, //CSIDL_DESKTOPDIRECTORY
{ MUtils::OS::FOLDER_DESKTOP_PUBL, 0x0019, { 0xC4AA340D, 0xF20F, 0x4863, { 0xAF, 0xEF, 0xF8, 0x7E, 0xF2, 0xE6, 0xBA, 0x25 } } }, //CSIDL_COMMON_DESKTOPDIRECTORY
{ MUtils::OS::FOLDER_PICTURES_USER, 0x0027, { 0X33E28130, 0X4E1E, 0X4676, { 0X83, 0X5A, 0X98, 0X39, 0X5C, 0X3B, 0XC3, 0XBB } } }, //CSIDL_MYPICTURES
{ MUtils::OS::FOLDER_PICTURES_PUBL, 0x0036, { 0XB6EBFB86, 0X6907, 0X413C, { 0X9A, 0XF7, 0X4F, 0XC2, 0XAB, 0XF0, 0X7C, 0XC5 } } }, //CSIDL_COMMON_PICTURES
{ MUtils::OS::FOLDER_MUSIC_USER, 0x000D, { 0X4BD8D571, 0X6D19, 0X48D3, { 0XBE, 0X97, 0X42, 0X22, 0X20, 0X08, 0X0E, 0X43 } } }, //CSIDL_MYMUSIC
{ MUtils::OS::FOLDER_MUSIC_PUBL, 0x0035, { 0X3214FAB5, 0X9757, 0X4298, { 0XBB, 0X61, 0X92, 0XA9, 0XDE, 0XAA, 0X44, 0XFF } } }, //CSIDL_COMMON_MUSIC
{ MUtils::OS::FOLDER_VIDEO_USER, 0X000E, { 0X18989B1D, 0X99B5, 0X455B, { 0X84, 0X1C, 0XAB, 0X7C, 0X74, 0XE4, 0XDD, 0XFC } } }, //CSIDL_MYVIDEO
{ MUtils::OS::FOLDER_VIDEO_PUBL, 0x0037, { 0X2400183A, 0X6185, 0X49FB, { 0XA2, 0XD8, 0X4A, 0X39, 0X2A, 0X60, 0X2B, 0XA3 } } }, //CSIDL_COMMON_VIDEO
{ MUtils::OS::FOLDER_PROGRAMFILES, 0x0026, { 0x905E63B6, 0xC1BF, 0x494E, { 0xB2, 0x9C, 0x65, 0xB7, 0x32, 0xD3, 0xD2, 0x1A } } }, //CSIDL_PROGRAM_FILES
{ MUtils::OS::FOLDER_SYSROOT, 0x0024, { 0xF38BF404, 0x1D43, 0x42F2, { 0x93, 0x05, 0x67, 0xDE, 0x0B, 0x28, 0xFC, 0x23 } } }, //CSIDL_WINDOWS
{ MUtils::OS::FOLDER_SYSTEM_DEF, 0x0025, { 0x1AC14E77, 0x02E7, 0x4E5D, { 0xB7, 0x44, 0x2E, 0xB1, 0xAE, 0x51, 0x98, 0xB7 } } }, //CSIDL_SYSTEM
{ MUtils::OS::FOLDER_SYSTEM_X86, 0x0029, { 0xD65231B0, 0xB2F1, 0x4857, { 0xA4, 0xCE, 0xA8, 0xE7, 0xC6, 0xEA, 0x7D, 0x27 } } }, //CSIDL_SYSTEMX86
{ static_cast<MUtils::OS::known_folder_t>(0) }
}; };
static QString known_folder_verify(const wchar_t *const path) static QString known_folder_verify(const wchar_t *const path)
{ {
CSIDL_PROFILE;
const QDir folderPath = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path))); const QDir folderPath = QDir(QDir::fromNativeSeparators(MUTILS_QSTR(path)));
if (folderPath.exists()) if (folderPath.exists())
{ {
@ -741,6 +756,8 @@ static QString known_folder_fallback(const size_t folderId)
MAXINT32 MAXINT32
}; };
if (s_known_folders_lut[folderId].csidl)
{
const SHGetFolderPathProc getFolderPath = MUtils::Win32Utils::resolve<SHGetFolderPathProc>(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW")); const SHGetFolderPathProc getFolderPath = MUtils::Win32Utils::resolve<SHGetFolderPathProc>(QLatin1String("shell32"), QLatin1String("SHGetFolderPathW"));
if (getFolderPath) if (getFolderPath)
{ {
@ -761,6 +778,7 @@ static QString known_folder_fallback(const size_t folderId)
} }
} }
} }
}
return QString(); /*failed!*/ return QString(); /*failed!*/
} }
@ -790,7 +808,7 @@ static QString known_folder_detect(const size_t folderId)
for (size_t i = 0; s_kfFlags[i] != MAXDWORD; ++i) for (size_t i = 0; s_kfFlags[i] != MAXDWORD; ++i)
{ {
WCHAR* path = NULL; WCHAR* path = NULL;
if (getKnownFolderPath(s_known_folders_lut[folderId].kfuid, s_kfFlags[i], NULL, &path) == S_OK) if (getKnownFolderPath(s_known_folders_lut[folderId].known_folder_id, s_kfFlags[i], NULL, &path) == S_OK)
{ {
//MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST); //MessageBoxW(0, path, L"SHGetKnownFolderPath", MB_TOPMOST);
const QString folderPath = known_folder_verify(path); const QString folderPath = known_folder_verify(path);
@ -808,18 +826,15 @@ static QString known_folder_detect(const size_t folderId)
static size_t known_folder_decode(const MUtils::OS::known_folder_t folder_id) static size_t known_folder_decode(const MUtils::OS::known_folder_t folder_id)
{ {
switch (folder_id) for (size_t i = 0; s_known_folders_lut[i].id; ++i)
{ {
case MUtils::OS::FOLDER_ROAMING_DATA: return 0U; if (s_known_folders_lut[i].id == folder_id)
case MUtils::OS::FOLDER_LOCALAPPDATA: return 1U; {
case MUtils::OS::FOLDER_USER_PROFILE: return 2U; return i;
case MUtils::OS::FOLDER_PROGRAMFILES: return 3U; }
case MUtils::OS::FOLDER_SYSTEMFOLDER: return 4U; }
case MUtils::OS::FOLDER_SYSTROOT_DIR: return 5U;
default:
qWarning("Invalid 'known' folder was requested!"); qWarning("Invalid 'known' folder was requested!");
return SIZE_MAX; return SIZE_MAX;
}
} }
const QString &MUtils::OS::known_folder(const known_folder_t folder_id) const QString &MUtils::OS::known_folder(const known_folder_t folder_id)

View File

@ -122,7 +122,7 @@ bool MUtils::Sound::play_sound_file(const QString &library, const unsigned short
QFileInfo libraryFile(library); QFileInfo libraryFile(library);
if(!libraryFile.isAbsolute()) if(!libraryFile.isAbsolute())
{ {
const QString &systemDir = MUtils::OS::known_folder(MUtils::OS::FOLDER_SYSTEMFOLDER); const QString &systemDir = MUtils::OS::known_folder(MUtils::OS::FOLDER_SYSTEM_DEF);
if(!systemDir.isEmpty()) if(!systemDir.isEmpty())
{ {
libraryFile.setFile(QDir(systemDir), libraryFile.fileName()); libraryFile.setFile(QDir(systemDir), libraryFile.fileName());