From 0f85899c0aad7d6dbc15490189e97a68b4653d1c Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 7 Jun 2012 02:18:27 +0300 Subject: Get real desktop dir in linux for screenshots. --- src/utils/paths.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils/paths.h | 2 ++ 2 files changed, 49 insertions(+) (limited to 'src/utils') 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 . */ +#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 #include #include @@ -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