+ Inset const * ptr = inset(idx);
+ // This might not be the most efficient method ...
+ for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
+ if (&(*it) == ptr) {
+ // this is basically BufferView::saveBookmark(0)
+ LyX::ref().session().bookmarks().save(
+ FileName(buf->fileName()),
+ it.bottom().pit(),
+ it.bottom().pos(),
+ it.paragraph().id(),
+ it.pos(),
+ 0
+ );
+ }
+ // this inset can not be located. There is something wrong that needs
+ // to be fixed.
+ BOOST_ASSERT(true);
+}
+
+
+string EmbeddedFile::availableFile(Buffer const * buf) const
+{
+ if (embedded())
+ return embeddedFile(buf);
+ else
+ return absFilename();
+}
+
+
+void EmbeddedFile::invalidate()
+{
+ // Clear inset_list_ because they will be registered again.
+ inset_list_.clear();
+ valid_ = false;
+}
+
+
+bool EmbeddedFile::extract(Buffer const * buf) const
+{
+
+ string ext_file = absFilename();
+ string emb_file = embeddedFile(buf);
+
+ if (!fs::exists(emb_file))
+ return false;
+
+ // if external file already exists ...
+ if (fs::exists(ext_file)) {
+ // no need to copy if the files are the same
+ if (sum(*this) == sum(FileName(emb_file)))
+ return true;
+ // otherwise, ask if overwrite
+ int ret = Alert::prompt(
+ _("Overwrite external file?"),
+ bformat(_("External file %1$s already exists, do you want to overwrite it"),
+ from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
+ if (ret != 0)
+ // if the user does not want to overwrite, we still consider it
+ // a successful operation.
+ return true;
+ }
+ // copy file
+ try {
+ // need to make directory?
+ string path = onlyPath(ext_file);
+ if (!fs::is_directory(path))
+ makedir(const_cast<char*>(path.c_str()), 0755);
+ fs::copy_file(emb_file, ext_file, false);
+ return true;
+ } catch (fs::filesystem_error const & fe) {
+ Alert::error(_("Copy file failure"),
+ bformat(_("Cannot copy file %1$s to %2$s.\n"
+ "Please check whether the directory exists and is writeable."),
+ from_utf8(emb_file), from_utf8(ext_file)));
+ LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
+ }
+ return false;
+}
+
+
+bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
+{
+ string ext_file = absFilename();
+ string emb_file = embeddedFile(buf);
+
+ if (!fs::exists(ext_file))
+ return false;
+
+ // if embedded file already exists ...
+ if (fs::exists(emb_file)) {
+ // no need to copy if the files are the same
+ if (sum(*this) == sum(FileName(emb_file)))
+ return true;
+ // other wise, ask if overwrite
+ int const ret = Alert::prompt(
+ _("Update embedded file?"),
+ bformat(_("Embeddedl file %1$s already exists, do you want to overwrite it"),
+ from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
+ if (ret != 0)
+ // if the user does not want to overwrite, we still consider it
+ // a successful operation.
+ return true;
+ }
+ // copy file
+ try {
+ // need to make directory?
+ string path = onlyPath(emb_file);
+ if (!fs::is_directory(path))
+ makedir(const_cast<char*>(path.c_str()), 0755);
+ fs::copy_file(ext_file, emb_file, false);
+ return true;
+ } catch (fs::filesystem_error const & fe) {
+ Alert::error(_("Copy file failure"),
+ bformat(_("Cannot copy file %1$s to %2$s.\n"
+ "Please check whether the directory exists and is writeable."),
+ from_utf8(ext_file), from_utf8(emb_file)));
+ LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
+ }
+ return false;