From 0f85899c0aad7d6dbc15490189e97a68b4653d1c Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Thu, 7 Jun 2012 02:18:27 +0300
Subject: Get real desktop dir in linux for screenshots.

---
 src/client.cpp                  | 12 +----------
 src/resources/resourcemanager.h |  7 +++---
 src/utils/paths.cpp             | 47 +++++++++++++++++++++++++++++++++++++++++
 src/utils/paths.h               |  2 ++
 4 files changed, 54 insertions(+), 14 deletions(-)

(limited to 'src')

diff --git a/src/client.cpp b/src/client.cpp
index e7083f943..95317f682 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -1898,19 +1898,9 @@ void Client::initScreenshotDir()
         std::string configScreenshotDir =
             config.getStringValue("screenshotDirectory");
         if (!configScreenshotDir.empty())
-        {
             mScreenshotDir = configScreenshotDir;
-        }
         else
-        {
-#ifdef WIN32
-            mScreenshotDir = getSpecialFolderLocation(CSIDL_MYPICTURES);
-            if (mScreenshotDir.empty())
-                mScreenshotDir = getSpecialFolderLocation(CSIDL_DESKTOP);
-#else
-            mScreenshotDir = std::string(PHYSFS_getUserDir()) + "Desktop";
-#endif
-        }
+            mScreenshotDir = getDesktopDir();
         //config.setValue("screenshotDirectory", mScreenshotDir);
         logger->log("screenshotDirectory: " + mScreenshotDir);
 
diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h
index f1bb35691..ca6bdb373 100644
--- a/src/resources/resourcemanager.h
+++ b/src/resources/resourcemanager.h
@@ -214,17 +214,18 @@ class ResourceManager
          * @return An allocated byte array containing the data that was loaded,
          *         or <code>NULL</code> on fail.
          */
-        void *loadFile(const std::string &fileName, int &fileSize);
+        static void *loadFile(const std::string &fileName, int &fileSize);
 
         /**
          * Retrieves the contents of a text file (PhysFS).
          */
-        bool loadTextFile(const std::string &fileName, StringVect &lines);
+        static bool loadTextFile(const std::string &fileName,
+                                 StringVect &lines);
 
         /**
          * Retrieves the contents of a text file.
          */
-        StringVect loadTextFileLocal(const std::string &fileName);
+        static StringVect loadTextFileLocal(const std::string &fileName);
 
         void saveTextFile(std::string path, std::string name,
                           std::string text);
diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp
index 228276efd..c9ade0e68 100644
--- a/src/utils/paths.cpp
+++ b/src/utils/paths.cpp
@@ -18,10 +18,18 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifdef _MSC_VER
+#  include "msvc/config.h"
+#elif defined(HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
 #include "utils/paths.h"
 
 #include "utils/stringutils.h"
 
+#include "resources/resourcemanager.h"
+
 #include <string.h>
 #include <cstdarg>
 #include <cstdio>
@@ -128,3 +136,42 @@ std::string getSelfName()
 }
 
 #endif
+
+std::string getDesktopDir()
+{
+#ifdef WIN32
+    std::string dir = getSpecialFolderLocation(CSIDL_MYPICTURES);
+    if (dir.empty())
+        dir = getSpecialFolderLocation(CSIDL_DESKTOP);
+    return dir;
+#elif USE_X11
+    char *xdg = getenv("XDG_CONFIG_HOME");
+    std::string file;
+    if (!xdg)
+        file = std::string(PHYSFS_getUserDir()) + "/.config/user-dirs.dirs";
+    else
+        file = std::string(xdg) + "/user-dirs.dirs";
+
+    StringVect arr = ResourceManager::loadTextFileLocal(file);
+    for(StringVectCIter it = arr.begin(), it_end = arr.end();
+        it != it_end; ++ it)
+    {
+        std::string str = *it;
+        if (findCutFirst(str, "XDG_DESKTOP_DIR=\""))
+        {
+            str = str.substr(0, str.size() - 1);
+            // use hack to replace $HOME var.
+            // if in string other vars, fallback to default path
+            replaceAll(str, "$HOME/", PHYSFS_getUserDir());
+            str = getRealPath(str);
+            if (str.empty())
+                str = std::string(PHYSFS_getUserDir()) + "Desktop";
+            return str;
+        }
+    }
+
+    return std::string(PHYSFS_getUserDir()) + "Desktop";
+#else
+    return std::string(PHYSFS_getUserDir()) + "Desktop";
+#endif
+}
diff --git a/src/utils/paths.h b/src/utils/paths.h
index ba87ab961..05b4fef07 100644
--- a/src/utils/paths.h
+++ b/src/utils/paths.h
@@ -35,4 +35,6 @@ std::string removeLast(std::string str);
 
 std::string getSelfName();
 
+std::string getDesktopDir();
+
 #endif // UTILS_PATHS_H
-- 
cgit v1.2.3-70-g09d2