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