2 * \file GraphicsConverter.C
3 * Read the file COPYING
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
13 #pragma implementation
16 #include "GraphicsConverter.h"
18 #include "converter.h"
21 #include "support/filetools.h"
22 #include "support/forkedcall.h"
23 #include "support/lyxlib.h"
25 #include <boost/bind.hpp>
26 #include <boost/signals/trackable.hpp>
29 #include "support/LOstream.h"
31 #include <sys/types.h> // needed for pid_t
38 struct Converter::Impl : public boost::signals::trackable {
40 Impl(string const &, string const &, string const &, string const &);
43 void startConversion();
45 /** This method is connected to a signal passed to the forked call
46 * class, passing control back here when the conversion is completed.
47 * Cleans-up the temporary files, emits the finishedConversion
48 * signal and removes the Converter from the list of all processes.
50 void converted(string const & cmd, pid_t pid, int retval);
52 /** At the end of the conversion process inform the outside world
53 * by emitting a signal.
55 typedef boost::signal1<void, bool> SignalType;
57 SignalType finishedConversion;
60 string script_command_;
72 bool Converter::isReachable(string const & from_format_name,
73 string const & to_format_name)
75 return converters.isReachable(from_format_name, to_format_name);
79 Converter::Converter(string const & from_file, string const & to_file_base,
80 string const & from_format, string const & to_format)
81 : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
85 // Empty d-tor out-of-line to keep boost::scoped_ptr happy.
86 Converter::~Converter()
90 void Converter::startConversion() const
92 pimpl_->startConversion();
96 boost::signals::connection Converter::connect(slot_type const & slot) const
98 return pimpl_->finishedConversion.connect(slot);
102 string const & Converter::convertedFile() const
104 static string const empty;
105 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
110 //------------------------------
111 // Implementation details follow
112 //------------------------------
116 /** Build the conversion script, returning true if able to build it.
117 * The script is output to the ostringstream 'script'.
119 bool build_script(string const & from_file, string const & to_file_base,
120 string const & from_format, string const & to_format,
128 Converter::Impl::Impl(string const & from_file, string const & to_file_base,
129 string const & from_format, string const & to_format)
130 : valid_process_(false), finished_(false)
132 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
133 << "\tfrom_file: " << from_file
134 << "\n\tto_file_base: " << to_file_base
135 << "\n\tfrom_format: " << from_format
136 << "\n\tto_format: " << to_format << endl;
138 // The converted image is to be stored in this file
139 to_file_ = ChangeExtension(to_file_base, formats.extension(to_format));
141 // The conversion commands are stored in a stringstream
142 ostringstream script;
143 script << "#!/bin/sh\n";
144 bool const success = build_script(from_file, to_file_base,
145 from_format, to_format, script);
149 "sh " + LibFileSearch("scripts", "convertDefault.sh") +
150 ' ' + from_format + ':' + from_file + ' ' +
151 to_format + ':' + to_file_;
153 lyxerr[Debug::GRAPHICS]
154 << "\tNo converter defined! I use convertDefault.sh\n\t"
155 << script_command_ << endl;
159 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
160 << "\n--------------------------------------\n"
161 << script.str().c_str()
162 << "\n--------------------------------------\n";
164 // Output the script to file.
165 static int counter = 0;
166 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
167 tostr(counter++) + ".sh";
169 std::ofstream fs(script_file_.c_str());
173 fs << script.str().c_str();
176 // The command needed to run the conversion process
177 // We create a dummy command for ease of understanding of the
178 // list of forked processes.
179 // Note that 'sh ' is absolutely essential, or execvp will fail.
180 script_command_ = "sh " + script_file_ + " " +
181 OnlyFilename(from_file) + " " + to_format;
183 // All is ready to go
184 valid_process_ = true;
188 void Converter::Impl::startConversion()
190 if (!valid_process_) {
191 converted(string(), 0, 1);
195 // Initiate the conversion
196 Forkedcall::SignalTypePtr convert_ptr;
197 convert_ptr.reset(new Forkedcall::SignalType);
199 convert_ptr->connect(
200 boost::bind(&Impl::converted, this, _1, _2, _3));
203 int retval = call.startscript(script_command_, convert_ptr);
205 // Unable to even start the script, so clean-up the mess!
206 converted(string(), 0, 1);
211 void Converter::Impl::converted(string const & /* cmd */,
212 pid_t /* pid */, int retval)
215 // We're done already!
219 // Clean-up behind ourselves
220 lyx::unlink(script_file_);
223 lyx::unlink(to_file_);
225 finishedConversion(false);
227 finishedConversion(true);
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"
244 << "if [ $? -ne 0 ]; then\n"
245 << "\t'cp' -f ${fromfile} ${tofile}\n"
246 << "\tif [ $? -ne 0 ]; then\n"
249 << "\t'rm' -f ${fromfile}\n"
252 return command.str().c_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 string const to_file = ChangeExtension(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\n";
321 // Test that this was successful. If not, remove
322 // ${outfile} and exit the shell script
323 script << "if [ $? -ne 0 ]; then\n"
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;