]> git.lyx.org Git - lyx.git/blob - src/EmbeddedFiles.cpp
getting rid of superfluous std:: statements.
[lyx.git] / src / EmbeddedFiles.cpp
1 // -*- C++ -*-
2 /**
3  * \file EmbeddedFiles.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "EmbeddedFiles.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "ErrorList.h"
20 #include "Format.h"
21 #include "InsetIterator.h"
22 #include "Lexer.h"
23 #include "LyX.h"
24 #include "Paragraph.h"
25 #include "Session.h"
26
27 #include "frontends/alert.h"
28
29 #include "support/debug.h"
30 #include "support/filetools.h"
31 #include "support/gettext.h"
32 #include "support/convert.h"
33 #include "support/lyxlib.h"
34 #include "support/lstrings.h"
35 #include "support/FileZipListDir.h"
36
37 #include <sstream>
38 #include <fstream>
39 #include <utility>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46 namespace Alert = frontend::Alert;
47
48
49 EmbeddedFile::EmbeddedFile(string const & file, string const & inzip_name,
50         bool embed, Inset const * inset)
51         : DocFileName(file, true), inzip_name_(inzip_name), embedded_(embed),
52                 inset_list_()
53 {
54         if (inset != NULL)
55                 inset_list_.push_back(inset);
56 }
57
58
59 string EmbeddedFile::embeddedFile(Buffer const * buf) const
60 {
61         return addName(buf->temppath(), inzip_name_);
62 }
63
64
65 void EmbeddedFile::addInset(Inset const * inset)
66 {
67         inset_list_.push_back(inset);
68 }
69
70
71 Inset const * EmbeddedFile::inset(int idx) const
72 {
73         BOOST_ASSERT(idx < refCount());
74         // some embedded file do not have a valid par iterator
75         return inset_list_[idx];
76 }
77
78
79 void EmbeddedFile::saveBookmark(Buffer const * buf, int idx) const
80 {
81         Inset const * ptr = inset(idx);
82         // This might not be the most efficient method ... 
83         for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
84                 if (&(*it) == ptr) {
85                         // this is basically BufferView::saveBookmark(0)
86                         LyX::ref().session().bookmarks().save(
87                                 FileName(buf->absFileName()),
88                                 it.bottom().pit(),
89                                 it.bottom().pos(),
90                                 it.paragraph().id(),
91                                 it.pos(),
92                                 0
93                         );
94                 }
95         // this inset can not be located. There is something wrong that needs
96         // to be fixed.
97         BOOST_ASSERT(true);
98 }
99
100
101 string EmbeddedFile::availableFile(Buffer const * buf) const
102 {
103         return embedded() ? embeddedFile(buf) : absFilename();
104 }
105
106
107 bool EmbeddedFile::extract(Buffer const * buf) const
108 {
109         string ext_file = absFilename();
110         string emb_file = embeddedFile(buf);
111
112         FileName emb(emb_file);
113         FileName ext(ext_file);
114
115         if (!emb.exists())
116                 return false;
117
118         // if external file already exists ...
119         if (ext.exists()) {
120                 // no need to copy if the files are the same
121                 if (checksum() == FileName(emb_file).checksum())
122                         return true;
123                 // otherwise, ask if overwrite
124                 int ret = Alert::prompt(
125                         _("Overwrite external file?"),
126                         bformat(_("External file %1$s already exists, do you want to overwrite it"),
127                                 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
128                 if (ret != 0)
129                         // if the user does not want to overwrite, we still consider it
130                         // a successful operation.
131                         return true;
132         }
133         // copy file
134
135         // need to make directory?
136         FileName path = ext.onlyPath();
137         if (!path.isDirectory())
138                 makedir(const_cast<char*>(path.absFilename().c_str()), 0755);
139         if (emb.copyTo(ext))
140                 return true;
141         Alert::error(_("Copy file failure"),
142                  bformat(_("Cannot copy file %1$s to %2$s.\n"
143                                  "Please check whether the directory exists and is writeable."),
144                                 from_utf8(emb_file), from_utf8(ext_file)));
145         //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
146         return false;
147 }
148
149
150 bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
151 {
152         string ext_file = absFilename();
153         string emb_file = embeddedFile(buf);
154
155         FileName emb(emb_file);
156         FileName ext(ext_file);
157
158         if (!ext.exists())
159                 return false;
160         
161         // if embedded file already exists ...
162         if (emb.exists()) {
163                 // no need to copy if the files are the same
164                 if (checksum() == FileName(emb_file).checksum())
165                         return true;
166                 // other wise, ask if overwrite
167                 int const ret = Alert::prompt(
168                         _("Update embedded file?"),
169                         bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
170                                 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
171                 if (ret != 0)
172                         // if the user does not want to overwrite, we still consider it
173                         // a successful operation.
174                         return true;
175         }
176         // copy file
177         // need to make directory?
178         FileName path = emb.onlyPath();
179         if (!path.isDirectory())
180                 makedir(const_cast<char*>(path.absFilename().c_str()), 0755);
181         if (ext.copyTo(emb))
182                 return true;
183         Alert::error(_("Copy file failure"),
184                  bformat(_("Cannot copy file %1$s to %2$s.\n"
185                            "Please check whether the directory exists and is writeable."),
186                                 from_utf8(ext_file), from_utf8(emb_file)));
187         //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
188         return false;
189 }
190
191
192 void EmbeddedFile::updateInsets(Buffer const * buf) const
193 {
194         vector<Inset const *>::const_iterator it = inset_list_.begin();
195         vector<Inset const *>::const_iterator it_end = inset_list_.end();
196         for (; it != it_end; ++it)
197                 const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
198 }
199
200
201 bool EmbeddedFiles::enabled() const
202 {
203         return buffer_->params().embedded;
204 }
205
206
207 void EmbeddedFiles::enable(bool flag)
208 {
209         if (enabled() != flag) {
210                 // if enable, copy all files to temppath()
211                 // if disable, extract all files
212                 if ((flag && !updateFromExternalFile()) || (!flag && !extract()))
213                         return;
214                 // if operation is successful
215                 buffer_->markDirty();
216                 buffer_->params().embedded = flag;
217                 if (flag)
218                         updateInsets();
219         }
220 }
221
222
223 EmbeddedFile & EmbeddedFiles::registerFile(string const & filename,
224         bool embed, Inset const * inset, string const & inzipName)
225 {
226         // filename can be relative or absolute, translate to absolute filename
227         string abs_filename = makeAbsPath(filename, buffer_->filePath()).absFilename();
228         // try to find this file from the list
229         EmbeddedFileList::iterator it = file_list_.begin();
230         EmbeddedFileList::iterator it_end = file_list_.end();
231         for (; it != it_end; ++it)
232                 if (it->absFilename() == abs_filename || it->embeddedFile(buffer_) == abs_filename)
233                         break;
234         // find this filename, keep the original embedding status
235         if (it != file_list_.end()) {
236                 it->addInset(inset);
237                 return *it;
238         }
239         //
240         file_list_.push_back(EmbeddedFile(abs_filename, 
241                 getInzipName(abs_filename, inzipName), embed, inset));
242         return file_list_.back();
243 }
244
245
246 void EmbeddedFiles::update()
247 {
248         file_list_.clear();
249
250         for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
251                 it->registerEmbeddedFiles(*buffer_, *this);
252 }
253
254
255 bool EmbeddedFiles::writeFile(DocFileName const & filename)
256 {
257         // file in the temporary path has the content
258         string const content = FileName(addName(buffer_->temppath(),
259                 "content.lyx")).toFilesystemEncoding();
260
261         vector<pair<string, string> > filenames;
262         // add content.lyx to filenames
263         filenames.push_back(make_pair(content, "content.lyx"));
264         // prepare list of embedded file
265         EmbeddedFileList::iterator it = file_list_.begin();
266         EmbeddedFileList::iterator it_end = file_list_.end();
267         for (; it != it_end; ++it) {
268                 if (it->embedded()) {
269                         string file = it->availableFile(buffer_);
270                         if (file.empty())
271                                 lyxerr << "File " << it->absFilename() << " does not exist. Skip embedding it. " << endl;
272                         else
273                                 filenames.push_back(make_pair(file, it->inzipName()));
274                 }
275         }
276         // write a zip file with all these files. Write to a temp file first, to
277         // avoid messing up the original file in case something goes terribly wrong.
278         DocFileName zipfile(addName(buffer_->temppath(),
279                 onlyFilename(changeExtension(
280                         filename.toFilesystemEncoding(), ".zip"))));
281
282         ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
283         // copy file back
284         if (!zipfile.copyTo(filename)) {
285                 Alert::error(_("Save failure"),
286                                  bformat(_("Cannot create file %1$s.\n"
287                                            "Please check whether the directory exists and is writeable."),
288                                          from_utf8(filename.absFilename())));
289                 //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
290         }
291         return true;
292 }
293
294
295 EmbeddedFiles::EmbeddedFileList::const_iterator
296 EmbeddedFiles::find(string filename) const
297 {
298         EmbeddedFileList::const_iterator it = file_list_.begin();
299         EmbeddedFileList::const_iterator it_end = file_list_.end();
300         for (; it != it_end; ++it)
301                 if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename)     
302                         return it;
303         return file_list_.end();
304 }
305
306
307 bool EmbeddedFiles::extract() const
308 {
309         EmbeddedFileList::const_iterator it = file_list_.begin();
310         EmbeddedFileList::const_iterator it_end = file_list_.end();
311         for (; it != it_end; ++it)
312                 if (it->embedded())
313                         if(!it->extract(buffer_))
314                                 return false;
315         return true;
316 }
317
318
319 bool EmbeddedFiles::updateFromExternalFile() const
320 {
321         EmbeddedFileList::const_iterator it = file_list_.begin();
322         EmbeddedFileList::const_iterator it_end = file_list_.end();
323         for (; it != it_end; ++it)
324                 if (it->embedded())
325                         if (!it->updateFromExternalFile(buffer_))
326                                 return false;
327         return true;
328 }
329
330
331 string const EmbeddedFiles::getInzipName(string const & abs_filename, string const & name)
332 {
333         // register a new one, using relative file path as inzip_name
334         string inzip_name = name;
335         if (name.empty())
336                 inzip_name = to_utf8(makeRelPath(from_utf8(abs_filename),
337                         from_utf8(buffer_->filePath())));
338         // if inzip_name is an absolute path, use filename only to avoid
339         // leaking of filesystem information in inzip_name
340         // The second case covers cases '../path/file' and '.'
341         if (absolutePath(inzip_name) || prefixIs(inzip_name, "."))
342                 inzip_name = onlyFilename(abs_filename);
343         // if this name has been used...
344         // use _1_name, _2_name etc
345         string tmp = inzip_name;
346         EmbeddedFileList::iterator it;
347         EmbeddedFileList::iterator it_end = file_list_.end();
348         bool unique_name = false;
349         size_t i = 0;
350         while (!unique_name) {
351                 unique_name = true;
352                 if (i > 0)
353                         inzip_name = convert<string>(i) + "_" + tmp;
354                 it = file_list_.begin();
355                 for (; it != it_end; ++it)
356                         if (it->inzipName() == inzip_name) {
357                                 unique_name = false;
358                                 ++i;
359                                 break;
360                         }
361         }
362         return inzip_name;
363 }
364
365
366 void EmbeddedFiles::updateInsets() const
367 {
368         EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
369         EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
370         for (; it != it_end; ++it)
371                 if (it->refCount() > 0)
372                         it->updateInsets(buffer_);
373 }
374
375
376 }