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>
31 string const move_file(string const & from_file, string const & to_file)
33 if (from_file == to_file)
36 ostringstream command;
37 command << "fromfile=" << from_file << "\n"
38 << "tofile=" << to_file << "\n\n"
39 << "'mv' -f ${fromfile} ${tofile}\n"
40 << "if [ $? -ne 0 ]; then\n"
41 << "\t'cp' -f ${fromfile} ${tofile}\n"
42 << "\tif [ $? -ne 0 ]; then\n"
45 << "\t'rm' -f ${fromfile}\n"
48 return command.str().c_str();
56 GConverter & GConverter::get()
58 static GConverter singleton;
63 bool GConverter::isReachable(string const & from_format_name,
64 string const & to_format_name) const
66 return converters.isReachable(from_format_name, to_format_name);
70 void GConverter::convert(string const & from_file, string const & to_file_base,
71 string const & from_format, string const & to_format,
72 SignalTypePtr on_finish)
74 lyxerr[Debug::GRAPHICS] << "[GraphicsConverter::convert]\n"
75 << "\tfrom_file: " << from_file
76 << "\n\tto_file_base: " << to_file_base
77 << "\n\tfrom_format: " << from_format
78 << "\n\tto_format: " << to_format << endl;
79 // The conversion commands are stored in a stringstream
81 script << "#!/bin/sh\n";
82 string script_command;
85 bool success = build_script(from_file, to_file_base,
86 from_format, to_format, script);
89 lyxerr[Debug::GRAPHICS] << "\tConversion script:\n"
90 << "--------------------------------------\n"
91 << script.str().c_str()
92 << "\n--------------------------------------\n";
94 // Output the script to file.
95 static int counter = 0;
96 script_file = OnlyPath(to_file_base) + "lyxconvert" +
97 tostr(counter++) + ".sh";
99 std::ofstream fs(script_file.c_str());
101 // Unable to output the conversion script to file.
105 fs << script.str().c_str();
108 // Create a dummy command for ease of understanding of the
109 // list of forked processes.
110 // Note that 'sh ' is absolutely essential, or execvp will fail.
112 "sh " + script_file + " " +
113 OnlyFilename(from_file) + " " + to_format;
117 string const to_file =
118 ChangeExtension(to_file_base, formats.extension(to_format));
121 script_file = string();
123 "convert -depth 8 " +
124 from_format + ':' + from_file + ' ' +
125 to_format + ':' + to_file;
126 lyxerr[Debug::GRAPHICS]
127 << "\tNo converter defined! I use convert from ImageMagic:\n\t"
128 << script_command << endl;
131 // Launch the conversion process.
132 ConvProcessPtr shared_ptr;
133 shared_ptr.reset(new ConvProcess(script_file, script_command,
134 to_file, on_finish));
135 all_processes_.push_back(shared_ptr);
141 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
145 Find_Ptr(ConvProcess * ptr) : ptr_(ptr) {}
147 bool operator()(ConvProcessPtr const & ptr)
149 return ptr.get() == ptr_;
159 void GConverter::erase(ConvProcess * process)
161 std::list<ConvProcessPtr>::iterator begin = all_processes_.begin();
162 std::list<ConvProcessPtr>::iterator end = all_processes_.end();
163 std::list<ConvProcessPtr>::iterator it =
164 std::find_if(begin, end, Find_Ptr(process));
169 all_processes_.erase(it);
173 bool GConverter::build_script(string const & from_file,
174 string const & to_file_base,
175 string const & from_format,
176 string const & to_format,
177 ostringstream & script) const
179 lyxerr[Debug::GRAPHICS] << "[GraphicsConverter::build_script] ... ";
180 typedef Converters::EdgePath EdgePath;
182 string const to_file = ChangeExtension(to_file_base,
183 formats.extension(to_format));
185 if (from_format == to_format) {
186 script << move_file(QuoteName(from_file), QuoteName(to_file));
187 lyxerr[Debug::GRAPHICS] << "ready (from == to)" << endl;
191 EdgePath edgepath = converters.getPath(from_format, to_format);
193 if (edgepath.empty()) {
194 lyxerr[Debug::GRAPHICS] << "ready (edgepath.empty())" << endl;
198 // Create a temporary base file-name for all intermediate steps.
199 // Remember to remove the temp file because we only want the name...
200 static int counter = 0;
201 string const tmp = "gconvert" + tostr(counter++);
202 string const to_base = lyx::tempName(string(), tmp);
203 lyx::unlink(to_base);
205 string outfile = from_file;
207 // The conversion commands may contain these tokens that need to be
208 // changed to infile, infile_base, outfile respectively.
209 string const token_from("$$i");
210 string const token_base("$$b");
211 string const token_to("$$o");
213 EdgePath::const_iterator it = edgepath.begin();
214 EdgePath::const_iterator end = edgepath.end();
215 for (; it != end; ++it) {
216 Converter const & conv = converters.get(*it);
218 // Build the conversion command
219 string const infile = outfile;
220 string const infile_base = ChangeExtension(infile, string());
221 outfile = ChangeExtension(to_base, conv.To->extension());
223 // Store these names in the shell script
224 script << "infile=" << QuoteName(infile) << '\n'
225 << "infile_base=" << QuoteName(infile_base) << '\n'
226 << "outfile=" << QuoteName(outfile) << '\n';
228 string command = conv.command;
229 command = subst(command, token_from, "${infile}");
230 command = subst(command, token_base, "${infile_base}");
231 command = subst(command, token_to, "${outfile}");
233 // Store in the shell script
234 script << "\n" << command << "\n\n";
236 // Test that this was successful. If not, remove
237 // ${outfile} and exit the shell script
238 script << "if [ $? -ne 0 ]; then\n"
239 << "\t'rm' -f ${outfile}\n"
243 // Test that the outfile exists.
244 // ImageMagick's convert will often create ${outfile}.0,
246 // If this occurs, move ${outfile}.0 to ${outfile}
247 // and delete ${outfile}.?
248 script << "if [ ! -f ${outfile} ]; then\n"
249 << "\tif [ -f ${outfile}.0 ]; then\n"
250 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
251 << "\t\t'rm' -f ${outfile}.?\n"
257 // Delete the infile, if it isn't the original, from_file.
258 if (infile != from_file) {
259 script << "'rm' -f ${infile}\n\n";
263 // Move the final outfile to to_file
264 script << move_file("${outfile}", QuoteName(to_file));
265 lyxerr[Debug::GRAPHICS] << "ready!" << endl;
271 ConvProcess::ConvProcess(string const & script_file,
272 string const & script_command,
273 string const & to_file, SignalTypePtr on_finish)
274 : script_file_(script_file), to_file_(to_file), on_finish_(on_finish)
276 Forkedcall::SignalTypePtr convert_ptr;
277 convert_ptr.reset(new Forkedcall::SignalType);
279 convert_ptr->connect(boost::bind(&ConvProcess::converted, this, _1, _2, _3));
282 int retval = call.startscript(script_command, convert_ptr);
284 // Unable to even start the script, so clean-up the mess!
285 converted(string(), 0, 1);
290 void ConvProcess::converted(string const &/* cmd */,
291 pid_t /* pid */, int retval)
293 // Clean-up behind ourselves
294 lyx::unlink(script_file_);
297 lyx::unlink(to_file_);
301 if (on_finish_.get()) {
302 on_finish_->operator()(to_file_);
305 grfx::GConverter::get().erase(this);