ProfilesSharingUtils : Implemented detection of the application DataDir to avoid manual set for this value.

Use just native functions (without wxWidgets)
Note: Most of code is extracted from wxWidgest (wxStandardPaths and wxFileName classes implementation) to avoid use of wxString
This commit is contained in:
YuSanka 2024-02-21 12:13:21 +01:00 committed by Lukas Matena
parent 4f0894aa6c
commit 4e997d8dd5
5 changed files with 130 additions and 14 deletions

View File

@ -15,6 +15,11 @@
#define _libslic3r_h_ #define _libslic3r_h_
#include "libslic3r_version.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_NAME "PrusaSlicer G-code Viewer"
#define GCODEVIEWER_APP_KEY "PrusaSlicerGcodeViewer" #define GCODEVIEWER_APP_KEY "PrusaSlicerGcodeViewer"

View File

@ -923,18 +923,7 @@ static boost::optional<Semver> parse_semver_from_ini(std::string path)
void GUI_App::init_app_config() void GUI_App::init_app_config()
{ {
// Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release. // Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release.
SetAppName(SLIC3R_APP_FULL_NAME);
// 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"
if (data_dir().empty()) { if (data_dir().empty()) {
#ifndef __linux__ #ifndef __linux__

View File

@ -1,4 +1,5 @@
#import "AppUpdater.hpp" #import "AppUpdater.hpp"
#import "ProfilesSharingUtils.hpp"
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@ -15,4 +16,15 @@ std::string get_downloads_path_mac()
//[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]]; //[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
//return std::string(); //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]);
}
} }

View File

@ -3,15 +3,109 @@
///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher ///|/ PrusaSlicer is released under the terms of the AGPLv3 or higher
///|/ ///|/
#include "ProfilesSharingUtils.hpp" #include "ProfilesSharingUtils.hpp"
#include "libslic3r/utils.hpp" #include "libslic3r/Utils.hpp"
#include "slic3r/GUI/ConfigWizard_private.hpp" #include "slic3r/GUI/ConfigWizard_private.hpp"
#include "slic3r/GUI/format.hpp" #include "slic3r/GUI/format.hpp"
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#if defined(_WIN32)
#include <shlobj.h>
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 <stdlib.h>
#include <pwd.h>
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 { 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; namespace pt = boost::property_tree;
using namespace GUI; 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) std::string get_json_printer_models(PrinterTechnology printer_technology)
{ {
if (!is_datadir())
return "";
// Build a property tree with all the information. // Build a property tree with all the information.
pt::ptree root; 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) 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}); PrinterAttr printer_attr({printer_model_name, printer_variant});
if (data_dir().empty()) { 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) std::string get_json_print_filament_profiles(const std::string& printer_profile)
{ {
if (!is_datadir())
return "";
if (data_dir().empty()) { if (data_dir().empty()) {
printf("Loading of all known vendors ."); printf("Loading of all known vendors .");
BundleMap bundles = BundleMap::load(); BundleMap bundles = BundleMap::load();

View File

@ -5,12 +5,19 @@
#ifndef slic3r_ProfilesSharingUtils_hpp_ #ifndef slic3r_ProfilesSharingUtils_hpp_
#define slic3r_ProfilesSharingUtils_hpp_ #define slic3r_ProfilesSharingUtils_hpp_
#include "libslic3r/Config.hpp"
namespace Slic3r { namespace Slic3r {
std::string get_json_printer_models(PrinterTechnology printer_technology); 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_printer_profiles(const std::string& printer_model, const std::string& printer_variant);
std::string get_json_print_filament_profiles(const std::string& printer_profile); 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 } // namespace Slic3r
#endif // slic3r_ProfilesSharingUtils_hpp_ #endif // slic3r_ProfilesSharingUtils_hpp_