2 * \file GraphicsConverter.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <leeming@lyx.org>
12 #pragma implementation
15 #include "GraphicsConverter.h"
17 #include "converter.h"
20 #include "support/filetools.h"
21 #include "support/forkedcall.h"
22 #include "support/lyxlib.h"
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
29 #include <sys/types.h> // needed for pid_t
35 struct Converter::Impl : public boost::signals::trackable {
37 Impl(string const &, string const &, string const &, string const &);
40 void startConversion();
42 /** This method is connected to a signal passed to the forked call
43 * class, passing control back here when the conversion is completed.
44 * Cleans-up the temporary files, emits the finishedConversion
45 * signal and removes the Converter from the list of all processes.
47 void converted(string const & cmd, pid_t pid, int retval);
49 /** At the end of the conversion process inform the outside world
50 * by emitting a signal.
52 typedef boost::signal1<void, bool> SignalType;
54 SignalType finishedConversion;
57 string script_command_;
69 bool Converter::isReachable(string const & from_format_name,
70 string const & to_format_name)
72 return converters.isReachable(from_format_name, to_format_name);
76 Converter::Converter(string const & from_file, string const & to_file_base,
77 string const & from_format, string const & to_format)
78 : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
82 Converter::~Converter()
86 void Converter::startConversion() const
88 pimpl_->startConversion();
92 boost::signals::connection Converter::connect(slot_type const & slot) const
94 return pimpl_->finishedConversion.connect(slot);
98 string const & Converter::convertedFile() const
100 static string const empty;
101 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
106 //------------------------------
107 // Implementation details follow
108 //------------------------------
112 /** Build the conversion script, returning true if able to build it.
113 * The script is output to the ostringstream 'script'.
115 bool build_script(string const & from_file, string const & to_file_base,
116 string const & from_format, string const & to_format,
117 ostringstream & script);
124 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
125 string const & from_format, string const & to_format)
126 : valid_process_(false), finished_(false)
128 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
129 << "\tfrom_file: " << from_file
130 << "\n\tto_file_base: " << to_file_base
131 << "\n\tfrom_format: " << from_format
132 << "\n\tto_format: " << to_format << endl;
134 // The conversion commands are stored in a stringstream
135 ostringstream script;
136 script << "#!/bin/sh\n";
137 bool const success = build_script(from_file, to_file_base,
138 from_format, to_format, script);
143 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
144 << "\n--------------------------------------\n"
145 << script.str().c_str()
146 << "\n--------------------------------------\n";
148 // Output the script to file.
149 static int counter = 0;
150 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
151 tostr(counter++) + ".sh";
153 std::ofstream fs(script_file_.c_str());
157 fs << script.str().c_str();
160 // The converted image is to be stored in this file
161 // We do not use ChangeExtension here because this is a
162 // basename, which may nevertheless contain a dot
163 to_file_ = to_file_base + '.' + formats.extension(to_format);
165 // The command needed to run the conversion process
166 // We create a dummy command for ease of understanding of the
167 // list of forked processes.
168 // Note that 'sh ' is absolutely essential, or execvp will fail.
169 script_command_ = "sh " + script_file_ + " " +
170 OnlyFilename(from_file) + " " + to_format;
172 // All is ready to go
173 valid_process_ = true;
177 void Converter::Impl::startConversion()
179 if (!valid_process_) {
180 converted(string(), 0, 1);
184 // Initiate the conversion
185 Forkedcall::SignalTypePtr convert_ptr;
186 convert_ptr.reset(new Forkedcall::SignalType);
188 convert_ptr->connect(
189 boost::bind(&Impl::converted, this, _1, _2, _3));
192 int retval = call.startscript(script_command_, convert_ptr);
194 // Unable to even start the script, so clean-up the mess!
195 converted(string(), 0, 1);
200 void Converter::Impl::converted(string const & /* cmd */,
201 pid_t /* pid */, int retval)
204 // We're done already!
208 // Clean-up behind ourselves
209 lyx::unlink(script_file_);
212 lyx::unlink(to_file_);
214 finishedConversion(false);
216 finishedConversion(true);
224 string const move_file(string const & from_file, string const & to_file)
226 if (from_file == to_file)
229 ostringstream command;
230 command << "fromfile=" << from_file << "\n"
231 << "tofile=" << to_file << "\n\n"
232 << "'mv' -f ${fromfile} ${tofile}\n"
233 << "if [ $? -ne 0 ]; then\n"
234 << "\t'cp' -f ${fromfile} ${tofile}\n"
235 << "\tif [ $? -ne 0 ]; then\n"
238 << "\t'rm' -f ${fromfile}\n"
241 return command.str().c_str();
244 bool build_script(string const & from_file,
245 string const & to_file_base,
246 string const & from_format,
247 string const & to_format,
248 ostringstream & script)
250 lyxerr[Debug::GRAPHICS] << "build_script ... ";
251 typedef Converters::EdgePath EdgePath;
253 string const to_file = ChangeExtension(to_file_base,
254 formats.extension(to_format));
256 if (from_format == to_format) {
257 script << move_file(QuoteName(from_file), QuoteName(to_file));
258 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
262 EdgePath edgepath = converters.getPath(from_format, to_format);
264 if (edgepath.empty()) {
265 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
269 // Create a temporary base file-name for all intermediate steps.
270 // Remember to remove the temp file because we only want the name...
271 static int counter = 0;
272 string const tmp = "gconvert" + tostr(counter++);
273 string const to_base = lyx::tempName(string(), tmp);
274 lyx::unlink(to_base);
276 string outfile = from_file;
278 // The conversion commands may contain these tokens that need to be
279 // changed to infile, infile_base, outfile respectively.
280 string const token_from("$$i");
281 string const token_base("$$b");
282 string const token_to("$$o");
284 EdgePath::const_iterator it = edgepath.begin();
285 EdgePath::const_iterator end = edgepath.end();
287 for (; it != end; ++it) {
288 ::Converter const & conv = converters.get(*it);
290 // Build the conversion command
291 string const infile = outfile;
292 string const infile_base = ChangeExtension(infile, string());
293 outfile = ChangeExtension(to_base, conv.To->extension());
295 // Store these names in the shell script
296 script << "infile=" << QuoteName(infile) << '\n'
297 << "infile_base=" << QuoteName(infile_base) << '\n'
298 << "outfile=" << QuoteName(outfile) << '\n';
300 string command = conv.command;
301 command = subst(command, token_from, "${infile}");
302 command = subst(command, token_base, "${infile_base}");
303 command = subst(command, token_to, "${outfile}");
304 command = LibScriptSearch(command);
306 // Store in the shell script
307 script << "\n" << command << "\n\n";
309 // Test that this was successful. If not, remove
310 // ${outfile} and exit the shell script
311 script << "if [ $? -ne 0 ]; then\n"
312 << "\t'rm' -f ${outfile}\n"
316 // Test that the outfile exists.
317 // ImageMagick's convert will often create ${outfile}.0,
319 // If this occurs, move ${outfile}.0 to ${outfile}
320 // and delete ${outfile}.?
321 script << "if [ ! -f ${outfile} ]; then\n"
322 << "\tif [ -f ${outfile}.0 ]; then\n"
323 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
324 << "\t\t'rm' -f ${outfile}.?\n"
330 // Delete the infile, if it isn't the original, from_file.
331 if (infile != from_file) {
332 script << "'rm' -f ${infile}\n\n";
336 // Move the final outfile to to_file
337 script << move_file("${outfile}", QuoteName(to_file));
338 lyxerr[Debug::GRAPHICS] << "ready!" << endl;