]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsConverter.cpp
e833a3570f32969ab6f0a236c70e8ea093015e56
[lyx.git] / src / graphics / GraphicsConverter.cpp
1 /**
2  * \file GraphicsConverter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GraphicsConverter.h"
14
15 #include "Converter.h"
16 #include "Format.h"
17
18 #include "support/lassert.h"
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h"
23 #include "support/ForkedCalls.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26
27 #include "support/bind.h"
28 #include "support/TempFile.h"
29
30 #include <sstream>
31 #include <fstream>
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38 namespace graphics {
39
40 class Converter::Impl : public boost::signals::trackable {
41 public:
42         ///
43         Impl(FileName const &, string const &, string const &, string const &);
44
45         ///
46         void startConversion();
47
48         /** This method is connected to a signal passed to the forked call
49          *  class, passing control back here when the conversion is completed.
50          *  Cleans-up the temporary files, emits the finishedConversion
51          *  signal and removes the Converter from the list of all processes.
52          */
53         void converted(pid_t pid, int retval);
54
55         /** At the end of the conversion process inform the outside world
56          *  by emitting a signal.
57          */
58         typedef boost::signal<void(bool)> SignalType;
59         ///
60         SignalType finishedConversion;
61
62         ///
63         string script_command_;
64         ///
65         FileName script_file_;
66         ///
67         FileName to_file_;
68         ///
69         bool valid_process_;
70         ///
71         bool finished_;
72 };
73
74
75 bool Converter::isReachable(string const & from_format_name,
76                             string const & to_format_name)
77 {
78         return theConverters().isReachable(from_format_name, to_format_name);
79 }
80
81
82 Converter::Converter(FileName const & from_file, string const & to_file_base,
83                      string const & from_format, string const & to_format)
84         : pimpl_(new Impl(from_file, to_file_base, from_format, to_format))
85 {}
86
87
88 Converter::~Converter()
89 {
90         delete pimpl_;
91 }
92
93
94 void Converter::startConversion() const
95 {
96         pimpl_->startConversion();
97 }
98
99
100 boost::signals::connection Converter::connect(slot_type const & slot) const
101 {
102         return pimpl_->finishedConversion.connect(slot);
103 }
104
105
106 FileName const & Converter::convertedFile() const
107 {
108         static FileName const empty;
109         return pimpl_->finished_ ? pimpl_->to_file_ : empty;
110 }
111
112 /** Build the conversion script.
113  *  The script is output to the stream \p script.
114  */
115 static void build_script(string const & from_file, string const & to_file_base,
116                   string const & from_format, string const & to_format,
117                   ostream & script);
118
119
120 Converter::Impl::Impl(FileName const & from_file, string const & to_file_base,
121                       string const & from_format, string const & to_format)
122         : valid_process_(false), finished_(false)
123 {
124         LYXERR(Debug::GRAPHICS, "Converter c-tor:\n"
125                 << "\tfrom_file:      " << from_file
126                 << "\n\tto_file_base: " << to_file_base
127                 << "\n\tfrom_format:  " << from_format
128                 << "\n\tto_format:    " << to_format);
129
130         // The converted image is to be stored in this file (we do not
131         // use ChangeExtension because this is a basename which may
132         // nevertheless contain a '.')
133         to_file_ = FileName(to_file_base + '.' +  formats.extension(to_format));
134
135         // The conversion commands are stored in a stringstream
136         ostringstream script;
137         build_script(from_file.toFilesystemEncoding(),
138                      to_file_.toFilesystemEncoding(),
139                      from_format, to_format, script);
140         LYXERR(Debug::GRAPHICS, "\tConversion script:"
141                    "\n--------------------------------------\n"
142                 << script.str()
143                 << "\n--------------------------------------\n");
144
145         // Output the script to file.
146         // FIXME THREAD
147         static int counter = 0;
148         script_file_ = FileName(onlyPath(to_file_base) + "lyxconvert" +
149                 convert<string>(counter++) + ".py");
150
151         ofstream fs(script_file_.toFilesystemEncoding().c_str());
152         if (!fs.good()) {
153                 lyxerr << "Unable to write the conversion script to \""
154                        << script_file_ << '\n'
155                        << "Please check your directory permissions."
156                        << endl;
157                 return;
158         }
159
160         fs << script.str();
161         fs.close();
162
163         // The command needed to run the conversion process
164         // We create a dummy command for ease of understanding of the
165         // list of forked processes.
166         // Note: 'python ' is absolutely essential, or execvp will fail.
167         script_command_ = os::python() + ' ' +
168                 quoteName(script_file_.toFilesystemEncoding()) + ' ' +
169                 quoteName(onlyFileName(from_file.toFilesystemEncoding())) + ' ' +
170                 quoteName(to_format);
171         // All is ready to go
172         valid_process_ = true;
173 }
174
175
176 void Converter::Impl::startConversion()
177 {
178         if (!valid_process_) {
179                 converted(0, 1);
180                 return;
181         }
182
183         ForkedCall::SignalTypePtr ptr =
184                 ForkedCallQueue::add(script_command_);
185         ptr->connect(bind(&Impl::converted, this, _1, _2));
186 }
187
188
189 void Converter::Impl::converted(pid_t /* pid */, int retval)
190 {
191         if (finished_)
192                 // We're done already!
193                 return;
194
195         finished_ = true;
196         // Clean-up behind ourselves
197         script_file_.removeFile();
198
199         if (retval > 0) {
200                 to_file_.removeFile();
201                 to_file_.erase();
202                 finishedConversion(false);
203         } else {
204                 finishedConversion(true);
205         }
206 }
207
208
209 static string const move_file(string const & from_file, string const & to_file)
210 {
211         if (from_file == to_file)
212                 return string();
213
214         ostringstream command;
215         command << "fromfile = " << from_file << "\n"
216                 << "tofile = "   << to_file << "\n\n"
217                 << "try:\n"
218                 << "  os.rename(fromfile, tofile)\n"
219                 << "except:\n"
220                 << "  try:\n"
221                 << "    shutil.copy(fromfile, tofile)\n"
222                 << "  except:\n"
223                 << "    sys.exit(1)\n"
224                 << "  unlinkNoThrow(fromfile)\n";
225
226         return command.str();
227 }
228
229
230 static void build_conversion_command(string const & command, ostream & script)
231 {
232         // Store in the python script
233         script << "\nif os.system(r'" << command << "') != 0:\n";
234
235         // Test that this was successful. If not, remove
236         // ${outfile} and exit the python script
237         script << "  unlinkNoThrow(outfile)\n"
238                << "  sys.exit(1)\n\n";
239
240         // Test that the outfile exists.
241         // ImageMagick's convert will often create ${outfile}.0,
242         // ${outfile}.1.
243         // If this occurs, move ${outfile}.0 to ${outfile}
244         // and delete ${outfile}.? (ignore errors)
245         script << "if not os.path.isfile(outfile):\n"
246                   "  if os.path.isfile(outfile + '.0'):\n"
247                   "    os.rename(outfile + '.0', outfile)\n"
248                   "    import glob\n"
249                   "    for file in glob.glob(outfile + '.?'):\n"
250                   "      unlinkNoThrow(file)\n"
251                   "  else:\n"
252                   "    sys.exit(1)\n\n";
253
254         // Delete the infile
255         script << "if infile != outfile:\n"
256                   "  unlinkNoThrow(infile)\n\n";
257 }
258
259
260 static string const strip_digit(string const & format)
261 {
262         // Strip trailing digits from format names e.g. "pdf6" -> "pdf"
263         return format.substr(0, format.find_last_not_of("0123456789") + 1);
264 }
265
266
267 static void build_script(string const & from_file,
268                   string const & to_file,
269                   string const & from_format,
270                   string const & to_format,
271                   ostream & script)
272 {
273         LASSERT(from_format != to_format, return);
274         LYXERR(Debug::GRAPHICS, "build_script ... ");
275         typedef Graph::EdgePath EdgePath;
276
277         script << "#!/usr/bin/env python\n"
278                   "# -*- coding: utf-8 -*-\n"
279                   "import os, shutil, sys\n\n"
280                   "def unlinkNoThrow(file):\n"
281                   "  ''' remove a file, do not throw if an error occurs '''\n"
282                   "  try:\n"
283                   "    os.unlink(file)\n"
284                   "  except:\n"
285                   "    pass\n\n";
286
287         EdgePath const edgepath = from_format.empty() ?
288                 EdgePath() :
289                 theConverters().getPath(from_format, to_format);
290
291         // Create a temporary base file-name for all intermediate steps.
292         // Remember to remove the temp file because we only want the name...
293         // FIXME THREAD
294         static int counter = 0;
295         string const tmp = "gconvert" + convert<string>(counter++);
296         TempFile tempfile(tmp);
297         tempfile.setAutoRemove(false);
298         string const to_base = tempfile.name().toFilesystemEncoding();
299
300         // Create a copy of the file in case the original name contains
301         // problematic characters like ' or ". We can work around that problem
302         // in python, but the converters might be shell scripts and have more
303         // troubles with it.
304         string outfile = addExtension(to_base, getExtension(from_file));
305         script << "infile = "
306                         << quoteName(from_file, quote_python)
307                         << "\n"
308                   "outfile = "
309                         << quoteName(outfile, quote_python) << "\n"
310                   "shutil.copy(infile, outfile)\n";
311
312         // Some converters (e.g. lilypond) can only output files to the
313         // current directory, so we need to change the current directory.
314         // This has the added benefit that all other files that may be
315         // generated by the converter are deleted when LyX closes and do not
316         // clutter the real working directory.
317         script << "os.chdir("
318                << quoteName(onlyPath(outfile)) << ")\n";
319
320         if (edgepath.empty()) {
321                 // Either from_format is unknown or we don't have a
322                 // converter path from from_format to to_format, so we use
323                 // the default converter.
324                 script << "infile = outfile\n"
325                        << "outfile = "
326                        << quoteName(to_file, quote_python) << "\n";
327
328                 ostringstream os;
329                 os << os::python() << ' '
330                    << commandPrep("$$s/scripts/convertDefault.py") << ' ';
331                 if (!from_format.empty())
332                         os << strip_digit(from_format) << ':';
333                 // The extra " quotes around infile and outfile are needed
334                 // because the filename may contain spaces and it is used
335                 // as argument of os.system().
336                 os << "' + '\"' + infile + '\"' + ' "
337                    << strip_digit(to_format) << ":' + '\"' + outfile + '\"' + '";
338                 string const command = os.str();
339
340                 LYXERR(Debug::GRAPHICS,
341                         "\tNo converter defined! I use convertDefault.py\n\t"
342                         << command);
343
344                 build_conversion_command(command, script);
345         }
346
347         // The conversion commands may contain these tokens that need to be
348         // changed to infile, infile_base, outfile and output directory respectively.
349         string const token_from  = "$$i";
350         string const token_base  = "$$b";
351         string const token_to    = "$$o";
352         string const token_todir = "$$d";
353
354         EdgePath::const_iterator it  = edgepath.begin();
355         EdgePath::const_iterator end = edgepath.end();
356
357         for (; it != end; ++it) {
358                 lyx::Converter const & conv = theConverters().get(*it);
359
360                 // Build the conversion command
361                 string const infile      = outfile;
362                 string const infile_base = changeExtension(infile, string());
363                 outfile = conv.result_file.empty()
364                         ? addExtension(to_base, conv.To->extension())
365                         : addName(subst(conv.result_dir,
366                                         token_base, infile_base),
367                                   subst(conv.result_file,
368                                         token_base, onlyFileName(infile_base)));
369
370                 // If two formats share the same extension we may get identical names
371                 if (outfile == infile && conv.result_file.empty()) {
372                         TempFile tempfile(tmp);
373                         tempfile.setAutoRemove(false);
374                         string const new_base = tempfile.name().toFilesystemEncoding();
375                         outfile = addExtension(new_base, conv.To->extension());
376                 }
377
378                 // Store these names in the python script
379                 script << "infile = "
380                                 << quoteName(infile, quote_python) << "\n"
381                           "infile_base = "
382                                 << quoteName(infile_base, quote_python) << "\n"
383                           "outfile = "
384                                 << quoteName(outfile, quote_python) << "\n"
385                           "outdir  = os.path.dirname(outfile)\n" ;
386
387                 // See comment about extra " quotes above (although that
388                 // applies only for the first loop run here).
389                 string command = conv.command;
390                 command = subst(command, token_from,  "' + '\"' + infile + '\"' + '");
391                 command = subst(command, token_base,  "' + '\"' + infile_base + '\"' + '");
392                 command = subst(command, token_to,    "' + '\"' + outfile + '\"' + '");
393                 command = subst(command, token_todir, "' + '\"' + outdir + '\"' + '");
394
395                 build_conversion_command(command, script);
396         }
397
398         // Move the final outfile to to_file
399         script << move_file("outfile", quoteName(to_file, quote_python));
400         LYXERR(Debug::GRAPHICS, "ready!");
401 }
402
403 } // namespace graphics
404 } // namespace lyx