]> git.lyx.org Git - lyx.git/blob - src/Exporter.cpp
e90c23f4e05543280edae110f86d18e01bd9189e
[lyx.git] / src / Exporter.cpp
1 /**
2  * \file Exporter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author Alfredo Braunstein
8  * \author Lars Gullik Bjønnes
9  * \author Jean Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "Exporter.h"
20
21 #include "gettext.h"
22 #include "Mover.h"
23 #include "frontends/alert.h"
24
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27 #include "support/Package.h"
28
29 using std::find;
30 using std::string;
31 using std::vector;
32
33
34 namespace lyx {
35
36 using support::bformat;
37 using support::FileName;
38 using support::makeDisplayPath;
39 using support::onlyFilename;
40 using support::onlyPath;
41 using support::package;
42 using support::prefixIs;
43
44 namespace Alert = frontend::Alert;
45
46 /// ask the user what to do if a file already exists
47 static int checkOverwrite(FileName const & filename)
48 {
49         if (!filename.exists())
50                 return 0;
51         docstring text = bformat(_("The file %1$s already exists.\n\n"
52                                                          "Do you want to overwrite that file?"),
53                                                 makeDisplayPath(filename.absFilename()));
54         return Alert::prompt(_("Overwrite file?"),
55                                          text, 0, 2,
56                                          _("&Overwrite"), _("Overwrite &all"),
57                                          _("&Cancel export"));
58 }
59
60
61 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
62  *  will be asked before existing files are overwritten.
63  *  \return
64  *  - SUCCESS if this file got copied
65  *  - FORCE   if subsequent calls should not ask for confirmation before
66  *            overwriting files anymore.
67  *  - CANCEL  if the export should be cancelled
68  */
69 CopyStatus copyFile(string const & format,
70                     FileName const & sourceFile, FileName const & destFile,
71                     string const & latexFile, bool force)
72 {
73         CopyStatus ret = force ? FORCE : SUCCESS;
74
75         // Only copy files that are in our tmp dir, all other files would
76         // overwrite themselves. This check could be changed to
77         // boost::filesystem::equivalent(sourceFile, destFile) if export to
78         // other directories than the document directory is desired.
79         if (!prefixIs(onlyPath(sourceFile.absFilename()), package().temp_dir().absFilename()))
80                 return ret;
81
82         if (!force) {
83                 switch(checkOverwrite(destFile)) {
84                 case 0:
85                         ret = SUCCESS;
86                         break;
87                 case 1:
88                         ret = FORCE;
89                         break;
90                 default:
91                         return CANCEL;
92                 }
93         }
94
95         Mover const & mover = getMover(format);
96         if (!mover.copy(sourceFile, destFile, latexFile))
97                 Alert::error(_("Couldn't copy file"),
98                              bformat(_("Copying %1$s to %2$s failed."),
99                                      makeDisplayPath(sourceFile.absFilename()),
100                                      makeDisplayPath(destFile.absFilename())));
101
102         return ret;
103 }
104
105
106 ExportedFile::ExportedFile(FileName const & s, string const & e)
107         : sourceName(s), exportName(e)
108 {}
109
110
111 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
112 {
113         return f1.sourceName == f2.sourceName &&
114                f1.exportName == f2.exportName;
115
116 }
117
118
119 void ExportData::addExternalFile(string const & format,
120                                  FileName const & sourceName,
121                                  string const & exportName)
122 {
123         // Make sure that we have every file only once, otherwise copyFile()
124         // would ask several times if it should overwrite a file.
125         vector<ExportedFile> & files = externalfiles[format];
126         ExportedFile file(sourceName, exportName);
127         if (find(files.begin(), files.end(), file) == files.end())
128                 files.push_back(file);
129 }
130
131
132 void ExportData::addExternalFile(string const & format,
133                                  FileName const & sourceName)
134 {
135         addExternalFile(format, sourceName, onlyFilename(sourceName.absFilename()));
136 }
137
138
139 vector<ExportedFile> const
140 ExportData::externalFiles(string const & format) const
141 {
142         FileMap::const_iterator cit = externalfiles.find(format);
143         if (cit != externalfiles.end())
144                 return cit->second;
145         return vector<ExportedFile>();
146 }
147
148
149 } // namespace lyx