1 /*
2 * Code for handling extensions (i.e., scripts)
3 *
4 * Authors:
5 * Bryce Harrington <bryce@osdl.org>
6 * Ted Gould <ted@gould.cx>
7 *
8 * Copyright (C) 2002-2005 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifndef INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H_SEEN
14 #define INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H_SEEN
16 #include "implementation.h"
17 #include <gtkmm/messagedialog.h>
19 namespace Inkscape {
20 namespace XML {
21 class Node;
22 }
23 }
26 namespace Inkscape {
27 namespace Extension {
28 namespace Implementation {
32 /**
33 * Utility class used for loading and launching script extensions
34 */
35 class Script : public Implementation {
37 public:
39 /**
40 *
41 */
42 Script(void);
44 /**
45 *
46 */
47 virtual ~Script();
50 /**
51 *
52 */
53 virtual bool load(Inkscape::Extension::Extension *module);
55 /**
56 *
57 */
58 virtual void unload(Inkscape::Extension::Extension *module);
60 /**
61 *
62 */
63 virtual bool check(Inkscape::Extension::Extension *module);
65 ImplementationDocumentCache * newDocCache(Inkscape::Extension::Extension * ext, Inkscape::UI::View::View * view);
67 /**
68 *
69 */
70 virtual Gtk::Widget *prefs_input(Inkscape::Extension::Input *module,
71 gchar const *filename);
73 /**
74 *
75 */
76 virtual SPDocument *open(Inkscape::Extension::Input *module,
77 gchar const *filename);
79 /**
80 *
81 */
82 virtual Gtk::Widget *prefs_output(Inkscape::Extension::Output *module);
84 /**
85 *
86 */
87 virtual void save(Inkscape::Extension::Output *module,
88 SPDocument *doc,
89 gchar const *filename);
91 /**
92 *
93 */
94 virtual void effect(Inkscape::Extension::Effect *module,
95 Inkscape::UI::View::View *doc,
96 ImplementationDocumentCache * docCache);
98 virtual bool cancelProcessing (void);
100 private:
101 bool _canceled;
102 Glib::Pid _pid;
103 Glib::RefPtr<Glib::MainLoop> _main_loop;
105 /**
106 * The command that has been dirived from
107 * the configuration file with appropriate directories
108 */
109 std::list<std::string> command;
111 /**
112 * This is the extension that will be used
113 * as the helper to read in or write out the
114 * data
115 */
116 Glib::ustring helper_extension;
118 std::string solve_reldir (Inkscape::XML::Node *reprin);
119 bool check_existence (const std::string &command);
120 void copy_doc (Inkscape::XML::Node * olddoc,
121 Inkscape::XML::Node * newdoc);
122 void checkStderr (const Glib::ustring &filename,
123 Gtk::MessageType type,
124 const Glib::ustring &message);
126 class file_listener {
127 Glib::ustring _string;
128 sigc::connection _conn;
129 Glib::RefPtr<Glib::IOChannel> _channel;
130 Glib::RefPtr<Glib::MainLoop> _main_loop;
131 bool _dead;
133 public:
134 file_listener () : _dead(false) { };
135 virtual ~file_listener () {
136 _conn.disconnect();
137 };
139 bool isDead () { return _dead; }
141 void init (int fd, Glib::RefPtr<Glib::MainLoop> main) {
142 _channel = Glib::IOChannel::create_from_fd(fd);
143 _channel->set_encoding();
144 _conn = Glib::signal_io().connect(sigc::mem_fun(*this, &file_listener::read), _channel, Glib::IO_IN | Glib::IO_HUP | Glib::IO_ERR);
145 _main_loop = main;
147 return;
148 };
150 bool read (Glib::IOCondition condition) {
151 if (condition != Glib::IO_IN) {
152 _main_loop->quit();
153 return false;
154 }
156 Glib::IOStatus status;
157 Glib::ustring out;
158 status = _channel->read_line(out);
159 _string += out;
161 if (status != Glib::IO_STATUS_NORMAL) {
162 _main_loop->quit();
163 _dead = true;
164 return false;
165 }
167 return true;
168 };
170 Glib::ustring string (void) { return _string; };
172 bool toFile (const Glib::ustring &name) {
173 try {
174 Glib::RefPtr<Glib::IOChannel> stdout_file = Glib::IOChannel::create_from_file(name, "w");
175 stdout_file->set_encoding();
176 stdout_file->write(_string);
177 } catch (Glib::FileError &e) {
178 return false;
179 }
180 return true;
181 };
182 };
184 int execute (const std::list<std::string> &in_command,
185 const std::list<std::string> &in_params,
186 const Glib::ustring &filein,
187 file_listener &fileout);
189 void pump_events(void);
191 /** \brief A definition of an interpreter, which can be specified
192 in the INX file, but we need to know what to call */
193 struct interpreter_t {
194 gchar const *identity; /**< The ID that is in the INX file */
195 gchar const *prefstring; /**< The preferences key that can override the default */
196 gchar const *defaultval; /**< The default value if there are no preferences */
197 };
198 static interpreter_t const interpreterTab[];
200 std::string resolveInterpreterExecutable(const Glib::ustring &interpNameArg);
202 }; // class Script
208 } // namespace Implementation
209 } // namespace Extension
210 } // namespace Inkscape
212 #endif // INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H_SEEN
214 /*
215 Local Variables:
216 mode:c++
217 c-file-style:"stroustrup"
218 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
219 indent-tabs-mode:nil
220 fill-column:99
221 End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :