Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
template-from-file.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Martin Owens <doctormo@geek-2.com>
5 *
6 * Copyright (C) 2022 Authors
7 *
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#include "template-from-file.h"
12
13#include <cstring> // for strcmp
14#include <algorithm> // for replace
15#include <memory> // for shared_ptr, alloc...
16#include <vector> // for vector
17
18#include <glib.h> // for GFileTest, G_FILE...
19#include <glibmm/miscutils.h> // for path_get_basename
20#include <glibmm/ustring.h> // for ustring
21
22#include "document.h"
23#include "extension/extension.h" // for INKSCAPE_EXTENSIO...
24#include "extension/internal/clear-n_.h" // for N_, NC_
25#include "extension/internal/template-from-file.h" // for TemplatePresetFile
26#include "extension/system.h" // for build_from_mem
27#include "io/file.h" // for ink_file_new
28#include "io/resource.h" // for get_filenames
29#include "io/sys.h" // for file_test
30#include "xml/document.h" // for Document
31#include "xml/node.h" // for Node
32#include "xml/repr.h" // for sp_repr_lookup_co...
33
34using namespace Inkscape::IO::Resource;
35
37
41TemplatePresetFile::TemplatePresetFile(Template *mod, const std::string &filename)
42 : TemplatePreset(mod, nullptr)
43{
44 _visibility = TEMPLATE_NEW_ICON; // No searching
45
46 // TODO: Add cache here.
47 _prefs["filename"] = filename;
48 _name = Glib::path_get_basename(filename);
49 std::replace(_name.begin(), _name.end(), '_', '-');
50 _name.replace(_name.rfind(".svg"), 4, 1, ' ');
51
52 Inkscape::XML::Document *rdoc = sp_repr_read_file(filename.c_str(), SP_SVG_NS_URI);
53 if (rdoc){
54 Inkscape::XML::Node *root = rdoc->root();
55 if (!strcmp(root->name(), "svg:svg")) {
56 Inkscape::XML::Node *templateinfo = sp_repr_lookup_name(root, "inkscape:templateinfo");
57 if (!templateinfo) {
58 templateinfo = sp_repr_lookup_name(root, "inkscape:_templateinfo"); // backwards-compatibility
59 }
60 if (templateinfo) {
61 _load_data(templateinfo);
62 }
63 }
64 }
65
66 // Key is just the whole filename, it's unique enough.
67 _key = filename;
68 std::replace(_key.begin(), _key.end(), '/', '.');
69 std::replace(_key.begin(), _key.end(), '\\', '.');
70}
71
73{
74 _name = sp_repr_lookup_content(root, "inkscape:name", _name);
75 _name = sp_repr_lookup_content(root, "inkscape:_name", _name); // backwards-compatibility
76 _desc = sp_repr_lookup_content(root, "inkscape:shortdesc", _key);
77 _desc = sp_repr_lookup_content(root, "inkscape:_shortdesc", _desc); // backwards-compatibility
78
79 _label = sp_repr_lookup_content(root, "inkscape:label", N_("Custom Template"));
80 _icon = sp_repr_lookup_content(root, "inkscape:icon", _icon);
81 _category = sp_repr_lookup_content(root, "inkscape:category", _category);
82
83 try {
84 _priority = std::stoi(sp_repr_lookup_content(root, "inkscape:priority", "-1"));
85 } catch(std::exception &err) {
86 g_warning("Template priority malformed number.");
87 _priority = -1;
88 }
89
90 // Original functionality not yet used...
91 // _author = sp_repr_lookup_content(root, "inkscape:author");
92 // _preview = sp_repr_lookup_content(root, "inkscape:preview");
93 // _date = sp_repr_lookup_name(root, "inkscape:date");
94 // _keywords = sp_repr_lookup_name(root, "inkscape:_keywords");
95}
96
97
99{
100 auto filename = tmod->get_param_string("filename", "");
101 if (Inkscape::IO::file_test(filename, G_FILE_TEST_EXISTS)) {
102 return ink_file_new(filename);
103 }
104 // Default template
105 g_error("Couldn't load filename I expected to exist.");
106 return tmod->get_template_document();
107}
108
110{
111 // clang-format off
113 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">"
114 "<id>org.inkscape.template.from-file</id>"
115 "<name>" N_("Load from User File") "</name>"
116 "<description>" N_("Custom list of templates for a folder") "</description>"
117 "<category>" NC_("TemplateCategory", "Custom") "</category>"
118
119 "<param name='filename' gui-text='" N_("Filename") "' type='string'></param>"
120 "<template icon='custom' priority='-1' visibility='both'>"
121 // Auto & lazy generated content (see function)
122 "</template>"
123 "</inkscape-extension>",
124 std::make_unique<TemplateFromFile>());
125}
126
131{
132 for(auto &filename: get_filenames(TEMPLATES, {".svg"}, {"default"})) {
133 if (filename.find("icons") != Glib::ustring::npos) continue;
134 presets.emplace_back(new TemplatePresetFile(const_cast<Template *>(tmod), filename));
135 }
136}
137
138} // namespace Inkscape::Extension::Internal
139
140/*
141 Local Variables:
142 mode:c++
143 c-file-style:"stroustrup"
144 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
145 indent-tabs-mode:nil
146 fill-column:99
147 End:
148*/
149// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
char const * get_param_string(char const *name, char const *alt) const
Like get_param_string but with a default on param_not_exist error.
std::unique_ptr< SPDocument > new_from_template(Inkscape::Extension::Template *tmod) override
void get_template_presets(const Template *tmod, TemplatePresets &presets) const override
Generate a list of available files as selectable presets.
TemplatePresetFile(Template *mod, std::string const &filename)
A file based template preset.
void _load_data(const Inkscape::XML::Node *root)
std::unique_ptr< SPDocument > get_template_document() const
Get the raw document svg for this template (pre-processing).
Definition template.cpp:407
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node * root()=0
Get the root node of this node's document.
A way to clear the N_ macro, which is defined as an inline function.
RootCluster root
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
std::unique_ptr< SPDocument > ink_file_new(std::string const &Template)
Create a blank document, remove any template data.
Definition file.cpp:34
static void err(const char *fmt,...)
Definition pov-out.cpp:57
std::vector< std::shared_ptr< TemplatePreset > > TemplatePresets
void build_from_mem(gchar const *buffer, std::unique_ptr< Implementation::Implementation > in_imp)
Create a module from a buffer holding an XML description.
Definition system.cpp:459
std::vector< std::string > get_filenames(Type type, std::vector< const char * > const &extensions, std::vector< const char * > const &exclusions)
Definition resource.cpp:267
bool file_test(char const *utf8name, GFileTest test)
Definition sys.cpp:116
Document * sp_repr_read_file(const gchar *filename, const gchar *default_ns, bool xinclude)
Reads XML from a file, and returns the Document.
Definition repr-io.cpp:274
Glib::ustring sp_repr_lookup_content(Inkscape::XML::Node const *repr, gchar const *name, Glib::ustring otherwise)
Inkscape::XML::Node const * sp_repr_lookup_name(Inkscape::XML::Node const *repr, gchar const *name, gint maxdepth)
C facade to Inkscape::XML::Node.
Inkscape::IO::Resource - simple resource API.
Interface for XML documents.
Definition document.h:43
Interface for XML documents.
Interface for XML nodes.