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/FileInfo.h"
34 #include "support/filetools.h"
35 #include "support/lyxlib.h"
36 #include "support/package.h"
38 using lyx::support::AddName;
39 using lyx::support::bformat;
40 using lyx::support::ChangeExtension;
41 using lyx::support::contains;
42 using lyx::support::MakeAbsPath;
43 using lyx::support::MakeDisplayPath;
44 using lyx::support::OnlyFilename;
45 using lyx::support::OnlyPath;
46 using lyx::support::package;
47 using lyx::support::prefixIs;
56 vector<string> const Backends(Buffer const & buffer)
59 if (buffer.params().getLyXTextClass().isTeXClassAvailable())
60 v.push_back(BufferFormat(buffer));
66 /// ask the user what to do if a file already exists
67 int checkOverwrite(string const & filename)
69 if (lyx::support::FileInfo(filename, true).exist()) {
70 string text = bformat(_("The file %1$s already exists.\n\n"
71 "Do you want to over-write that file?"),
72 MakeDisplayPath(filename));
73 return Alert::prompt(_("Over-write file?"),
75 _("&Over-write"), _("Over-write &all"),
89 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
90 * will be asked before existing files are overwritten.
92 * - SUCCESS if this file got copied
93 * - FORCE if subsequent calls should not ask for confirmation before
94 * overwriting files anymore.
95 * - CANCEL if the export should be cancelled
97 CopyStatus copyFile(string const & format,
98 string const & sourceFile, string const & destFile,
99 string const & latexFile, bool force)
101 CopyStatus ret = force ? FORCE : SUCCESS;
103 // Only copy files that are in our tmp dir, all other files would
104 // overwrite themselves. This check could be changed to
105 // boost::filesystem::equivalent(sourceFile, destFile) if export to
106 // other directories than the document directory is desired.
107 if (!prefixIs(OnlyPath(sourceFile), package().temp_dir()))
111 switch(checkOverwrite(destFile)) {
123 Mover const & mover = movers(format);
124 if (!mover.copy(sourceFile, destFile, latexFile))
125 Alert::error(_("Couldn't copy file"),
126 bformat(_("Copying %1$s to %2$s failed."),
127 MakeDisplayPath(sourceFile),
128 MakeDisplayPath(destFile)));
136 bool Exporter::Export(Buffer * buffer, string const & format,
137 bool put_in_tempdir, string & result_file)
139 string backend_format;
140 OutputParams runparams;
141 runparams.flavor = OutputParams::LATEX;
142 runparams.linelen = lyxrc.ascii_linelen;
143 vector<string> backends = Backends(*buffer);
144 if (find(backends.begin(), backends.end(), format) == backends.end()) {
145 for (vector<string>::const_iterator it = backends.begin();
146 it != backends.end(); ++it) {
148 converters.getPath(*it, format);
150 runparams.flavor = converters.getFlavor(p);
151 backend_format = *it;
155 if (backend_format.empty()) {
156 Alert::error(_("Couldn't export file"),
157 bformat(_("No information for exporting the format %1$s."),
158 formats.prettyName(format)));
162 backend_format = format;
164 string filename = buffer->getLatexName(false);
165 filename = AddName(buffer->temppath(), filename);
166 filename = ChangeExtension(filename,
167 formats.extension(backend_format));
170 if (backend_format == "text")
171 writeFileAscii(*buffer, filename, runparams);
173 else if (buffer->isLinuxDoc()) {
174 runparams.nice = !put_in_tempdir;
175 buffer->makeLinuxDocFile(filename, runparams);
178 else if (buffer->isDocBook()) {
179 runparams.nice = !put_in_tempdir;
180 buffer->makeDocBookFile(filename, runparams);
183 else if (backend_format == format) {
184 runparams.nice = true;
185 buffer->makeLaTeXFile(filename, string(), runparams);
186 } else if (contains(buffer->filePath(), ' ')) {
187 Alert::error(_("File name error"),
188 _("The directory path to the document cannot contain spaces."));
191 runparams.nice = false;
192 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
195 if (!converters.convert(buffer, filename, filename,
196 backend_format, format, result_file))
199 if (!put_in_tempdir) {
200 string const tmp_result_file = result_file;
201 result_file = ChangeExtension(buffer->fileName(),
202 formats.extension(format));
203 // We need to copy referenced files (e. g. included graphics
204 // if format == "dvi") to the result dir.
205 vector<ExportedFile> const files =
206 runparams.exportdata->externalFiles(format);
207 string const dest = OnlyPath(result_file);
208 CopyStatus status = SUCCESS;
209 for (vector<ExportedFile>::const_iterator it = files.begin();
210 it != files.end() && status != CANCEL; ++it) {
212 formats.getFormatFromFile(it->sourceName);
213 status = copyFile(fmt, it->sourceName,
214 MakeAbsPath(it->exportName, dest),
215 it->exportName, status == FORCE);
217 if (status == CANCEL) {
218 buffer->message(_("Document export cancelled."));
220 // Finally copy the main file
221 status = copyFile(format, tmp_result_file,
222 result_file, result_file,
224 buffer->message(bformat(_("Document exported as %1$s"
226 formats.prettyName(format),
227 MakeDisplayPath(result_file)));
235 bool Exporter::Export(Buffer * buffer, string const & format,
239 return Export(buffer, format, put_in_tempdir, result_file);
243 bool Exporter::Preview(Buffer * buffer, string const & format)
246 if (!Export(buffer, format, true, result_file))
248 return formats.view(*buffer, result_file, format);
252 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
254 vector<string> backends = Backends(buffer);
255 for (vector<string>::const_iterator it = backends.begin();
256 it != backends.end(); ++it)
257 if (converters.isReachable(*it, format))
263 vector<Format const *> const
264 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
266 vector<string> backends = Backends(buffer);
267 vector<Format const *> result =
268 converters.getReachable(backends[0], only_viewable, true);
269 for (vector<string>::const_iterator it = backends.begin() + 1;
270 it != backends.end(); ++it) {
271 vector<Format const *> r =
272 converters.getReachable(*it, only_viewable, false);
273 result.insert(result.end(), r.begin(), r.end());
279 ExportedFile::ExportedFile(string const & s, string const & e) :
280 sourceName(s), exportName(e) {}
283 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
285 return f1.sourceName == f2.sourceName &&
286 f1.exportName == f2.exportName;
291 void ExportData::addExternalFile(string const & format,
292 string const & sourceName,
293 string const & exportName)
295 BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
297 // Make sure that we have every file only once, otherwise copyFile()
298 // would ask several times if it should overwrite a file.
299 vector<ExportedFile> & files = externalfiles[format];
300 ExportedFile file(sourceName, exportName);
301 if (find(files.begin(), files.end(), file) == files.end())
302 files.push_back(file);
306 void ExportData::addExternalFile(string const & format,
307 string const & sourceName)
309 addExternalFile(format, sourceName, OnlyFilename(sourceName));
313 vector<ExportedFile> const
314 ExportData::externalFiles(string const & format) const
316 FileMap::const_iterator cit = externalfiles.find(format);
317 if (cit != externalfiles.end())
319 return vector<ExportedFile>();