]> git.lyx.org Git - lyx.git/blobdiff - src/EmbeddedFiles.h
adjust
[lyx.git] / src / EmbeddedFiles.h
index 7fdabbe1bc42b578db6a464a64f7da17bd483bd3..1d526973d3cbafd6abc1179aea28e4fc71fa48e6 100644 (file)
@@ -33,20 +33,10 @@ Expected features:
 1. With embedding enabled (disabled by default), .lyx file can embed graphics,
 listings, bib file etc.
 
-2. Embedding of certain files are automatic (graphics, bib etc), and
-other files can be embedded manually.
-
-3. Embedded file.lyx file is a zip file, with file.lyx, manifest.txt
+2. Embedded file.lyx file is a zip file, with file.lyx, manifest.txt
 and embedded files. 
 
-4. Embedded files can be "EMBEDDED", "EXTERNAL", or "AUTO". In the
-"AUTO" mode, external files will be used if available; otherwise the
-embedded version will be used. In this way, users can work as usual by
-modifying external listings, graphics, and do not have to worry about
-embedding. "EMBEDDED" and "EXTERNAL" modes ignore or use external files
-respectively.
-
-5. An embedding dialog is provided to change embedding status (buffer
+3. An embedding dialog is provided to change embedding status (buffer
 level or individual embedded files), manually embed, extract, view
 or edit files.
 
@@ -59,9 +49,11 @@ of .lyx file and better use of version control systems.
 b. The embedded way. Figures etc are inserted to .lyx file and will
 be embedded. These embedded files can be viewed or edited through
 the embedding dialog. This file can be shared with others more
-easily. The advantage of lyx' embedding approach is that external files
-will be automatically used and embedded if the file is in "AUTO" mode.
+easily. 
 
+Format a anb b can be converted easily, by enable/disable embedding. Diabling
+embedding is also called unpacking because all embedded files will be copied
+to their original locations.
 
 Implementation:
 ======================
@@ -73,12 +65,11 @@ This class keeps a manifest that has
   b. inzip filename. It is the relative path name if the embedded file is
     in or under the document directory, or file name otherwise. Name aliasing
     is used if two external files share the same name.
-  c. embedding mode.
+  c. embedding status.
 It also provides functions to
   a. manipulate manifest
   b. scan a buffer for embeddable files
-  c. look up inzipname from external filename
-  d. look up external filename from inzipname
+  c. determine which file to use according to embedding status
 
 2. When a file is saved, it is scanned for embedded files. (c.f.
 EmbeddedFiles::update(), Inset::registerEmbeddedFiles()).
@@ -86,7 +77,7 @@ EmbeddedFiles::update(), Inset::registerEmbeddedFiles()).
 3. When a lyx file file.lyx is saved, it is save to tmppath() first.
 Embedded files are compressed along with file.lyx and a manifest.txt. 
 If embedding is disabled, file.lyx is saved in the usual pure-text form.
-(c.f. Buffer::writeFile(), EmbeddedFiles::write())
+(c.f. Buffer::writeFile(), EmbeddedFiles::writeFile())
 
 4. When a lyx file.lyx file is opened, if it is a zip file, it is
 decompressed to tmppath(). If manifest.txt and file.lyx exists in
@@ -99,18 +90,11 @@ a embedding dialog. It handles a EmbddedFiles point directly.
 From this dialog, a user can disable embedding, change embedding status,
 or embed other files, extract, view, edit files.
 
-6. When a lyx file is loaded, Embedded files can have
-  a. both external and internal copy
-  b. only external copy (filename())
-  c. only embedded copy (temppath()/inzipname)
-And each can have "AUTO", "EXTERNAL", "EMBEDDED" status. Proper
-handling of each case is required.
-
-7. If embedding of a .lyx file with embedded files is disabled, all its
+6. If embedding of a .lyx file with embedded files is disabled, all its
 embedded files are copied to their respective external filenames. This
 is why external filename will exist even if a file is at "EMBEDDED" status.
 
-8. Individual embeddable insets should find ways to handle embedded files.
+7. Individual embeddable insets should find ways to handle embedded files.
 InsetGraphics replace params().filename with its temppath()/inzipname version
 when the inset is created. The filename appears as /tmp/..../inzipname
 when lyx runs. When params().filename is saved, lyx checks if this is an
@@ -123,42 +107,42 @@ embedded file (check path == temppath()), if so, save filename() instead.
 namespace lyx {
 
 class Buffer;
+class Lexer;
+class ErrorList;
 
 class EmbeddedFile : public support::DocFileName
 {
 public:
-       /**
-               Embedding status of this DocFileName.
-        */
-       enum STATUS {
-               // uninitialized/invalid status
-               NONE,
-               // If the external version of the file is available, it will be used
-               // to generate output, and be embedded to the saved lyx file.
-               // Otherwise, embedded version will be used.
-               AUTO,
-               // Always use embedded version.
-               EMBEDDED,
-               // Do not embed this file, always use external version.
-               EXTERNAL
-       };
+       EmbeddedFile() {};
 
        EmbeddedFile(std::string const & file, std::string const & inzip_name,
-               STATUS status, ParConstIterator const & pit);
+               bool embedded, Inset const * inset);
 
        /// filename in the zip file, usually the relative path
        std::string inzipName() const { return inzip_name_; }
+       void setInzipName(std::string name) { inzip_name_ = name; }
        /// embedded file, equals to temppath()/inzipName()
        std::string embeddedFile(Buffer const * buf) const;
-
-       /// paragraph id
-       void setParIter(ParConstIterator const & pit);
-       int const parID() const { return par_it_->id(); }
+       /// embeddedFile() or absFilename() depending on embedding status
+       std::string availableFile(Buffer const * buf) const;
+
+       /// add an inset that refers to this file
+       void addInset(Inset const * inset);
+       Inset const * inset(int idx) const;
+       /// save the location of this inset as bookmark so that
+       /// it can be located using LFUN_BOOKMARK_GOTO
+       void saveBookmark(Buffer const * buf, int idx) const;
+       /// Number of Insets this file item is referred
+       /// If refCount() == 0, this file must be manually inserted.
+       /// This fact is used by the update() function to skip updating
+       /// such items.
+       int refCount() const { return inset_list_.size(); }
 
        /// embedding status of this file
-       bool embedded() const { return status_ != EXTERNAL; }
-       STATUS status() const { return status_; }
-       void setStatus(STATUS status) { status_ = status; }
+       bool embedded() const { return embedded_; }
+       /// set embedding status. updateFromExternal() should be called before this
+       /// to copy or sync the embedded file with external one.
+       void setEmbed(bool embed) { embedded_ = embed; }
 
        // A flag indicating whether or not this filename is valid.
        // When lyx runs, InsetGraphics etc may be added or removed so filename
@@ -168,19 +152,29 @@ public:
        // status setting untouched.
        bool valid() const { return valid_; }
        void validate() { valid_ = true; }
-       void invalidate() {     valid_ = false; }
+       void invalidate();
+
+       /// extract file, does not change embedding status
+       bool extract(Buffer const * buf) const;
+       /// update embedded file from external file, does not change embedding status
+       bool updateFromExternalFile(Buffer const * buf) const;
+       ///
+       /// After the embedding status is changed, update all insets related
+       /// to this file item.
+       /// Because inset pointers may not be up to date, EmbeddedFiles::update()
+       /// would better be called before this function is called.
+       void updateInsets(Buffer const * buf) const;
 
 private:
        /// filename in zip file
        std::string inzip_name_;
        /// the status of this docfile
-       STATUS status_;
+       bool embedded_;
        ///
        bool valid_;
-       /// Current position of the item, used to locate the files
-       /// A figure may be referred by several items. In this case
-       /// only the last location is recorded.
-       ParConstIterator par_it_;
+       /// Current position of the item, used to locate the files. Because one
+       /// file item can be referred by several Insets, a vector is used.
+       std::vector<Inset const *> inset_list_;
 };
 
 
@@ -189,51 +183,63 @@ public:
        typedef std::vector<EmbeddedFile> EmbeddedFileList;
 public:
        ///
-       EmbeddedFiles(Buffer * buffer = NULL): file_list_(), buffer_(buffer) {}
+       EmbeddedFiles(Buffer * buffer = 0) : file_list_(), buffer_(buffer) {}
        ///
        ~EmbeddedFiles() {}
 
        /// return buffer params embedded flag
        bool enabled() const;
-       /// set buffer params embedded flag
+       /// set buffer params embedded flag. Files will be updated or extracted
+       /// if such an operation fails, enable will fail.
        void enable(bool flag);
 
-       /// add a file item
-       void registerFile(std::string const & filename,
-               EmbeddedFile::STATUS status = EmbeddedFile::AUTO,
-               ParConstIterator const & pit = ParConstIterator());
+       /// add a file item. 
+       /* \param filename filename to add
+        * \param embed embedding status. For a new file item, this is always true.
+        *    If the file already exists, this parameter is ignored.
+        * \param inset Inset pointer
+        * \param inzipName suggested inzipname
+        */
+       EmbeddedFile & registerFile(std::string const & filename, bool embed = false,
+               Inset const * inset = 0,
+               std::string const & inzipName = std::string());
 
        /// scan the buffer and get a list of EmbeddedFile
        void update();
 
        /// write a zip file
-       bool write(support::DocFileName const & filename);
+       bool writeFile(support::DocFileName const & filename);
 
        void clear() { file_list_.clear(); }
 
+       ///
+       EmbeddedFile & operator[](size_t idx) { return *(file_list_.begin() + idx); }
+       EmbeddedFile const & operator[](size_t idx) const { return *(file_list_.begin() + idx); }
        ///
        EmbeddedFileList::iterator begin() { return file_list_.begin(); }
        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;
+       /// extract all file items, used when disable embedding
+       bool extract() const;
+       /// update all files from external, used when enable embedding
+       bool updateFromExternalFile() const;
        ///
-       friend std::istream & operator>> (std::istream & is, EmbeddedFiles &);
-
-       friend std::ostream & operator<< (std::ostream & os, EmbeddedFiles const &);
+       bool readManifest(Lexer & lex, ErrorList & errorList);
+       void writeManifest(std::ostream & os) const;
+       /// update all insets to use embedded files when embedding status is changed
+       void updateInsets() const;
 private:
-       /// if a inzip name already exists
-       bool validInzipName(std::string const & name);
+       /// get a unique inzip name, a suggestion can be given.
+       std::string const getInzipName(std::string const & name, std::string const & inzipName);
        /// list of embedded files
        EmbeddedFileList file_list_;
        ///
        Buffer * buffer_;
 };
 
+} // namespace lyx
 
-std::istream & operator>> (std::istream & is, EmbeddedFiles &);
-
-std::ostream & operator<< (std::ostream & os, EmbeddedFiles const &);
-
-}
 #endif