The attached patch

1. add Inset::updateEmbeddedFile, that will be called after the
embedding status of a file is changed. A typical use is

+void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
+       EmbeddedFile const & file)
+{
+       params_.filename.set(file.availableFile(&buf), buf.filePath());
+}

The problem here is that even if the underlying filename is changed,
the Graphic dialog is not updated. Does anyone know how to do this?

2. add EmbeddedFile::updateInsets(Buffer&) and
EmbeddedFiles::updateInsets() to update related insets.

+void EmbeddedFile::updateInsets(Buffer const * buf) const
+{
+       vector<Inset const *>::const_iterator it = inset_list_.begin();
+       vector<Inset const *>::const_iterator it_end = inset_list_.end();
+       for (; it != it_end; ++it)
+               const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
+}
+

3. call updateInsets at appropriate times. Such as when embedding is
enabled, and when a file embedding status is changed. Because stored
inset pointers are not guranteed to be up to date,
EmbeddedFiles::update() is called before such calls.

This adds another virtual function to Inset, so I need at least one OK. Jose?

Bo
Index: src/insets/InsetGraphics.h
===================================================================
--- src/insets/InsetGraphics.h	(revision 20211)
+++ src/insets/InsetGraphics.h	(working copy)
@@ -80,6 +80,8 @@
 	bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const;
 	/// all graphics can be embedded
 	void registerEmbeddedFiles(Buffer const &, EmbeddedFiles &) const;
+	///
+	void updateEmbeddedFile(Buffer const &, EmbeddedFile const &);
 protected:
 	InsetGraphics(InsetGraphics const &);
 	///
Index: src/insets/InsetGraphics.cpp
===================================================================
--- src/insets/InsetGraphics.cpp	(revision 20211)
+++ src/insets/InsetGraphics.cpp	(working copy)
@@ -239,6 +239,20 @@
 }
 
 
+void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
+	EmbeddedFile const & file)
+{
+	BOOST_ASSERT(buf.embeddedFiles().enabled());
+	LYXERR(Debug::FILES) << "Update InsetGraphics file from " 
+		<< params_.filename.toFilesystemEncoding() << std::endl;
+	params_.filename.set(file.availableFile(&buf), buf.filePath());
+	LYXERR(Debug::FILES) << " to " 
+		<< params_.filename.toFilesystemEncoding() << std::endl;
+	// FIXME: graphics dialog is not updated even if the underlying
+	// filename is updated. What should I do?
+}
+
+
 void InsetGraphics::edit(Cursor & cur, bool)
 {
 	InsetGraphicsMailer(*this).showDialog(&cur.bv());
Index: src/insets/Inset.h
===================================================================
--- src/insets/Inset.h	(revision 20211)
+++ src/insets/Inset.h	(working copy)
@@ -49,6 +49,7 @@
 class ParIterator;
 class Text;
 class TocList;
+class EmbeddedFile;
 class EmbeddedFiles;
 
 
@@ -441,6 +442,8 @@
 	virtual void addToToc(TocList &, Buffer const &, ParConstIterator const &) const {}
 	/// report files that can be embedded with the lyx file
 	virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFiles &) const {};
+	/// use embedded or external file after the embedding status of a file is changed
+	virtual void updateEmbeddedFile(Buffer const &, EmbeddedFile const &) {}
 	/// Fill keys with BibTeX information
 	virtual void fillWithBibKeys(Buffer const &,
 		BiblioInfo &, InsetIterator const &) const { return; }
Index: src/EmbeddedFiles.cpp
===================================================================
--- src/EmbeddedFiles.cpp	(revision 20212)
+++ src/EmbeddedFiles.cpp	(working copy)
@@ -225,6 +225,15 @@
 }
 
 
+void EmbeddedFile::updateInsets(Buffer const * buf) const
+{
+	vector<Inset const *>::const_iterator it = inset_list_.begin();
+	vector<Inset const *>::const_iterator it_end = inset_list_.end();
+	for (; it != it_end; ++it)
+		const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
+}
+
+
 bool EmbeddedFiles::enabled() const
 {
 	return buffer_->params().embedded;
@@ -241,6 +250,8 @@
 		// if operation is successful
 		buffer_->markDirty();
 		buffer_->params().embedded = flag;
+		if (flag)
+			updateInsets();
 	}
 }
 
@@ -468,4 +479,14 @@
 }
 
 
+void EmbeddedFiles::updateInsets() const
+{
+	EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
+	EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
+	for (; it != it_end; ++it)
+		if (it->valid() && it->refCount() > 0)
+			it->updateInsets(buffer_);
 }
+
+
+}
Index: src/frontends/controllers/ControlEmbeddedFiles.cpp
===================================================================
--- src/frontends/controllers/ControlEmbeddedFiles.cpp	(revision 20213)
+++ src/frontends/controllers/ControlEmbeddedFiles.cpp	(working copy)
@@ -89,6 +89,7 @@
 			item.updateFromExternalFile(&buffer());
 		else
 			item.extract(&buffer());
+		item.updateInsets(&buffer());
 	}
 }
 
Index: src/EmbeddedFiles.h
===================================================================
--- src/EmbeddedFiles.h	(revision 20212)
+++ src/EmbeddedFiles.h	(working copy)
@@ -155,6 +155,12 @@
 	bool extract(Buffer const * buf) const;
 	/// update embedded file from external file, does not change embedding status
 	bool updateFromExternalFile(Buffer const * buf) const;
+	///
+	/// After the embedding status is changed, update all insets related
+	/// to this file item.
+	/// Because inset pointers may not be up to date, EmbeddedFiles::update()
+	/// would better be called before this function is called.
+	void updateInsets(Buffer const * buf) const;
 
 private:
 	/// filename in zip file
@@ -220,6 +226,8 @@
 	///
 	bool readManifest(Lexer & lex, ErrorList & errorList);
 	void writeManifest(std::ostream & os) const;
+	///
+	void updateInsets() const;
 private:
 	/// get a unique inzip name, a suggestion can be given.
 	std::string const getInzipName(std::string const & name, std::string const & inzipName);

Reply via email to