3 * \file EmbeddedFiles.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"
17 #include "BufferParams.h"
18 #include "Paragraph.h"
19 #include "InsetIterator.h"
24 #include "ErrorList.h"
26 #include "frontends/alert.h"
28 #include <boost/filesystem/operations.hpp>
30 #include "support/filetools.h"
31 #include "support/fs_extras.h"
32 #include "support/convert.h"
33 #include "support/lyxlib.h"
34 #include "support/lstrings.h"
52 using std::istringstream;
56 namespace fs = boost::filesystem;
57 namespace Alert = frontend::Alert;
59 using support::FileName;
60 using support::DocFileName;
61 using support::makeAbsPath;
62 using support::addName;
63 using support::onlyPath;
64 using support::absolutePath;
65 using support::onlyFilename;
66 using support::makeRelPath;
67 using support::changeExtension;
68 using support::bformat;
69 using support::prefixIs;
71 using support::makedir;
74 EmbeddedFile::EmbeddedFile(string const & file, string const & inzip_name,
75 bool embed, Inset const * inset)
76 : DocFileName(file, true), inzip_name_(inzip_name), embedded_(embed),
77 valid_(true), inset_list_()
80 inset_list_.push_back(inset);
84 string EmbeddedFile::embeddedFile(Buffer const * buf) const
86 return addName(buf->temppath(), inzip_name_);
90 void EmbeddedFile::addInset(Inset const * inset)
92 inset_list_.push_back(inset);
96 Inset const * EmbeddedFile::inset(int idx) const
98 BOOST_ASSERT(idx < refCount());
99 // some embedded file do not have a valid par iterator
100 return inset_list_[idx];
104 void EmbeddedFile::saveBookmark(Buffer const * buf, int idx) const
106 Inset const * ptr = inset(idx);
107 // This might not be the most efficient method ...
108 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
110 // this is basically BufferView::saveBookmark(0)
111 LyX::ref().session().bookmarks().save(
112 FileName(buf->fileName()),
120 // this inset can not be located. There is something wrong that needs
126 string EmbeddedFile::availableFile(Buffer const * buf) const
129 return embeddedFile(buf);
131 return absFilename();
135 void EmbeddedFile::invalidate()
137 // Clear inset_list_ because they will be registered again.
143 bool EmbeddedFile::extract(Buffer const * buf) const
146 string ext_file = absFilename();
147 string emb_file = embeddedFile(buf);
149 if (!fs::exists(emb_file))
152 // if external file already exists ...
153 if (fs::exists(ext_file)) {
154 // no need to copy if the files are the same
155 if (sum(*this) == sum(FileName(emb_file)))
157 // otherwise, ask if overwrite
158 int ret = Alert::prompt(
159 _("Overwrite external file?"),
160 bformat(_("External file %1$s already exists, do you want to overwrite it"),
161 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
163 // if the user does not want to overwrite, we still consider it
164 // a successful operation.
169 // need to make directory?
170 string path = onlyPath(ext_file);
171 if (!fs::is_directory(path))
172 makedir(const_cast<char*>(path.c_str()), 0755);
173 fs::copy_file(emb_file, ext_file, false);
175 } catch (fs::filesystem_error const & fe) {
176 Alert::error(_("Copy file failure"),
177 bformat(_("Cannot copy file %1$s to %2$s.\n"
178 "Please check whether the directory exists and is writeable."),
179 from_utf8(emb_file), from_utf8(ext_file)));
180 LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
186 bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
188 string ext_file = absFilename();
189 string emb_file = embeddedFile(buf);
191 if (!fs::exists(ext_file))
194 // if embedded file already exists ...
195 if (fs::exists(emb_file)) {
196 // no need to copy if the files are the same
197 if (sum(*this) == sum(FileName(emb_file)))
199 // other wise, ask if overwrite
200 int const ret = Alert::prompt(
201 _("Update embedded file?"),
202 bformat(_("Embeddedl file %1$s already exists, do you want to overwrite it"),
203 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
205 // if the user does not want to overwrite, we still consider it
206 // a successful operation.
211 // need to make directory?
212 string path = onlyPath(emb_file);
213 if (!fs::is_directory(path))
214 makedir(const_cast<char*>(path.c_str()), 0755);
215 fs::copy_file(ext_file, emb_file, false);
217 } catch (fs::filesystem_error const & fe) {
218 Alert::error(_("Copy file failure"),
219 bformat(_("Cannot copy file %1$s to %2$s.\n"
220 "Please check whether the directory exists and is writeable."),
221 from_utf8(ext_file), from_utf8(emb_file)));
222 LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
228 void EmbeddedFile::updateInsets(Buffer const * buf) const
230 vector<Inset const *>::const_iterator it = inset_list_.begin();
231 vector<Inset const *>::const_iterator it_end = inset_list_.end();
232 for (; it != it_end; ++it)
233 const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
237 bool EmbeddedFiles::enabled() const
239 return buffer_->params().embedded;
243 void EmbeddedFiles::enable(bool flag)
245 if (enabled() != flag) {
246 // if enable, copy all files to temppath()
247 // if disable, extract all files
248 if ((flag && !updateFromExternalFile()) || (!flag && !extract()))
250 // if operation is successful
251 buffer_->markDirty();
252 buffer_->params().embedded = flag;
259 void EmbeddedFiles::registerFile(string const & filename,
260 bool embed, Inset const * inset, string const & inzipName)
262 // filename can be relative or absolute, translate to absolute filename
263 string abs_filename = makeAbsPath(filename, buffer_->filePath()).absFilename();
264 // try to find this file from the list
265 EmbeddedFileList::iterator it = file_list_.begin();
266 EmbeddedFileList::iterator it_end = file_list_.end();
267 for (; it != it_end; ++it)
268 if (it->absFilename() == abs_filename || it->embeddedFile(buffer_) == abs_filename)
270 // find this filename, keep the original embedding status
271 if (it != file_list_.end()) {
276 // try to be more careful
277 file_list_.push_back(EmbeddedFile(abs_filename,
278 getInzipName(abs_filename, inzipName), embed, inset));
282 void EmbeddedFiles::update()
284 // invalidate all files, obsolete files will then not be validated by the
285 // following document scan. These files will still be kept though, because
286 // they may be added later and their embedding status will be meaningful
287 // again (thinking of cut/paste of an InsetInclude).
288 EmbeddedFileList::iterator it = file_list_.begin();
289 EmbeddedFileList::iterator it_end = file_list_.end();
290 for (; it != it_end; ++it)
291 // we do not update items that are manually inserted
292 if (it->refCount() > 0)
295 for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
296 it->registerEmbeddedFiles(*buffer_, *this);
300 bool EmbeddedFiles::writeFile(DocFileName const & filename)
302 // file in the temporary path has the content
303 string const content = FileName(addName(buffer_->temppath(),
304 "content.lyx")).toFilesystemEncoding();
306 vector<pair<string, string> > filenames;
307 // add content.lyx to filenames
308 filenames.push_back(make_pair(content, "content.lyx"));
309 // prepare list of embedded file
310 EmbeddedFileList::iterator it = file_list_.begin();
311 EmbeddedFileList::iterator it_end = file_list_.end();
312 for (; it != it_end; ++it) {
313 if (it->valid() && it->embedded()) {
314 string file = it->availableFile(buffer_);
316 lyxerr << "File " << it->absFilename() << " does not exist. Skip embedding it. " << endl;
318 filenames.push_back(make_pair(file, it->inzipName()));
321 // write a zip file with all these files. Write to a temp file first, to
322 // avoid messing up the original file in case something goes terribly wrong.
323 DocFileName zipfile(addName(buffer_->temppath(),
324 onlyFilename(changeExtension(
325 filename.toFilesystemEncoding(), ".zip"))));
327 ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
330 fs::copy_file(zipfile.toFilesystemEncoding(), filename.toFilesystemEncoding(), false);
331 } catch (fs::filesystem_error const & fe) {
332 Alert::error(_("Save failure"),
333 bformat(_("Cannot create file %1$s.\n"
334 "Please check whether the directory exists and is writeable."),
335 from_utf8(filename.absFilename())));
336 LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
342 EmbeddedFiles::EmbeddedFileList::const_iterator EmbeddedFiles::find(std::string filename) const
344 EmbeddedFileList::const_iterator it = file_list_.begin();
345 EmbeddedFileList::const_iterator it_end = file_list_.end();
346 for (; it != it_end; ++it)
347 if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename)
349 return file_list_.end();
353 bool EmbeddedFiles::extract() const
355 EmbeddedFileList::const_iterator it = file_list_.begin();
356 EmbeddedFileList::const_iterator it_end = file_list_.end();
357 for (; it != it_end; ++it)
358 if (it->valid() && it->embedded())
359 if(!it->extract(buffer_))
365 bool EmbeddedFiles::updateFromExternalFile() const
367 EmbeddedFileList::const_iterator it = file_list_.begin();
368 EmbeddedFileList::const_iterator it_end = file_list_.end();
369 for (; it != it_end; ++it)
370 if (it->valid() && it->embedded())
371 if (!it->updateFromExternalFile(buffer_))
377 string const EmbeddedFiles::getInzipName(string const & abs_filename, string const & name)
379 // register a new one, using relative file path as inzip_name
380 string inzip_name = name;
382 inzip_name = to_utf8(makeRelPath(from_utf8(abs_filename),
383 from_utf8(buffer_->filePath())));
384 // if inzip_name is an absolute path, use filename only to avoid
385 // leaking of filesystem information in inzip_name
386 // The second case covers cases '../path/file' and '.'
387 if (absolutePath(inzip_name) || prefixIs(inzip_name, "."))
388 inzip_name = onlyFilename(abs_filename);
389 // if this name has been used...
390 // use _1_name, _2_name etc
391 string tmp = inzip_name;
392 EmbeddedFileList::iterator it;
393 EmbeddedFileList::iterator it_end = file_list_.end();
394 bool unique_name = false;
396 while (!unique_name) {
399 inzip_name = convert<string>(i) + "_" + tmp;
400 it = file_list_.begin();
401 for (; it != it_end; ++it)
402 if (it->inzipName() == inzip_name) {
412 bool EmbeddedFiles::readManifest(Lexer & lex, ErrorList & errorList)
415 int begin_manifest_line = -1;
420 bool embedded = false;
424 string const token = lex.getString();
429 if (token == "\\end_manifest")
433 if (token == "\\begin_manifest") {
434 begin_manifest_line = line;
438 LYXERR(Debug::PARSER) << "Handling document manifest token: `"
439 << token << '\'' << endl;
441 if (token == "\\filename")
443 else if (token == "\\inzipName")
445 else if (token == "\\embed") {
447 registerFile(filename, embedded, NULL, inzipName);
451 docstring const s = _("\\begin_file is missing");
452 errorList.push_back(ErrorItem(_("Manifest error"),
456 if (begin_manifest_line) {
457 docstring const s = _("\\begin_manifest is missing");
458 errorList.push_back(ErrorItem(_("Manifest error"),
465 void EmbeddedFiles::writeManifest(ostream & os) const
467 EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
468 EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
469 for (; it != it_end; ++it) {
472 // save the relative path
474 << to_utf8(makeRelPath(from_utf8(it->absFilename()),
475 from_utf8(buffer_->filePath()))) << '\n'
476 << "\\inzipName " << it->inzipName() << '\n'
477 << "\\embed " << (it->embedded() ? "true" : "false") << '\n';
482 void EmbeddedFiles::updateInsets() const
484 EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
485 EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
486 for (; it != it_end; ++it)
487 if (it->valid() && it->refCount() > 0)
488 it->updateInsets(buffer_);