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