]> git.lyx.org Git - lyx.git/commitdiff
Embedding feature patch 5: handling of embedded files (operation without external...
authorBo Peng <bpeng@lyx.org>
Wed, 5 Sep 2007 17:50:07 +0000 (17:50 +0000)
committerBo Peng <bpeng@lyx.org>
Wed, 5 Sep 2007 17:50:07 +0000 (17:50 +0000)
*  src/insets/InsetGraphicsParams.h|cpp:
*  src/insets/InsetExternal.cpp: read/write buffer with embedded file
*  src/insets/InsetGraphics.cpp: ditto
*  src/EmbeddedFiles.h|cpp: add find() function

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20075 a592a061-630c-0410-9148-cb99ea01b6c8

src/EmbeddedFiles.cpp
src/EmbeddedFiles.h
src/insets/InsetExternal.cpp
src/insets/InsetGraphics.cpp
src/insets/InsetGraphicsParams.cpp
src/insets/InsetGraphicsParams.h

index 180b88ed921db8766f6aa646ca0d7b6ff8a26272..9f25de6f727a9bfa66053f40b08daecba1fe7f1d 100644 (file)
@@ -275,6 +275,17 @@ bool EmbeddedFiles::write(DocFileName const & filename)
 }
 
 
+EmbeddedFiles::EmbeddedFileList::const_iterator EmbeddedFiles::find(std::string filename) const
+{
+       EmbeddedFileList::const_iterator it = file_list_.begin();
+       EmbeddedFileList::const_iterator it_end = file_list_.end();
+       for (; it != it_end; ++it)
+               if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename)     
+                       return it;
+       return file_list_.end();
+}
+
+
 bool EmbeddedFiles::extractAll() const
 {
        EmbeddedFileList::const_iterator it = file_list_.begin();
index 132c84927c6025bb153c136c1b43701824166153..d1aad34ecf8ece15be5d32c85ddd9ffd14dde03a 100644 (file)
@@ -225,6 +225,8 @@ public:
        EmbeddedFileList::iterator end() { return file_list_.end(); }
        EmbeddedFileList::const_iterator begin() const { return file_list_.begin(); }
        EmbeddedFileList::const_iterator end() const { return file_list_.end(); }
+       // try to locate filename, using either absFilename() or embeddedFile()
+       EmbeddedFileList::const_iterator find(std::string filename) const;
        ///
        bool extractAll() const;
        ///
index 31115a4a85b1d4b1e675ea66ea2f3996a8a5c5a6..29e7e70c12ad43f76fe9cd405d668d300cc96f2e 100644 (file)
@@ -50,6 +50,7 @@ using std::ostream;
 using std::ostringstream;
 using std::vector;
 
+using lyx::support::DocFileName;
 
 namespace {
 
@@ -186,10 +187,15 @@ void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
        os << "External\n"
           << "\ttemplate " << templatename() << '\n';
 
-       if (!filename.empty())
-               os << "\tfilename "
-                  << filename.outputFilename(buffer.filePath())
-                  << '\n';
+       if (!filename.empty()) {
+               // when we save, we still use the original filename
+               EmbeddedFiles::EmbeddedFileList::const_iterator it = 
+                       buffer.embeddedFiles().find(filename.toFilesystemEncoding());
+               if (it != buffer.embeddedFiles().end())
+                       os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n';
+               else
+                       os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
+       }
 
        if (display != defaultDisplayType)
                os << "\tdisplay "
@@ -297,6 +303,12 @@ bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
                        lex.eatLine();
                        string const name = lex.getString();
                        filename.set(name, buffer.filePath());
+                       // maybe this file is embedded
+                       EmbeddedFiles::EmbeddedFileList::const_iterator it = buffer.embeddedFiles().find(filename.toFilesystemEncoding());
+                       if (it != buffer.embeddedFiles().end())
+                               // using available file, embedded or external, depending on file availability and
+                               // embedding status.
+                               filename = DocFileName(it->availableFile(&buffer));
                        break;
                }
 
index 247268fada8a18f50d9671242b13d739e27bdda9..f7aac86629cc7d58f08b0ee580d9e3d6b04b0edf 100644 (file)
@@ -270,7 +270,7 @@ Inset::EDITABLE InsetGraphics::editable() const
 void InsetGraphics::write(Buffer const & buf, ostream & os) const
 {
        os << "Graphics\n";
-       params().Write(os, buf.filePath());
+       params().Write(os, buf);
 }
 
 
@@ -283,6 +283,13 @@ void InsetGraphics::read(Buffer const & buf, Lexer & lex)
        else
                LYXERR(Debug::GRAPHICS) << "Not a Graphics inset!" << endl;
 
+       // InsetGraphics is read, with filename in params_. We do not know if this file actually
+       // exists or is embedded so we need to get the 'availableFile' from buf.embeddedFiles()
+       EmbeddedFiles::EmbeddedFileList::const_iterator it = buf.embeddedFiles().find(params_.filename.toFilesystemEncoding());
+       if (it != buf.embeddedFiles().end())
+               // using available file, embedded or external, depending on file availability and
+               // embedding status.
+               params_.filename = DocFileName(it->availableFile(&buf));
        graphic_->update(params().as_grfxParams());
 }
 
@@ -1016,7 +1023,7 @@ InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
 {
        ostringstream data;
        data << name_ << ' ';
-       params.Write(data, buffer.filePath());
+       params.Write(data, buffer);
        data << "\\end_inset\n";
        return data.str();
 }
index b80e10fbf0d89d46fce21175e1dab095de4221ea..6b1bc4cfd4d1e779548b688d6787aaf3efa90649 100644 (file)
@@ -17,6 +17,8 @@
 #include "LyX.h" // for use_gui
 #include "Lexer.h"
 #include "LyXRC.h"
+#include "Buffer.h"
+#include "EmbeddedFiles.h"
 
 #include "graphics/GraphicsParams.h"
 
@@ -32,6 +34,7 @@ namespace lyx {
 using support::float_equal;
 using support::readBB_from_PSFile;
 using support::token;
+using support::DocFileName;
 
 using std::string;
 using std::ostream;
@@ -148,12 +151,18 @@ bool operator!=(InsetGraphicsParams const & left,
 }
 
 
-void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
+void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
 {
        // Do not write the default values
 
        if (!filename.empty()) {
-               os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
+               // when we save, we still use the original filename
+               EmbeddedFiles::EmbeddedFileList::const_iterator it = 
+                       buffer.embeddedFiles().find(filename.toFilesystemEncoding());
+               if (it != buffer.embeddedFiles().end())
+                       os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n';
+               else
+                       os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
        }
        if (lyxscale != 100)
                os << "\tlyxscale " << lyxscale << '\n';
index b6cea1fd9bae7f05c02bd6024a13c595c2f1ca82..172215dd2263cc3293f35c52e00607b78ea5a0dc 100644 (file)
@@ -23,6 +23,7 @@ namespace lyx {
 namespace graphics { class Params; }
 
 class Lexer;
+class Buffer;
 
 
 /// This class holds all the parameters needed by insetGraphics.
@@ -73,7 +74,8 @@ public:
        ///
        InsetGraphicsParams & operator=(InsetGraphicsParams const &);
        /// Save the parameters in the LyX format stream.
-       void Write(std::ostream & os, std::string const & bufpath) const;
+       /// Buffer is needed to figure out if a figure is embedded.
+       void Write(std::ostream & os, Buffer const & buf) const;
        /// If the token belongs to our parameters, read it.
        bool Read(Lexer & lex, std::string const & token, std::string const & bufpath);
        /// convert