From 42a2a0f289e951cc7fd4f6ef25a286f3ef400497 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Tue, 9 Mar 2010 03:25:47 +0000 Subject: [PATCH] Cheap fix for #6554. When we save a Buffer to a new location, we check the included insets and make sure that the files they reference are still where they are supposed to be. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@33686 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 37 +++++++++++++++++++++++++++++++++++ src/Buffer.h | 2 ++ src/frontends/qt4/GuiView.cpp | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 8f3355b6b7..9a3d86af66 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -3920,4 +3920,41 @@ bool Buffer::reload() } +// FIXME We could do better here, but it is complicated. What would be +// nice is to offer either (a) to save the child buffer to an appropriate +// location, so that it would "move with the master", or else (b) to update +// the InsetInclude so that it pointed to the same file. But (a) is a bit +// complicated, because the code for this lives in GuiView. +void Buffer::checkChildBuffers() +{ + Impl::BufferPositionMap::iterator it = d->children_positions.begin(); + Impl::BufferPositionMap::iterator const en = d->children_positions.end(); + for (; it != en; ++it) { + DocIterator dit = it->second; + Buffer * cbuf = const_cast(it->first); + if (!cbuf || !theBufferList().isLoaded(cbuf)) + continue; + Inset * inset = dit.nextInset(); + LASSERT(inset && inset->lyxCode() == INCLUDE_CODE, continue); + InsetInclude * inset_inc = static_cast(inset); + docstring const & incfile = inset_inc->getParam("filename"); + string oldloc = cbuf->absFileName(); + string newloc = makeAbsPath(to_utf8(incfile), + onlyPath(absFileName())).absFilename(); + if (oldloc == newloc) + continue; + // the location of the child file is incorrect. + Alert::warning(_("Included File Invalid"), + bformat(_("Saving this document to a new location has made the file:\n" + " %1$s\n" + "inaccessible. You will need to update the included filename."), + from_utf8(oldloc))); + cbuf->setParent(0); + inset_inc->setChildBuffer(0); + } + // invalidate cache of children + d->children_positions.clear(); + d->position_to_children.clear(); +} + } // namespace lyx diff --git a/src/Buffer.h b/src/Buffer.h index 4e372c296d..7f19fd1f58 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -561,6 +561,8 @@ public: /// \return progress if a new word was found. int spellCheck(DocIterator & from, DocIterator & to, WordLangTuple & word_lang, docstring_list & suggestions) const; + /// + void checkChildBuffers(); private: /// diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index b8b8d2cd99..a2a5eaf746 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -2083,6 +2083,7 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname) fname.changeExtension(".lyx"); } + // fname is now the new Buffer location. if (FileName(fname).exists()) { docstring const file = makeDisplayPath(fname.absFilename(), 30); docstring text = bformat(_("The document %1$s already " @@ -2119,6 +2120,11 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname) return false; } + // the file has now been saved to the new location. + // we need to check that the locations of child buffers + // are still valid. + b.checkChildBuffers(); + return true; }