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"
19 #include "support/filetools.h"
20 #include "support/forkedcallqueue.h"
21 #include "support/tostr.h"
22 #include "support/lstrings.h"
23 #include "support/lyxlib.h"
25 #include <boost/bind.hpp>
27 #include "support/std_sstream.h"
30 namespace support = lyx::support;
32 using support::ChangeExtension;
33 using support::Forkedcall;
34 using support::ForkedCallQueue;
35 using support::LibFileSearch;
36 using support::LibScriptSearch;
37 using support::OnlyPath;
38 using support::OnlyFilename;
39 using support::QuoteName;
41 using support::tempName;
42 using support::unlink;
46 using std::ostringstream;
53 struct Converter::Impl : public boost::signals::trackable {
55 Impl(string const &, string const &, string const &, string const &);
58 void startConversion();
60 /** This method is connected to a signal passed to the forked call
61 * class, passing control back here when the conversion is completed.
62 * Cleans-up the temporary files, emits the finishedConversion
63 * signal and removes the Converter from the list of all processes.
65 void converted(pid_t pid, int retval);
67 /** At the end of the conversion process inform the outside world
68 * by emitting a signal.
70 typedef boost::signal1<void, bool> SignalType;
72 SignalType finishedConversion;
75 string script_command_;
87 bool Converter::isReachable(string const & from_format_name,
88 string const & to_format_name)
90 return converters.isReachable(from_format_name, to_format_name);
94 Converter::Converter(string const & from_file, string const & to_file_base,
95 string const & from_format, string const & to_format)
96 : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
100 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
101 Converter::~Converter()
105 void Converter::startConversion() const
107 pimpl_->startConversion();
111 boost::signals::connection Converter::connect(slot_type const & slot) const
113 return pimpl_->finishedConversion.connect(slot);
117 string const & Converter::convertedFile() const
119 static string const empty;
120 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
123 } // namespace graphics
127 //------------------------------
128 // Implementation details follow
129 //------------------------------
133 /** Build the conversion script, returning true if able to build it.
134 * The script is output to the ostringstream 'script'.
136 bool build_script(string const & from_file, string const & to_file_base,
137 string const & from_format, string const & to_format,
146 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
147 string const & from_format, string const & to_format)
148 : valid_process_(false), finished_(false)
150 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
151 << "\tfrom_file: " << from_file
152 << "\n\tto_file_base: " << to_file_base
153 << "\n\tfrom_format: " << from_format
154 << "\n\tto_format: " << to_format << endl;
156 // The converted image is to be stored in this file (we do not
157 // use ChangeExtension because this is a basename which may
158 // nevertheless contain a '.')
159 to_file_ = to_file_base + '.' + formats.extension(to_format);
161 // The conversion commands are stored in a stringstream
162 ostringstream script;
163 script << "#!/bin/sh\n";
164 bool const success = build_script(from_file, to_file_base,
165 from_format, to_format, script);
169 "sh " + LibFileSearch("scripts", "convertDefault.sh") +
170 ' ' + from_format + ':' + from_file + ' ' +
171 to_format + ':' + to_file_;
173 lyxerr[Debug::GRAPHICS]
174 << "\tNo converter defined! I use convertDefault.sh\n\t"
175 << script_command_ << endl;
179 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
180 << "\n--------------------------------------\n"
182 << "\n--------------------------------------\n";
184 // Output the script to file.
185 static int counter = 0;
186 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
187 tostr(counter++) + ".sh";
189 std::ofstream fs(script_file_.c_str());
196 // The command needed to run the conversion process
197 // We create a dummy command for ease of understanding of the
198 // list of forked processes.
199 // Note: 'sh ' is absolutely essential, or execvp will fail.
200 script_command_ = "sh " + script_file_ + ' ' +
201 OnlyFilename(from_file) + ' ' + to_format;
203 // All is ready to go
204 valid_process_ = true;
208 void Converter::Impl::startConversion()
210 if (!valid_process_) {
215 Forkedcall::SignalTypePtr
216 ptr = ForkedCallQueue::get().add(script_command_);
218 ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
222 void Converter::Impl::converted(pid_t /* pid */, int retval)
225 // We're done already!
229 // Clean-up behind ourselves
230 unlink(script_file_);
235 finishedConversion(false);
237 finishedConversion(true);
241 } // namespace graphics
246 string const move_file(string const & from_file, string const & to_file)
248 if (from_file == to_file)
251 ostringstream command;
252 command << "fromfile=" << from_file << "\n"
253 << "tofile=" << to_file << "\n\n"
254 << "'mv' -f ${fromfile} ${tofile} ||\n"
256 << "\t'cp' -f ${fromfile} ${tofile} ||\n"
260 << "\t'rm' -f ${fromfile}\n"
263 return command.str();
267 bool build_script(string const & from_file,
268 string const & to_file_base,
269 string const & from_format,
270 string const & to_format,
273 lyxerr[Debug::GRAPHICS] << "build_script ... ";
274 typedef Converters::EdgePath EdgePath;
276 // we do not use ChangeExtension because this is a basename
277 // which may nevertheless contain a '.'
278 string const to_file = to_file_base + '.'
279 + formats.extension(to_format);
281 if (from_format == to_format) {
282 script << move_file(QuoteName(from_file), QuoteName(to_file));
283 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
287 EdgePath edgepath = converters.getPath(from_format, to_format);
289 if (edgepath.empty()) {
290 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
294 // Create a temporary base file-name for all intermediate steps.
295 // Remember to remove the temp file because we only want the name...
296 static int counter = 0;
297 string const tmp = "gconvert" + tostr(counter++);
298 string const to_base = tempName(string(), tmp);
301 string outfile = from_file;
303 // The conversion commands may contain these tokens that need to be
304 // changed to infile, infile_base, outfile respectively.
305 string const token_from("$$i");
306 string const token_base("$$b");
307 string const token_to("$$o");
309 EdgePath::const_iterator it = edgepath.begin();
310 EdgePath::const_iterator end = edgepath.end();
312 for (; it != end; ++it) {
313 ::Converter const & conv = converters.get(*it);
315 // Build the conversion command
316 string const infile = outfile;
317 string const infile_base = ChangeExtension(infile, string());
318 outfile = ChangeExtension(to_base, conv.To->extension());
320 // Store these names in the shell script
321 script << "infile=" << QuoteName(infile) << '\n'
322 << "infile_base=" << QuoteName(infile_base) << '\n'
323 << "outfile=" << QuoteName(outfile) << '\n';
325 string command = conv.command;
326 command = subst(command, token_from, "${infile}");
327 command = subst(command, token_base, "${infile_base}");
328 command = subst(command, token_to, "${outfile}");
329 command = LibScriptSearch(command);
331 // Store in the shell script
332 script << "\n" << command << " ||\n";
334 // Test that this was successful. If not, remove
335 // ${outfile} and exit the shell script
337 << "\t'rm' -f ${outfile}\n"
341 // Test that the outfile exists.
342 // ImageMagick's convert will often create ${outfile}.0,
344 // If this occurs, move ${outfile}.0 to ${outfile}
345 // and delete ${outfile}.?
346 script << "if [ ! -f ${outfile} ]; then\n"
347 << "\tif [ -f ${outfile}.0 ]; then\n"
348 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
349 << "\t\t'rm' -f ${outfile}.?\n"
355 // Delete the infile, if it isn't the original, from_file.
356 if (infile != from_file) {
357 script << "'rm' -f ${infile}\n\n";
361 // Move the final outfile to to_file
362 script << move_file("${outfile}", QuoteName(to_file));
363 lyxerr[Debug::GRAPHICS] << "ready!" << endl;