]> git.lyx.org Git - lyx.git/blob - src/Exporter.cpp
7cb141bcf562ab76137e3c3f72e3613411c95368
[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 "Mover.h"
22
23 #include "frontends/alert.h"
24
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/Package.h"
29
30 #include <algorithm>
31
32 using std::find;
33 using std::string;
34 using std::vector;
35
36
37 namespace lyx {
38
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;
46
47 namespace Alert = frontend::Alert;
48
49 /// ask the user what to do if a file already exists
50 static int checkOverwrite(FileName const & filename)
51 {
52         if (!filename.exists())
53                 return 0;
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?"),
58                                          text, 0, 2,
59                                          _("&Overwrite"), _("Overwrite &all"),
60                                          _("&Cancel export"));
61 }
62
63
64 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
65  *  will be asked before existing files are overwritten.
66  *  \return
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
71  */
72 CopyStatus copyFile(string const & format,
73                     FileName const & sourceFile, FileName const & destFile,
74                     string const & latexFile, bool force)
75 {
76         CopyStatus ret = force ? FORCE : SUCCESS;
77
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()))
83                 return ret;
84
85         if (!force) {
86                 switch(checkOverwrite(destFile)) {
87                 case 0:
88                         ret = SUCCESS;
89                         break;
90                 case 1:
91                         ret = FORCE;
92                         break;
93                 default:
94                         return CANCEL;
95                 }
96         }
97
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())));
104
105         return ret;
106 }
107
108
109 ExportedFile::ExportedFile(FileName const & s, string const & e)
110         : sourceName(s), exportName(e)
111 {}
112
113
114 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
115 {
116         return f1.sourceName == f2.sourceName &&
117                f1.exportName == f2.exportName;
118
119 }
120
121
122 void ExportData::addExternalFile(string const & format,
123                                  FileName const & sourceName,
124                                  string const & exportName)
125 {
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);
132 }
133
134
135 void ExportData::addExternalFile(string const & format,
136                                  FileName const & sourceName)
137 {
138         addExternalFile(format, sourceName, onlyFilename(sourceName.absFilename()));
139 }
140
141
142 vector<ExportedFile> const
143 ExportData::externalFiles(string const & format) const
144 {
145         FileMap::const_iterator cit = externalfiles.find(format);
146         if (cit != externalfiles.end())
147                 return cit->second;
148         return vector<ExportedFile>();
149 }
150
151
152 } // namespace lyx