git repos / PlatformFolders

commit 137981b4

Poul Sander · 2018-06-17 13:38
137981b4b29bc93313ad947f63c0fad94e5212a8 patch · browse files
parent 58f17ad3564e12576c01004146369b2754b85ad5
parent 5612f69ba4a8bea890649127bb49a89bb0f73eaf

Merge pull request #13 from sago007/AddPublic

Add "Public" as an option.

Changed files

M platform_folders.cpp before
M sago/platform_folders.cpp before
M sago/platform_folders.h before
M test/CMakeLists.txt before
A test/getPublicFolder.cpp
M test/integration.cpp before
diff --git a/platform_folders.cpp b/platform_folders.cpp index b1b34c9..08a23df 100644 --- a/platform_folders.cpp +++ b/platform_folders.cpp
@@ -34,6 +34,7 @@ int main() {
std::cout << "Documents: " << sago::getDocumentsFolder() << "\n";
std::cout << "Desktop: " << sago::getDesktopFolder() << "\n";
std::cout << "Pictures: " << sago::getPicturesFolder() << "\n";
+ std::cout << "Public: " << sago::getPublicFolder() << "\n";
std::cout << "Music: " << sago::getMusicFolder() << "\n";
std::cout << "Video: " << sago::getVideoFolder() << "\n";
std::cout << "Download: " << sago::getDownloadFolder() << "\n";
diff --git a/sago/platform_folders.cpp b/sago/platform_folders.cpp index 4f89040..93b2941 100644 --- a/sago/platform_folders.cpp +++ b/sago/platform_folders.cpp
@@ -32,6 +32,40 @@ SOFTWARE.
#include <cstdio>
#include <cstdlib>
+#ifndef _WIN32
+
+#include <pwd.h>
+#include <unistd.h>
+
+/**
+ * Retrives the effective user's home dir.
+ * If the user is running as root we ignore the HOME environment. It works badly with sudo.
+ * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
+ * @return The home directory. HOME environment is respected for non-root users if it exists.
+ */
+static std::string getHome() {
+ std::string res;
+ int uid = getuid();
+ const char* homeEnv = std::getenv("HOME");
+ if ( uid != 0 && homeEnv) {
+ //We only acknowlegde HOME if not root.
+ res = homeEnv;
+ return res;
+ }
+ struct passwd* pw = getpwuid(uid);
+ if (!pw) {
+ throw std::runtime_error("Unable to get passwd struct.");
+ }
+ const char* tempRes = pw->pw_dir;
+ if (!tempRes) {
+ throw std::runtime_error("User has no home directory");
+ }
+ res = tempRes;
+ return res;
+}
+
+#endif
+
#ifdef _WIN32
// Make sure we don't bring in all the extra junk with windows.h
#ifndef WIN32_LEAN_AND_MEAN
@@ -115,8 +149,6 @@ static std::string GetMacFolder(OSType folderType, const char* errorMsg) {
#else
#include <map>
#include <fstream>
-#include <pwd.h>
-#include <unistd.h>
#include <sys/types.h>
// For strlen and strtok
#include <cstring>
@@ -130,32 +162,7 @@ static void throwOnRelative(const char* envName, const char* envValue) {
}
}
-/**
- * Retrives the effective user's home dir.
- * If the user is running as root we ignore the HOME environment. It works badly with sudo.
- * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
- * @return The home directory. HOME environment is respected for non-root users if it exists.
- */
-static std::string getHome() {
- std::string res;
- int uid = getuid();
- const char* homeEnv = std::getenv("HOME");
- if ( uid != 0 && homeEnv) {
- //We only acknowlegde HOME if not root.
- res = homeEnv;
- return res;
- }
- struct passwd* pw = getpwuid(uid);
- if (!pw) {
- throw std::runtime_error("Unable to get passwd struct.");
- }
- const char* tempRes = pw->pw_dir;
- if (!tempRes) {
- throw std::runtime_error("User has no home directory");
- }
- res = tempRes;
- return res;
-}
+
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
std::string res;
@@ -338,6 +345,16 @@ std::string PlatformFolders::getPicturesFolder() const {
#endif
}
+std::string PlatformFolders::getPublicFolder() const {
+#ifdef _WIN32
+ return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
+#elif defined(__APPLE__)
+ return getHome()+"/Public";
+#else
+ return data->folders["XDG_PUBLICSHARE_DIR"];
+#endif
+}
+
std::string PlatformFolders::getDownloadFolder1() const {
#ifdef _WIN32
return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
@@ -400,6 +417,10 @@ std::string getPicturesFolder() {
return PlatformFolders().getPicturesFolder();
}
+std::string getPublicFolder() {
+ return PlatformFolders().getPublicFolder();
+}
+
std::string getMusicFolder() {
return PlatformFolders().getMusicFolder();
}
diff --git a/sago/platform_folders.h b/sago/platform_folders.h index 6102c81..99837c2 100644 --- a/sago/platform_folders.h +++ b/sago/platform_folders.h
@@ -142,6 +142,12 @@ std::string getDownloadFolder1();
std::string getPicturesFolder();
/**
+ * This returns the folder that can be used for sharing files with other users on the same system.
+ * @return Absolute path to the "Public" folder
+ */
+std::string getPublicFolder();
+
+/**
* The folder where music is stored
* @return Absolute path to the music folder
*/
@@ -208,6 +214,10 @@ public:
*/
std::string getPicturesFolder() const;
/**
+ * Use sago::getPublicFolder() instead!
+ */
+ std::string getPublicFolder() const;
+ /**
* The folder where files are downloaded.
* @note Windows: This version is XP compatible and returns the Desktop. Vista and later has a dedicated folder.
* @return Absolute path to the folder where files are downloaded to.
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8b11d44..a556883 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt
@@ -45,6 +45,7 @@ _def_test("getDocumentsFolder")
_def_test("getDownloadFolder1")
_def_test("getMusicFolder")
_def_test("getPicturesFolder")
+_def_test("getPublicFolder")
_def_test("getSaveGamesFolder1")
_def_test("getVideoFolder")
_def_test("integration")
diff --git a/test/getPublicFolder.cpp b/test/getPublicFolder.cpp new file mode 100644 index 0000000..c323e9f --- /dev/null +++ b/test/getPublicFolder.cpp
@@ -0,0 +1,7 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+
+int main() {
+ run_test(sago::getPublicFolder());
+ return 0;
+}
diff --git a/test/integration.cpp b/test/integration.cpp index a3084cc..b61369c 100644 --- a/test/integration.cpp +++ b/test/integration.cpp
@@ -15,6 +15,7 @@ int main() {
run_test(sago::getDownloadFolder());
run_test(sago::getDownloadFolder1());
run_test(sago::getPicturesFolder());
+ run_test(sago::getPublicFolder());
run_test(sago::getMusicFolder());
run_test(sago::getVideoFolder());
run_test(sago::getSaveGamesFolder1());