]> git.lyx.org Git - lyx.git/blob - src/EmbeddedFiles.cpp
* completion infrastructure
[lyx.git] / src / EmbeddedFiles.cpp
1 // -*- C++ -*-
2 /**
3  * \file EmbeddedFileList.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/lstrings.h"
34 #include "support/ExceptionMessage.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 EmbeddedFile::EmbeddedFile(string const & file, std::string const & buffer_path)
49         : DocFileName("", false), inzip_name_(""), embedded_(false), inset_list_(),
50         temp_path_("")
51 {
52         set(file, buffer_path);
53 }
54
55
56 void EmbeddedFile::set(std::string const & filename, std::string const & buffer_path)
57 {
58         DocFileName::set(filename, buffer_path);
59         if (filename.empty())
60                 return;
61
62         inzip_name_ = calcInzipName(buffer_path);
63 }
64
65
66 void EmbeddedFile::setInzipName(std::string const & name)
67 {
68         if (name.empty() || name == inzip_name_)
69                 return;
70
71         // an enabled EmbeededFile should have this problem handled
72         BOOST_ASSERT(!enabled());
73         // file will be synced when it is enabled
74         inzip_name_ = name;
75 }
76
77
78 string EmbeddedFile::embeddedFile() const
79 {
80         BOOST_ASSERT(enabled());
81         return temp_path_ + inzip_name_;
82 }
83
84
85 FileName EmbeddedFile::availableFile() const
86 {
87         if (enabled() && embedded())
88                 return FileName(embeddedFile());
89         else
90                 return *this;
91 }
92
93
94 string EmbeddedFile::latexFilename(std::string const & buffer_path) const
95 {
96         return (enabled() && embedded()) ? inzip_name_ : relFilename(buffer_path);
97 }
98
99
100 void EmbeddedFile::addInset(Inset const * inset)
101 {
102         if (inset != NULL)
103                 inset_list_.push_back(inset);
104 }
105
106
107 void EmbeddedFile::setEmbed(bool embed)
108 {
109         embedded_ = embed;
110 }
111
112
113 void EmbeddedFile::enable(bool flag, Buffer const * buf)
114 {
115         if (enabled() == flag)
116                 return;
117
118         if (flag) {
119                 temp_path_ = buf->temppath();
120                 if (!suffixIs(temp_path_, '/'))
121                         temp_path_ += '/';
122                 if (embedded()) {
123                         if (inzip_name_ != calcInzipName(buf->filePath()))
124                                 syncInzipFile(buf->filePath());
125                         updateFromExternalFile();
126                 } else
127                         extract();
128         } else {
129                 extract();
130                 temp_path_ = "";
131         }
132 }
133
134
135 bool EmbeddedFile::extract() const
136 {
137         BOOST_ASSERT(enabled());
138
139         string ext_file = absFilename();
140         string emb_file = embeddedFile();
141
142         FileName emb(emb_file);
143         FileName ext(ext_file);
144
145         if (!emb.exists()) {
146                 if (ext.exists())
147                         return true;
148                 else
149                         throw ExceptionMessage(ErrorException, _("Failed to extract file"),
150                                 bformat(_("Cannot extract file '%1$s'.\n"
151                                 "Source file %2$s does not exist"),
152                                 from_utf8(outputFilename()), from_utf8(emb_file)));
153         }
154
155         // if external file already exists ...
156         if (ext.exists()) {
157                 // no need to copy if the files are the same
158                 if (checksum() == FileName(emb_file).checksum())
159                         return true;
160                 // otherwise, ask if overwrite
161                 int ret = Alert::prompt(
162                         _("Overwrite external file?"),
163                         bformat(_("External file %1$s already exists, do you want to overwrite it"),
164                                 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
165                 if (ret != 0)
166                         // if the user does not want to overwrite, we still consider it
167                         // a successful operation.
168                         return true;
169         }
170         // copy file
171
172         // need to make directory?
173         FileName path = ext.onlyPath();
174         if (!path.createPath()) {
175                 throw ExceptionMessage(ErrorException, _("Copy file failure"),
176                         bformat(_("Cannot create file path '%1$s'.\n"
177                         "Please check whether the path is writeable."),
178                         from_utf8(path.absFilename())));
179                 return false;
180         }
181
182         if (emb.copyTo(ext)) {
183                 LYXERR(Debug::FILES, "Extract file " << emb_file << " to " << ext_file << endl);
184                 return true;
185         }
186
187         throw ExceptionMessage(ErrorException, _("Copy file failure"),
188                  bformat(_("Cannot copy file %1$s to %2$s.\n"
189                                  "Please check whether the directory exists and is writeable."),
190                                 from_utf8(emb_file), from_utf8(ext_file)));
191         return false;
192 }
193
194
195 bool EmbeddedFile::updateFromExternalFile() const
196 {
197         BOOST_ASSERT(enabled());
198
199         string ext_file = absFilename();
200         string emb_file = embeddedFile();
201
202         FileName emb(emb_file);
203         FileName ext(ext_file);
204
205         if (!ext.exists()) {
206                 // no need to update
207                 if (emb.exists())
208                         return true;
209                 // no external and internal file
210                 throw ExceptionMessage(ErrorException,
211                         _("Failed to embed file"),
212                         bformat(_("Failed to embed file %1$s.\n"
213                            "Please check whether this file exists and is readable."),
214                                 from_utf8(ext_file)));
215         }
216
217         // if embedded file already exists ...
218         if (emb.exists()) {
219                 // no need to copy if the files are the same
220                 if (checksum() == FileName(emb_file).checksum())
221                         return true;
222                 // other wise, ask if overwrite
223                 int const ret = Alert::prompt(
224                         _("Update embedded file?"),
225                         bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
226                                 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
227                 if (ret != 0)
228                         // if the user does not want to overwrite, we still consider it
229                         // a successful operation.
230                         return true;
231         }
232         // copy file
233         // need to make directory?
234         FileName path = emb.onlyPath();
235         if (!path.isDirectory())
236                 path.createPath();
237         if (ext.copyTo(emb))
238                 return true;
239         throw ExceptionMessage(ErrorException,
240                 _("Copy file failure"),
241                 bformat(_("Cannot copy file %1$s to %2$s.\n"
242                            "Please check whether the directory exists and is writeable."),
243                                 from_utf8(ext_file), from_utf8(emb_file)));
244         //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
245         return false;
246 }
247
248
249 void EmbeddedFile::updateInsets(Buffer const * buf) const
250 {
251         vector<Inset const *>::const_iterator it = inset_list_.begin();
252         vector<Inset const *>::const_iterator it_end = inset_list_.end();
253         for (; it != it_end; ++it)
254                 const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
255 }
256
257
258 bool EmbeddedFile::isReadableFile() const
259 {
260         return availableFile().isReadableFile();
261 }
262
263
264 unsigned long EmbeddedFile::checksum() const
265 {
266         return availableFile().checksum();
267 }
268
269 /**
270 Under the lyx temp directory, content.lyx and its embedded files are usually
271 saved as
272
273 $temp/$embDirName/file.lyx
274 $temp/$embDirName/figure1.png     for ./figure1.png)
275 $temp/$embDirName/sub/figure2.png for ./sub/figure2.png)
276
277 This works fine for embedded files that are in the current or deeper directory
278 of the document directory, but not for files such as ../figures/figure.png.
279 A unique name $upDirName is chosen to represent .. in such filenames so that
280 'up' directories can be stored 'down' the directory tree:
281
282 $temp/$embDirName/$upDirName/figures/figure.png     for ../figures/figure.png
283 $temp/$embDirName/$upDirName/$upDirName/figure.png  for ../../figure.png
284
285 This name has to be fixed because it is used in lyx bundled .zip file.
286
287 Using a similar trick, we use $absDirName for absolute path so that
288 an absolute filename can be saved as
289
290 $temp/$embDirName/$absDirName/a/absolute/path for /a/absolute/path
291
292 */
293 const std::string embDirName = "LyX.Embedded.Files";
294 const std::string upDirName = "LyX.Embed.Dir.Up";
295 const std::string absDirName = "LyX.Embed.Dir.Abs";
296 const std::string driveName = "LyX.Embed.Drive";
297 const std::string spaceName = "LyX.Embed.Space";
298
299 std::string EmbeddedFile::calcInzipName(std::string const & buffer_path)
300 {
301         string inzipName = to_utf8(makeRelPath(from_utf8(absFilename()),
302                         from_utf8(buffer_path)));
303         
304         if (FileName(inzipName).isAbsolute())
305                 inzipName = absDirName + '/' + inzipName;
306
307         // replace .. by upDirName
308         if (prefixIs(inzipName, "."))
309                 inzipName = subst(inzipName, "..", upDirName);
310         // replace special characters by their value
311         inzipName = subst(inzipName, ":", driveName);
312         inzipName = subst(inzipName, " ", spaceName);
313
314         // to avoid name conflict between $docu_path/file and $temp_path/file
315         // embedded files are in a subdirectory of $temp_path.
316         inzipName = embDirName + '/' + inzipName;
317         return inzipName;
318 }
319
320
321 void EmbeddedFile::syncInzipFile(std::string const & buffer_path)
322 {
323         BOOST_ASSERT(enabled());
324         string old_emb_file = temp_path_ + '/' + inzip_name_;
325         FileName old_emb(old_emb_file);
326
327         LYXERR(Debug::FILES, " OLD ZIP " << old_emb_file <<
328                 " NEW ZIP " << calcInzipName(buffer_path));
329
330         //BOOST_ASSERT(old_emb.exists());
331         
332         string new_inzip_name = calcInzipName(buffer_path);
333         string new_emb_file = temp_path_ + '/' + new_inzip_name;
334         FileName new_emb(new_emb_file);
335         
336         // need to make directory?
337         FileName path = new_emb.onlyPath();
338         if (!path.createPath()) {
339                 throw ExceptionMessage(ErrorException, _("Sync file failure"),
340                         bformat(_("Cannot create file path '%1$s'.\n"
341                         "Please check whether the path is writeable."),
342                         from_utf8(path.absFilename())));
343                 return;
344         }
345
346         if (old_emb.copyTo(new_emb)) {
347                 LYXERR(Debug::FILES, "Sync inzip file from " << inzip_name_ 
348                         << " to " << new_inzip_name);
349                 inzip_name_ = new_inzip_name;
350                 return;
351         }
352         throw ExceptionMessage(ErrorException, _("Sync file failure"),
353                  bformat(_("Cannot copy file %1$s to %2$s.\n"
354                                  "Please check whether the directory exists and is writeable."),
355                                 from_utf8(old_emb_file), from_utf8(new_emb_file)));
356 }
357
358
359 bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
360 {
361         return lhs.absFilename() == rhs.absFilename()
362                 && lhs.saveAbsPath() == rhs.saveAbsPath()
363                 && lhs.embedded() == rhs.embedded();
364 }
365
366
367 bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
368 {
369         return !(lhs == rhs);
370 }
371
372
373 void EmbeddedFileList::enable(bool flag, Buffer & buffer)
374 {
375         if (buffer.embedded() == flag)
376                 return;
377         
378         // update embedded file list
379         update(buffer);
380         
381         int count_embedded = 0;
382         int count_external = 0;
383         std::vector<EmbeddedFile>::iterator it = begin();
384         std::vector<EmbeddedFile>::iterator it_end = end();
385         // an exception may be thrown
386         for (; it != it_end; ++it) {
387                 it->enable(flag, &buffer);
388                 if (it->embedded())
389                         count_embedded ++;
390                 else
391                         count_external ++;
392         }
393         // if operation is successful (no exception is thrown)
394         buffer.markDirty();
395         buffer.params().embedded = flag;
396
397         // if the operation is successful, update insets
398         for (it = begin(); it != it_end; ++it)
399                 it->updateInsets(&buffer);
400         
401         // show result
402         if (flag) {
403                 docstring const msg = bformat(_("%1$d external files are ignored.\n"
404                         "%2$d embeddable files are embedded.\n"), count_external, count_embedded);
405                 Alert::information(_("Packing all files"), msg);
406         } else {
407                 docstring const msg = bformat(_("%1$d external files are ignored.\n"
408                         "%2$d embedded files are extracted.\n"), count_external, count_embedded);
409                 Alert::information(_("Unpacking all files"), msg);
410         }
411 }
412
413
414 void EmbeddedFileList::registerFile(EmbeddedFile const & file,
415         Inset const * inset, Buffer const & buffer)
416 {
417         BOOST_ASSERT(!buffer.embedded() || file.availableFile().exists());
418         BOOST_ASSERT(!buffer.embedded() || file.enabled());
419
420         // try to find this file from the list
421         std::vector<EmbeddedFile>::iterator it = begin();
422         std::vector<EmbeddedFile>::iterator it_end = end();
423         for (; it != it_end; ++it)
424                 if (it->absFilename() == file.absFilename()) {
425                         if (it->embedded() != file.embedded()) {
426                                 Alert::error(_("Wrong embedding status."),
427                                         bformat(_("File %1$s is included in more than one insets, "
428                                                 "but with different embedding status. Assuming embedding status."),
429                                                 from_utf8(it->outputFilename())));
430                                 it->setEmbed(true);
431                                 // update the inset with this embedding status.
432                                 const_cast<Inset*>(inset)->updateEmbeddedFile(buffer, *it);
433                         }
434                         it->addInset(inset);
435                         return;
436                 }
437         //
438         push_back(file);
439         back().addInset(inset);
440 }
441
442
443 void EmbeddedFileList::update(Buffer const & buffer)
444 {
445         clear();
446
447         for (InsetIterator it = inset_iterator_begin(buffer.inset()); it; ++it)
448                 it->registerEmbeddedFiles(buffer, *this);
449 }
450
451
452 bool EmbeddedFileList::writeFile(DocFileName const & filename, Buffer const & buffer)
453 {
454         // file in the temporary path has the content
455         string const content = FileName(addName(buffer.temppath(),
456                 "content.lyx")).toFilesystemEncoding();
457
458         vector<pair<string, string> > filenames;
459         // add content.lyx to filenames
460         filenames.push_back(make_pair(content, "content.lyx"));
461         // prepare list of embedded file
462         update(buffer);
463         std::vector<EmbeddedFile>::iterator it = begin();
464         std::vector<EmbeddedFile>::iterator it_end = end();
465         for (; it != it_end; ++it) {
466                 if (it->embedded()) {
467                         string file = it->embeddedFile();
468                         if (!FileName(file).exists())
469                                 throw ExceptionMessage(ErrorException, _("Failed to write file"),
470                                         bformat(_("Embedded file %1$s does not exist. Did you tamper lyx temporary directory?"),
471                                                 it->displayName()));
472                         filenames.push_back(make_pair(file, it->inzipName()));
473                         LYXERR(Debug::FILES, "Writing file " << it->outputFilename()
474                                 << " as " << it->inzipName() << endl);
475                 }
476         }
477         // write a zip file with all these files. Write to a temp file first, to
478         // avoid messing up the original file in case something goes terribly wrong.
479         DocFileName zipfile(addName(buffer.temppath(),
480                 onlyFilename(changeExtension(
481                         filename.toFilesystemEncoding(), ".zip"))));
482
483         ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
484         // copy file back
485         if (!zipfile.copyTo(filename)) {
486                 Alert::error(_("Save failure"),
487                                  bformat(_("Cannot create file %1$s.\n"
488                                            "Please check whether the directory exists and is writeable."),
489                                          from_utf8(filename.absFilename())));
490                 //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
491         }
492         return true;
493 }
494
495 }