]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.h
Some more fixes to compiler warnings.
[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 #include <sys/types.h> // needed for pid_t
29
30 #ifdef __GNUG__
31 #pragma interface
32 #endif
33
34 namespace grfx {
35
36 class GConverter : boost::noncopyable {
37 public:
38
39         /// This is a singleton class. Get the instance.
40         static GConverter & get();
41
42         /// Can the conversion be performed?
43         bool isReachable(string const & from_format_name,
44                          string const & to_format_name) const;
45
46         /** Convert the file and at the end return it by emitting this signal
47          *  If successful, the returned string will be the name of the
48          *  converted file (to_file_base + extension(to_format_name)).
49          *  If unsuccessful, the string will be empty.
50          */
51         typedef SigC::Signal1<void, string const &> SignalType;
52         ///
53         typedef boost::shared_ptr<SignalType> SignalTypePtr;
54         ///
55         void convert(string const & from_file,   string const & to_file_base,
56                      string const & from_format, string const & to_format,
57                      SignalTypePtr on_finish);
58
59 private:
60         /** Make the c-tor private so we can control how many objects
61          *  are instantiated.
62          */
63         GConverter() {}
64
65         /** Build the conversion script, returning true if able to build it.
66          *  The script is output to the ostringstream 'script'.
67          */
68         bool build_script(string const & from_file, string const & to_file_base,
69                           string const & from_format, string const & to_format,
70                           ostringstream & script) const;
71
72         /** Remove the ConvProcess from the list of all processes.
73          *  Called by ConvProcess::converted.
74          */
75         friend class ConvProcess;
76         ///
77         void erase(ConvProcess *);
78
79         /// The list of all conversion processs
80         typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
81         ///
82         std::list<ConvProcessPtr> all_processes_;
83 };
84
85
86 /// Each ConvProcess represents a single conversion process.
87 struct ConvProcess : public SigC::Object
88 {
89         ///
90         typedef GConverter::SignalTypePtr SignalTypePtr;
91
92         /** Each ConvProcess represents a single conversion process.
93          *  It is passed :
94          *  1. The name of the script_file, which it deletes once the
95          *     conversion is comlpeted;
96          *  2. The script command itself, which it passes on to the forked
97          *     call process;
98          *  3. The name of the output file, which it returns to the calling
99          *     process on successfull completion, by emitting
100          *  4. The signal on_finish.
101          */
102         ConvProcess(string const & script_file, string const & script_command,
103                     string const & to_file, SignalTypePtr on_finish);
104
105         /** This method is connected to a signal passed to the forked call
106          *  class, passing control back here when the conversion is completed.
107          *  Cleans-up the temporary files, emits the on_finish signal and
108          *  removes the ConvProcess from the list of all processes.
109          */
110         void converted(string cmd, pid_t pid, int retval);
111
112         ///
113         string script_file_;
114         ///
115         string to_file_;
116         ///
117         SignalTypePtr on_finish_;
118 };
119
120 } // namespace grfx
121
122 #endif // GRAPHICSCONVERTER_H