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