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__
14 #define __INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H__
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 /**
119 * Just a quick function to find and resolve relative paths for
120 * the incoming scripts
121 */
122 Glib::ustring solve_reldir (Inkscape::XML::Node *reprin);
124 /**
125 *
126 */
127 bool check_existance (const Glib::ustring &command);
129 /**
130 *
131 */
132 void copy_doc (Inkscape::XML::Node * olddoc,
133 Inkscape::XML::Node * newdoc);
135 /**
136 *
137 */
138 void checkStderr (const Glib::ustring &filename,
139 Gtk::MessageType type,
140 const Glib::ustring &message);
143 class file_listener {
144 Glib::ustring _string;
145 sigc::connection _conn;
146 Glib::RefPtr<Glib::IOChannel> _channel;
147 Glib::RefPtr<Glib::MainLoop> _main_loop;
148 bool _dead;
150 public:
151 file_listener () : _dead(false) { };
152 virtual ~file_listener () {
153 _conn.disconnect();
154 };
156 bool isDead () { return _dead; }
158 void init (int fd, Glib::RefPtr<Glib::MainLoop> main) {
159 _channel = Glib::IOChannel::create_from_fd(fd);
160 _channel->set_encoding();
161 _conn = Glib::signal_io().connect(sigc::mem_fun(*this, &file_listener::read), _channel, Glib::IO_IN | Glib::IO_HUP | Glib::IO_ERR);
162 _main_loop = main;
164 return;
165 };
167 bool read (Glib::IOCondition condition) {
168 if (condition != Glib::IO_IN) {
169 _main_loop->quit();
170 return false;
171 }
173 Glib::IOStatus status;
174 Glib::ustring out;
175 status = _channel->read_line(out);
176 _string += out;
178 if (status != Glib::IO_STATUS_NORMAL) {
179 _main_loop->quit();
180 _dead = true;
181 return false;
182 }
184 return true;
185 };
187 // Note, doing a copy here, on purpose
188 Glib::ustring string (void) { return _string; };
190 void toFile (const Glib::ustring &name) {
191 Glib::RefPtr<Glib::IOChannel> stdout_file = Glib::IOChannel::create_from_file(name, "w");
192 stdout_file->set_encoding();
193 stdout_file->write(_string);
194 return;
195 };
196 };
198 int execute (const std::list<std::string> &in_command,
199 const std::list<std::string> &in_params,
200 const Glib::ustring &filein,
201 file_listener &fileout);
203 void pump_events (void);
205 /** \brief A definition of an interpreter, which can be specified
206 in the INX file, but we need to know what to call */
207 struct interpreter_t {
208 gchar const *identity; /**< The ID that is in the INX file */
209 gchar const *prefstring; /**< The preferences key that can override the default */
210 gchar const *defaultval; /**< The default value if there are no preferences */
211 };
212 static interpreter_t const interpreterTab[];
214 Glib::ustring resolveInterpreterExecutable(const Glib::ustring &interpNameArg);
216 }; // class Script
222 } // namespace Implementation
223 } // namespace Extension
224 } // namespace Inkscape
226 #endif /* __INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H__ */
228 /*
229 Local Variables:
230 mode:c++
231 c-file-style:"stroustrup"
232 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
233 indent-tabs-mode:nil
234 fill-column:99
235 End:
236 */
237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :