]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.h
The graphics inset now has:
[lyx.git] / src / graphics / GraphicsConverter.h
1 // -*- C++ -*-
2 /*
3  * \file GraphicsConverter.h
4  * Copyright 2002 the LyX Team
5  * Read the file COPYING
6  *
7  * \author Angus Leeming <a.leeming@ic.ac.uk>
8  *
9  * class grfx::GConverter enables graphics files to be converted asynchronously
10  * to a loadable format. It does this by building a shell script of all
11  * the conversion commands needed for the transformation. This script is then
12  * sent to the forked calls controller for non-blocking execution. When it
13  * is finished a signal is emitted, thus informing us to proceed with the
14  * loading of the image.
15  *
16  * Ultimately, this class should be wrapped back into Dekel's converter class.
17  */
18
19 #ifndef GRAPHICSCONVERTER_H
20 #define GRAPHICSCONVERTER_H
21
22 #include "LString.h"
23 #include "Lsstream.h"
24 #include <boost/smart_ptr.hpp>
25 #include <boost/utility.hpp>
26 #include <sigc++/signal_system.h>
27 #include <list>
28
29 #ifdef __GNUG__
30 #pragma interface
31 #endif
32
33 namespace grfx {
34
35 class GConverter : boost::noncopyable {
36 public:
37
38         /// This is a singleton class. Get the instance.
39         static GConverter & get();
40
41         /// Can the conversion be performed?
42         bool isReachable(string const & from_format_name,
43                          string const & to_format_name) const;
44
45         /** Convert the file and at the end return it by emitting this signal
46          *  If successful, the returned string will be the name of the
47          *  converted file (to_file_base + extension(to_format_name)).
48          *  If unsuccessful, the string will be empty.
49          */
50         typedef SigC::Signal1<void, string const &> SignalType;
51         ///
52         typedef boost::shared_ptr<SignalType> SignalTypePtr;
53         ///
54         void convert(string const & from_file,   string const & to_file_base,
55                      string const & from_format, string const & to_format,
56                      SignalTypePtr on_finish);
57
58 private:
59         /** Make the c-tor private so we can control how many objects
60          *  are instantiated.
61          */
62         GConverter() {}
63
64         /** Build the conversion script, returning true if able to build it.
65          *  The script is output to the ostringstream 'script'.
66          */
67         bool build_script(string const & from_file, string const & to_file_base,
68                           string const & from_format, string const & to_format,
69                           ostringstream & script) const;
70
71         /** Remove the ConvProcess from the list of all processes.
72          *  Called by ConvProcess::converted.
73          */
74         friend class ConvProcess;
75         ///
76         void erase(ConvProcess *);
77
78         /// The list of all conversion processs
79         typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
80         ///
81         std::list<ConvProcessPtr> all_processes_;
82 };
83
84
85 /// Each ConvProcess represents a single conversion process.
86 struct ConvProcess : public SigC::Object
87 {
88         ///
89         typedef GConverter::SignalTypePtr SignalTypePtr;
90
91         /** Each ConvProcess represents a single conversion process.
92          *  It is passed :
93          *  1. The name of the script_file, which it deletes once the
94          *     conversion is comlpeted;
95          *  2. The script command itself, which it passes on to the forked
96          *     call process;
97          *  3. The name of the output file, which it returns to the calling
98          *     process on successfull completion, by emitting
99          *  4. The signal on_finish.
100          */
101         ConvProcess(string const & script_file, string const & script_command,
102                     string const & to_file, SignalTypePtr on_finish);
103
104         /** This method is connected to a signal passed to the forked call
105          *  class, passing control back here when the conversion is completed.
106          *  Cleans-up the temporary files, emits the on_finish signal and
107          *  removes the ConvProcess from the list of all processes.
108          */
109         void converted(string cmd, pid_t pid, int retval);
110
111         ///
112         string script_file_;
113         ///
114         string to_file_;
115         ///
116         SignalTypePtr on_finish_;
117 };
118
119 } // namespace grfx
120
121 #endif // GRAPHICSCONVERTER_H