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/convert.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 class Converter::Impl : public boost::signals::trackable {
56 Impl(string const &, string const &, string const &, string const &);
59 void startConversion();
61 /** This method is connected to a signal passed to the forked call
62 * class, passing control back here when the conversion is completed.
63 * Cleans-up the temporary files, emits the finishedConversion
64 * signal and removes the Converter from the list of all processes.
66 void converted(pid_t pid, int retval);
68 /** At the end of the conversion process inform the outside world
69 * by emitting a signal.
71 typedef boost::signal<void(bool)> SignalType;
73 SignalType finishedConversion;
76 string script_command_;
88 bool Converter::isReachable(string const & from_format_name,
89 string const & to_format_name)
91 return converters.isReachable(from_format_name, to_format_name);
95 Converter::Converter(string const & from_file, string const & to_file_base,
96 string const & from_format, string const & to_format)
97 : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
101 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
102 Converter::~Converter()
106 void Converter::startConversion() const
108 pimpl_->startConversion();
112 boost::signals::connection Converter::connect(slot_type const & slot) const
114 return pimpl_->finishedConversion.connect(slot);
118 string const & Converter::convertedFile() const
120 static string const empty;
121 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
124 } // namespace graphics
128 //------------------------------
129 // Implementation details follow
130 //------------------------------
134 /** Build the conversion script, returning true if able to build it.
135 * The script is output to the ostringstream 'script'.
137 bool build_script(string const & from_file, string const & to_file_base,
138 string const & from_format, string const & to_format,
147 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
148 string const & from_format, string const & to_format)
149 : valid_process_(false), finished_(false)
151 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
152 << "\tfrom_file: " << from_file
153 << "\n\tto_file_base: " << to_file_base
154 << "\n\tfrom_format: " << from_format
155 << "\n\tto_format: " << to_format << endl;
157 // The converted image is to be stored in this file (we do not
158 // use ChangeExtension because this is a basename which may
159 // nevertheless contain a '.')
160 to_file_ = to_file_base + '.' + formats.extension(to_format);
162 // The conversion commands are stored in a stringstream
163 ostringstream script;
164 script << "#!/bin/sh\n";
165 bool const success = build_script(from_file, to_file_base,
166 from_format, to_format, script);
171 quoteName(libFileSearch("scripts", "convertDefault.sh")) +
173 quoteName((from_format.empty() ? "" : from_format + ':') + from_file) +
175 quoteName(to_format + ':' + to_file_);
177 lyxerr[Debug::GRAPHICS]
178 << "\tNo converter defined! I use convertDefault.sh\n\t"
179 << script_command_ << endl;
183 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
184 << "\n--------------------------------------\n"
186 << "\n--------------------------------------\n";
188 // Output the script to file.
189 static int counter = 0;
190 script_file_ = onlyPath(to_file_base) + "lyxconvert" +
191 convert<string>(counter++) + ".sh";
193 std::ofstream fs(script_file_.c_str());
195 lyxerr << "Unable to write the conversion script to \""
196 << script_file_ << '\n'
197 << "Please check your directory permissions."
205 // The command needed to run the conversion process
206 // We create a dummy command for ease of understanding of the
207 // list of forked processes.
208 // Note: 'sh ' is absolutely essential, or execvp will fail.
209 script_command_ = "sh " + quoteName(script_file_) + ' ' +
210 quoteName(onlyFilename(from_file)) + ' ' +
211 quoteName(to_format);
213 // All is ready to go
214 valid_process_ = true;
218 void Converter::Impl::startConversion()
220 if (!valid_process_) {
225 Forkedcall::SignalTypePtr
226 ptr = ForkedCallQueue::get().add(script_command_);
228 ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
232 void Converter::Impl::converted(pid_t /* pid */, int retval)
235 // We're done already!
239 // Clean-up behind ourselves
240 unlink(script_file_);
245 finishedConversion(false);
247 finishedConversion(true);
251 } // namespace graphics
256 string const move_file(string const & from_file, string const & to_file)
258 if (from_file == to_file)
261 ostringstream command;
262 command << "fromfile=" << from_file << "\n"
263 << "tofile=" << to_file << "\n\n"
264 << "'mv' -f \"${fromfile}\" \"${tofile}\" ||\n"
266 << "\t'cp' -f \"${fromfile}\" \"${tofile}\" ||\n"
270 << "\t'rm' -f \"${fromfile}\"\n"
273 return command.str();
277 bool build_script(string const & from_file,
278 string const & to_file_base,
279 string const & from_format,
280 string const & to_format,
283 lyxerr[Debug::GRAPHICS] << "build_script ... ";
284 typedef Converters::EdgePath EdgePath;
286 if (from_format.empty())
289 // we do not use ChangeExtension because this is a basename
290 // which may nevertheless contain a '.'
291 string const to_file = to_file_base + '.'
292 + formats.extension(to_format);
294 if (from_format == to_format) {
295 script << move_file(quoteName(from_file), quoteName(to_file));
296 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
300 EdgePath edgepath = converters.getPath(from_format, to_format);
302 if (edgepath.empty()) {
303 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
307 // Create a temporary base file-name for all intermediate steps.
308 // Remember to remove the temp file because we only want the name...
309 static int counter = 0;
310 string const tmp = "gconvert" + convert<string>(counter++);
311 string const to_base = tempName(string(), tmp);
314 string outfile = from_file;
316 // The conversion commands may contain these tokens that need to be
317 // changed to infile, infile_base, outfile respectively.
318 string const token_from("$$i");
319 string const token_base("$$b");
320 string const token_to("$$o");
322 EdgePath::const_iterator it = edgepath.begin();
323 EdgePath::const_iterator end = edgepath.end();
325 for (; it != end; ++it) {
326 ::Converter const & conv = converters.get(*it);
328 // Build the conversion command
329 string const infile = outfile;
330 string const infile_base = changeExtension(infile, string());
331 outfile = changeExtension(to_base, conv.To->extension());
333 // Store these names in the shell script
334 script << "infile=" << quoteName(infile) << '\n'
335 << "infile_base=" << quoteName(infile_base) << '\n'
336 << "outfile=" << quoteName(outfile) << '\n';
338 string command = conv.command;
339 command = subst(command, token_from, "\"${infile}\"");
340 command = subst(command, token_base, "\"${infile_base}\"");
341 command = subst(command, token_to, "\"${outfile}\"");
342 command = libScriptSearch(command);
344 // Store in the shell script
345 script << "\n" << command << " ||\n";
347 // Test that this was successful. If not, remove
348 // ${outfile} and exit the shell script
350 << "\t'rm' -f \"${outfile}\"\n"
354 // Test that the outfile exists.
355 // ImageMagick's convert will often create ${outfile}.0,
357 // If this occurs, move ${outfile}.0 to ${outfile}
358 // and delete ${outfile}.?
359 script << "if [ ! -f \"${outfile}\" ]; then\n"
360 << "\tif [ -f \"${outfile}\".0 ]; then\n"
361 << "\t\t'mv' -f \"${outfile}\".0 \"${outfile}\"\n"
362 << "\t\t'rm' -f \"${outfile}\".?\n"
368 // Delete the infile, if it isn't the original, from_file.
369 if (infile != from_file) {
370 script << "'rm' -f \"${infile}\"\n\n";
374 // Move the final outfile to to_file
375 script << move_file("\"${outfile}\"", quoteName(to_file));
376 lyxerr[Debug::GRAPHICS] << "ready!" << endl;