diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index 4b39b4bc40..0f24b85f5e 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -15,6 +15,11 @@ #define _libslic3r_h_ #include "libslic3r_version.h" + +//#define SLIC3R_APP_FULL_NAME SLIC3R_APP_NAME +#define SLIC3R_APP_FULL_NAME SLIC3R_APP_NAME "-alpha" +//#define SLIC3R_APP_FULL_NAME SLIC3R_APP_NAME "-beta" + #define GCODEVIEWER_APP_NAME "PrusaSlicer G-code Viewer" #define GCODEVIEWER_APP_KEY "PrusaSlicerGcodeViewer" diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index ebd79f8892..eb9c04cfcb 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -923,18 +923,7 @@ static boost::optional parse_semver_from_ini(std::string path) void GUI_App::init_app_config() { // Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release. - -// SetAppName(SLIC3R_APP_KEY); - SetAppName(SLIC3R_APP_KEY "-alpha"); -// SetAppName(SLIC3R_APP_KEY "-beta"); - - -// SetAppDisplayName(SLIC3R_APP_NAME); - - // Set the Slic3r data directory at the Slic3r XS module. - // Unix: ~/ .Slic3rP - // Windows : "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r" - // Mac : "~/Library/Application Support/Slic3r" + SetAppName(SLIC3R_APP_FULL_NAME); if (data_dir().empty()) { #ifndef __linux__ diff --git a/src/slic3r/Utils/MacUtils.mm b/src/slic3r/Utils/MacUtils.mm index 31a28f14e4..b570dd566d 100644 --- a/src/slic3r/Utils/MacUtils.mm +++ b/src/slic3r/Utils/MacUtils.mm @@ -1,4 +1,5 @@ #import "AppUpdater.hpp" +#import "ProfilesSharingUtils.hpp" #import @@ -15,4 +16,15 @@ std::string get_downloads_path_mac() //[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]]; //return std::string(); } + +// ProfilesSharingUtils.hpp +std::string GetDataDir() +{ + NSURL* url = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil create:NO error:nil]; + + return std::string([(CFStringRef)url.path UTF8String]); +} + } diff --git a/src/slic3r/Utils/ProfilesSharingUtils.cpp b/src/slic3r/Utils/ProfilesSharingUtils.cpp index adfbab9bdb..081182b5de 100644 --- a/src/slic3r/Utils/ProfilesSharingUtils.cpp +++ b/src/slic3r/Utils/ProfilesSharingUtils.cpp @@ -3,15 +3,109 @@ ///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher ///|/ #include "ProfilesSharingUtils.hpp" -#include "libslic3r/utils.hpp" - +#include "libslic3r/Utils.hpp" #include "slic3r/GUI/ConfigWizard_private.hpp" #include "slic3r/GUI/format.hpp" #include +#if defined(_WIN32) + +#include + +static std::string GetDataDir() +{ + HRESULT hr = E_FAIL; + + std::wstring buffer; + buffer.resize(MAX_PATH); + + hr = ::SHGetFolderPath + ( + NULL, // parent window, not used + CSIDL_APPDATA, + NULL, // access token (current user) + SHGFP_TYPE_CURRENT, // current path, not just default value + (LPWSTR)buffer.data() + ); + + // somewhat incredibly, the error code in the Unicode version is + // different from the one in ASCII version for this function +#if wxUSE_UNICODE + if (hr == E_FAIL) +#else + if (hr == S_FALSE) +#endif + { + // directory doesn't exist, maybe we can get its default value? + hr = ::SHGetFolderPath + ( + NULL, + CSIDL_APPDATA, + NULL, + SHGFP_TYPE_DEFAULT, + (LPWSTR)buffer.data() + ); + } + + for (int i=0; i< MAX_PATH; i++) + if (buffer.data()[i] == '\0') { + buffer.resize(i); + break; + } + + return boost::nowide::narrow(buffer); +} + +#elif defined(__linux__) + +#include +#include + +static std::string GetDataDir() +{ + std::string dir; + + char* ptr; + if ((ptr = getenv("XDG_CONFIG_HOME"))) + dir = std::string(ptr); + else { + if ((ptr = getenv("HOME"))) + dir = std::string(ptr); + else { + struct passwd* who = (struct passwd*)NULL; + if ((ptr = getenv("USER")) || (ptr = getenv("LOGNAME"))) + who = getpwnam(ptr); + // make sure the user exists! + if (!who) + who = getpwuid(getuid()); + + dir = std::string(who ? who->pw_dir : 0); + } + dir += "/.config"; + } + + if (dir.empty()) + printf("GetDataDir() > unsupported file layout \n"); + + return dir; +} + +#endif + namespace Slic3r { +static bool is_datadir() +{ + if (!data_dir().empty()) + return true; + + const std::string config_dir = GetDataDir(); + const std::string data_dir = (boost::filesystem::path(config_dir) / SLIC3R_APP_FULL_NAME).make_preferred().string(); + + set_data_dir(data_dir); + return true; +} namespace pt = boost::property_tree; using namespace GUI; @@ -127,6 +221,9 @@ static bool load_preset_bandle_from_datadir(PresetBundle& preset_bundle) std::string get_json_printer_models(PrinterTechnology printer_technology) { + if (!is_datadir()) + return ""; + // Build a property tree with all the information. pt::ptree root; @@ -208,6 +305,9 @@ static std::string get_printer_profiles(const VendorProfile* vendor_profile, std::string get_json_printer_profiles(const std::string& printer_model_name, const std::string& printer_variant) { + if (!is_datadir()) + return ""; + PrinterAttr printer_attr({printer_model_name, printer_variant}); if (data_dir().empty()) { @@ -308,6 +408,9 @@ static std::string get_installed_print_and_filament_profiles(const PresetBundle* std::string get_json_print_filament_profiles(const std::string& printer_profile) { + if (!is_datadir()) + return ""; + if (data_dir().empty()) { printf("Loading of all known vendors ."); BundleMap bundles = BundleMap::load(); diff --git a/src/slic3r/Utils/ProfilesSharingUtils.hpp b/src/slic3r/Utils/ProfilesSharingUtils.hpp index ab870f8984..4de698cc7d 100644 --- a/src/slic3r/Utils/ProfilesSharingUtils.hpp +++ b/src/slic3r/Utils/ProfilesSharingUtils.hpp @@ -5,12 +5,19 @@ #ifndef slic3r_ProfilesSharingUtils_hpp_ #define slic3r_ProfilesSharingUtils_hpp_ +#include "libslic3r/Config.hpp" + namespace Slic3r { std::string get_json_printer_models(PrinterTechnology printer_technology); std::string get_json_printer_profiles(const std::string& printer_model, const std::string& printer_variant); std::string get_json_print_filament_profiles(const std::string& printer_profile); +#if __APPLE__ +//implemented at MacUtils.mm +std::string GetDataDir(); +#endif //__APPLE__ + } // namespace Slic3r #endif // slic3r_ProfilesSharingUtils_hpp_