]> git.lyx.org Git - features.git/commitdiff
Embedding: when an inset with an embedded file is copied to another buffer, the embed...
authorBo Peng <bpeng@lyx.org>
Sat, 15 Mar 2008 04:48:31 +0000 (04:48 +0000)
committerBo Peng <bpeng@lyx.org>
Sat, 15 Mar 2008 04:48:31 +0000 (04:48 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23758 a592a061-630c-0410-9148-cb99ea01b6c8

src/EmbeddedFiles.cpp
src/EmbeddedFiles.h
src/insets/InsetBibtex.cpp
src/insets/InsetBibtex.h
src/insets/InsetExternal.cpp
src/insets/InsetExternal.h
src/insets/InsetGraphics.cpp
src/insets/InsetGraphics.h
src/insets/InsetInclude.cpp

index a11bc8206edb6bf00e586bd6b6569db94e9db0ef..e5d7ab35c1620f6d1771e6f2420172bc647f13dd 100644 (file)
@@ -254,6 +254,64 @@ bool EmbeddedFile::updateFromExternalFile() const
 }
 
 
+EmbeddedFile EmbeddedFile::copyTo(Buffer const * buf)
+{
+       EmbeddedFile file = EmbeddedFile(absFilename(), buf->filePath());
+       file.setEmbed(embedded());
+       file.enable(buf->embedded(), buf, false);
+       
+       // use external file.
+       if (!embedded())
+               return file;
+
+       LYXERR(Debug::FILES, "Copy " << availableFile()
+               << " to " << file.availableFile());
+
+       FileName from_file = availableFile();
+       FileName to_file = file.availableFile();
+
+       if (!from_file.exists()) {
+               // no from file
+               throw ExceptionMessage(ErrorException,
+                       _("Failed to copy embedded file"),
+                       bformat(_("Failed to embed file %1$s.\n"
+                          "Please check whether the source file is available"),
+                               from_utf8(absFilename())));
+               file.setEmbed(false);
+               return file;
+       }
+
+       // if destination file already exists ...
+       if (to_file.exists()) {
+               // no need to copy if the files are the same
+               if (checksum() == to_file.checksum())
+                       return file;
+               // other wise, ask if overwrite
+               int const ret = Alert::prompt(
+                       _("Update embedded file?"),
+                       bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
+                               from_utf8(to_file.absFilename())), 1, 1, _("&Overwrite"), _("&Cancel"));
+               if (ret != 0)
+                       // if the user does not want to overwrite, we still consider it
+                       // a successful operation.
+                       return file;
+       }
+       // copy file
+       // need to make directory?
+       FileName path = to_file.onlyPath();
+       if (!path.isDirectory())
+               path.createPath();
+       if (from_file.copyTo(to_file))
+               return file;
+       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(from_file.absFilename()), from_utf8(to_file.absFilename())));
+       return file;
+}
+
+
 void EmbeddedFile::updateInsets() const
 {
        vector<Inset const *>::const_iterator it = inset_list_.begin();
@@ -414,7 +472,7 @@ void EmbeddedFileList::enable(bool flag, Buffer & buffer, bool updateFile)
        for (it = begin(); it != it_end; ++it)
                it->updateInsets();
 
-       if (!updateFile)
+       if (!updateFile || (count_external == 0 && count_embedded == 0))
                return;
 
        // show result
index a2fa1a7f94db039686b07e716a474ff9991a6f46..5a8df6985393719aabe14ee989497689731b6cd7 100644 (file)
@@ -158,6 +158,8 @@ public:
        bool extract() const;
        /// update embedded file from external file, does not change embedding status
        bool updateFromExternalFile() const;
+       /// copy an embedded file to another buffer
+       EmbeddedFile copyTo(Buffer const * buf);
        ///
        /// After the embedding status is changed, update all insets related
        /// to this file item. For example, a graphic inset may need to monitor
index 2b12c9f03beaec8bad6669734067aa1c0478400d..69e8f7bba24b174a7d84e813741daef357614ace 100644 (file)
@@ -52,6 +52,18 @@ InsetBibtex::InsetBibtex(InsetCommandParams const & p)
 {}
 
 
+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);
+       }
+       Inset::setBuffer(buffer);
+}
+
+
 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
 {
        static ParamInfo param_info_;
index ff511d2479d1ce994ce68fa0131f2d3cb8e19645..efee21ef44158b4d78a74f89572f454f27e5022c 100644 (file)
@@ -27,6 +27,8 @@ public:
        ///
        InsetBibtex(InsetCommandParams const &);
        ///
+       void setBuffer(Buffer & buffer);
+       ///
        docstring screenLabel() const;
        ///
        EDITABLE editable() const { return IS_EDITABLE; }
index c6f1142bc92119e291345833cc9f19cc0a7d4535..63102bc506737faee5e3229e5ffde71a69ba33df 100644 (file)
@@ -419,6 +419,14 @@ InsetExternal::~InsetExternal()
 }
 
 
+void InsetExternal::setBuffer(Buffer & buffer)
+{
+       if (buffer_)
+               params_.filename = params_.filename.copyTo(&buffer);
+       Inset::setBuffer(buffer);
+}
+
+
 void InsetExternal::statusChanged() const
 {
        updateFrontend();
index b5adef3d658d72d862a644292ca7ac40095118e0..97c4b7a44088c42bfb0d30c5df3b65a6e0e7d466 100644 (file)
@@ -112,6 +112,8 @@ public:
        ///
        ~InsetExternal();
        ///
+       void setBuffer(Buffer & buffer);
+       ///
        InsetCode lyxCode() const { return EXTERNAL_CODE; }
        ///
        EDITABLE editable() const { return IS_EDITABLE; }
index 39c5d8ad3af427f7d92e1879ffe2c55dd2740da1..3f61db4aaf894e54057ee5e491930d868c119efc 100644 (file)
@@ -154,6 +154,14 @@ InsetGraphics::~InsetGraphics()
 }
 
 
+void InsetGraphics::setBuffer(Buffer & buffer)
+{
+       if (buffer_)
+               params_.filename = params_.filename.copyTo(&buffer);
+       Inset::setBuffer(buffer);
+}
+
+
 void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
index b1270bb30c321d9b2fe4f4e94703a30968ac9b78..b5c8043c23a38bd2f1ee57bb805be4cb8f37cce7 100644 (file)
@@ -34,6 +34,8 @@ public:
        ///
        ~InsetGraphics();
        ///
+       void setBuffer(Buffer & buffer);
+       ///
        bool isLabeled() const { return true; }
        void metrics(MetricsInfo &, Dimension &) const;
        ///
index 6b5ba0477693300f8964ed636aee4fe0f09854ab..9b8428ae303117841e43caedf130986198203619 100644 (file)
@@ -215,6 +215,12 @@ 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()));
+       }
        buffer_ = &buffer;
        if (label_)
                label_->setBuffer(buffer);