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/convert.h"
32 #include "support/lyxlib.h"
33 #include "support/lstrings.h"
51 using std::istringstream;
55 namespace fs = boost::filesystem;
56 namespace Alert = frontend::Alert;
58 using support::FileName;
59 using support::DocFileName;
60 using support::makeAbsPath;
61 using support::addName;
62 using support::absolutePath;
63 using support::onlyFilename;
64 using support::makeRelPath;
65 using support::changeExtension;
66 using support::bformat;
67 using support::prefixIs;
69 using support::makedir;
72 EmbeddedFile::EmbeddedFile(string const & file, string const & inzip_name,
73 bool embed, Inset const * inset)
74 : DocFileName(file, true), inzip_name_(inzip_name), embedded_(embed),
78 inset_list_.push_back(inset);
82 string EmbeddedFile::embeddedFile(Buffer const * buf) const
84 return addName(buf->temppath(), inzip_name_);
88 void EmbeddedFile::addInset(Inset const * inset)
90 inset_list_.push_back(inset);
94 Inset const * EmbeddedFile::inset(int idx) const
96 BOOST_ASSERT(idx < refCount());
97 // some embedded file do not have a valid par iterator
98 return inset_list_[idx];
102 void EmbeddedFile::saveBookmark(Buffer const * buf, int idx) const
104 Inset const * ptr = inset(idx);
105 // This might not be the most efficient method ...
106 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
108 // this is basically BufferView::saveBookmark(0)
109 LyX::ref().session().bookmarks().save(
110 FileName(buf->absFileName()),
118 // this inset can not be located. There is something wrong that needs
124 string EmbeddedFile::availableFile(Buffer const * buf) const
126 return embedded() ? embeddedFile(buf) : absFilename();
130 bool EmbeddedFile::extract(Buffer const * buf) const
132 string ext_file = absFilename();
133 string emb_file = embeddedFile(buf);
135 if (!fs::exists(emb_file))
138 // if external file already exists ...
139 if (fs::exists(ext_file)) {
140 // no need to copy if the files are the same
141 if (sum(*this) == sum(FileName(emb_file)))
143 // otherwise, ask if overwrite
144 int ret = Alert::prompt(
145 _("Overwrite external file?"),
146 bformat(_("External file %1$s already exists, do you want to overwrite it"),
147 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
149 // if the user does not want to overwrite, we still consider it
150 // a successful operation.
155 // need to make directory?
156 string path = support::onlyPath(ext_file);
157 if (!fs::is_directory(path))
158 makedir(const_cast<char*>(path.c_str()), 0755);
159 FileName emb(emb_file);
160 FileName ext(ext_file);
161 if (emb.copyTo(ext, false))
163 Alert::error(_("Copy file failure"),
164 bformat(_("Cannot copy file %1$s to %2$s.\n"
165 "Please check whether the directory exists and is writeable."),
166 from_utf8(emb_file), from_utf8(ext_file)));
167 //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
172 bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
174 string ext_file = absFilename();
175 string emb_file = embeddedFile(buf);
177 if (!fs::exists(ext_file))
180 // if embedded file already exists ...
181 if (fs::exists(emb_file)) {
182 // no need to copy if the files are the same
183 if (sum(*this) == sum(FileName(emb_file)))
185 // other wise, ask if overwrite
186 int const ret = Alert::prompt(
187 _("Update embedded file?"),
188 bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
189 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
191 // if the user does not want to overwrite, we still consider it
192 // a successful operation.
196 FileName emb(emb_file);
197 FileName ext(ext_file);
198 // need to make directory?
199 string path = support::onlyPath(emb_file);
200 if (!fs::is_directory(path))
201 makedir(const_cast<char*>(path.c_str()), 0755);
202 if (ext.copyTo(emb, false))
204 Alert::error(_("Copy file failure"),
205 bformat(_("Cannot copy file %1$s to %2$s.\n"
206 "Please check whether the directory exists and is writeable."),
207 from_utf8(ext_file), from_utf8(emb_file)));
208 //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
213 void EmbeddedFile::updateInsets(Buffer const * buf) const
215 vector<Inset const *>::const_iterator it = inset_list_.begin();
216 vector<Inset const *>::const_iterator it_end = inset_list_.end();
217 for (; it != it_end; ++it)
218 const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
222 bool EmbeddedFiles::enabled() const
224 return buffer_->params().embedded;
228 void EmbeddedFiles::enable(bool flag)
230 if (enabled() != flag) {
231 // if enable, copy all files to temppath()
232 // if disable, extract all files
233 if ((flag && !updateFromExternalFile()) || (!flag && !extract()))
235 // if operation is successful
236 buffer_->markDirty();
237 buffer_->params().embedded = flag;
244 EmbeddedFile & EmbeddedFiles::registerFile(string const & filename,
245 bool embed, Inset const * inset, string const & inzipName)
247 // filename can be relative or absolute, translate to absolute filename
248 string abs_filename = makeAbsPath(filename, buffer_->filePath()).absFilename();
249 // try to find this file from the list
250 EmbeddedFileList::iterator it = file_list_.begin();
251 EmbeddedFileList::iterator it_end = file_list_.end();
252 for (; it != it_end; ++it)
253 if (it->absFilename() == abs_filename || it->embeddedFile(buffer_) == abs_filename)
255 // find this filename, keep the original embedding status
256 if (it != file_list_.end()) {
261 file_list_.push_back(EmbeddedFile(abs_filename,
262 getInzipName(abs_filename, inzipName), embed, inset));
263 return file_list_.back();
267 void EmbeddedFiles::update()
271 for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
272 it->registerEmbeddedFiles(*buffer_, *this);
276 bool EmbeddedFiles::writeFile(DocFileName const & filename)
278 // file in the temporary path has the content
279 string const content = FileName(addName(buffer_->temppath(),
280 "content.lyx")).toFilesystemEncoding();
282 vector<pair<string, string> > filenames;
283 // add content.lyx to filenames
284 filenames.push_back(make_pair(content, "content.lyx"));
285 // prepare list of embedded file
286 EmbeddedFileList::iterator it = file_list_.begin();
287 EmbeddedFileList::iterator it_end = file_list_.end();
288 for (; it != it_end; ++it) {
289 if (it->embedded()) {
290 string file = it->availableFile(buffer_);
292 lyxerr << "File " << it->absFilename() << " does not exist. Skip embedding it. " << endl;
294 filenames.push_back(make_pair(file, it->inzipName()));
297 // write a zip file with all these files. Write to a temp file first, to
298 // avoid messing up the original file in case something goes terribly wrong.
299 DocFileName zipfile(addName(buffer_->temppath(),
300 onlyFilename(changeExtension(
301 filename.toFilesystemEncoding(), ".zip"))));
303 ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
305 if (!zipfile.copyTo(filename, false)) {
306 Alert::error(_("Save failure"),
307 bformat(_("Cannot create file %1$s.\n"
308 "Please check whether the directory exists and is writeable."),
309 from_utf8(filename.absFilename())));
310 //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
316 EmbeddedFiles::EmbeddedFileList::const_iterator
317 EmbeddedFiles::find(std::string filename) const
319 EmbeddedFileList::const_iterator it = file_list_.begin();
320 EmbeddedFileList::const_iterator it_end = file_list_.end();
321 for (; it != it_end; ++it)
322 if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename)
324 return file_list_.end();
328 bool EmbeddedFiles::extract() const
330 EmbeddedFileList::const_iterator it = file_list_.begin();
331 EmbeddedFileList::const_iterator it_end = file_list_.end();
332 for (; it != it_end; ++it)
334 if(!it->extract(buffer_))
340 bool EmbeddedFiles::updateFromExternalFile() const
342 EmbeddedFileList::const_iterator it = file_list_.begin();
343 EmbeddedFileList::const_iterator it_end = file_list_.end();
344 for (; it != it_end; ++it)
346 if (!it->updateFromExternalFile(buffer_))
352 string const EmbeddedFiles::getInzipName(string const & abs_filename, string const & name)
354 // register a new one, using relative file path as inzip_name
355 string inzip_name = name;
357 inzip_name = to_utf8(makeRelPath(from_utf8(abs_filename),
358 from_utf8(buffer_->filePath())));
359 // if inzip_name is an absolute path, use filename only to avoid
360 // leaking of filesystem information in inzip_name
361 // The second case covers cases '../path/file' and '.'
362 if (absolutePath(inzip_name) || prefixIs(inzip_name, "."))
363 inzip_name = onlyFilename(abs_filename);
364 // if this name has been used...
365 // use _1_name, _2_name etc
366 string tmp = inzip_name;
367 EmbeddedFileList::iterator it;
368 EmbeddedFileList::iterator it_end = file_list_.end();
369 bool unique_name = false;
371 while (!unique_name) {
374 inzip_name = convert<string>(i) + "_" + tmp;
375 it = file_list_.begin();
376 for (; it != it_end; ++it)
377 if (it->inzipName() == inzip_name) {
387 void EmbeddedFiles::updateInsets() const
389 EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
390 EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
391 for (; it != it_end; ++it)
392 if (it->refCount() > 0)
393 it->updateInsets(buffer_);