3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Alfredo Braunstein
8 * \author Lars Gullik Bjønnes
9 * \author Jean Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * Full author contact details are available in file CREDITS.
22 #include "buffer_funcs.h"
23 #include "bufferparams.h"
24 #include "converter.h"
29 #include "output_plaintext.h"
30 #include "outputparams.h"
31 #include "frontends/Alert.h"
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/package.h"
37 #include <boost/filesystem/operations.hpp>
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;
54 namespace fs = boost::filesystem;
58 vector<string> const Backends(Buffer const & buffer)
61 if (buffer.params().getLyXTextClass().isTeXClassAvailable())
62 v.push_back(BufferFormat(buffer));
68 /// ask the user what to do if a file already exists
69 int checkOverwrite(string const & filename)
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?"),
77 _("&Over-write"), _("Over-write &all"),
91 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
92 * will be asked before existing files are overwritten.
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
99 CopyStatus copyFile(string const & format,
100 string const & sourceFile, string const & destFile,
101 string const & latexFile, bool force)
103 CopyStatus ret = force ? FORCE : SUCCESS;
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()))
113 switch(checkOverwrite(destFile)) {
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)));
138 bool Exporter::Export(Buffer * buffer, string const & format,
139 bool put_in_tempdir, string & result_file)
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) {
150 converters.getPath(*it, format);
152 runparams.flavor = converters.getFlavor(p);
153 backend_format = *it;
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)));
164 backend_format = format;
166 string filename = buffer->getLatexName(false);
167 filename = AddName(buffer->temppath(), filename);
168 filename = ChangeExtension(filename,
169 formats.extension(backend_format));
172 if (backend_format == "text")
173 writeFileAscii(*buffer, filename, runparams);
175 else if (buffer->isLinuxDoc()) {
176 runparams.nice = !put_in_tempdir;
177 buffer->makeLinuxDocFile(filename, runparams);
180 else if (buffer->isDocBook()) {
181 runparams.nice = !put_in_tempdir;
182 buffer->makeDocBookFile(filename, runparams);
185 else if (backend_format == format) {
186 runparams.nice = true;
187 buffer->makeLaTeXFile(filename, string(), runparams);
188 } else if (!lyxrc.tex_allows_spaces
189 && contains(buffer->filePath(), ' ')) {
190 Alert::error(_("File name error"),
191 _("The directory path to the document cannot contain spaces."));
194 runparams.nice = false;
195 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
198 if (!converters.convert(buffer, filename, filename,
199 backend_format, format, result_file))
202 if (!put_in_tempdir) {
203 string const tmp_result_file = result_file;
204 result_file = ChangeExtension(buffer->fileName(),
205 formats.extension(format));
206 // We need to copy referenced files (e. g. included graphics
207 // if format == "dvi") to the result dir.
208 vector<ExportedFile> const files =
209 runparams.exportdata->externalFiles(format);
210 string const dest = OnlyPath(result_file);
211 CopyStatus status = SUCCESS;
212 for (vector<ExportedFile>::const_iterator it = files.begin();
213 it != files.end() && status != CANCEL; ++it) {
215 formats.getFormatFromFile(it->sourceName);
216 status = copyFile(fmt, it->sourceName,
217 MakeAbsPath(it->exportName, dest),
218 it->exportName, status == FORCE);
220 if (status == CANCEL) {
221 buffer->message(_("Document export cancelled."));
223 // Finally copy the main file
224 status = copyFile(format, tmp_result_file,
225 result_file, result_file,
227 buffer->message(bformat(_("Document exported as %1$s"
229 formats.prettyName(format),
230 MakeDisplayPath(result_file)));
238 bool Exporter::Export(Buffer * buffer, string const & format,
242 return Export(buffer, format, put_in_tempdir, result_file);
246 bool Exporter::Preview(Buffer * buffer, string const & format)
249 if (!Export(buffer, format, true, result_file))
251 return formats.view(*buffer, result_file, format);
255 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
257 vector<string> backends = Backends(buffer);
258 for (vector<string>::const_iterator it = backends.begin();
259 it != backends.end(); ++it)
260 if (converters.isReachable(*it, format))
266 vector<Format const *> const
267 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
269 vector<string> backends = Backends(buffer);
270 vector<Format const *> result =
271 converters.getReachable(backends[0], only_viewable, true);
272 for (vector<string>::const_iterator it = backends.begin() + 1;
273 it != backends.end(); ++it) {
274 vector<Format const *> r =
275 converters.getReachable(*it, only_viewable, false);
276 result.insert(result.end(), r.begin(), r.end());
282 ExportedFile::ExportedFile(string const & s, string const & e) :
283 sourceName(s), exportName(e) {}
286 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
288 return f1.sourceName == f2.sourceName &&
289 f1.exportName == f2.exportName;
294 void ExportData::addExternalFile(string const & format,
295 string const & sourceName,
296 string const & exportName)
298 BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
300 // Make sure that we have every file only once, otherwise copyFile()
301 // would ask several times if it should overwrite a file.
302 vector<ExportedFile> & files = externalfiles[format];
303 ExportedFile file(sourceName, exportName);
304 if (find(files.begin(), files.end(), file) == files.end())
305 files.push_back(file);
309 void ExportData::addExternalFile(string const & format,
310 string const & sourceName)
312 addExternalFile(format, sourceName, OnlyFilename(sourceName));
316 vector<ExportedFile> const
317 ExportData::externalFiles(string const & format) const
319 FileMap::const_iterator cit = externalfiles.find(format);
320 if (cit != externalfiles.end())
322 return vector<ExportedFile>();