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/Package.h"
28 #include <boost/filesystem/operations.hpp>
37 using support::bformat
;
38 using support::FileName
;
39 using support::makeDisplayPath
;
40 using support::onlyFilename
;
41 using support::onlyPath
;
42 using support::package
;
43 using support::prefixIs
;
45 namespace Alert
= frontend::Alert
;
46 namespace fs
= boost::filesystem
;
48 /// ask the user what to do if a file already exists
49 static int checkOverwrite(FileName
const & filename
)
51 if (!filename
.exists())
53 docstring text
= bformat(_("The file %1$s already exists.\n\n"
54 "Do you want to overwrite that file?"),
55 makeDisplayPath(filename
.absFilename()));
56 return Alert::prompt(_("Overwrite file?"),
58 _("&Overwrite"), _("Overwrite &all"),
63 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
64 * will be asked before existing files are overwritten.
66 * - SUCCESS if this file got copied
67 * - FORCE if subsequent calls should not ask for confirmation before
68 * overwriting files anymore.
69 * - CANCEL if the export should be cancelled
71 CopyStatus
copyFile(string
const & format
,
72 FileName
const & sourceFile
, FileName
const & destFile
,
73 string
const & latexFile
, bool force
)
75 CopyStatus ret
= force
? FORCE
: SUCCESS
;
77 // Only copy files that are in our tmp dir, all other files would
78 // overwrite themselves. This check could be changed to
79 // boost::filesystem::equivalent(sourceFile, destFile) if export to
80 // other directories than the document directory is desired.
81 if (!prefixIs(onlyPath(sourceFile
.absFilename()), package().temp_dir().absFilename()))
85 switch(checkOverwrite(destFile
)) {
97 Mover
const & mover
= getMover(format
);
98 if (!mover
.copy(sourceFile
, destFile
, latexFile
))
99 Alert::error(_("Couldn't copy file"),
100 bformat(_("Copying %1$s to %2$s failed."),
101 makeDisplayPath(sourceFile
.absFilename()),
102 makeDisplayPath(destFile
.absFilename())));
108 ExportedFile::ExportedFile(FileName
const & s
, string
const & e
)
109 : sourceName(s
), exportName(e
)
113 bool operator==(ExportedFile
const & f1
, ExportedFile
const & f2
)
115 return f1
.sourceName
== f2
.sourceName
&&
116 f1
.exportName
== f2
.exportName
;
121 void ExportData::addExternalFile(string
const & format
,
122 FileName
const & sourceName
,
123 string
const & exportName
)
125 // Make sure that we have every file only once, otherwise copyFile()
126 // would ask several times if it should overwrite a file.
127 vector
<ExportedFile
> & files
= externalfiles
[format
];
128 ExportedFile
file(sourceName
, exportName
);
129 if (find(files
.begin(), files
.end(), file
) == files
.end())
130 files
.push_back(file
);
134 void ExportData::addExternalFile(string
const & format
,
135 FileName
const & sourceName
)
137 addExternalFile(format
, sourceName
, onlyFilename(sourceName
.absFilename()));
141 vector
<ExportedFile
> const
142 ExportData::externalFiles(string
const & format
) const
144 FileMap::const_iterator cit
= externalfiles
.find(format
);
145 if (cit
!= externalfiles
.end())
147 return vector
<ExportedFile
>();