OSDN Git Service

Implemented known_folder() function.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 17:45:01 +0000 (18:45 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 17:45:01 +0000 (18:45 +0100)
MUtilities_VS2013.vcxproj
MUtilities_VS2013.vcxproj.filters
include/MUtils/Global.h
include/MUtils/OSSupport.h [new file with mode: 0644]
src/Global.cpp
src/OSSupport_Win32.cpp [new file with mode: 0644]

index f4d0451..f8c4cf3 100644 (file)
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="src\Global.cpp" />
+    <ClCompile Include="src\OSSupport_Win32.cpp" />
     <ClCompile Include="src\UpdateChecker.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="include\MUtils\Global.h" />
+    <ClInclude Include="include\MUtils\OSSupport.h" />
     <ClInclude Include="include\Mutils\UpdateChecker.h" />
   </ItemGroup>
   <PropertyGroup Label="Globals">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
     <IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
-    <TargetName>libMUtils-1</TargetName>
+    <TargetName>libMUtils32-1</TargetName>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     <OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
     <IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
-    <TargetName>libMUtils-1</TargetName>
+    <TargetName>libMUtils32-1</TargetName>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Static|Win32'">
     <OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
     <IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
-    <TargetName>libMUtils-1</TargetName>
+    <TargetName>libMUtils32-1</TargetName>
   </PropertyGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <ClCompile>
index 09e3505..b077129 100644 (file)
@@ -21,6 +21,9 @@
     <ClCompile Include="src\Global.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="src\OSSupport_Win32.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="include\Mutils\UpdateChecker.h">
@@ -29,5 +32,8 @@
     <ClInclude Include="include\MUtils\Global.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="include\MUtils\OSSupport.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file
index c929451..8e57887 100644 (file)
@@ -48,3 +48,17 @@ namespace MUtils
        } \
 } \
 while(0)
+
+#define MUTILS_DELETE_ARRAY(PTR) do \
+{ \
+       if((PTR)) \
+       { \
+               delete [] (PTR); \
+               (PTR) = NULL; \
+       } \
+} \
+while(0)
+
+#define MUTILS_QUTF8(STR) ((STR).toUtf8().constData())
+#define MUTILS_QSTR2WCHAR(STR) (reinterpret_cast<const wchar_t*>((STR).utf16()))
+#define MUTILS_WCHAR2QSTR(STR) (QString::fromUtf16(reinterpret_cast<const unsigned short*>((STR))))
diff --git a/include/MUtils/OSSupport.h b/include/MUtils/OSSupport.h
new file mode 100644 (file)
index 0000000..b5bcaee
--- /dev/null
@@ -0,0 +1,47 @@
+///////////////////////////////////////////////////////////////////////////////
+// MuldeR's Utilities for Qt
+// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//
+// http://www.gnu.org/licenses/lgpl-2.1.txt
+//////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include <QString>
+
+///////////////////////////////////////////////////////////////////////////////
+
+namespace MUtils
+{
+       namespace OS
+       {
+               //Known Folders IDs
+               typedef enum
+               {
+                       FOLDER_LOCALAPPDATA = 0,
+                       FOLDER_PROGRAMFILES = 2,
+                       FOLDER_SYSTEMFOLDER = 3,
+                       FOLDER_SYSTROOT_DIR = 4
+               }
+               known_folder_t;
+               
+               //Get known Folder
+               const QString &known_folder(known_folder_t folder_id);
+       }
+}
+
+///////////////////////////////////////////////////////////////////////////////
index 042cbcf..8f53d4c 100644 (file)
@@ -24,6 +24,7 @@
 #endif
 
 #include <MUtils/Global.h>
+#include <MUtils/OSSupport.h>
 
 //Qt
 #include <QDir>
@@ -205,13 +206,13 @@ const QString &MUtils::temp_folder(void)
        if(g_temp_folder_path->isEmpty())
        {
                qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
-               static const lamexp_known_folder_t folderId[2] = { lamexp_folder_localappdata, lamexp_folder_systroot_dir };
-               for(size_t id = 0; (g_lamexp_temp_folder.path->isEmpty() && (id < 2)); id++)
+               static const OS::known_folder_t folderId[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
+               for(size_t id = 0; (g_temp_folder_path->isEmpty() && (id < 2)); id++)
                {
-                       const QString &knownFolder = lamexp_known_folder(folderId[id]);
+                       const QString &knownFolder = OS::known_folder(folderId[id]);
                        if(!knownFolder.isEmpty())
                        {
-                               tempPath = try_init_folder(QString("%1/Temp").arg(knownFolder));
+                               tempPath = try_init_folder(QString("%1/Temp").arg(knownFolder), g_temp_folder_file);
                                if(!tempPath.isEmpty())
                                {
                                        INIT_TEMP_FOLDER_RAND(g_temp_folder_path, g_temp_folder_file, tempPath);
diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp
new file mode 100644 (file)
index 0000000..ee9e2ba
--- /dev/null
@@ -0,0 +1,157 @@
+///////////////////////////////////////////////////////////////////////////////
+// MuldeR's Utilities for Qt
+// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//
+// http://www.gnu.org/licenses/lgpl-2.1.txt
+//////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+//Internal
+#include <MUtils/Global.h>
+#include <MUtils/OSSupport.h>
+
+//Win32 API
+#define WIN32_LEAN_AND_MEAN 1
+#include <Windows.h>
+#include <Objbase.h>
+
+//Qt
+#include <QMap>
+#include <QReadWriteLock>
+#include <QLibrary>
+#include <QDir>
+
+///////////////////////////////////////////////////////////////////////////////
+// KNWON FOLDERS
+///////////////////////////////////////////////////////////////////////////////
+
+typedef HRESULT (WINAPI *SHGetKnownFolderPath_t)(const GUID &rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
+typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
+
+static QMap<size_t, QString>* g_known_folders_map;
+static SHGetKnownFolderPath_t g_known_folders_fpGetKnownFolderPath;
+static SHGetFolderPath_t      g_known_folders_fpGetFolderPath;
+static QReadWriteLock         g_known_folders_lock;
+
+const QString &MUtils::OS::known_folder(known_folder_t folder_id)
+{
+       static const int CSIDL_FLAG_CREATE = 0x8000;
+       typedef enum { KF_FLAG_CREATE = 0x00008000 } kf_flags_t;
+       
+       struct
+       {
+               const int csidl;
+               const GUID guid;
+       }
+       static s_folders[] =
+       {
+               { 0x001c, {0xF1B32785,0x6FBA,0x4FCF,{0x9D,0x55,0x7B,0x8E,0x7F,0x15,0x70,0x91}} },  //CSIDL_LOCAL_APPDATA
+               { 0x0026, {0x905e63b6,0xc1bf,0x494e,{0xb2,0x9c,0x65,0xb7,0x32,0xd3,0xd2,0x1a}} },  //CSIDL_PROGRAM_FILES
+               { 0x0024, {0xF38BF404,0x1D43,0x42F2,{0x93,0x05,0x67,0xDE,0x0B,0x28,0xFC,0x23}} },  //CSIDL_WINDOWS_FOLDER
+               { 0x0025, {0x1AC14E77,0x02E7,0x4E5D,{0xB7,0x44,0x2E,0xB1,0xAE,0x51,0x98,0xB7}} },  //CSIDL_SYSTEM_FOLDER
+       };
+
+       size_t folderId = size_t(-1);
+
+       switch(folder_id)
+       {
+               case FOLDER_LOCALAPPDATA: folderId = 0; break;
+               case FOLDER_PROGRAMFILES: folderId = 1; break;
+               case FOLDER_SYSTROOT_DIR: folderId = 2; break;
+               case FOLDER_SYSTEMFOLDER: folderId = 3; break;
+       }
+
+       if(folderId == size_t(-1))
+       {
+               qWarning("Invalid 'known' folder was requested!");
+               return *reinterpret_cast<QString*>(NULL);
+       }
+
+       QReadLocker readLock(&g_known_folders_lock);
+
+       //Already in cache?
+       if(g_known_folders_map)
+       {
+               if(g_known_folders_map->contains(folderId))
+               {
+                       return g_known_folders_map->operator[](folderId);
+               }
+       }
+
+       //Obtain write lock to initialize
+       readLock.unlock();
+       QWriteLocker writeLock(&g_known_folders_lock);
+
+       //Still not in cache?
+       if(g_known_folders_map)
+       {
+               if(g_known_folders_map->contains(folderId))
+               {
+                       return g_known_folders_map->operator[](folderId);
+               }
+       }
+
+       //Initialize on first call
+       if(!g_known_folders_map)
+       {
+               QLibrary shell32("shell32.dll");
+               if(shell32.load())
+               {
+                       g_known_folders_fpGetFolderPath =      (SHGetFolderPath_t)      shell32.resolve("SHGetFolderPathW");
+                       g_known_folders_fpGetKnownFolderPath = (SHGetKnownFolderPath_t) shell32.resolve("SHGetKnownFolderPath");
+               }
+               g_known_folders_map = new QMap<size_t, QString>();
+       }
+
+       QString folderPath;
+
+       //Now try to get the folder path!
+       if(g_known_folders_fpGetKnownFolderPath)
+       {
+               WCHAR *path = NULL;
+               if(g_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_WCHAR2QSTR(path)));
+                       if(folderTemp.exists())
+                       {
+                               folderPath = folderTemp.canonicalPath();
+                       }
+                       CoTaskMemFree(path);
+               }
+       }
+       else if(g_known_folders_fpGetFolderPath)
+       {
+               QScopedArrayPointer<WCHAR> path(new WCHAR[4096]);
+               if(g_known_folders_fpGetFolderPath(NULL, s_folders[folderId].csidl | CSIDL_FLAG_CREATE, NULL, NULL, path.data()) == S_OK)
+               {
+                       //MessageBoxW(0, path, L"SHGetFolderPathW", MB_TOPMOST);
+                       QDir folderTemp = QDir(QDir::fromNativeSeparators(MUTILS_WCHAR2QSTR(path.data())));
+                       if(folderTemp.exists())
+                       {
+                               folderPath = folderTemp.canonicalPath();
+                       }
+               }
+       }
+
+       //Update cache
+       g_known_folders_map->insert(folderId, folderPath);
+       return g_known_folders_map->operator[](folderId);
+}
+
+///////////////////////////////////////////////////////////////////////////////