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/forkedcall.h"
21 #include "support/forkedcallqueue.h"
22 #include "support/tostr.h"
23 #include "support/lstrings.h"
24 #include "support/lyxlib.h"
26 #include <boost/bind.hpp>
27 #include <boost/signals/trackable.hpp>
30 #include "support/LOstream.h"
32 #include <sys/types.h> // needed for pid_t
34 using namespace lyx::support;
42 struct Converter::Impl : public boost::signals::trackable {
44 Impl(string const &, string const &, string const &, string const &);
47 void startConversion();
49 /** This method is connected to a signal passed to the forked call
50 * class, passing control back here when the conversion is completed.
51 * Cleans-up the temporary files, emits the finishedConversion
52 * signal and removes the Converter from the list of all processes.
54 void converted(pid_t pid, int retval);
56 /** At the end of the conversion process inform the outside world
57 * by emitting a signal.
59 typedef boost::signal1<void, bool> SignalType;
61 SignalType finishedConversion;
64 string script_command_;
76 bool Converter::isReachable(string const & from_format_name,
77 string const & to_format_name)
79 return converters.isReachable(from_format_name, to_format_name);
83 Converter::Converter(string const & from_file, string const & to_file_base,
84 string const & from_format, string const & to_format)
85 : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
89 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
90 Converter::~Converter()
94 void Converter::startConversion() const
96 pimpl_->startConversion();
100 boost::signals::connection Converter::connect(slot_type const & slot) const
102 return pimpl_->finishedConversion.connect(slot);
106 string const & Converter::convertedFile() const
108 static string const empty;
109 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
112 } // namespace graphics
116 //------------------------------
117 // Implementation details follow
118 //------------------------------
122 /** Build the conversion script, returning true if able to build it.
123 * The script is output to the ostringstream 'script'.
125 bool build_script(string const & from_file, string const & to_file_base,
126 string const & from_format, string const & to_format,
135 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
136 string const & from_format, string const & to_format)
137 : valid_process_(false), finished_(false)
139 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
140 << "\tfrom_file: " << from_file
141 << "\n\tto_file_base: " << to_file_base
142 << "\n\tfrom_format: " << from_format
143 << "\n\tto_format: " << to_format << endl;
145 // The converted image is to be stored in this file (we do not
146 // use ChangeExtension because this is a basename which may
147 // nevertheless contain a '.')
148 to_file_ = to_file_base + '.' + formats.extension(to_format);
150 // The conversion commands are stored in a stringstream
151 ostringstream script;
152 script << "#!/bin/sh\n";
153 bool const success = build_script(from_file, to_file_base,
154 from_format, to_format, script);
158 "sh " + LibFileSearch("scripts", "convertDefault.sh") +
159 ' ' + from_format + ':' + from_file + ' ' +
160 to_format + ':' + to_file_;
162 lyxerr[Debug::GRAPHICS]
163 << "\tNo converter defined! I use convertDefault.sh\n\t"
164 << script_command_ << endl;
168 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
169 << "\n--------------------------------------\n"
170 << STRCONV(script.str())
171 << "\n--------------------------------------\n";
173 // Output the script to file.
174 static int counter = 0;
175 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
176 tostr(counter++) + ".sh";
178 std::ofstream fs(script_file_.c_str());
182 fs << STRCONV(script.str());
185 // The command needed to run the conversion process
186 // We create a dummy command for ease of understanding of the
187 // list of forked processes.
188 // Note: 'sh ' is absolutely essential, or execvp will fail.
189 script_command_ = "sh " + script_file_ + ' ' +
190 OnlyFilename(from_file) + ' ' + to_format;
192 // All is ready to go
193 valid_process_ = true;
197 void Converter::Impl::startConversion()
199 if (!valid_process_) {
204 Forkedcall::SignalTypePtr
205 ptr = ForkedCallQueue::get().add(script_command_);
207 ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
211 void Converter::Impl::converted(pid_t /* pid */, int retval)
214 // We're done already!
218 // Clean-up behind ourselves
219 unlink(script_file_);
224 finishedConversion(false);
226 finishedConversion(true);
230 } // namespace graphics
235 string const move_file(string const & from_file, string const & to_file)
237 if (from_file == to_file)
240 ostringstream command;
241 command << "fromfile=" << from_file << "\n"
242 << "tofile=" << to_file << "\n\n"
243 << "'mv' -f ${fromfile} ${tofile} ||\n"
245 << "\t'cp' -f ${fromfile} ${tofile} ||\n"
249 << "\t'rm' -f ${fromfile}\n"
252 return STRCONV(command.str());
256 bool build_script(string const & from_file,
257 string const & to_file_base,
258 string const & from_format,
259 string const & to_format,
262 lyxerr[Debug::GRAPHICS] << "build_script ... ";
263 typedef Converters::EdgePath EdgePath;
265 // we do not use ChangeExtension because this is a basename
266 // which may nevertheless contain a '.'
267 string const to_file = to_file_base + '.'
268 + formats.extension(to_format);
270 if (from_format == to_format) {
271 script << move_file(QuoteName(from_file), QuoteName(to_file));
272 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
276 EdgePath edgepath = converters.getPath(from_format, to_format);
278 if (edgepath.empty()) {
279 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
283 // Create a temporary base file-name for all intermediate steps.
284 // Remember to remove the temp file because we only want the name...
285 static int counter = 0;
286 string const tmp = "gconvert" + tostr(counter++);
287 string const to_base = tempName(string(), tmp);
290 string outfile = from_file;
292 // The conversion commands may contain these tokens that need to be
293 // changed to infile, infile_base, outfile respectively.
294 string const token_from("$$i");
295 string const token_base("$$b");
296 string const token_to("$$o");
298 EdgePath::const_iterator it = edgepath.begin();
299 EdgePath::const_iterator end = edgepath.end();
301 for (; it != end; ++it) {
302 ::Converter const & conv = converters.get(*it);
304 // Build the conversion command
305 string const infile = outfile;
306 string const infile_base = ChangeExtension(infile, string());
307 outfile = ChangeExtension(to_base, conv.To->extension());
309 // Store these names in the shell script
310 script << "infile=" << QuoteName(infile) << '\n'
311 << "infile_base=" << QuoteName(infile_base) << '\n'
312 << "outfile=" << QuoteName(outfile) << '\n';
314 string command = conv.command;
315 command = subst(command, token_from, "${infile}");
316 command = subst(command, token_base, "${infile_base}");
317 command = subst(command, token_to, "${outfile}");
318 command = LibScriptSearch(command);
320 // Store in the shell script
321 script << "\n" << command << " ||\n";
323 // Test that this was successful. If not, remove
324 // ${outfile} and exit the shell script
326 << "\t'rm' -f ${outfile}\n"
330 // Test that the outfile exists.
331 // ImageMagick's convert will often create ${outfile}.0,
333 // If this occurs, move ${outfile}.0 to ${outfile}
334 // and delete ${outfile}.?
335 script << "if [ ! -f ${outfile} ]; then\n"
336 << "\tif [ -f ${outfile}.0 ]; then\n"
337 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
338 << "\t\t'rm' -f ${outfile}.?\n"
344 // Delete the infile, if it isn't the original, from_file.
345 if (infile != from_file) {
346 script << "'rm' -f ${infile}\n\n";
350 // Move the final outfile to to_file
351 script << move_file("${outfile}", QuoteName(to_file));
352 lyxerr[Debug::GRAPHICS] << "ready!" << endl;