summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/extension/internal/filter/filter-file.cpp77
-rw-r--r--src/extension/internal/filter/filter.h1
-rw-r--r--src/io/resource.cpp64
-rw-r--r--src/io/resource.h14
4 files changed, 93 insertions, 63 deletions
diff --git a/src/extension/internal/filter/filter-file.cpp b/src/extension/internal/filter/filter-file.cpp
index dfe889ddf..c18879430 100644
--- a/src/extension/internal/filter/filter-file.cpp
+++ b/src/extension/internal/filter/filter-file.cpp
@@ -30,71 +30,19 @@ namespace Extension {
namespace Internal {
namespace Filter {
-void filters_load_domain(Domain domain, gchar *menuname);
-
-void Filter::filters_all_files(void)
-{
- filters_load_domain(SYSTEM, _("Bundled"));
- filters_load_domain(USER, _("Personal"));
-}
-
-#define INKSCAPE_FILTER_FILE ".svg"
-
-void filters_load_domain(Domain domain, gchar *menuname)
-{
- char const *dirname = get_path(domain, FILTERS);
-
- if (!dirname) {
- g_warning("%s", _("Null external module directory name. Filters will not be loaded."));
- return;
- }
-
- if (!Glib::file_test(std::string(dirname), Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
- return;
- }
-
- GError *err;
- GDir *directory = g_dir_open(dirname, 0, &err);
- if (!directory) {
- gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
- g_warning(_("Modules directory (%s) is unavailable. External modules in that directory will not be loaded."), safeDir);
- g_free(safeDir);
- return;
- }
-
- gchar *filename;
- while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
- if (strlen(filename) < strlen(INKSCAPE_FILTER_FILE)) {
- continue;
- }
-
- if (strcmp(INKSCAPE_FILTER_FILE, filename + (strlen(filename) - strlen(INKSCAPE_FILTER_FILE)))) {
- continue;
- }
-
- gchar *pathname = g_build_filename(dirname, filename, NULL);
- Filter::filters_load_file(pathname, menuname);
- g_free(pathname);
- }
-
- g_dir_close(directory);
-
- return;
-}
-
void
-Filter::filters_load_file (gchar * filename, gchar * menuname)
+filters_load_file (Glib::ustring filename, gchar * menuname)
{
- Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI);
+ Inkscape::XML::Document *doc = sp_repr_read_file(filename.c_str(), INKSCAPE_EXTENSION_URI);
if (doc == NULL) {
- g_warning("File (%s) is not parseable as XML. Ignored.", filename);
+ g_warning("File (%s) is not parseable as XML. Ignored.", filename.c_str());
return;
}
Inkscape::XML::Node * root = doc->root();
if (strcmp(root->name(), "svg:svg")) {
Inkscape::GC::release(doc);
- g_warning("File (%s) is not SVG. Ignored.", filename);
+ g_warning("File (%s) is not SVG. Ignored.", filename.c_str());
return;
}
@@ -104,7 +52,7 @@ Filter::filters_load_file (gchar * filename, gchar * menuname)
for (Inkscape::XML::Node * defs = child->firstChild();
defs != NULL; defs = defs->next()) {
if (!strcmp(defs->name(), "svg:filter")) {
- filters_load_node(defs, menuname);
+ Filter::filters_load_node(defs, menuname);
} // oh! a filter
} //defs
} // is defs
@@ -114,6 +62,17 @@ Filter::filters_load_file (gchar * filename, gchar * menuname)
return;
}
+void Filter::filters_all_files(void)
+{
+ for(auto &filename: get_filenames(USER, FILTERS, {".svg"})) {
+ filters_load_file(filename, _("Personal"));
+ }
+ for(auto &filename: get_filenames(SYSTEM, FILTERS, {".svg"})) {
+ filters_load_file(filename, _("Bundled"));
+ }
+}
+
+
#include "extension/internal/clear-n_.h"
class mywriter : public Inkscape::IO::BasicWriter {
@@ -150,8 +109,8 @@ Filter::filters_load_node (Inkscape::XML::Node * node, gchar * menuname)
"<object-type>all</object-type>\n"
"<effects-menu>\n"
"<submenu name=\"" N_("Filters") "\">\n"
- "<submenu name=\"%s\"/>\n"
- "</submenu>\n"
+ "<submenu name=\"%s\"/>\n"
+ "</submenu>\n"
"</effects-menu>\n"
"<menu-tip>%s</menu-tip>\n"
"</effect>\n"
diff --git a/src/extension/internal/filter/filter.h b/src/extension/internal/filter/filter.h
index d4728b69c..8fa086b69 100644
--- a/src/extension/internal/filter/filter.h
+++ b/src/extension/internal/filter/filter.h
@@ -53,7 +53,6 @@ public:
/* File loader related */
static void filters_all_files(void);
- static void filters_load_file(gchar * filename, gchar * menuname);
static void filters_load_node(Inkscape::XML::Node * node, gchar * menuname);
};
diff --git a/src/io/resource.cpp b/src/io/resource.cpp
index 73dc7117e..dbce8a3f4 100644
--- a/src/io/resource.cpp
+++ b/src/io/resource.cpp
@@ -20,6 +20,9 @@
#include "config.h"
#endif
+#include <glibmm/miscutils.h>
+#include <glibmm/stringutils.h>
+#include <glibmm/fileutils.h>
#include "path-prefix.h"
#include "io/sys.h"
#include "io/resource.h"
@@ -57,7 +60,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename)
case THEMES: temp = INKSCAPE_THEMEDIR; break;
case TUTORIALS: temp = INKSCAPE_TUTORIALSDIR; break;
case UIS: temp = INKSCAPE_UIDIR; break;
- default: g_assert_not_reached();
+ default: temp = "";
}
path = g_strdup(temp);
} break;
@@ -67,7 +70,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename)
case GRADIENTS: temp = CREATE_GRADIENTSDIR; break;
case PALETTES: temp = CREATE_PALETTESDIR; break;
case PATTERNS: temp = CREATE_PATTERNSDIR; break;
- default: g_assert_not_reached();
+ default: temp = "";
}
path = g_strdup(temp);
} break;
@@ -96,7 +99,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename)
} break;
}
- if (filename) {
+ if (filename && path) {
gchar *temp=g_build_filename(path, filename, NULL);
g_free(path);
path = temp;
@@ -173,6 +176,61 @@ Glib::ustring get_filename(Type type, char const *filename, char const *locale)
return result;
}
+/*
+ * Get's all the files in a given type, for all domain types.
+ *
+ * domain - Optional domain (overload), will check return domains if not.
+ * type - The type of files, e.g. TEMPLATES
+ * extentions - A list of extensions to return, e.g. xml, svg
+ * exclusions - A list of names to exclude e.g. default.xml
+ */
+std::vector<Glib::ustring> get_filenames(Type type, std::vector<const char *> extensions, std::vector<const char *> exclusions)
+{
+ std::vector<Glib::ustring> ret;
+ get_filenames_from_path(ret, get_path_ustring(USER, type), extensions, exclusions);
+ get_filenames_from_path(ret, get_path_ustring(SYSTEM, type), extensions, exclusions);
+ get_filenames_from_path(ret, get_path_ustring(CREATE, type), extensions, exclusions);
+ return ret;
+}
+std::vector<Glib::ustring> get_filenames(Domain domain, Type type, std::vector<const char *> extensions, std::vector<const char *> exclusions)
+{
+ std::vector<Glib::ustring> ret;
+ get_filenames_from_path(ret, get_path_ustring(domain, type), extensions, exclusions);
+ return ret;
+}
+
+/*
+ * Get all the files from a specific path, populating &files vector
+ *
+ * &files - Output list to populate
+ * path - The directory to parse, will add nothing if directory doesn't exist
+ * extensions - Only add files with these extensions.
+ * exclusions - Exclude files that exactly match these names.
+ */
+void get_filenames_from_path(std::vector<Glib::ustring> &files, Glib::ustring path, std::vector<const char *> extensions, std::vector<const char *> exclusions)
+{
+ if(!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
+ return;
+ }
+
+ Glib::Dir dir(path);
+ std::string file = dir.read_name();
+ while (!file.empty()){
+ bool reject = false;
+ for (auto &ext: extensions) {
+ reject = reject || !Glib::str_has_suffix(file, ext);
+ }
+ for (auto &exc: exclusions) {
+ reject = reject || (file == exc);
+ }
+ if(!reject) {
+ files.push_back(Glib::build_filename(path, file));
+ }
+ file = dir.read_name();
+ }
+}
+
+
/**
* Get, or guess, or decide the location where the preferences.xml
* file should be located. This also indicates where all other inkscape
diff --git a/src/io/resource.h b/src/io/resource.h
index 0ea5ab4f0..ed877729c 100644
--- a/src/io/resource.h
+++ b/src/io/resource.h
@@ -64,6 +64,20 @@ Glib::ustring get_path_ustring(Domain domain, Type type,
Glib::ustring get_filename(Type type, char const *filename,
char const *locale=NULL);
+std::vector<Glib::ustring> get_filenames(Type type,
+ std::vector<const char *> extensions={},
+ std::vector<const char *> exclusions={});
+
+std::vector<Glib::ustring> get_filenames(Domain domain, Type type,
+ std::vector<const char *> extensions={},
+ std::vector<const char *> exclusions={});
+
+void get_filenames_from_path(std::vector<Glib::ustring> &files,
+ Glib::ustring path,
+ std::vector<const char *> extensions={},
+ std::vector<const char *> exclusions={});
+
+
char *profile_path(const char *filename);
char *homedir_path(const char *filename);
char *log_path(const char *filename);