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"
21 #include "support/filetools.h"
22 #include "support/forkedcall.h"
23 #include "support/path.h"
25 #include <boost/bind.hpp>
34 string const move_file(string const & from_file, string const & to_file)
36 if (from_file == to_file)
39 ostringstream command;
40 command << "fromfile=" << from_file << "\n"
41 << "tofile=" << to_file << "\n\n"
42 << "'mv' -f ${fromfile} ${tofile}\n"
43 << "if [ $? -ne 0 ]; then\n"
44 << "\t'cp' -f ${fromfile} ${tofile}\n"
45 << "\tif [ $? -ne 0 ]; then\n"
48 << "\t'rm' -f ${fromfile}\n"
51 return command.str().c_str();
59 GConverter & GConverter::get()
61 static GConverter singleton;
66 bool GConverter::isReachable(string const & from_format_name,
67 string const & to_format_name) const
69 return converters.isReachable(from_format_name, to_format_name);
73 void GConverter::convert(string const & from_file, string const & to_file_base,
74 string const & from_format, string const & to_format,
75 SignalTypePtr on_finish)
77 lyxerr[Debug::GRAPHICS] << "[GraphicsConverter::convert]\n"
78 << "\tfrom_file: " << from_file
79 << "\n\tto_file_base: " << to_file_base
80 << "\n\tfrom_format: " << from_format
81 << "\n\tto_format: " << to_format << endl;
82 // The conversion commands are stored in a stringstream
84 script << "#!/bin/sh\n";
85 string script_command;
88 bool success = build_script(from_file, to_file_base,
89 from_format, to_format, script);
92 lyxerr[Debug::GRAPHICS] << "\tConversion script:\n"
93 << "--------------------------------------\n"
94 << script.str().c_str()
95 << "\n--------------------------------------\n";
97 // Output the script to file.
98 static int counter = 0;
99 script_file = OnlyPath(to_file_base) + "lyxconvert" +
100 tostr(counter++) + ".sh";
102 std::ofstream fs(script_file.c_str());
104 // Unable to output the conversion script to file.
108 fs << script.str().c_str();
111 // Create a dummy command for ease of understanding of the
112 // list of forked processes.
113 // Note that 'sh ' is absolutely essential, or execvp will fail.
115 "sh " + script_file + " " +
116 OnlyFilename(from_file) + " " + to_format;
120 string const to_file =
121 ChangeExtension(to_file_base, formats.extension(to_format));
124 script_file = string();
126 "convert -depth 8 " +
127 from_format + ':' + from_file + ' ' +
128 to_format + ':' + to_file;
129 lyxerr[Debug::GRAPHICS]
130 << "\tNo converter defined! I use convert from ImageMagic:\n\t"
131 << script_command << endl;
134 // Launch the conversion process.
135 ConvProcessPtr shared_ptr;
136 shared_ptr.reset(new ConvProcess(script_file, script_command,
137 to_file, on_finish));
138 all_processes_.push_back(shared_ptr);
144 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
148 Find_Ptr(ConvProcess * ptr) : ptr_(ptr) {}
150 bool operator()(ConvProcessPtr const & ptr)
152 return ptr.get() == ptr_;
162 void GConverter::erase(ConvProcess * process)
164 std::list<ConvProcessPtr>::iterator begin = all_processes_.begin();
165 std::list<ConvProcessPtr>::iterator end = all_processes_.end();
166 std::list<ConvProcessPtr>::iterator it =
167 std::find_if(begin, end, Find_Ptr(process));
172 all_processes_.erase(it);
176 bool GConverter::build_script(string const & from_file,
177 string const & to_file_base,
178 string const & from_format,
179 string const & to_format,
180 ostringstream & script) const
182 lyxerr[Debug::GRAPHICS] << "[GraphicsConverter::build_script] ... ";
183 typedef Converters::EdgePath EdgePath;
185 string const to_file = ChangeExtension(to_file_base,
186 formats.extension(to_format));
188 if (from_format == to_format) {
189 script << move_file(QuoteName(from_file), QuoteName(to_file));
190 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
194 EdgePath edgepath = converters.getPath(from_format, to_format);
196 if (edgepath.empty()) {
197 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
201 // Create a temporary base file-name for all intermediate steps.
202 // Remember to remove the temp file because we only want the name...
203 static int counter = 0;
204 string const tmp = "gconvert" + tostr(counter++);
205 string const to_base = lyx::tempName(string(), tmp);
206 lyx::unlink(to_base);
208 string outfile = from_file;
210 // The conversion commands may contain these tokens that need to be
211 // changed to infile, infile_base, outfile respectively.
212 string const token_from("$$i");
213 string const token_base("$$b");
214 string const token_to("$$o");
216 EdgePath::const_iterator it = edgepath.begin();
217 EdgePath::const_iterator end = edgepath.end();
218 for (; it != end; ++it) {
219 Converter const & conv = converters.get(*it);
221 // Build the conversion command
222 string const infile = outfile;
223 string const infile_base = ChangeExtension(infile, string());
224 outfile = ChangeExtension(to_base, conv.To->extension());
226 // Store these names in the shell script
227 script << "infile=" << QuoteName(infile) << '\n'
228 << "infile_base=" << QuoteName(infile_base) << '\n'
229 << "outfile=" << QuoteName(outfile) << '\n';
231 string command = conv.command;
232 command = subst(command, token_from, "${infile}");
233 command = subst(command, token_base, "${infile_base}");
234 command = subst(command, token_to, "${outfile}");
236 // Store in the shell script
237 script << "\n" << command << "\n\n";
239 // Test that this was successful. If not, remove
240 // ${outfile} and exit the shell script
241 script << "if [ $? -ne 0 ]; then\n"
242 << "\t'rm' -f ${outfile}\n"
246 // Test that the outfile exists.
247 // ImageMagick's convert will often create ${outfile}.0,
249 // If this occurs, move ${outfile}.0 to ${outfile}
250 // and delete ${outfile}.?
251 script << "if [ ! -f ${outfile} ]; then\n"
252 << "\tif [ -f ${outfile}.0 ]; then\n"
253 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
254 << "\t\t'rm' -f ${outfile}.?\n"
260 // Delete the infile, if it isn't the original, from_file.
261 if (infile != from_file) {
262 script << "'rm' -f ${infile}\n\n";
266 // Move the final outfile to to_file
267 script << move_file("${outfile}", QuoteName(to_file));
268 lyxerr[Debug::GRAPHICS] << "ready!" << endl;
274 ConvProcess::ConvProcess(string const & script_file,
275 string const & script_command,
276 string const & to_file, SignalTypePtr on_finish)
277 : script_file_(script_file), to_file_(to_file), on_finish_(on_finish)
279 Forkedcall::SignalTypePtr convert_ptr;
280 convert_ptr.reset(new Forkedcall::SignalType);
282 convert_ptr->connect(boost::bind(&ConvProcess::converted, this, _1, _2, _3));
285 int retval = call.startscript(script_command, convert_ptr);
287 // Unable to even start the script, so clean-up the mess!
288 converted(string(), 0, 1);
293 void ConvProcess::converted(string const &/* cmd */,
294 pid_t /* pid */, int retval)
296 // Clean-up behind ourselves
297 lyx::unlink(script_file_);
300 lyx::unlink(to_file_);
304 if (on_finish_.get()) {
305 on_finish_->operator()(to_file_);
308 grfx::GConverter::get().erase(this);