]> git.lyx.org Git - lyx.git/blob - src/exporter.C
ditch FileInfo -> use boost.filesystem
[lyx.git] / src / exporter.C
1 /**
2  * \file exporter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author Alfredo Braunstein
8  * \author Lars Gullik Bjønnes
9  * \author Jean Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "exporter.h"
20
21 #include "buffer.h"
22 #include "buffer_funcs.h"
23 #include "bufferparams.h"
24 #include "converter.h"
25 #include "format.h"
26 #include "gettext.h"
27 #include "lyxrc.h"
28 #include "mover.h"
29 #include "output_plaintext.h"
30 #include "outputparams.h"
31 #include "frontends/Alert.h"
32
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/package.h"
36
37 #include <boost/filesystem/operations.hpp>
38
39 using lyx::support::AddName;
40 using lyx::support::bformat;
41 using lyx::support::ChangeExtension;
42 using lyx::support::contains;
43 using lyx::support::MakeAbsPath;
44 using lyx::support::MakeDisplayPath;
45 using lyx::support::OnlyFilename;
46 using lyx::support::OnlyPath;
47 using lyx::support::package;
48 using lyx::support::prefixIs;
49
50 using std::find;
51 using std::string;
52 using std::vector;
53
54 namespace fs = boost::filesystem;
55
56 namespace {
57
58 vector<string> const Backends(Buffer const & buffer)
59 {
60         vector<string> v;
61         if (buffer.params().getLyXTextClass().isTeXClassAvailable())
62                 v.push_back(BufferFormat(buffer));
63         v.push_back("text");
64         return v;
65 }
66
67
68 /// ask the user what to do if a file already exists
69 int checkOverwrite(string const & filename)
70 {
71         if (fs::exists(filename)) {
72                 string text = bformat(_("The file %1$s already exists.\n\n"
73                                         "Do you want to over-write that file?"),
74                                       MakeDisplayPath(filename));
75                 return Alert::prompt(_("Over-write file?"),
76                                      text, 0, 2,
77                                      _("&Over-write"), _("Over-write &all"),
78                                      _("&Cancel export"));
79         }
80         return 0;
81 }
82
83
84 enum CopyStatus {
85         SUCCESS,
86         FORCE,
87         CANCEL
88 };
89
90
91 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
92  *  will be asked before existing files are overwritten.
93  *  \return
94  *  - SUCCESS if this file got copied
95  *  - FORCE   if subsequent calls should not ask for confirmation before
96  *            overwriting files anymore.
97  *  - CANCEL  if the export should be cancelled
98  */
99 CopyStatus copyFile(string const & format,
100                     string const & sourceFile, string const & destFile,
101                     string const & latexFile, bool force)
102 {
103         CopyStatus ret = force ? FORCE : SUCCESS;
104
105         // Only copy files that are in our tmp dir, all other files would
106         // overwrite themselves. This check could be changed to
107         // boost::filesystem::equivalent(sourceFile, destFile) if export to
108         // other directories than the document directory is desired.
109         if (!prefixIs(OnlyPath(sourceFile), package().temp_dir()))
110                 return ret;
111
112         if (!force) {
113                 switch(checkOverwrite(destFile)) {
114                 case 0:
115                         ret = SUCCESS;
116                         break;
117                 case 1:
118                         ret = FORCE;
119                         break;
120                 default:
121                         return CANCEL;
122                 }
123         }
124
125         Mover const & mover = movers(format);
126         if (!mover.copy(sourceFile, destFile, latexFile))
127                 Alert::error(_("Couldn't copy file"),
128                              bformat(_("Copying %1$s to %2$s failed."),
129                                      MakeDisplayPath(sourceFile),
130                                      MakeDisplayPath(destFile)));
131
132         return ret;
133 }
134
135 } //namespace anon
136
137
138 bool Exporter::Export(Buffer * buffer, string const & format,
139                       bool put_in_tempdir, string & result_file)
140 {
141         string backend_format;
142         OutputParams runparams;
143         runparams.flavor = OutputParams::LATEX;
144         runparams.linelen = lyxrc.ascii_linelen;
145         vector<string> backends = Backends(*buffer);
146         if (find(backends.begin(), backends.end(), format) == backends.end()) {
147                 for (vector<string>::const_iterator it = backends.begin();
148                      it != backends.end(); ++it) {
149                         Graph::EdgePath p =
150                                 converters.getPath(*it, format);
151                         if (!p.empty()) {
152                                 runparams.flavor = converters.getFlavor(p);
153                                 backend_format = *it;
154                                 break;
155                         }
156                 }
157                 if (backend_format.empty()) {
158                         Alert::error(_("Couldn't export file"),
159                                 bformat(_("No information for exporting the format %1$s."),
160                                    formats.prettyName(format)));
161                         return false;
162                 }
163         } else
164                 backend_format = format;
165
166         string filename = buffer->getLatexName(false);
167         filename = AddName(buffer->temppath(), filename);
168         filename = ChangeExtension(filename,
169                                    formats.extension(backend_format));
170
171         // Ascii backend
172         if (backend_format == "text")
173                 writeFileAscii(*buffer, filename, runparams);
174         // Linuxdoc backend
175         else if (buffer->isLinuxDoc()) {
176                 runparams.nice = !put_in_tempdir;
177                 buffer->makeLinuxDocFile(filename, runparams);
178         }
179         // Docbook backend
180         else if (buffer->isDocBook()) {
181                 runparams.nice = !put_in_tempdir;
182                 buffer->makeDocBookFile(filename, runparams);
183         }
184         // LaTeX backend
185         else if (backend_format == format) {
186                 runparams.nice = true;
187                 buffer->makeLaTeXFile(filename, string(), runparams);
188         } else if (contains(buffer->filePath(), ' ')) {
189                 Alert::error(_("File name error"),
190                            _("The directory path to the document cannot contain spaces."));
191                 return false;
192         } else {
193                 runparams.nice = false;
194                 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
195         }
196
197         if (!converters.convert(buffer, filename, filename,
198                                 backend_format, format, result_file))
199                 return false;
200
201         if (!put_in_tempdir) {
202                 string const tmp_result_file = result_file;
203                 result_file = ChangeExtension(buffer->fileName(),
204                                               formats.extension(format));
205                 // We need to copy referenced files (e. g. included graphics
206                 // if format == "dvi") to the result dir.
207                 vector<ExportedFile> const files =
208                         runparams.exportdata->externalFiles(format);
209                 string const dest = OnlyPath(result_file);
210                 CopyStatus status = SUCCESS;
211                 for (vector<ExportedFile>::const_iterator it = files.begin();
212                                 it != files.end() && status != CANCEL; ++it) {
213                         string const fmt =
214                                 formats.getFormatFromFile(it->sourceName);
215                         status = copyFile(fmt, it->sourceName,
216                                           MakeAbsPath(it->exportName, dest),
217                                           it->exportName, status == FORCE);
218                 }
219                 if (status == CANCEL) {
220                         buffer->message(_("Document export cancelled."));
221                 } else {
222                         // Finally copy the main file
223                         status = copyFile(format, tmp_result_file,
224                                           result_file, result_file,
225                                           status == FORCE);
226                         buffer->message(bformat(_("Document exported as %1$s"
227                                                   "to file `%2$s'"),
228                                                 formats.prettyName(format),
229                                                 MakeDisplayPath(result_file)));
230                 }
231         }
232
233         return true;
234 }
235
236
237 bool Exporter::Export(Buffer * buffer, string const & format,
238                       bool put_in_tempdir)
239 {
240         string result_file;
241         return Export(buffer, format, put_in_tempdir, result_file);
242 }
243
244
245 bool Exporter::Preview(Buffer * buffer, string const & format)
246 {
247         string result_file;
248         if (!Export(buffer, format, true, result_file))
249                 return false;
250         return formats.view(*buffer, result_file, format);
251 }
252
253
254 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
255 {
256         vector<string> backends = Backends(buffer);
257         for (vector<string>::const_iterator it = backends.begin();
258              it != backends.end(); ++it)
259                 if (converters.isReachable(*it, format))
260                         return true;
261         return false;
262 }
263
264
265 vector<Format const *> const
266 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
267 {
268         vector<string> backends = Backends(buffer);
269         vector<Format const *> result =
270                 converters.getReachable(backends[0], only_viewable, true);
271         for (vector<string>::const_iterator it = backends.begin() + 1;
272              it != backends.end(); ++it) {
273                 vector<Format const *>  r =
274                         converters.getReachable(*it, only_viewable, false);
275                 result.insert(result.end(), r.begin(), r.end());
276         }
277         return result;
278 }
279
280
281 ExportedFile::ExportedFile(string const & s, string const & e) :
282         sourceName(s), exportName(e) {}
283
284
285 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
286 {
287         return f1.sourceName == f2.sourceName &&
288                f1.exportName == f2.exportName;
289
290 }
291
292
293 void ExportData::addExternalFile(string const & format,
294                                  string const & sourceName,
295                                  string const & exportName)
296 {
297         BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
298
299         // Make sure that we have every file only once, otherwise copyFile()
300         // would ask several times if it should overwrite a file.
301         vector<ExportedFile> & files = externalfiles[format];
302         ExportedFile file(sourceName, exportName);
303         if (find(files.begin(), files.end(), file) == files.end())
304                 files.push_back(file);
305 }
306
307
308 void ExportData::addExternalFile(string const & format,
309                                  string const & sourceName)
310 {
311         addExternalFile(format, sourceName, OnlyFilename(sourceName));
312 }
313
314
315 vector<ExportedFile> const
316 ExportData::externalFiles(string const & format) const
317 {
318         FileMap::const_iterator cit = externalfiles.find(format);
319         if (cit != externalfiles.end())
320                 return cit->second;
321         return vector<ExportedFile>();
322 }