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>
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());
191 lyxerr << "Unable to write the conversion script to \""
192 << script_file_ << '\n'
193 << "Please check your directory permissions."
201 // The command needed to run the conversion process
202 // We create a dummy command for ease of understanding of the
203 // list of forked processes.
204 // Note: 'sh ' is absolutely essential, or execvp will fail.
205 script_command_ = "sh " + script_file_ + ' ' +
206 OnlyFilename(from_file) + ' ' + to_format;
208 // All is ready to go
209 valid_process_ = true;
213 void Converter::Impl::startConversion()
215 if (!valid_process_) {
220 Forkedcall::SignalTypePtr
221 ptr = ForkedCallQueue::get().add(script_command_);
223 ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
227 void Converter::Impl::converted(pid_t /* pid */, int retval)
230 // We're done already!
234 // Clean-up behind ourselves
235 unlink(script_file_);
240 finishedConversion(false);
242 finishedConversion(true);
246 } // namespace graphics
251 string const move_file(string const & from_file, string const & to_file)
253 if (from_file == to_file)
256 ostringstream command;
257 command << "fromfile=" << from_file << "\n"
258 << "tofile=" << to_file << "\n\n"
259 << "'mv' -f ${fromfile} ${tofile} ||\n"
261 << "\t'cp' -f ${fromfile} ${tofile} ||\n"
265 << "\t'rm' -f ${fromfile}\n"
268 return command.str();
272 bool build_script(string const & from_file,
273 string const & to_file_base,
274 string const & from_format,
275 string const & to_format,
278 lyxerr[Debug::GRAPHICS] << "build_script ... ";
279 typedef Converters::EdgePath EdgePath;
281 // we do not use ChangeExtension because this is a basename
282 // which may nevertheless contain a '.'
283 string const to_file = to_file_base + '.'
284 + formats.extension(to_format);
286 if (from_format == to_format) {
287 script << move_file(QuoteName(from_file), QuoteName(to_file));
288 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
292 EdgePath edgepath = converters.getPath(from_format, to_format);
294 if (edgepath.empty()) {
295 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
299 // Create a temporary base file-name for all intermediate steps.
300 // Remember to remove the temp file because we only want the name...
301 static int counter = 0;
302 string const tmp = "gconvert" + tostr(counter++);
303 string const to_base = tempName(string(), tmp);
306 string outfile = from_file;
308 // The conversion commands may contain these tokens that need to be
309 // changed to infile, infile_base, outfile respectively.
310 string const token_from("$$i");
311 string const token_base("$$b");
312 string const token_to("$$o");
314 EdgePath::const_iterator it = edgepath.begin();
315 EdgePath::const_iterator end = edgepath.end();
317 for (; it != end; ++it) {
318 ::Converter const & conv = converters.get(*it);
320 // Build the conversion command
321 string const infile = outfile;
322 string const infile_base = ChangeExtension(infile, string());
323 outfile = ChangeExtension(to_base, conv.To->extension());
325 // Store these names in the shell script
326 script << "infile=" << QuoteName(infile) << '\n'
327 << "infile_base=" << QuoteName(infile_base) << '\n'
328 << "outfile=" << QuoteName(outfile) << '\n';
330 string command = conv.command;
331 command = subst(command, token_from, "${infile}");
332 command = subst(command, token_base, "${infile_base}");
333 command = subst(command, token_to, "${outfile}");
334 command = LibScriptSearch(command);
336 // Store in the shell script
337 script << "\n" << command << " ||\n";
339 // Test that this was successful. If not, remove
340 // ${outfile} and exit the shell script
342 << "\t'rm' -f ${outfile}\n"
346 // Test that the outfile exists.
347 // ImageMagick's convert will often create ${outfile}.0,
349 // If this occurs, move ${outfile}.0 to ${outfile}
350 // and delete ${outfile}.?
351 script << "if [ ! -f ${outfile} ]; then\n"
352 << "\tif [ -f ${outfile}.0 ]; then\n"
353 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
354 << "\t\t'rm' -f ${outfile}.?\n"
360 // Delete the infile, if it isn't the original, from_file.
361 if (infile != from_file) {
362 script << "'rm' -f ${infile}\n\n";
366 // Move the final outfile to to_file
367 script << move_file("${outfile}", QuoteName(to_file));
368 lyxerr[Debug::GRAPHICS] << "ready!" << endl;