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"
29 string const move_file(string const & from_file, string const & to_file)
31 if (from_file == to_file)
34 ostringstream command;
35 command << "fromfile=" << from_file << "\n"
36 << "tofile=" << to_file << "\n\n"
37 << "'mv' -f ${fromfile} ${tofile}\n"
38 << "if [ $? -ne 0 ]; then\n"
39 << "\t'cp' -f ${fromfile} ${tofile}\n"
40 << "\tif [ $? -ne 0 ]; then\n"
43 << "\t'rm' -f ${fromfile}\n"
46 return command.str().c_str();
54 GConverter & GConverter::get()
56 static GConverter singleton;
61 bool GConverter::isReachable(string const & from_format_name,
62 string const & to_format_name) const
64 return converters.isReachable(from_format_name, to_format_name);
68 void GConverter::convert(string const & from_file, string const & to_file_base,
69 string const & from_format, string const & to_format,
70 SignalTypePtr on_finish)
72 // The conversion commands are stored in a stringstream
74 script << "#!/bin/sh\n";
76 bool const success = build_script(from_file, to_file_base,
77 from_format, to_format, script);
80 lyxerr[Debug::GRAPHICS]
81 << "Unable to build the conversion script" << std::endl;
82 on_finish->emit(string());
86 lyxerr[Debug::GRAPHICS] << "Conversion script:\n\n"
87 << script.str().c_str() << "\n" << std::endl;
89 // Output the script to file.
90 static int counter = 0;
91 string const script_file = OnlyPath(to_file_base) + "lyxconvert" +
92 tostr(counter++) + ".sh";
94 std::ofstream fs(script_file.c_str());
96 // Unable to output the conversion script to file.
97 on_finish->emit(string());
101 fs << script.str().c_str();
104 // Create a dummy command for ease of understanding of the
105 // list of forked processes.
106 // Note that 'sh ' is absolutely essential, or execvp will fail.
107 string const script_command =
108 "sh " + script_file + " " +
109 OnlyFilename(from_file) + " " + to_format;
111 string const to_file =
112 ChangeExtension(to_file_base, formats.extension(to_format));
114 // Launch the conversion process.
115 ConvProcessPtr shared_ptr;
116 shared_ptr.reset(new ConvProcess(script_file, script_command,
117 to_file, on_finish));
118 all_processes_.push_back(shared_ptr);
124 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
128 Find_Ptr(ConvProcess * ptr) : ptr_(ptr) {}
130 bool operator()(ConvProcessPtr const & ptr)
132 return ptr.get() == ptr_;
142 void GConverter::erase(ConvProcess * process)
144 std::list<ConvProcessPtr>::iterator begin = all_processes_.begin();
145 std::list<ConvProcessPtr>::iterator end = all_processes_.end();
146 std::list<ConvProcessPtr>::iterator it =
147 std::find_if(begin, end, Find_Ptr(process));
152 all_processes_.erase(it);
156 bool GConverter::build_script(string const & from_file,
157 string const & to_file_base,
158 string const & from_format,
159 string const & to_format,
160 ostringstream & script) const
162 typedef Converters::EdgePath EdgePath;
164 string const to_file = ChangeExtension(to_file_base,
165 formats.extension(to_format));
167 if (from_format == to_format) {
168 script << move_file(QuoteName(from_file), QuoteName(to_file));
172 EdgePath edgepath = converters.getPath(from_format, to_format);
174 if (edgepath.empty()) {
178 // Create a temporary base file-name for all intermediate steps.
179 // Remember to remove the temp file because we only want the name...
180 static int counter = 0;
181 string const tmp = "gconvert" + tostr(counter++);
182 string const to_base = lyx::tempName(string(), tmp);
183 lyx::unlink(to_base);
185 string outfile = from_file;
187 // The conversion commands may contain these tokens that need to be
188 // changed to infile, infile_base, outfile respectively.
189 string const token_from("$$i");
190 string const token_base("$$b");
191 string const token_to("$$o");
193 EdgePath::const_iterator it = edgepath.begin();
194 EdgePath::const_iterator end = edgepath.end();
195 for (; it != end; ++it) {
196 Converter const & conv = converters.get(*it);
198 // Build the conversion command
199 string const infile = outfile;
200 string const infile_base = ChangeExtension(infile, string());
201 outfile = ChangeExtension(to_base, conv.To->extension());
203 // Store these names in the shell script
204 script << "infile=" << QuoteName(infile) << '\n'
205 << "infile_base=" << QuoteName(infile_base) << '\n'
206 << "outfile=" << QuoteName(outfile) << '\n';
208 string command = conv.command;
209 command = subst(command, token_from, "${infile}");
210 command = subst(command, token_base, "${infile_base}");
211 command = subst(command, token_to, "${outfile}");
213 // Store in the shell script
214 script << "\n" << command << "\n\n";
216 // Test that this was successful. If not, remove
217 // ${outfile} and exit the shell script
218 script << "if [ $? -ne 0 ]; then\n"
219 << "\t'rm' -f ${outfile}\n"
223 // Test that the outfile exists.
224 // ImageMagick's convert will often create ${outfile}.0,
226 // If this occurs, move ${outfile}.0 to ${outfile}
227 // and delete ${outfile}.?
228 script << "if [ ! -f ${outfile} ]; then\n"
229 << "\tif [ -f ${outfile}.0 ]; then\n"
230 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
231 << "\t\t'rm' -f ${outfile}.?\n"
237 // Delete the infile, if it isn't the original, from_file.
238 if (infile != from_file) {
239 script << "'rm' -f ${infile}\n\n";
243 // Move the final outfile to to_file
244 script << move_file("${outfile}", QuoteName(to_file));
250 ConvProcess::ConvProcess(string const & script_file,
251 string const & script_command,
252 string const & to_file, SignalTypePtr on_finish)
253 : script_file_(script_file), to_file_(to_file), on_finish_(on_finish)
255 Forkedcall::SignalTypePtr convert_ptr;
256 convert_ptr.reset(new Forkedcall::SignalType);
258 convert_ptr->connect(SigC::slot(this, &ConvProcess::converted));
261 int retval = call.startscript(script_command, convert_ptr);
263 // Unable to even start the script, so clean-up the mess!
264 converted(string(), 0, 1);
269 void ConvProcess::converted(string const &/* cmd */,
270 pid_t /* pid */, int retval)
272 // Clean-up behind ourselves
273 lyx::unlink(script_file_);
276 lyx::unlink(to_file_);
280 if (on_finish_.get()) {
281 on_finish_->emit(to_file_);
284 grfx::GConverter::get().erase(this);