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