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.
23 #include "frontends/alert.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/Package.h"
39 using support::bformat;
40 using support::FileName;
41 using support::makeDisplayPath;
42 using support::onlyFilename;
43 using support::onlyPath;
44 using support::package;
45 using support::prefixIs;
47 namespace Alert = frontend::Alert;
49 /// ask the user what to do if a file already exists
50 static int checkOverwrite(FileName const & filename)
52 if (!filename.exists())
54 docstring text = bformat(_("The file %1$s already exists.\n\n"
55 "Do you want to overwrite that file?"),
56 makeDisplayPath(filename.absFilename()));
57 return Alert::prompt(_("Overwrite file?"),
59 _("&Overwrite"), _("Overwrite &all"),
64 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
65 * will be asked before existing files are overwritten.
67 * - SUCCESS if this file got copied
68 * - FORCE if subsequent calls should not ask for confirmation before
69 * overwriting files anymore.
70 * - CANCEL if the export should be cancelled
72 CopyStatus copyFile(string const & format,
73 FileName const & sourceFile, FileName const & destFile,
74 string const & latexFile, bool force)
76 CopyStatus ret = force ? FORCE : SUCCESS;
78 // Only copy files that are in our tmp dir, all other files would
79 // overwrite themselves. This check could be changed to
80 // boost::filesystem::equivalent(sourceFile, destFile) if export to
81 // other directories than the document directory is desired.
82 if (!prefixIs(onlyPath(sourceFile.absFilename()), package().temp_dir().absFilename()))
86 switch(checkOverwrite(destFile)) {
98 Mover const & mover = getMover(format);
99 if (!mover.copy(sourceFile, destFile, latexFile))
100 Alert::error(_("Couldn't copy file"),
101 bformat(_("Copying %1$s to %2$s failed."),
102 makeDisplayPath(sourceFile.absFilename()),
103 makeDisplayPath(destFile.absFilename())));
109 ExportedFile::ExportedFile(FileName const & s, string const & e)
110 : sourceName(s), exportName(e)
114 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
116 return f1.sourceName == f2.sourceName &&
117 f1.exportName == f2.exportName;
122 void ExportData::addExternalFile(string const & format,
123 FileName const & sourceName,
124 string const & exportName)
126 // Make sure that we have every file only once, otherwise copyFile()
127 // would ask several times if it should overwrite a file.
128 vector<ExportedFile> & files = externalfiles[format];
129 ExportedFile file(sourceName, exportName);
130 if (find(files.begin(), files.end(), file) == files.end())
131 files.push_back(file);
135 void ExportData::addExternalFile(string const & format,
136 FileName const & sourceName)
138 addExternalFile(format, sourceName, onlyFilename(sourceName.absFilename()));
142 vector<ExportedFile> const
143 ExportData::externalFiles(string const & format) const
145 FileMap::const_iterator cit = externalfiles.find(format);
146 if (cit != externalfiles.end())
148 return vector<ExportedFile>();