]> git.mxchange.org Git - simgear.git/commitdiff
Add desktop() accessor to SGPath
authorJames Turner <zakalawe@mac.com>
Sun, 26 Jan 2014 16:03:31 +0000 (16:03 +0000)
committerJames Turner <zakalawe@mac.com>
Sun, 26 Jan 2014 16:03:31 +0000 (16:03 +0000)
(Windows-only for the moment)

simgear/misc/sg_path.cxx
simgear/misc/sg_path.hxx

index 6c6194520e5ebfd4d196400f4a1bb3cb013d52d1..adc4df5b50b8739db0d9d3d876fd00ba19525308 100644 (file)
@@ -55,6 +55,34 @@ static const char sgSearchPathSep = ';';
 static const char sgSearchPathSep = ':';
 #endif
 
+#ifdef _WIN32
+
+#include <ShlObj.h> // for CSIDL
+
+static SGPath pathForCSIDL(int csidl, const SGPath::PermissonChecker& checker)
+{
+       typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
+       static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
+
+       // lazy open+resolve of shell32
+       if (!SHGetSpecialFolderPath) {
+               HINSTANCE shellDll = ::LoadLibrary("shell32");
+               SHGetSpecialFolderPath = (GetSpecialFolderPath) GetProcAddress(shellDll, "SHGetSpecialFolderPathA");
+       }
+
+       if (!SHGetSpecialFolderPath){
+               return SGPath();
+       }
+
+       char path[MAX_PATH];
+       if (SHGetSpecialFolderPath(0, path, csidl, false)) {
+               return SGPath(path, checker);
+       }
+
+       return SGPath();
+}
+
+#endif
 
 // For windows, replace "\" by "/".
 void
@@ -644,31 +672,26 @@ SGPath SGPath::home(const SGPath& def)
 #endif
 
 #ifdef _WIN32
-
-#include <ShlObj.h> // for CSIDL
-
 //------------------------------------------------------------------------------
 SGPath SGPath::desktop(const SGPath& def)
 {
-       typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPSTR, int, BOOL);
-       static GetSpecialFolderPath SHGetSpecialFolderPath = NULL;
-
-       // lazy open+resolve of shell32
-       if (!SHGetSpecialFolderPath) {
-               HINSTANCE shellDll = ::LoadLibrary("shell32");
-               SHGetSpecialFolderPath = (GetSpecialFolderPath) GetProcAddress(shellDll, "SHGetSpecialFolderPathA");
+       SGPath r = pathForCSIDL(CSIDL_DESKTOPDIRECTORY, def._permission_checker);
+       if (!r.isNull()) {
+               return r;
        }
 
-       if (!SHGetSpecialFolderPath){
-               return def;
-       }
+       SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::desktop() failed, bad" );
+       return def;
+}
 
-       char path[MAX_PATH];
-       if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, false)) {
-               return SGPath(path, def._permission_checker);
+SGPath SGPath::documents(const SGPath& def)
+{
+       SGPath r = pathForCSIDL(CSIDL_MYDOCUMENTS, def._permission_checker);
+       if (!r.isNull()) {
+               return r;
        }
 
-       SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::desktop() failed, bad" );
+       SG_LOG(SG_GENERAL, SG_ALERT, "SGPath::documents() failed, bad" );
        return def;
 }
 #elif __APPLE__
index 2261bef09d0256eca71d52c6c0a816dbc93ba772..2cade3206628555fa46ebeb8994609fec3a58109 100644 (file)
@@ -276,6 +276,10 @@ public:
      */
     static SGPath desktop(const SGPath& def = SGPath());
 
+       /**
+     * Get path to the user's documents directory
+     */
+    static SGPath documents(const SGPath& def = SGPath());
 private:
 
     void fix();