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