Code

r15480@tres: ted | 2007-05-20 23:51:34 -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     /**
66      *
67      */
68     virtual Gtk::Widget *prefs_input(Inkscape::Extension::Input *module,
69                                      gchar const *filename);
71     /**
72      *
73      */
74     virtual SPDocument *open(Inkscape::Extension::Input *module,
75                              gchar const *filename);
77     /**
78      *
79      */
80     virtual Gtk::Widget *prefs_output(Inkscape::Extension::Output *module);
82     /**
83      *
84      */
85     virtual void save(Inkscape::Extension::Output *module,
86                       SPDocument *doc,
87                       gchar const *filename);
88     /**
89      *
90      */
91     virtual Gtk::Widget *prefs_effect(Inkscape::Extension::Effect *module,
92                                       Inkscape::UI::View::View * view,
93                                       Glib::SignalProxy0<void> * changeSignal);
95     /**
96      *
97      */
98     virtual void effect(Inkscape::Extension::Effect *module,
99                         Inkscape::UI::View::View *doc);
101     virtual bool cancelProcessing (void);
103 private:
104     bool _canceled;
105     Glib::Pid _pid;
106     Glib::RefPtr<Glib::MainLoop> _main_loop;
108     /**
109      * The command that has been dirived from
110      * the configuration file with appropriate directories
111      */
112     std::list<std::string> command;
114      /**
115       * This is the extension that will be used
116       * as the helper to read in or write out the
117       * data
118       */
119     Glib::ustring helper_extension;
121     /**
122      * Just a quick function to find and resolve relative paths for
123      * the incoming scripts
124      */
125     Glib::ustring solve_reldir (Inkscape::XML::Node *reprin);
127     /**
128      *
129      */
130     bool check_existance (const Glib::ustring &command);
132     /**
133      *
134      */
135     void copy_doc (Inkscape::XML::Node * olddoc,
136                    Inkscape::XML::Node * newdoc);
138     /**
139      *
140      */
141     void checkStderr (const Glib::ustring &filename, 
142                       Gtk::MessageType type,
143                       const Glib::ustring &message);
146     class file_listener {
147         Glib::ustring _string;
148         sigc::connection _conn;
149         Glib::RefPtr<Glib::IOChannel> _channel;
150         Glib::RefPtr<Glib::MainLoop> _main_loop;
151         
152     public:
153         file_listener () { };
154         ~file_listener () {
155             _conn.disconnect();
156         };
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_to_end(out);
177             if (status != Glib::IO_STATUS_NORMAL) {
178                 _main_loop->quit();
179                 return false;
180             }
182             _string += out;
183             return true;
184         };
186         // Note, doing a copy here, on purpose
187         Glib::ustring string (void) { return _string; };
189         void toFile (const Glib::ustring &name) {
190             Glib::RefPtr<Glib::IOChannel> stdout_file = Glib::IOChannel::create_from_file(name, "w");
191             stdout_file->write(_string);
192             return;
193         };
194     };
196     int execute (const std::list<std::string> &in_command,
197                  const std::list<std::string> &in_params,
198                  const Glib::ustring &filein,
199                  file_listener &fileout);
200 }; // class Script
206 }  // namespace Implementation
207 }  // namespace Extension
208 }  // namespace Inkscape
210 #endif /* __INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H__ */
212 /*
213   Local Variables:
214   mode:c++
215   c-file-style:"stroustrup"
216   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
217   indent-tabs-mode:nil
218   fill-column:99
219   End:
220 */
221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :