]> git.lyx.org Git - lyx.git/blob - src/Exporter.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37 namespace Alert = frontend::Alert;
38
39 /// ask the user what to do if a file already exists
40 static int checkOverwrite(FileName const & filename)
41 {
42         if (!filename.exists())
43                 return 0;
44         docstring text = bformat(_("The file %1$s already exists.\n\n"
45                                                          "Do you want to overwrite that file?"),
46                                                 makeDisplayPath(filename.absFilename()));
47         return Alert::prompt(_("Overwrite file?"),
48                                          text, 0, 2,
49                                          _("&Overwrite"), _("Overwrite &all"),
50                                          _("&Cancel export"));
51 }
52
53
54 /** copy file \p sourceFile to \p destFile. If \p force is false, the user
55  *  will be asked before existing files are overwritten.
56  *  \return
57  *  - SUCCESS if this file got copied
58  *  - FORCE   if subsequent calls should not ask for confirmation before
59  *            overwriting files anymore.
60  *  - CANCEL  if the export should be cancelled
61  */
62 CopyStatus copyFile(string const & format,
63                     FileName const & sourceFile, FileName const & destFile,
64                     string const & latexFile, bool force)
65 {
66         CopyStatus ret = force ? FORCE : SUCCESS;
67
68         // Only copy files that are in our tmp dir, all other files would
69         // overwrite themselves. This check could be changed to
70         // boost::filesystem::equivalent(sourceFile, destFile) if export to
71         // other directories than the document directory is desired.
72         if (!prefixIs(onlyPath(sourceFile.absFilename()), package().temp_dir().absFilename()))
73                 return ret;
74
75         if (!force) {
76                 switch(checkOverwrite(destFile)) {
77                 case 0:
78                         ret = SUCCESS;
79                         break;
80                 case 1:
81                         ret = FORCE;
82                         break;
83                 default:
84                         return CANCEL;
85                 }
86         }
87
88         Mover const & mover = getMover(format);
89         if (!mover.copy(sourceFile, destFile, latexFile))
90                 Alert::error(_("Couldn't copy file"),
91                              bformat(_("Copying %1$s to %2$s failed."),
92                                      makeDisplayPath(sourceFile.absFilename()),
93                                      makeDisplayPath(destFile.absFilename())));
94
95         return ret;
96 }
97
98
99 ExportedFile::ExportedFile(FileName const & s, string const & e)
100         : sourceName(s), exportName(e)
101 {}
102
103
104 bool operator==(ExportedFile const & f1, ExportedFile const & f2)
105 {
106         return f1.sourceName == f2.sourceName &&
107                f1.exportName == f2.exportName;
108
109 }
110
111
112 void ExportData::addExternalFile(string const & format,
113                                  FileName const & sourceName,
114                                  string const & exportName)
115 {
116         // Make sure that we have every file only once, otherwise copyFile()
117         // would ask several times if it should overwrite a file.
118         vector<ExportedFile> & files = externalfiles[format];
119         ExportedFile file(sourceName, exportName);
120         if (find(files.begin(), files.end(), file) == files.end())
121                 files.push_back(file);
122 }
123
124
125 void ExportData::addExternalFile(string const & format,
126                                  FileName const & sourceName)
127 {
128         addExternalFile(format, sourceName, onlyFilename(sourceName.absFilename()));
129 }
130
131
132 vector<ExportedFile> const
133 ExportData::externalFiles(string const & format) const
134 {
135         FileMap::const_iterator cit = externalfiles.find(format);
136         if (cit != externalfiles.end())
137                 return cit->second;
138         return vector<ExportedFile>();
139 }
140
141
142 } // namespace lyx