From ed2b1631db33ba8265c2df141d77f207dca6c967 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Thu, 3 Jan 2008 16:41:06 +0000 Subject: [PATCH] Showing a message box after embedding status is changed. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22349 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 10 ++++++-- src/EmbeddedFiles.cpp | 60 ++++++++++++++++++++++++++++++++----------- src/EmbeddedFiles.h | 2 +- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 7afe959d58..19e16da5f0 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -68,6 +68,7 @@ #include "support/convert.h" #include "support/debug.h" +#include "support/ExceptionMessage.h" #include "support/FileFilterList.h" #include "support/filetools.h" #include "support/gettext.h" @@ -1168,10 +1169,15 @@ bool BufferView::dispatch(FuncRequest const & cmd) buffer_.params().compressed = !buffer_.params().compressed; break; - case LFUN_BUFFER_TOGGLE_EMBEDDING: + case LFUN_BUFFER_TOGGLE_EMBEDDING: { // turn embedding on/off - buffer_.embeddedFiles().enable(!buffer_.params().embedded); + try { + buffer_.embeddedFiles().enable(!buffer_.params().embedded); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + } break; + } case LFUN_NEXT_INSET_TOGGLE: { // this is the real function we want to invoke diff --git a/src/EmbeddedFiles.cpp b/src/EmbeddedFiles.cpp index 8927dffcad..3b48bcc4bd 100644 --- a/src/EmbeddedFiles.cpp +++ b/src/EmbeddedFiles.cpp @@ -31,6 +31,7 @@ #include "support/gettext.h" #include "support/convert.h" #include "support/lstrings.h" +#include "support/ExceptionMessage.h" #include "support/FileZipListDir.h" #include @@ -134,7 +135,7 @@ bool EmbeddedFile::extract(Buffer const * buf) const // need to make directory? FileName path = ext.onlyPath(); if (!path.createPath()) { - Alert::error(_("Copy file failure"), + throw ExceptionMessage(ErrorException, _("Copy file failure"), bformat(_("Cannot create file path '%1$s'.\n" "Please check whether the path is writeable."), from_utf8(path.absFilename()))); @@ -144,7 +145,7 @@ bool EmbeddedFile::extract(Buffer const * buf) const if (emb.copyTo(ext)) return true; - Alert::error(_("Copy file failure"), + throw ExceptionMessage(ErrorException, _("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))); @@ -185,8 +186,9 @@ bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const path.createPath(); if (ext.copyTo(emb)) return true; - Alert::error(_("Copy file failure"), - bformat(_("Cannot copy file %1$s to %2$s.\n" + throw ExceptionMessage(ErrorException, + _("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()); @@ -212,11 +214,16 @@ bool EmbeddedFiles::enabled() const void EmbeddedFiles::enable(bool flag) { if (enabled() != flag) { - // if enable, copy all files to temppath() - // if disable, extract all files - if ((flag && !updateFromExternalFile()) || (!flag && !extract())) - return; - // if operation is successful + // update embedded file list + update(); + // An exception may be thrown. + if (flag) + // if enable, copy all files to temppath() + updateFromExternalFile(); + else + // if disable, extract all files + extractAll(); + // if operation is successful (no exception is thrown) buffer_->markDirty(); buffer_->params().embedded = flag; if (flag) @@ -309,14 +316,25 @@ EmbeddedFiles::find(string filename) const } -bool EmbeddedFiles::extract() const +bool EmbeddedFiles::extractAll() const { EmbeddedFileList::const_iterator it = file_list_.begin(); EmbeddedFileList::const_iterator it_end = file_list_.end(); + int count_extracted = 0; + int count_external = 0; for (; it != it_end; ++it) - if (it->embedded()) - if(!it->extract(buffer_)) - return false; + if (it->embedded()) { + if(!it->extract(buffer_)) { + throw ExceptionMessage(ErrorException, + _("Failed to extract file"), + bformat(_("Error: can not extract file %1$s.\n"), it->displayName())); + } else + count_extracted += 1; + } else + count_external += 1; + docstring const msg = bformat(_("%1$d external files are ignored.\n" + "%2$d embedded files are extracted.\n"), count_external, count_extracted); + Alert::information(_("Unpacking all files"), msg); return true; } @@ -325,10 +343,22 @@ bool EmbeddedFiles::updateFromExternalFile() const { EmbeddedFileList::const_iterator it = file_list_.begin(); EmbeddedFileList::const_iterator it_end = file_list_.end(); + int count_embedded = 0; + int count_external = 0; for (; it != it_end; ++it) - if (it->embedded()) - if (!it->updateFromExternalFile(buffer_)) + if (it->embedded()) { + if (!it->updateFromExternalFile(buffer_)) { + throw ExceptionMessage(ErrorException, + _("Failed to embed file"), + bformat(_("Error: can not embed file %1$s.\n"), it->displayName())); return false; + } else + count_external += 1; + } else + count_external += 1; + docstring const msg = bformat(_("%1$d external files are ignored.\n" + "%2$d embeddable files are embedded.\n"), count_external, count_embedded); + Alert::information(_("Packing all files"), msg); return true; } diff --git a/src/EmbeddedFiles.h b/src/EmbeddedFiles.h index e7d80f48a8..f775829e42 100644 --- a/src/EmbeddedFiles.h +++ b/src/EmbeddedFiles.h @@ -193,7 +193,7 @@ public: // try to locate filename, using either absFilename() or embeddedFile() EmbeddedFileList::const_iterator find(std::string filename) const; /// extract all file items, used when disable embedding - bool extract() const; + bool extractAll() const; /// update all files from external, used when enable embedding bool updateFromExternalFile() const; /// -- 2.39.5