3 * \file GraphicsConverter.h
4 * Copyright 2002 the LyX Team
5 * Read the file COPYING
7 * \author Angus Leeming <a.leeming@ic.ac.uk>
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.
16 * Ultimately, this class should be wrapped back into Dekel's converter class.
19 #ifndef GRAPHICSCONVERTER_H
20 #define GRAPHICSCONVERTER_H
25 #include <boost/shared_ptr.hpp>
26 #include <boost/utility.hpp>
28 #include <boost/signals/signal1.hpp>
29 #include <boost/signals/trackable.hpp>
33 #include <sys/types.h> // needed for pid_t
43 class GConverter : boost::noncopyable {
46 /// This is a singleton class. Get the instance.
47 static GConverter & get();
49 /// Can the conversion be performed?
50 bool isReachable(string const & from_format_name,
51 string const & to_format_name) const;
53 /** Convert the file and at the end return it by emitting this signal
54 * If successful, the returned string will be the name of the
55 * converted file (to_file_base + extension(to_format_name)).
56 * If unsuccessful, the string will be empty.
58 typedef boost::signal1<void, string const &> SignalType;
60 typedef boost::shared_ptr<SignalType> SignalTypePtr;
62 void convert(string const & from_file, string const & to_file_base,
63 string const & from_format, string const & to_format,
64 SignalTypePtr on_finish);
67 /** Make the c-tor private so we can control how many objects
72 /** Build the conversion script, returning true if able to build it.
73 * The script is output to the ostringstream 'script'.
75 bool build_script(string const & from_file, string const & to_file_base,
76 string const & from_format, string const & to_format,
77 ostringstream & script) const;
79 /** Remove the ConvProcess from the list of all processes.
80 * Called by ConvProcess::converted.
82 friend class ConvProcess;
84 void erase(ConvProcess *);
86 /// The list of all conversion processs
87 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
89 std::list<ConvProcessPtr> all_processes_;
93 /// Each ConvProcess represents a single conversion process.
94 struct ConvProcess : public boost::signals::trackable
97 typedef GConverter::SignalTypePtr SignalTypePtr;
99 /** Each ConvProcess represents a single conversion process.
101 * 1. The name of the script_file, which it deletes once the
102 * conversion is comlpeted;
103 * 2. The script command itself, which it passes on to the forked
105 * 3. The name of the output file, which it returns to the calling
106 * process on successfull completion, by emitting
107 * 4. The signal on_finish.
109 ConvProcess(string const & script_file, string const & script_command,
110 string const & to_file, SignalTypePtr on_finish);
112 /** This method is connected to a signal passed to the forked call
113 * class, passing control back here when the conversion is completed.
114 * Cleans-up the temporary files, emits the on_finish signal and
115 * removes the ConvProcess from the list of all processes.
117 void converted(string const & cmd, pid_t pid, int retval);
124 SignalTypePtr on_finish_;
129 #endif // GRAPHICSCONVERTER_H