Code

r15432@tres: ted | 2007-05-12 21:24:09 -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);
94     /**
95      *
96      */
97     virtual void effect(Inkscape::Extension::Effect *module,
98                         Inkscape::UI::View::View *doc);
100     virtual bool cancelProcessing (void);
102 private:
103     bool _canceled;
104     Glib::Pid _pid;
105     Glib::RefPtr<Glib::MainLoop> _main_loop;
107     /**
108      * The command that has been dirived from
109      * the configuration file with appropriate directories
110      */
111     std::list<std::string> command;
113      /**
114       * This is the extension that will be used
115       * as the helper to read in or write out the
116       * data
117       */
118     Glib::ustring helper_extension;
120     /**
121      * Just a quick function to find and resolve relative paths for
122      * the incoming scripts
123      */
124     Glib::ustring solve_reldir (Inkscape::XML::Node *reprin);
126     /**
127      *
128      */
129     bool check_existance (const Glib::ustring &command);
131     /**
132      *
133      */
134     void copy_doc (Inkscape::XML::Node * olddoc,
135                    Inkscape::XML::Node * newdoc);
137     /**
138      *
139      */
140     void checkStderr (const Glib::ustring &filename, 
141                       Gtk::MessageType type,
142                       const Glib::ustring &message);
145     class file_listener {
146         Glib::ustring _string;
147         sigc::connection _conn;
148         Glib::RefPtr<Glib::IOChannel> _channel;
149         Glib::RefPtr<Glib::MainLoop> _main_loop;
150         
151     public:
152         file_listener () { };
153         ~file_listener () {
154             _conn.disconnect();
155         };
157         void init (int fd, Glib::RefPtr<Glib::MainLoop> main) {
158             _channel = Glib::IOChannel::create_from_fd(fd);
159             _channel->set_encoding();
160             _conn = Glib::signal_io().connect(sigc::mem_fun(*this, &file_listener::read), _channel, Glib::IO_IN | Glib::IO_HUP | Glib::IO_ERR);
161             _main_loop = main;
163             return;
164         };
166         bool read (Glib::IOCondition condition) {
167             if (condition != Glib::IO_IN) {
168                 _main_loop->quit();
169                 return false;
170             }
172             Glib::IOStatus status;
173             Glib::ustring out;
174             status = _channel->read_to_end(out);
176             if (status != Glib::IO_STATUS_NORMAL) {
177                 _main_loop->quit();
178                 return false;
179             }
181             _string += out;
182             return true;
183         };
185         // Note, doing a copy here, on purpose
186         Glib::ustring string (void) { return _string; };
188         void toFile (const Glib::ustring &name) {
189             Glib::RefPtr<Glib::IOChannel> stdout_file = Glib::IOChannel::create_from_file(name, "w");
190             stdout_file->write(_string);
191             return;
192         };
193     };
195     int execute (const std::list<std::string> &in_command,
196                  const std::list<std::string> &in_params,
197                  const Glib::ustring &filein,
198                  file_listener &fileout);
199 }; // class Script
205 }  // namespace Implementation
206 }  // namespace Extension
207 }  // namespace Inkscape
209 #endif /* __INKSCAPE_EXTENSION_IMPEMENTATION_SCRIPT_H__ */
211 /*
212   Local Variables:
213   mode:c++
214   c-file-style:"stroustrup"
215   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
216   indent-tabs-mode:nil
217   fill-column:99
218   End:
219 */
220 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :