/*
6 * MenTaLguY <mental@rydia.net>
7 * Martin Owens <doctormo@gmail.com>
9 * Copyright (C) 2018 Authors
10 * Released under GNU GPL v2+, read the file
'COPYING' for more information.
27#include <glibmm/convert.h>
28#include <glibmm/i18n.h>
29#include <glibmm/miscutils.h>
30#include <glibmm/stringutils.h>
31#include <glibmm/fileutils.h>
42#define INKSCAPE_PROFILE_DIR "inkscape"
62 char const *
name =
nullptr;
63 char const *sysdir =
nullptr;
64 char const *envor =
nullptr;
72 default:
return nullptr;
76 g_assert(type ==
NONE);
77 return g_build_filename(g_get_user_cache_dir(),
"inkscape", filename, extra,
nullptr);
88 case EXTENSIONS:
name =
"extensions"; envor =
"INKSCAPE_EXTENSIONS_DIR";
break;
103 default: g_assert_not_reached();
109 if (envor && domain ==
USER) {
110 std::string env_dir = Glib::getenv(envor);
111 if (!env_dir.empty()) {
112 return g_build_filename(env_dir.c_str(), filename, extra,
nullptr);
122 }
else if (domain ==
SHARED) {
126 return g_build_filename(
shared_path().c_str(),
name, filename, extra,
nullptr);
131 return g_build_filename(
profile_path().c_str(),
name, filename, extra,
nullptr);
139 char *path =
_get_path(domain, type, filename, extra);
151 char *path =
_get_path(domain, type, filename, extra);
174 char *user_filename =
nullptr;
175 char *shared_filename =
nullptr;
176 char *sys_filename =
nullptr;
177 char *user_filename_localized =
nullptr;
178 char *sys_filename_localized =
nullptr;
182 localized = localized && strcmp(_(
"en"),
"en");
185 std::string localized_filename = filename;
186 localized_filename.insert(localized_filename.rfind(
'.'),
".");
187 localized_filename.insert(localized_filename.rfind(
'.'), _(
"en"));
189 user_filename_localized =
_get_path(
USER, type, localized_filename.c_str());
190 sys_filename_localized =
_get_path(
SYSTEM, type, localized_filename.c_str());
198 if (localized &&
file_test(user_filename_localized, G_FILE_TEST_EXISTS)) {
199 result = user_filename_localized;
200 g_info(
"Found localized version of resource file '%s' in profile directory:\n\t%s", filename,
result.c_str());
201 }
else if (
file_test(user_filename, G_FILE_TEST_EXISTS)) {
203 g_info(
"Found resource file '%s' in profile directory:\n\t%s", filename,
result.c_str());
204 }
else if (
file_test(shared_filename, G_FILE_TEST_EXISTS)) {
206 g_info(
"Found resource file '%s' in profile directory:\n\t%s", filename,
result.c_str());
207 }
else if (localized &&
file_test(sys_filename_localized, G_FILE_TEST_EXISTS)) {
208 result = sys_filename_localized;
209 g_info(
"Found localized version of resource file '%s' in system directory:\n\t%s", filename,
result.c_str());
210 }
else if (
file_test(sys_filename, G_FILE_TEST_EXISTS)) {
212 g_info(
"Found resource file '%s' in system directory:\n\t%s", filename,
result.c_str());
213 }
else if (!silent) {
215 g_warning(
"Failed to find resource file '%s'. Looked in:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s",
216 filename, user_filename_localized, user_filename, shared_filename, sys_filename_localized, sys_filename);
218 g_warning(
"Failed to find resource file '%s'. Looked in:\n\t%s\n\t%s\n\t%s",
219 filename, user_filename, shared_filename, sys_filename);
223 g_free(user_filename);
224 g_free(shared_filename);
225 g_free(sys_filename);
226 g_free(user_filename_localized);
227 g_free(sys_filename_localized);
238std::string
get_filename(std::string
const& path, std::string
const& filename)
241 if (Glib::file_test(path, Glib::FileTest::IS_REGULAR)) {
242 auto dirname = Glib::path_get_dirname(path);
243 g_assert(!Glib::file_test(dirname, Glib::FileTest::IS_REGULAR));
246 if (g_path_is_absolute(filename.c_str())) {
247 if (Glib::file_test(filename, Glib::FileTest::EXISTS)) {
251 auto ret = Glib::build_filename(path, filename);
252 if (Glib::file_test(ret, Glib::FileTest::EXISTS)) {
267std::vector<std::string>
get_filenames(
Type type, std::vector<const char *>
const &extensions, std::vector<const char *>
const &exclusions)
269 std::vector<std::string> ret;
277std::vector<std::string>
get_filenames(
Domain domain,
Type type, std::vector<const char *>
const &extensions, std::vector<const char *>
const &exclusions)
279 std::vector<std::string> ret;
283std::vector<std::string>
get_filenames(std::string path, std::vector<const char *>
const &extensions, std::vector<const char *>
const &exclusions)
285 std::vector<std::string> ret;
300 std::vector<std::string> ret;
310 std::vector<std::string> ret;
315std::vector<std::string>
get_foldernames(std::string
const &path, std::vector<const char *>
const &exclusions)
317 std::vector<std::string> ret;
332 std::vector<const char *>
const &extensions, std::vector<const char *>
const &exclusions)
334 if(!Glib::file_test(path, Glib::FileTest::IS_DIR)) {
339 std::string file = dir.read_name();
340 while (!file.empty()){
342 bool reject = !extensions.empty();
345 for (
auto &ext: extensions) {
346 reject ^= Glib::str_has_suffix(file, ext);
350 for (
auto &exc: exclusions) {
351 reject |= Glib::str_has_prefix(file, exc);
355 auto filename = Glib::build_filename(path, file);
357 if(Glib::file_test(filename, Glib::FileTest::IS_DIR)) {
359 }
else if(Glib::file_test(filename, Glib::FileTest::IS_REGULAR) && !reject) {
360 files.push_back(Glib::filename_to_utf8(filename));
362 file = dir.read_name();
374 std::vector<const char *>
const &exclusions)
376 if (!Glib::file_test(path, Glib::FileTest::IS_DIR)) {
381 std::string file = dir.read_name();
382 while (!file.empty()) {
387 for (
auto &exc : exclusions) {
388 reject |= Glib::str_has_prefix(file, exc);
392 auto filename = Glib::build_filename(path, file);
394 if (Glib::file_test(filename, Glib::FileTest::IS_DIR) && !reject) {
395 folders.push_back(Glib::filename_to_utf8(filename));
397 file = dir.read_name();
410 return std::string(
"");
418 static std::string prefdir =
"";
420 if (prefdir.empty()) {
422 prefdir = Glib::getenv(
"INKSCAPE_PROFILE_DIR");
427 if (prefdir.empty()) {
428 ITEMIDLIST *pidl = 0;
429 if ( SHGetFolderLocation( NULL, CSIDL_APPDATA, NULL, 0, &pidl ) == S_OK ) {
430 gchar * utf8Path = NULL;
433 wchar_t pathBuf[MAX_PATH+1];
434 g_assert(
sizeof(
wchar_t) ==
sizeof(gunichar2));
436 if ( SHGetPathFromIDListW( pidl, pathBuf ) ) {
437 utf8Path = g_utf16_to_utf8( (gunichar2*)(&pathBuf[0]), -1, NULL, NULL, NULL );
442 if (!g_utf8_validate(utf8Path, -1, NULL)) {
443 g_warning(
"SHGetPathFromIDListW() resulted in invalid UTF-8");
452 if (!prefdir.empty()) {
453 prefdir = Glib::build_filename(prefdir, INKSCAPE_PROFILE_DIR);
457 if (prefdir.empty()) {
470 if (g_mkdir_with_parents(prefdir.c_str(),
mode) == -1 ) {
472 g_warning(
"Unable to create profile directory (%s) (%d)", g_strerror(problem), problem);
474 gchar
const *userDirs[] = {
"keys",
"templates",
"icons",
"extensions",
"ui",
475 "symbols",
"paint",
"themes",
"palettes",
"fontcollections",
nullptr };
477 gchar *dir = g_build_filename(prefdir.c_str(), *
name,
nullptr);
478 g_mkdir_with_parents(dir,
mode);
496 std::string shared_dir = prefs->
getString(
"/options/resources/sharedpath");
497 if (!shared_dir.empty() && Glib::file_test(shared_dir, Glib::FileTest::IS_DIR)) {
501 return std::string(
"");
515 return g_get_home_dir();
static InkscapeApplication * instance()
Singleton instance.
Preference storage class.
Glib::ustring getString(Glib::ustring const &pref_path, Glib::ustring const &def="")
Retrieve an UTF-8 string.
static Preferences * get()
Access the singleton Preferences object.
Util::ptr_shared get_path(Domain domain, Type type, char const *filename, char const *extra)
void get_foldernames_from_path(std::vector< std::string > &folders, std::string const &path, std::vector< const char * > const &exclusions)
std::string get_path_string(Domain domain, Type type, char const *filename, char const *extra)
std::string homedir_path()
gchar * _get_path(Domain domain, Type type, char const *filename, char const *extra=nullptr)
std::string shared_path()
std::string get_filename(Type type, char const *filename, bool localized, bool silent)
std::vector< std::string > get_foldernames(Type type, std::vector< const char * > const &exclusions)
void get_filenames_from_path(std::vector< std::string > &files, std::string const &path, std::vector< const char * > const &extensions, std::vector< const char * > const &exclusions)
std::string profile_path()
std::vector< std::string > get_filenames(Type type, std::vector< const char * > const &extensions, std::vector< const char * > const &exclusions)
std::string log_path(const char *filename)
bool file_test(char const *utf8name, GFileTest test)
ptr_shared share_string(char const *string)
char const * get_inkscape_datadir()
Determine the location of the Inkscape data directory (typically the share/ folder from where Inkscap...
const char * get_user_config_dir()
Get the user configuration directory.
TODO: insert short description here.
Singleton class to access the preferences file in a convenient way.
Inkscape::IO::Resource - simple resource API.