2 * \file GraphicsConverter.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
13 #include "GraphicsConverter.h"
15 #include "converter.h"
18 #include "support/filetools.h"
19 #include "support/forkedcall.h"
20 #include "support/lyxlib.h"
22 #include <boost/bind.hpp>
23 #include <boost/signals/trackable.hpp>
26 #include "support/LOstream.h"
28 #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(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 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
83 Converter::~Converter()
87 void Converter::startConversion() const
89 pimpl_->startConversion();
93 boost::signals::connection Converter::connect(slot_type const & slot) const
95 return pimpl_->finishedConversion.connect(slot);
99 string const & Converter::convertedFile() const
101 static string const empty;
102 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
107 //------------------------------
108 // Implementation details follow
109 //------------------------------
113 /** Build the conversion script, returning true if able to build it.
114 * The script is output to the ostringstream 'script'.
116 bool build_script(string const & from_file, string const & to_file_base,
117 string const & from_format, string const & to_format,
125 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
126 string const & from_format, string const & to_format)
127 : valid_process_(false), finished_(false)
129 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
130 << "\tfrom_file: " << from_file
131 << "\n\tto_file_base: " << to_file_base
132 << "\n\tfrom_format: " << from_format
133 << "\n\tto_format: " << to_format << endl;
135 // The converted image is to be stored in this file (we do not
136 // use ChangeExtension because this is a basename which may
137 // nevertheless contain a '.')
138 to_file_ = to_file_base + '.' + formats.extension(to_format);
140 // The conversion commands are stored in a stringstream
141 ostringstream script;
142 script << "#!/bin/sh\n";
143 bool const success = build_script(from_file, to_file_base,
144 from_format, to_format, script);
148 "sh " + LibFileSearch("scripts", "convertDefault.sh") +
149 ' ' + from_format + ':' + from_file + ' ' +
150 to_format + ':' + to_file_;
152 lyxerr[Debug::GRAPHICS]
153 << "\tNo converter defined! I use convertDefault.sh\n\t"
154 << script_command_ << endl;
158 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
159 << "\n--------------------------------------\n"
160 << STRCONV(script.str())
161 << "\n--------------------------------------\n";
163 // Output the script to file.
164 static int counter = 0;
165 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
166 tostr(counter++) + ".sh";
168 std::ofstream fs(script_file_.c_str());
172 fs << STRCONV(script.str());
175 // The command needed to run the conversion process
176 // We create a dummy command for ease of understanding of the
177 // list of forked processes.
178 // Note that 'sh ' is absolutely essential, or execvp will fail.
179 script_command_ = "sh " + script_file_ + ' ' +
180 OnlyFilename(from_file) + ' ' + to_format;
182 // All is ready to go
183 valid_process_ = true;
187 void Converter::Impl::startConversion()
189 if (!valid_process_) {
194 // Initiate the conversion
195 Forkedcall::SignalTypePtr convert_ptr;
196 convert_ptr.reset(new Forkedcall::SignalType);
198 convert_ptr->connect(
199 boost::bind(&Impl::converted, this, _1, _2));
202 int retval = call.startscript(script_command_, convert_ptr);
204 // Unable to even start the script, so clean-up the mess!
210 void Converter::Impl::converted(pid_t /* pid */, int retval)
213 // We're done already!
217 // Clean-up behind ourselves
218 lyx::unlink(script_file_);
221 lyx::unlink(to_file_);
223 finishedConversion(false);
225 finishedConversion(true);
233 string const move_file(string const & from_file, string const & to_file)
235 if (from_file == to_file)
238 ostringstream command;
239 command << "fromfile=" << from_file << "\n"
240 << "tofile=" << to_file << "\n\n"
241 << "'mv' -f ${fromfile} ${tofile} ||\n"
243 << "\t'cp' -f ${fromfile} ${tofile} ||\n"
247 << "\t'rm' -f ${fromfile}\n"
250 return STRCONV(command.str());
254 bool build_script(string const & from_file,
255 string const & to_file_base,
256 string const & from_format,
257 string const & to_format,
260 lyxerr[Debug::GRAPHICS] << "build_script ... ";
261 typedef Converters::EdgePath EdgePath;
263 // we do not use ChangeExtension because this is a basename
264 // which may nevertheless contain a '.'
265 string const to_file = to_file_base + '.'
266 + formats.extension(to_format);
268 if (from_format == to_format) {
269 script << move_file(QuoteName(from_file), QuoteName(to_file));
270 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
274 EdgePath edgepath = converters.getPath(from_format, to_format);
276 if (edgepath.empty()) {
277 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
281 // Create a temporary base file-name for all intermediate steps.
282 // Remember to remove the temp file because we only want the name...
283 static int counter = 0;
284 string const tmp = "gconvert" + tostr(counter++);
285 string const to_base = lyx::tempName(string(), tmp);
286 lyx::unlink(to_base);
288 string outfile = from_file;
290 // The conversion commands may contain these tokens that need to be
291 // changed to infile, infile_base, outfile respectively.
292 string const token_from("$$i");
293 string const token_base("$$b");
294 string const token_to("$$o");
296 EdgePath::const_iterator it = edgepath.begin();
297 EdgePath::const_iterator end = edgepath.end();
299 for (; it != end; ++it) {
300 ::Converter const & conv = converters.get(*it);
302 // Build the conversion command
303 string const infile = outfile;
304 string const infile_base = ChangeExtension(infile, string());
305 outfile = ChangeExtension(to_base, conv.To->extension());
307 // Store these names in the shell script
308 script << "infile=" << QuoteName(infile) << '\n'
309 << "infile_base=" << QuoteName(infile_base) << '\n'
310 << "outfile=" << QuoteName(outfile) << '\n';
312 string command = conv.command;
313 command = subst(command, token_from, "${infile}");
314 command = subst(command, token_base, "${infile_base}");
315 command = subst(command, token_to, "${outfile}");
316 command = LibScriptSearch(command);
318 // Store in the shell script
319 script << "\n" << command << " ||\n";
321 // Test that this was successful. If not, remove
322 // ${outfile} and exit the shell script
324 << "\t'rm' -f ${outfile}\n"
328 // Test that the outfile exists.
329 // ImageMagick's convert will often create ${outfile}.0,
331 // If this occurs, move ${outfile}.0 to ${outfile}
332 // and delete ${outfile}.?
333 script << "if [ ! -f ${outfile} ]; then\n"
334 << "\tif [ -f ${outfile}.0 ]; then\n"
335 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
336 << "\t\t'rm' -f ${outfile}.?\n"
342 // Delete the infile, if it isn't the original, from_file.
343 if (infile != from_file) {
344 script << "'rm' -f ${infile}\n\n";
348 // Move the final outfile to to_file
349 script << move_file("${outfile}", QuoteName(to_file));
350 lyxerr[Debug::GRAPHICS] << "ready!" << endl;