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"
28 #include "output_plaintext.h"
29 #include "outputparams.h"
30 #include "frontends/Alert.h"
32 #include "support/FileInfo.h"
33 #include "support/filetools.h"
34 #include "support/lyxlib.h"
35 #include "support/os.h"
37 using lyx::support::AddName;
38 using lyx::support::bformat;
39 using lyx::support::ChangeExtension;
40 using lyx::support::contains;
41 using lyx::support::MakeAbsPath;
42 using lyx::support::MakeDisplayPath;
43 using lyx::support::OnlyFilename;
44 using lyx::support::OnlyPath;
45 using lyx::support::prefixIs;
54 vector<string> const Backends(Buffer const & buffer)
57 if (buffer.params().getLyXTextClass().isTeXClassAvailable())
58 v.push_back(BufferFormat(buffer));
64 /// ask the user what to do if a file already exists
65 int checkOverwrite(string const & filename)
67 if (lyx::support::FileInfo(filename, true).exist()) {
68 string text = bformat(_("The file %1$s already exists.\n\n"
69 "Do you want to over-write that file?"),
70 MakeDisplayPath(filename));
71 return Alert::prompt(_("Over-write file?"),
73 _("&Over-write"), _("Over-write &all"),
87 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
88 * will be asked before existing files are overwritten.
90 * - SUCCESS if this file got copied
91 * - FORCE if subsequent calls should not ask for confirmation before
92 * overwriting files anymore.
93 * - CANCEL if the export should be cancelled
95 CopyStatus copyFile(string const & sourceFile, string const & destFile,
98 CopyStatus ret = force ? FORCE : SUCCESS;
100 // Only copy files that are in our tmp dir, all other files would
101 // overwrite themselves. This check could be changed to
102 // boost::filesystem::equivalent(sourceFile, destFile) if export to
103 // other directories than the document directory is desired.
104 if (!prefixIs(OnlyPath(sourceFile), lyx::support::os::getTmpDir()))
108 switch(checkOverwrite(destFile)) {
120 if (!lyx::support::copy(sourceFile, destFile))
121 Alert::error(_("Couldn't copy file"),
122 bformat(_("Copying %1$s to %2$s failed."),
123 MakeDisplayPath(sourceFile),
124 MakeDisplayPath(destFile)));
132 bool Exporter::Export(Buffer * buffer, string const & format,
133 bool put_in_tempdir, string & result_file)
135 string backend_format;
136 OutputParams runparams;
137 runparams.flavor = OutputParams::LATEX;
138 runparams.linelen = lyxrc.ascii_linelen;
139 ExportData exportdata;
140 runparams.exportdata = &exportdata;
141 vector<string> backends = Backends(*buffer);
142 if (find(backends.begin(), backends.end(), format) == backends.end()) {
143 for (vector<string>::const_iterator it = backends.begin();
144 it != backends.end(); ++it) {
146 converters.getPath(*it, format);
148 runparams.flavor = converters.getFlavor(p);
149 backend_format = *it;
153 if (backend_format.empty()) {
154 Alert::error(_("Couldn't export file"),
155 bformat(_("No information for exporting the format %1$s."),
156 formats.prettyName(format)));
160 backend_format = format;
162 string filename = buffer->getLatexName(false);
163 filename = AddName(buffer->temppath(), filename);
164 filename = ChangeExtension(filename,
165 formats.extension(backend_format));
168 if (backend_format == "text")
169 writeFileAscii(*buffer, filename, runparams);
171 else if (buffer->isLinuxDoc()) {
172 runparams.nice = !put_in_tempdir;
173 buffer->makeLinuxDocFile(filename, runparams);
176 else if (buffer->isDocBook()) {
177 runparams.nice = !put_in_tempdir;
178 buffer->makeDocBookFile(filename, runparams);
181 else if (backend_format == format) {
182 runparams.nice = true;
183 buffer->makeLaTeXFile(filename, string(), runparams);
184 } else if (contains(buffer->filePath(), ' ')) {
185 Alert::error(_("File name error"),
186 _("The directory path to the document cannot contain spaces."));
189 runparams.nice = false;
190 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
193 if (!converters.convert(buffer, filename, filename,
194 backend_format, format, result_file))
197 if (!put_in_tempdir) {
198 string const tmp_result_file = result_file;
199 result_file = ChangeExtension(buffer->fileName(),
200 formats.extension(format));
201 // We need to copy referenced files (e. g. included graphics
202 // if format == "dvi") to the result dir.
203 vector<ExportedFile> const files = exportdata.externalFiles(format);
204 string const dest = OnlyPath(result_file);
205 CopyStatus status = SUCCESS;
206 for (vector<ExportedFile>::const_iterator it = files.begin();
207 it != files.end() && status != CANCEL; ++it)
208 status = copyFile(it->sourceName,
209 MakeAbsPath(it->exportName, dest),
211 if (status == CANCEL) {
212 buffer->message(_("Document export cancelled."));
214 // Finally copy the main file
215 status = copyFile(tmp_result_file, result_file,
217 buffer->message(bformat(_("Document exported as %1$s"
219 formats.prettyName(format),
220 MakeDisplayPath(result_file)));
228 bool Exporter::Export(Buffer * buffer, string const & format,
232 return Export(buffer, format, put_in_tempdir, result_file);
236 bool Exporter::Preview(Buffer * buffer, string const & format)
239 if (!Export(buffer, format, true, result_file))
241 return formats.view(*buffer, result_file, format);
245 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
247 vector<string> backends = Backends(buffer);
248 for (vector<string>::const_iterator it = backends.begin();
249 it != backends.end(); ++it)
250 if (converters.isReachable(*it, format))
256 vector<Format const *> const
257 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
259 vector<string> backends = Backends(buffer);
260 vector<Format const *> result =
261 converters.getReachable(backends[0], only_viewable, true);
262 for (vector<string>::const_iterator it = backends.begin() + 1;
263 it != backends.end(); ++it) {
264 vector<Format const *> r =
265 converters.getReachable(*it, only_viewable, false);
266 result.insert(result.end(), r.begin(), r.end());
272 ExportedFile::ExportedFile(string const & s, string const & e) :
273 sourceName(s), exportName(e) {}
276 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
278 return f1.sourceName == f2.sourceName &&
279 f1.exportName == f2.exportName;
284 void ExportData::addExternalFile(string const & format,
285 string const & sourceName,
286 string const & exportName)
288 BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
290 // Make sure that we have every file only once, otherwise copyFile()
291 // would ask several times if it should overwrite a file.
292 vector<ExportedFile> & files = externalfiles[format];
293 ExportedFile file(sourceName, exportName);
294 if (find(files.begin(), files.end(), file) == files.end())
295 files.push_back(file);
299 void ExportData::addExternalFile(string const & format,
300 string const & sourceName)
302 addExternalFile(format, sourceName, OnlyFilename(sourceName));
306 vector<ExportedFile> const
307 ExportData::externalFiles(string const & format) const
309 FileMap::const_iterator cit = externalfiles.find(format);
310 if (cit != externalfiles.end())
312 return vector<ExportedFile>();