]> git.lyx.org Git - features.git/commitdiff
Showing a message box after embedding status is changed.
authorBo Peng <bpeng@lyx.org>
Thu, 3 Jan 2008 16:41:06 +0000 (16:41 +0000)
committerBo Peng <bpeng@lyx.org>
Thu, 3 Jan 2008 16:41:06 +0000 (16:41 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22349 a592a061-630c-0410-9148-cb99ea01b6c8

src/BufferView.cpp
src/EmbeddedFiles.cpp
src/EmbeddedFiles.h

index 7afe959d586557692b90f6d9e9b566d758bd633d..19e16da5f012c65d101361d3829bae3c0294a4e0 100644 (file)
@@ -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
index 8927dffcad9c77a819deb06424f88bc027ca3eba..3b48bcc4bdce07207987dda3e3a74a3dae2c9c80 100644 (file)
@@ -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 <sstream>
@@ -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;
 }
 
index e7d80f48a848379b7254221aa7bbf0b0d677802e..f775829e42e9a7cf962868a646fa57143387d716 100644 (file)
@@ -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;
        ///