3 * \file EmbeddedFileList.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "EmbeddedFiles.h"
18 #include "BufferParams.h"
19 #include "ErrorList.h"
21 #include "InsetIterator.h"
24 #include "Paragraph.h"
27 #include "frontends/alert.h"
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"
42 using namespace lyx::support;
46 namespace Alert = frontend::Alert;
48 EmbeddedFile::EmbeddedFile(string const & file, std::string const & buffer_path)
49 : DocFileName("", false), inzip_name_(""), embedded_(false), inset_list_(),
52 set(file, buffer_path);
56 void EmbeddedFile::set(std::string const & filename, std::string const & buffer_path)
58 DocFileName::set(filename, buffer_path);
62 inzip_name_ = calcInzipName(buffer_path);
66 void EmbeddedFile::setInzipName(std::string const & name)
68 if (name.empty() || name == inzip_name_)
71 // an enabled EmbeededFile should have this problem handled
72 BOOST_ASSERT(!enabled());
73 // file will be synced when it is enabled
78 string EmbeddedFile::embeddedFile() const
80 BOOST_ASSERT(enabled());
81 return temp_path_ + inzip_name_;
85 FileName EmbeddedFile::availableFile() const
87 if (enabled() && embedded())
88 return FileName(embeddedFile());
94 string EmbeddedFile::latexFilename(std::string const & buffer_path) const
96 return (enabled() && embedded()) ? inzip_name_ : relFilename(buffer_path);
100 void EmbeddedFile::addInset(Inset const * inset)
103 inset_list_.push_back(inset);
107 void EmbeddedFile::setEmbed(bool embed)
113 void EmbeddedFile::enable(bool flag, Buffer const * buf)
115 if (enabled() == flag)
119 temp_path_ = buf->temppath();
120 if (!suffixIs(temp_path_, '/'))
123 if (inzip_name_ != calcInzipName(buf->filePath()))
124 syncInzipFile(buf->filePath());
125 updateFromExternalFile();
135 bool EmbeddedFile::extract() const
137 BOOST_ASSERT(enabled());
139 string ext_file = absFilename();
140 string emb_file = embeddedFile();
142 FileName emb(emb_file);
143 FileName ext(ext_file);
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)));
155 // if external file already exists ...
157 // no need to copy if the files are the same
158 if (checksum() == FileName(emb_file).checksum())
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"));
166 // if the user does not want to overwrite, we still consider it
167 // a successful operation.
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())));
182 if (emb.copyTo(ext)) {
183 LYXERR(Debug::FILES, "Extract file " << emb_file << " to " << ext_file << endl);
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)));
195 bool EmbeddedFile::updateFromExternalFile() const
197 BOOST_ASSERT(enabled());
199 string ext_file = absFilename();
200 string emb_file = embeddedFile();
202 FileName emb(emb_file);
203 FileName ext(ext_file);
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)));
217 // if embedded file already exists ...
219 // no need to copy if the files are the same
220 if (checksum() == FileName(emb_file).checksum())
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"));
228 // if the user does not want to overwrite, we still consider it
229 // a successful operation.
233 // need to make directory?
234 FileName path = emb.onlyPath();
235 if (!path.isDirectory())
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());
249 void EmbeddedFile::updateInsets(Buffer const * buf) const
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);
258 bool EmbeddedFile::isReadableFile() const
260 return availableFile().isReadableFile();
264 unsigned long EmbeddedFile::checksum() const
266 return availableFile().checksum();
270 Under the lyx temp directory, content.lyx and its embedded files are usually
273 $temp/$embDirName/file.lyx
274 $temp/$embDirName/figure1.png for ./figure1.png)
275 $temp/$embDirName/sub/figure2.png for ./sub/figure2.png)
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:
282 $temp/$embDirName/$upDirName/figures/figure.png for ../figures/figure.png
283 $temp/$embDirName/$upDirName/$upDirName/figure.png for ../../figure.png
285 This name has to be fixed because it is used in lyx bundled .zip file.
287 Using a similar trick, we use $absDirName for absolute path so that
288 an absolute filename can be saved as
290 $temp/$embDirName/$absDirName/a/absolute/path for /a/absolute/path
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";
299 std::string EmbeddedFile::calcInzipName(std::string const & buffer_path)
301 string inzipName = to_utf8(makeRelPath(from_utf8(absFilename()),
302 from_utf8(buffer_path)));
304 if (FileName(inzipName).isAbsolute())
305 inzipName = absDirName + '/' + inzipName;
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);
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;
321 void EmbeddedFile::syncInzipFile(std::string const & buffer_path)
323 BOOST_ASSERT(enabled());
324 string old_emb_file = temp_path_ + '/' + inzip_name_;
325 FileName old_emb(old_emb_file);
327 LYXERR(Debug::FILES, " OLD ZIP " << old_emb_file <<
328 " NEW ZIP " << calcInzipName(buffer_path));
330 //BOOST_ASSERT(old_emb.exists());
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);
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())));
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;
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)));
359 bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
361 return lhs.absFilename() == rhs.absFilename()
362 && lhs.saveAbsPath() == rhs.saveAbsPath()
363 && lhs.embedded() == rhs.embedded();
367 bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
369 return !(lhs == rhs);
373 void EmbeddedFileList::enable(bool flag, Buffer & buffer)
375 if (buffer.embedded() == flag)
378 // update embedded file list
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);
393 // if operation is successful (no exception is thrown)
395 buffer.params().embedded = flag;
397 // if the operation is successful, update insets
398 for (it = begin(); it != it_end; ++it)
399 it->updateInsets(&buffer);
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);
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);
414 void EmbeddedFileList::registerFile(EmbeddedFile const & file,
415 Inset const * inset, Buffer const & buffer)
417 BOOST_ASSERT(!buffer.embedded() || file.availableFile().exists());
418 BOOST_ASSERT(!buffer.embedded() || file.enabled());
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())));
431 // update the inset with this embedding status.
432 const_cast<Inset*>(inset)->updateEmbeddedFile(buffer, *it);
439 back().addInset(inset);
443 void EmbeddedFileList::update(Buffer const & buffer)
447 for (InsetIterator it = inset_iterator_begin(buffer.inset()); it; ++it)
448 it->registerEmbeddedFiles(buffer, *this);
452 bool EmbeddedFileList::writeFile(DocFileName const & filename, Buffer const & buffer)
454 // file in the temporary path has the content
455 string const content = FileName(addName(buffer.temppath(),
456 "content.lyx")).toFilesystemEncoding();
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
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?"),
472 filenames.push_back(make_pair(file, it->inzipName()));
473 LYXERR(Debug::FILES, "Writing file " << it->outputFilename()
474 << " as " << it->inzipName() << endl);
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"))));
483 ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
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());