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"
33 using namespace lyx::support;
37 namespace Alert = frontend::Alert;
39 /// Ask the user what to do if a file already exists
40 static int checkOverwrite(FileName const & filename)
42 if (!filename.exists())
45 docstring text = bformat(_("The file %1$s already exists.\n\n"
46 "Do you want to overwrite that file?"),
47 makeDisplayPath(filename.absFileName()));
48 return Alert::prompt(_("Overwrite file?"),
50 _("&Keep file"), _("&Overwrite"),
51 _("Overwrite &all"), _("&Cancel export"));
55 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
56 * will be asked before existing files are overwritten.
58 * - SUCCESS if this file got copied
59 * - FORCE if subsequent calls should not ask for confirmation before
60 * overwriting files anymore.
61 * - CANCEL if the export should be cancelled
63 CopyStatus copyFile(string const & format,
64 FileName const & sourceFile, FileName const & destFile,
65 string const & latexFile, bool force)
67 CopyStatus ret = force ? FORCE : SUCCESS;
69 // Only copy files that are in our tmp dir, all other files would
70 // overwrite themselves. This check could be changed to
71 // boost::filesystem::equivalent(sourceFile, destFile) if export to
72 // other directories than the document directory is desired.
73 // Also don't overwrite files that already exist and are identical
74 // to the source files.
75 if (!prefixIs(onlyPath(sourceFile.absFileName()), package().temp_dir().absFileName())
76 || sourceFile.checksum() == destFile.checksum())
80 switch(checkOverwrite(destFile)) {
94 Mover const & mover = getMover(format);
95 if (!mover.copy(sourceFile, destFile, latexFile))
96 Alert::error(_("Couldn't copy file"),
97 bformat(_("Copying %1$s to %2$s failed."),
98 makeDisplayPath(sourceFile.absFileName()),
99 makeDisplayPath(destFile.absFileName())));
105 ExportedFile::ExportedFile(FileName const & s, string const & e)
106 : sourceName(s), exportName(e)
110 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
112 return f1.sourceName == f2.sourceName &&
113 f1.exportName == f2.exportName;
118 void ExportData::addExternalFile(string const & format,
119 FileName const & sourceName,
120 string const & exportName)
122 // Make sure that we have every file only once, otherwise copyFile()
123 // would ask several times if it should overwrite a file.
124 vector<ExportedFile> & files = externalfiles_[format];
125 ExportedFile file(sourceName, exportName);
126 if (find(files.begin(), files.end(), file) == files.end())
127 files.push_back(file);
131 void ExportData::addExternalFile(string const & format,
132 FileName const & sourceName)
134 addExternalFile(format, sourceName, onlyFileName(sourceName.absFileName()));
138 vector<ExportedFile> const
139 ExportData::externalFiles(string const & format) const
141 FileMap::const_iterator cit = externalfiles_.find(format);
142 if (cit != externalfiles_.end())
144 return vector<ExportedFile>();