]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.C
Remove the Alert popups from the graphics cache.
[lyx.git] / src / graphics / GraphicsConverter.C
1 /*
2  * \file GraphicsConverter.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming <a.leeming@ic.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "GraphicsConverter.h"
16
17 #include "converter.h"
18 #include "debug.h"
19 #include "gettext.h"
20
21 #include "support/filetools.h"
22 #include "support/forkedcall.h"
23 #include "support/path.h"
24
25 #include <fstream>
26
27 namespace {
28
29 string const move_file(string const & from_file, string const & to_file)
30 {
31         if (from_file == to_file)
32                 return string();
33
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"
41                 << "\t\texit 1\n"
42                 << "\tfi\n"
43                 << "\t'rm' -f ${fromfile}\n"
44                 << "fi\n";
45
46         return command.str().c_str();
47 }
48
49 } // namespace anon
50
51
52 namespace grfx {
53
54 GConverter & GConverter::get()
55 {
56         static GConverter singleton;
57         return singleton;
58 }
59
60
61 bool GConverter::isReachable(string const & from_format_name,
62                              string const & to_format_name) const
63 {
64         return converters.isReachable(from_format_name, to_format_name);
65 }
66
67
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)
71 {
72         // The conversion commands are stored in a stringstream
73         ostringstream script;
74         script << "#!/bin/sh\n";
75
76         bool const success = build_script(from_file, to_file_base,
77                                           from_format, to_format, script);
78
79         if (!success) {
80                 lyxerr[Debug::GRAPHICS]
81                         << "Unable to build the conversion script" << std::endl;
82                 on_finish->emit(string());
83                 return;
84         }
85
86         lyxerr[Debug::GRAPHICS] << "Conversion script:\n\n"
87                                 << script.str().c_str() << "\n" << std::endl;
88
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";
93
94         std::ofstream fs(script_file.c_str());
95         if (!fs.good()) {
96                 // Unable to output the conversion script to file.
97                 on_finish->emit(string());
98                 return;
99         }
100
101         fs << script.str().c_str();
102         fs.close();
103
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;
110
111         string const to_file =
112                 ChangeExtension(to_file_base, formats.extension(to_format));
113
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);
119 }
120
121
122 namespace {
123
124 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
125
126 class Find_Ptr {
127 public:
128         Find_Ptr(ConvProcess * ptr) : ptr_(ptr) {}
129
130         bool operator()(ConvProcessPtr const & ptr)
131         {
132                 return ptr.get() == ptr_;
133         }
134
135 private:
136         ConvProcess * ptr_;
137 };
138
139 } // namespace anon
140
141
142 void GConverter::erase(ConvProcess * process)
143 {
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));
148
149         if (it == end)
150                 return;
151
152         all_processes_.erase(it);
153 }
154
155
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
161 {
162         typedef Converters::EdgePath EdgePath;
163
164         string const to_file = ChangeExtension(to_file_base,
165                                                formats.extension(to_format));
166
167         if (from_format == to_format) {
168                 script << move_file(QuoteName(from_file), QuoteName(to_file));
169                 return true;
170         }
171
172         EdgePath edgepath = converters.getPath(from_format, to_format);
173
174         if (edgepath.empty()) {
175                 return false;
176         }
177
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);
184
185         string outfile = from_file;
186
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");
192
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);
197
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());
202
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';
207
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}");
212
213                 // Store in the shell script
214                 script << "\n" << command << "\n\n";
215
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"
220                        << "\texit 1\n"
221                        << "fi\n\n";
222
223                 // Test that the outfile exists.
224                 // ImageMagick's convert will often create ${outfile}.0,
225                 // ${outfile}.1.
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"
232                        << "\telse\n"
233                        << "\t\texit 1\n"
234                        << "\tfi\n"
235                        << "fi\n\n";
236
237                 // Delete the infile, if it isn't the original, from_file.
238                 if (infile != from_file) {
239                         script << "'rm' -f ${infile}\n\n";
240                 }
241         }
242
243         // Move the final outfile to to_file
244         script << move_file("${outfile}", QuoteName(to_file));
245
246         return true;
247 }
248
249
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)
254 {
255         Forkedcall::SignalTypePtr convert_ptr;
256         convert_ptr.reset(new Forkedcall::SignalType);
257
258         convert_ptr->connect(SigC::slot(this, &ConvProcess::converted));
259
260         Forkedcall call;
261         int retval = call.startscript(script_command, convert_ptr);
262         if (retval > 0) {
263                 // Unable to even start the script, so clean-up the mess!
264                 converted(string(), 0, 1);
265         }
266 }
267
268
269 void ConvProcess::converted(string const &/* cmd */,
270                             pid_t /* pid */, int retval)
271 {
272         // Clean-up behind ourselves
273         lyx::unlink(script_file_);
274
275         if (retval > 0) {
276                 lyx::unlink(to_file_);
277                 to_file_.erase();
278         }
279
280         if (on_finish_.get()) {
281                 on_finish_->emit(to_file_);
282         }
283
284         grfx::GConverter::get().erase(this);
285 }
286
287
288 } // namespace grfx