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 vector<string> backends = Backends(*buffer);
140 if (find(backends.begin(), backends.end(), format) == backends.end()) {
141 for (vector<string>::const_iterator it = backends.begin();
142 it != backends.end(); ++it) {
144 converters.getPath(*it, format);
146 runparams.flavor = converters.getFlavor(p);
147 backend_format = *it;
151 if (backend_format.empty()) {
152 Alert::error(_("Couldn't export file"),
153 bformat(_("No information for exporting the format %1$s."),
154 formats.prettyName(format)));
158 backend_format = format;
160 string filename = buffer->getLatexName(false);
161 filename = AddName(buffer->temppath(), filename);
162 filename = ChangeExtension(filename,
163 formats.extension(backend_format));
166 if (backend_format == "text")
167 writeFileAscii(*buffer, filename, runparams);
169 else if (buffer->isLinuxDoc()) {
170 runparams.nice = !put_in_tempdir;
171 buffer->makeLinuxDocFile(filename, runparams);
174 else if (buffer->isDocBook()) {
175 runparams.nice = !put_in_tempdir;
176 buffer->makeDocBookFile(filename, runparams);
179 else if (backend_format == format) {
180 runparams.nice = true;
181 buffer->makeLaTeXFile(filename, string(), runparams);
182 } else if (contains(buffer->filePath(), ' ')) {
183 Alert::error(_("File name error"),
184 _("The directory path to the document cannot contain spaces."));
187 runparams.nice = false;
188 buffer->makeLaTeXFile(filename, buffer->filePath(), runparams);
191 if (!converters.convert(buffer, filename, filename,
192 backend_format, format, result_file))
195 if (!put_in_tempdir) {
196 string const tmp_result_file = result_file;
197 result_file = ChangeExtension(buffer->fileName(),
198 formats.extension(format));
199 // We need to copy referenced files (e. g. included graphics
200 // if format == "dvi") to the result dir.
201 vector<ExportedFile> const files =
202 runparams.exportdata->externalFiles(format);
203 string const dest = OnlyPath(result_file);
204 CopyStatus status = SUCCESS;
205 for (vector<ExportedFile>::const_iterator it = files.begin();
206 it != files.end() && status != CANCEL; ++it)
207 status = copyFile(it->sourceName,
208 MakeAbsPath(it->exportName, dest),
210 if (status == CANCEL) {
211 buffer->message(_("Document export cancelled."));
213 // Finally copy the main file
214 status = copyFile(tmp_result_file, result_file,
216 buffer->message(bformat(_("Document exported as %1$s"
218 formats.prettyName(format),
219 MakeDisplayPath(result_file)));
227 bool Exporter::Export(Buffer * buffer, string const & format,
231 return Export(buffer, format, put_in_tempdir, result_file);
235 bool Exporter::Preview(Buffer * buffer, string const & format)
238 if (!Export(buffer, format, true, result_file))
240 return formats.view(*buffer, result_file, format);
244 bool Exporter::IsExportable(Buffer const & buffer, string const & format)
246 vector<string> backends = Backends(buffer);
247 for (vector<string>::const_iterator it = backends.begin();
248 it != backends.end(); ++it)
249 if (converters.isReachable(*it, format))
255 vector<Format const *> const
256 Exporter::GetExportableFormats(Buffer const & buffer, bool only_viewable)
258 vector<string> backends = Backends(buffer);
259 vector<Format const *> result =
260 converters.getReachable(backends[0], only_viewable, true);
261 for (vector<string>::const_iterator it = backends.begin() + 1;
262 it != backends.end(); ++it) {
263 vector<Format const *> r =
264 converters.getReachable(*it, only_viewable, false);
265 result.insert(result.end(), r.begin(), r.end());
271 ExportedFile::ExportedFile(string const & s, string const & e) :
272 sourceName(s), exportName(e) {}
275 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
277 return f1.sourceName == f2.sourceName &&
278 f1.exportName == f2.exportName;
283 void ExportData::addExternalFile(string const & format,
284 string const & sourceName,
285 string const & exportName)
287 BOOST_ASSERT(lyx::support::AbsolutePath(sourceName));
289 // Make sure that we have every file only once, otherwise copyFile()
290 // would ask several times if it should overwrite a file.
291 vector<ExportedFile> & files = externalfiles[format];
292 ExportedFile file(sourceName, exportName);
293 if (find(files.begin(), files.end(), file) == files.end())
294 files.push_back(file);
298 void ExportData::addExternalFile(string const & format,
299 string const & sourceName)
301 addExternalFile(format, sourceName, OnlyFilename(sourceName));
305 vector<ExportedFile> const
306 ExportData::externalFiles(string const & format) const
308 FileMap::const_iterator cit = externalfiles.find(format);
309 if (cit != externalfiles.end())
311 return vector<ExportedFile>();