From ccb09162e5cee68557088f631ce8597132ba23eb Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Sun, 16 Mar 2008 05:10:41 +0000 Subject: [PATCH] Embedding: paste an inset with an embedded file may fail, if the source buffer has been closed and the embedded file has been removed. This patch fixes this. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23771 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetBibtex.cpp | 11 +++++++++-- src/insets/InsetExternal.cpp | 18 ++++++++++++++++-- src/insets/InsetGraphics.cpp | 13 +++++++++++-- src/insets/InsetInclude.cpp | 16 ++++++++++++---- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp index 69e8f7bba2..a74be8e1f0 100644 --- a/src/insets/InsetBibtex.cpp +++ b/src/insets/InsetBibtex.cpp @@ -57,8 +57,15 @@ void InsetBibtex::setBuffer(Buffer & buffer) if (buffer_) { EmbeddedFileList::iterator it = bibfiles_.begin(); EmbeddedFileList::iterator it_end = bibfiles_.end(); - for (; it != it_end; ++it) - *it = it->copyTo(&buffer); + for (; it != it_end; ++it) { + try { + *it = it->copyTo(&buffer); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + it->setEmbed(false); + } + } } Inset::setBuffer(buffer); } diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 63102bc506..79d47ca8a5 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -30,9 +30,12 @@ #include "MetricsInfo.h" #include "OutputParams.h" +#include "frontends/alert.h" + #include "graphics/PreviewLoader.h" #include "support/debug.h" +#include "support/ExceptionMessage.h" #include "support/filetools.h" #include "support/gettext.h" #include "support/lstrings.h" @@ -60,6 +63,8 @@ string defaultTemplateName; namespace lyx { +namespace Alert = frontend::Alert; + namespace external { TempName::TempName() @@ -421,8 +426,17 @@ InsetExternal::~InsetExternal() void InsetExternal::setBuffer(Buffer & buffer) { - if (buffer_) - params_.filename = params_.filename.copyTo(&buffer); + if (buffer_) { + try { + // a file may not be copied successfully when, e.g. buffer_ + // has already been closed. + params_.filename = params_.filename.copyTo(&buffer); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + params_.filename.setEmbed(false); + } + } Inset::setBuffer(buffer); } diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp index 3f61db4aaf..0953912cf9 100644 --- a/src/insets/InsetGraphics.cpp +++ b/src/insets/InsetGraphics.cpp @@ -156,8 +156,17 @@ InsetGraphics::~InsetGraphics() void InsetGraphics::setBuffer(Buffer & buffer) { - if (buffer_) - params_.filename = params_.filename.copyTo(&buffer); + if (buffer_) { + try { + // a file may not be copied successfully when, e.g. buffer_ + // has already been closed. + params_.filename = params_.filename.copyTo(&buffer); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + params_.filename.setEmbed(false); + } + } Inset::setBuffer(buffer); } diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 9b8428ae30..c0ce30f8b8 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -47,6 +47,7 @@ #include "support/debug.h" #include "support/docstream.h" +#include "support/ExceptionMessage.h" #include "support/FileNameList.h" #include "support/filetools.h" #include "support/gettext.h" @@ -216,10 +217,17 @@ InsetInclude::~InsetInclude() void InsetInclude::setBuffer(Buffer & buffer) { if (buffer_) { - EmbeddedFile file_from = includedFilename(*buffer_, params()); - EmbeddedFile file_to = file_from.copyTo(&buffer); - if (file_to.embedded()) - setParam("embed", from_utf8(file_to.inzipName())); + try { + EmbeddedFile file_from = includedFilename(*buffer_, params()); + EmbeddedFile file_to = file_from.copyTo(&buffer); + if (file_to.embedded()) + setParam("embed", from_utf8(file_to.inzipName())); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + setParam("embed", docstring()); + } + } buffer_ = &buffer; if (label_)