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