Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
actions-helper.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Gio::Actions for selection tied to the application and without GUI.
4 *
5 * Copyright (C) 2018 Tavmjong Bah
6 *
7 * The contents of this file may be used under the GNU General Public License Version 2 or later.
8 *
9 * TODO: REMOVE THIS FILE It's really not necessary.
10 */
11
12#include "actions-helper.h"
13
14#include <cstdio>
15#include <glibmm/convert.h>
16#include <glibmm/miscutils.h>
17#include <glibmm/ustring.h>
18
20#include "xml/document.h" // for Document
21#include "xml/node.h" // for Node
22#include "xml/repr.h" // for sp_repr_document_new, sp_repr_save...
23
24static bool use_active_window = false;
26
27// this function is called when in command line we call with parameter --active-window | -q
28// is called by a auto add new start and end action that fire first this action
29// and keep on till last inserted action is done
31{
32 use_active_window = true;
33 active_window_data = sp_repr_document_new("activewindowdata");
34}
35
36// this is the end of previous function. Finish the wrap of actions to active desktop
37// it also save a file to allow print in the caller terminal the output to be readable by
38// external programs like extensions.
40{
41 auto const tmpfile = get_active_desktop_commands_location();
42 auto const tmpfile_next = tmpfile + ".next";
43 sp_repr_save_file(active_window_data, tmpfile_next.c_str());
44 std::rename(tmpfile_next.c_str(), tmpfile.c_str());
45 use_active_window = false;
47 active_window_data = nullptr;
48}
49
51{
52 return Glib::build_filename(g_get_user_cache_dir(), "inkscape-active_desktop_commands.xml");
53}
54
55void
56show_output(Glib::ustring const &data, bool const is_cerr)
57{
58 if (is_cerr) {
59 std::cerr << data << std::endl;
60 } else {
61 std::cout << data << std::endl;
62 }
63
65 if (auto root = active_window_data->root()) {
66 Inkscape::XML::Node * node = nullptr;
67 if (is_cerr) {
69 } else {
71 }
74 auto txtnode = active_window_data->createTextNode("", true);
75 node->appendChild(txtnode);
76 Inkscape::GC::release(txtnode);
77 txtnode->setContent(data.c_str());
78 }
79 }
80}
81
82// Helper function: returns true if both document and selection found. Maybe this should
83// work on current view. Or better, application could return the selection of the current view.
84bool
86{
87 *document = app->get_active_document();
88 if (!(*document)) {
89 show_output("get_document_and_selection: No document!");
90 return false;
91 }
92
93 *selection = app->get_active_selection();
94 if (!*selection) {
95 show_output("get_document_and_selection: No selection!");
96 return false;
97 }
98
99 return true;
100}
101
102/*
103 Local Variables:
104 mode:c++
105 c-file-style:"stroustrup"
106 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
107 indent-tabs-mode:nil
108 fill-column:99
109 End:
110*/
111// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
static bool use_active_window
void show_output(Glib::ustring const &data, bool const is_cerr)
std::string get_active_desktop_commands_location()
void active_window_start_helper()
static Inkscape::XML::Document * active_window_data
void active_window_end_helper()
bool get_document_and_selection(InkscapeApplication *app, SPDocument **document, Inkscape::Selection **selection)
SPDocument * get_active_document()
Inkscape::Selection * get_active_selection()
The set of selected SPObjects for a given document and layer model.
Definition selection.h:80
Interface for refcounted XML nodes.
Definition node.h:80
virtual void appendChild(Node *child)=0
Append a node as the last child of this node.
virtual Node * root()=0
Get the root node of this node's document.
Typed SVG document implementation.
Definition document.h:101
RootCluster root
Inkscape::XML::Node * node
static R & release(R &r)
Decrements the reference count of a anchored object.
bool sp_repr_save_file(Document *doc, gchar const *const filename, gchar const *default_ns)
Returns true iff file successfully saved.
Definition repr-io.cpp:724
Inkscape::XML::Document * sp_repr_document_new(char const *rootname)
Returns new document having as first child a node named rootname.
Definition repr.cpp:32
C facade to Inkscape::XML::Node.
static const Point data[]
Interface for XML documents.
Definition document.h:43
virtual Node * createTextNode(char const *content)=0
virtual Node * createElement(char const *name)=0
Interface for XML documents.
Interface for XML nodes.