2 * \file GraphicsConverter.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 #pragma implementation
15 #include "GraphicsConverter.h"
17 #include "converter.h"
20 #include "support/filetools.h"
21 #include "support/forkedcall.h"
22 #include "support/lyxlib.h"
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
29 #include <sys/types.h> // needed for pid_t
35 struct Converter::Impl : public boost::signals::trackable {
38 string const &, string const &, string const &, string const &);
41 void startConversion();
43 /** This method is connected to a signal passed to the forked call
44 * class, passing control back here when the conversion is completed.
45 * Cleans-up the temporary files, emits the finishedConversion
46 * signal and removes the Converter from the list of all processes.
48 void converted(string const & cmd, pid_t pid, int retval);
51 string script_command_;
65 Converter::Converter(string const & from_file, string const & to_file_base,
66 string const & from_format, string const & to_format)
67 : pimpl_(new Impl(*this,
68 from_file, to_file_base, from_format, to_format))
72 Converter::~Converter()
76 void Converter::startConversion()
78 pimpl_->startConversion();
82 bool Converter::isReachable(string const & from_format_name,
83 string const & to_format_name)
85 return converters.isReachable(from_format_name, to_format_name);
89 string const & Converter::convertedFile() const
91 static string const empty;
92 return pimpl_->finished_ ? pimpl_->to_file_ : empty;
97 //------------------------------
98 // Implementation details follow
99 //------------------------------
103 /** Build the conversion script, returning true if able to build it.
104 * The script is output to the ostringstream 'script'.
106 bool build_script(string const & from_file, string const & to_file_base,
107 string const & from_format, string const & to_format,
108 ostringstream & script);
115 Converter::Impl::Impl(Converter & p,
116 string const & from_file, string const & to_file_base,
117 string const & from_format, string const & to_format)
118 : parent_(p), valid_process_(false), finished_(false)
120 lyxerr[Debug::GRAPHICS] << "Converter c-tor:\n"
121 << "\tfrom_file: " << from_file
122 << "\n\tto_file_base: " << to_file_base
123 << "\n\tfrom_format: " << from_format
124 << "\n\tto_format: " << to_format << endl;
126 // The conversion commands are stored in a stringstream
127 ostringstream script;
128 script << "#!/bin/sh\n";
129 bool const success = build_script(from_file, to_file_base,
130 from_format, to_format, script);
135 lyxerr[Debug::GRAPHICS] << "\tConversion script:"
136 << "\n--------------------------------------\n"
137 << script.str().c_str()
138 << "\n--------------------------------------\n";
140 // Output the script to file.
141 static int counter = 0;
142 script_file_ = OnlyPath(to_file_base) + "lyxconvert" +
143 tostr(counter++) + ".sh";
145 std::ofstream fs(script_file_.c_str());
149 fs << script.str().c_str();
152 // The converted image is to be stored in this file
153 to_file_ = ChangeExtension(to_file_base, formats.extension(to_format));
155 // The command needed to run the conversion process
156 // We create a dummy command for ease of understanding of the
157 // list of forked processes.
158 // Note that 'sh ' is absolutely essential, or execvp will fail.
159 script_command_ = "sh " + script_file_ + " " +
160 OnlyFilename(from_file) + " " + to_format;
162 // All is ready to go
163 valid_process_ = true;
167 void Converter::Impl::startConversion()
169 if (!valid_process_) {
170 converted(string(), 0, 1);
174 // Initiate the conversion
175 Forkedcall::SignalTypePtr convert_ptr;
176 convert_ptr.reset(new Forkedcall::SignalType);
178 convert_ptr->connect(
179 boost::bind(&Impl::converted, this, _1, _2, _3));
182 int retval = call.startscript(script_command_, convert_ptr);
184 // Unable to even start the script, so clean-up the mess!
185 converted(string(), 0, 1);
190 void Converter::Impl::converted(string const & /* cmd */,
191 pid_t /* pid */, int retval)
194 // We're done already!
198 // Clean-up behind ourselves
199 lyx::unlink(script_file_);
202 lyx::unlink(to_file_);
204 parent_.finishedConversion(false);
206 parent_.finishedConversion(true);
214 string const move_file(string const & from_file, string const & to_file)
216 if (from_file == to_file)
219 ostringstream command;
220 command << "fromfile=" << from_file << "\n"
221 << "tofile=" << to_file << "\n\n"
222 << "'mv' -f ${fromfile} ${tofile}\n"
223 << "if [ $? -ne 0 ]; then\n"
224 << "\t'cp' -f ${fromfile} ${tofile}\n"
225 << "\tif [ $? -ne 0 ]; then\n"
228 << "\t'rm' -f ${fromfile}\n"
231 return command.str().c_str();
234 bool build_script(string const & from_file,
235 string const & to_file_base,
236 string const & from_format,
237 string const & to_format,
238 ostringstream & script)
240 lyxerr[Debug::GRAPHICS] << "build_script ... ";
241 typedef Converters::EdgePath EdgePath;
243 string const to_file = ChangeExtension(to_file_base,
244 formats.extension(to_format));
246 if (from_format == to_format) {
247 script << move_file(QuoteName(from_file), QuoteName(to_file));
248 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
252 EdgePath edgepath = converters.getPath(from_format, to_format);
254 if (edgepath.empty()) {
255 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
259 // Create a temporary base file-name for all intermediate steps.
260 // Remember to remove the temp file because we only want the name...
261 static int counter = 0;
262 string const tmp = "gconvert" + tostr(counter++);
263 string const to_base = lyx::tempName(string(), tmp);
264 lyx::unlink(to_base);
266 string outfile = from_file;
268 // The conversion commands may contain these tokens that need to be
269 // changed to infile, infile_base, outfile respectively.
270 string const token_from("$$i");
271 string const token_base("$$b");
272 string const token_to("$$o");
274 EdgePath::const_iterator it = edgepath.begin();
275 EdgePath::const_iterator end = edgepath.end();
276 for (; it != end; ++it) {
277 ::Converter const & conv = converters.get(*it);
279 // Build the conversion command
280 string const infile = outfile;
281 string const infile_base = ChangeExtension(infile, string());
282 outfile = ChangeExtension(to_base, conv.To->extension());
284 // Store these names in the shell script
285 script << "infile=" << QuoteName(infile) << '\n'
286 << "infile_base=" << QuoteName(infile_base) << '\n'
287 << "outfile=" << QuoteName(outfile) << '\n';
289 string command = conv.command;
290 command = subst(command, token_from, "${infile}");
291 command = subst(command, token_base, "${infile_base}");
292 command = subst(command, token_to, "${outfile}");
294 // Store in the shell script
295 script << "\n" << command << "\n\n";
297 // Test that this was successful. If not, remove
298 // ${outfile} and exit the shell script
299 script << "if [ $? -ne 0 ]; then\n"
300 << "\t'rm' -f ${outfile}\n"
304 // Test that the outfile exists.
305 // ImageMagick's convert will often create ${outfile}.0,
307 // If this occurs, move ${outfile}.0 to ${outfile}
308 // and delete ${outfile}.?
309 script << "if [ ! -f ${outfile} ]; then\n"
310 << "\tif [ -f ${outfile}.0 ]; then\n"
311 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
312 << "\t\t'rm' -f ${outfile}.?\n"
318 // Delete the infile, if it isn't the original, from_file.
319 if (infile != from_file) {
320 script << "'rm' -f ${infile}\n\n";
324 // Move the final outfile to to_file
325 script << move_file("${outfile}", QuoteName(to_file));
326 lyxerr[Debug::GRAPHICS] << "ready!" << endl;