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 FileName emb(emb_file);
136 FileName ext(ext_file);
141 // if external file already exists ...
143 // no need to copy if the files are the same
144 if (sum(*this) == sum(FileName(emb_file)))
146 // otherwise, ask if overwrite
147 int ret = Alert::prompt(
148 _("Overwrite external file?"),
149 bformat(_("External file %1$s already exists, do you want to overwrite it"),
150 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
152 // if the user does not want to overwrite, we still consider it
153 // a successful operation.
158 // need to make directory?
159 string path = support::onlyPath(ext_file);
160 if (!fs::is_directory(path))
161 makedir(const_cast<char*>(path.c_str()), 0755);
162 if (emb.copyTo(ext, false))
164 Alert::error(_("Copy file failure"),
165 bformat(_("Cannot copy file %1$s to %2$s.\n"
166 "Please check whether the directory exists and is writeable."),
167 from_utf8(emb_file), from_utf8(ext_file)));
168 //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
173 bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
175 string ext_file = absFilename();
176 string emb_file = embeddedFile(buf);
178 FileName emb(emb_file);
179 FileName ext(ext_file);
184 // if embedded file already exists ...
186 // no need to copy if the files are the same
187 if (sum(*this) == sum(FileName(emb_file)))
189 // other wise, ask if overwrite
190 int const ret = Alert::prompt(
191 _("Update embedded file?"),
192 bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
193 from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
195 // if the user does not want to overwrite, we still consider it
196 // a successful operation.
200 // need to make directory?
201 string path = support::onlyPath(emb_file);
202 if (!fs::is_directory(path))
203 makedir(const_cast<char*>(path.c_str()), 0755);
204 if (ext.copyTo(emb, false))
206 Alert::error(_("Copy file failure"),
207 bformat(_("Cannot copy file %1$s to %2$s.\n"
208 "Please check whether the directory exists and is writeable."),
209 from_utf8(ext_file), from_utf8(emb_file)));
210 //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
215 void EmbeddedFile::updateInsets(Buffer const * buf) const
217 vector<Inset const *>::const_iterator it = inset_list_.begin();
218 vector<Inset const *>::const_iterator it_end = inset_list_.end();
219 for (; it != it_end; ++it)
220 const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
224 bool EmbeddedFiles::enabled() const
226 return buffer_->params().embedded;
230 void EmbeddedFiles::enable(bool flag)
232 if (enabled() != flag) {
233 // if enable, copy all files to temppath()
234 // if disable, extract all files
235 if ((flag && !updateFromExternalFile()) || (!flag && !extract()))
237 // if operation is successful
238 buffer_->markDirty();
239 buffer_->params().embedded = flag;
246 EmbeddedFile & EmbeddedFiles::registerFile(string const & filename,
247 bool embed, Inset const * inset, string const & inzipName)
249 // filename can be relative or absolute, translate to absolute filename
250 string abs_filename = makeAbsPath(filename, buffer_->filePath()).absFilename();
251 // try to find this file from the list
252 EmbeddedFileList::iterator it = file_list_.begin();
253 EmbeddedFileList::iterator it_end = file_list_.end();
254 for (; it != it_end; ++it)
255 if (it->absFilename() == abs_filename || it->embeddedFile(buffer_) == abs_filename)
257 // find this filename, keep the original embedding status
258 if (it != file_list_.end()) {
263 file_list_.push_back(EmbeddedFile(abs_filename,
264 getInzipName(abs_filename, inzipName), embed, inset));
265 return file_list_.back();
269 void EmbeddedFiles::update()
273 for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
274 it->registerEmbeddedFiles(*buffer_, *this);
278 bool EmbeddedFiles::writeFile(DocFileName const & filename)
280 // file in the temporary path has the content
281 string const content = FileName(addName(buffer_->temppath(),
282 "content.lyx")).toFilesystemEncoding();
284 vector<pair<string, string> > filenames;
285 // add content.lyx to filenames
286 filenames.push_back(make_pair(content, "content.lyx"));
287 // prepare list of embedded file
288 EmbeddedFileList::iterator it = file_list_.begin();
289 EmbeddedFileList::iterator it_end = file_list_.end();
290 for (; it != it_end; ++it) {
291 if (it->embedded()) {
292 string file = it->availableFile(buffer_);
294 lyxerr << "File " << it->absFilename() << " does not exist. Skip embedding it. " << endl;
296 filenames.push_back(make_pair(file, it->inzipName()));
299 // write a zip file with all these files. Write to a temp file first, to
300 // avoid messing up the original file in case something goes terribly wrong.
301 DocFileName zipfile(addName(buffer_->temppath(),
302 onlyFilename(changeExtension(
303 filename.toFilesystemEncoding(), ".zip"))));
305 ::zipFiles(zipfile.toFilesystemEncoding(), filenames);
307 if (!zipfile.copyTo(filename, false)) {
308 Alert::error(_("Save failure"),
309 bformat(_("Cannot create file %1$s.\n"
310 "Please check whether the directory exists and is writeable."),
311 from_utf8(filename.absFilename())));
312 //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
318 EmbeddedFiles::EmbeddedFileList::const_iterator
319 EmbeddedFiles::find(std::string filename) const
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->absFilename() == filename || it->embeddedFile(buffer_) == filename)
326 return file_list_.end();
330 bool EmbeddedFiles::extract() const
332 EmbeddedFileList::const_iterator it = file_list_.begin();
333 EmbeddedFileList::const_iterator it_end = file_list_.end();
334 for (; it != it_end; ++it)
336 if(!it->extract(buffer_))
342 bool EmbeddedFiles::updateFromExternalFile() const
344 EmbeddedFileList::const_iterator it = file_list_.begin();
345 EmbeddedFileList::const_iterator it_end = file_list_.end();
346 for (; it != it_end; ++it)
348 if (!it->updateFromExternalFile(buffer_))
354 string const EmbeddedFiles::getInzipName(string const & abs_filename, string const & name)
356 // register a new one, using relative file path as inzip_name
357 string inzip_name = name;
359 inzip_name = to_utf8(makeRelPath(from_utf8(abs_filename),
360 from_utf8(buffer_->filePath())));
361 // if inzip_name is an absolute path, use filename only to avoid
362 // leaking of filesystem information in inzip_name
363 // The second case covers cases '../path/file' and '.'
364 if (absolutePath(inzip_name) || prefixIs(inzip_name, "."))
365 inzip_name = onlyFilename(abs_filename);
366 // if this name has been used...
367 // use _1_name, _2_name etc
368 string tmp = inzip_name;
369 EmbeddedFileList::iterator it;
370 EmbeddedFileList::iterator it_end = file_list_.end();
371 bool unique_name = false;
373 while (!unique_name) {
376 inzip_name = convert<string>(i) + "_" + tmp;
377 it = file_list_.begin();
378 for (; it != it_end; ++it)
379 if (it->inzipName() == inzip_name) {
389 void EmbeddedFiles::updateInsets() const
391 EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
392 EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
393 for (; it != it_end; ++it)
394 if (it->refCount() > 0)
395 it->updateInsets(buffer_);