Code

r16896@shi: ted | 2007-10-30 12:16:06 -0700
[inkscape.git] / src / extension / implementation / script.h
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);
90     /**
91      *
92      */
93     virtual Gtk::Widget *prefs_effect(Inkscape::Extension::Effect *module,
94                                       Inkscape::UI::View::View * view,
95                                       sigc::signal<void> * changeSignal,
96                                                                           ImplementationDocumentCache * docCache);
98     /**
99      *
100      */
101     virtual void effect(Inkscape::Extension::Effect *module,
102                         Inkscape::UI::View::View *doc,
103                                                 ImplementationDocumentCache * docCache);
105     virtual bool cancelProcessing (void);
107 private:
108     bool _canceled;
109     Glib::Pid _pid;
110     Glib::RefPtr<Glib::MainLoop> _main_loop;
112     /**
113      * The command that has been dirived from
114      * the configuration file with appropriate directories
115      */
116     std::list<std::string> command;
118      /**
119       * This is the extension that will be used
120       * as the helper to read in or write out the
121       * data
122       */
123     Glib::ustring helper_extension;
125     /**
126      * Just a quick function to find and resolve relative paths for
127      * the incoming scripts
128      */
129     Glib::ustring solve_reldir (Inkscape::XML::Node *reprin);
131     /**
132      *
133      */
134     bool check_existance (const Glib::ustring &command);
136     /**
137      *
138      */
139     void copy_doc (Inkscape::XML::Node * olddoc,
140                    Inkscape::XML::Node * newdoc);
142     /**
143      *
144      */
145     void checkStderr (const Glib::ustring &filename, 
146                       Gtk::MessageType type,
147                       const Glib::ustring &message);
150     class file_listener {
151         Glib::ustring _string;
152         sigc::connection _conn;
153         Glib::RefPtr<Glib::IOChannel> _channel;
154         Glib::RefPtr<Glib::MainLoop> _main_loop;
155         bool _dead;
156         
157     public:
158         file_listener () : _dead(false) { };
159         ~file_listener () {
160             _conn.disconnect();
161         };
163         bool isDead () { return _dead; }
165         void init (int fd, Glib::RefPtr<Glib::MainLoop> main) {
166             _channel = Glib::IOChannel::create_from_fd(fd);
167             _channel->set_encoding();
168             _conn = Glib::signal_io().connect(sigc::mem_fun(*this, &file_listener::read), _channel, Glib::IO_IN | Glib::IO_HUP | Glib::IO_ERR);
169             _main_loop = main;
171             return;
172         };
174         bool read (Glib::IOCondition condition) {
175             if (condition != Glib::IO_IN) {
176                 _main_loop->quit();
177                 return false;
178             }
180             Glib::IOStatus status;
181             Glib::ustring out;
182             status = _channel->read_line(out);
183             _string += out;
185             if (status != Glib::IO_STATUS_NORMAL) {
186                 _main_loop->quit();
187                 _dead = true;
188                 return false;
189             }
191             return true;
192         };
194         // Note, doing a copy here, on purpose
195         Glib::ustring string (void) { return _string; };
197         void toFile (const Glib::ustring &name) {
198             Glib::RefPtr<Glib::IOChannel> stdout_file = Glib::IOChannel::create_from_file(name, "w");
199             stdout_file->write(_string);
200             return;
201         };
202     };
204     int execute (const std::list<std::string> &in_command,
205                  const std::list<std::string> &in_params,
206                  const Glib::ustring &filein,
207                  file_listener &fileout);
208 }; // class Script
214 }  // namespace Implementation
215 }  // namespace Extension
216 }  // namespace Inkscape
218 #endif /* __INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H__ */
220 /*
221   Local Variables:
222   mode:c++
223   c-file-style:"stroustrup"
224   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
225   indent-tabs-mode:nil
226   fill-column:99
227   End:
228 */
229 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :