]> git.lyx.org Git - lyx.git/blobdiff - src/EmbeddedFiles.h
Cosmetics.
[lyx.git] / src / EmbeddedFiles.h
index 115a8e5b6c424fdc3756188f7ff6ee93418baebb..46c5c5b725aa6d90ec56d38316ce95730cb718cd 100644 (file)
 
 #include "support/FileName.h"
 
+#include <string>
 #include <vector>
-#include <utility>
-
-#include "ParIterator.h"
-#include "Paragraph.h"
 
 /**
 
-This file, and the embedding dialog implemented in src/frontends, implements
+This file, and the embed checkbox in dialogs of InsetGraphics etc, implements
 an 'Embedded Files' feature of lyx.
 
 
 Expected features:
 =========================
 
-1. With embedding enabled (disabled by default), .lyx file can embed graphics,
-listings, bib file etc.
+1. Bundled .lyx file can embed graphics, listings, bib file etc. The bundle
+format is used when Document->Save in bundled format is selected.
+
+2. Embedded file.lyx file is a zip file, with content.lyx and embedded files. 
+
+3. The embedding status of embedded files are set from individual insets.
 
-2. Embedded file.lyx file is a zip file, with file.lyx, manifest.txt
-and embedded files. 
+4. Extra files such as .cls and .layout can be embedded from Document->
+Settings->Embedded Files->Extra Embedded Files.
 
-3. An embedding dialog is provided to change embedding status (buffer
-level or individual embedded files), manually embed, extract, view
-or edit files.
+5. When Document->Save in bundled format is selected, all embedded files
+become bundled. Changes to the external version of this file does not
+affect the output of the .lyx file.
+
+6. When Document->Save in bundled format is unchecked, all embedded files
+are copied to their original locations.
 
 Overall, this feature allows two ways of editing a .lyx file
 
@@ -49,184 +53,173 @@ 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. 
+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.
+Format a and b can be converted easily, by packing/unpacking a .lyx file.
 
 Implementation:
 ======================
 
 1. An EmbeddedFiles class is implemented to keep the embedded files (
 class EmbeddedFile). (c.f. src/EmbeddedFiles.[h|cpp])
-This class keeps a manifest that has
-  a. external relative filename
-  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 status.
-It also provides functions to
-  a. manipulate manifest
-  b. scan a buffer for embeddable files
-  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()).
 
-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.
+3. When a lyx file file.lyx is saved, it is save to tmppath()/content.lyx
+first. Embedded files are compressed along with content.lyx.
+If embedding is disabled, file.lyx is saved in the usual pure-text format.
 (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
-tmppath(), the manifest is read to buffer, and tmppath()/file.lyx is
-read as usual. If file.lyx is not a zip file, it is read as usual.
+decompressed to tmppath() and tmppath()/content.lyx is read as usual.
 (c.f. bool Buffer::readFile())
 
-5. A menu item Document -> Embedded Files is provided to open
-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.
+5. A menu item Document -> Save in bundled format is provided to pack/unpack
+a .lyx file.
 
 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.
 
-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
-embedded file (check path == temppath()), if so, save filename() instead.
-(c.f. InsetGraphic::read(), InsetGraphics::edit(), InsetGraphicsParams::write())
-
-
 */
 
 namespace lyx {
 
 class Buffer;
+class Inset;
 class Lexer;
 class ErrorList;
 
 class EmbeddedFile : public support::DocFileName
 {
 public:
-       EmbeddedFile(std::string const & file, std::string const & inzip_name,
-               bool embedded, Inset const * inset);
+       ///
+       EmbeddedFile(std::string const & file = std::string(),
+               std::string const & buffer_path = std::string());
+       
+       /// set filename and inzipName.
+       /**
+        * NOTE: inzip_name_ is not unique across operation systems and is not 
+        * guaranteed to be the same across different versions of LyX.
+        * inzip_name_ will be saved to the LyX file, and is used to indicate 
+        * whether or not a file is embedded, and where the embedded file is in
+        * the bundled file. When a file is read, the stored inzip names are used
+        * at first. EmbeddedFiles::validate() will then scan these embedded files
+        * and update their inzip name, moving bundled files around if needed.
+        * This scheme has the advantage that it is safe to change how inzip files
+        * are saved in a bundled file.
+        *
+        * NOTE that this treatment does not welcome an UUID solution because
+        * all embedded files will have to be renamed when an embedded file is
+        * opened. It is of course possible to use saved inzipname, but that is
+        * not easy. For example, when a new EmbeddedFile is created with the same
+        * file as an old one, it needs to be synced to the old inzipname...
+       **/
+       void set(std::string const & filename, std::string const & buffer_path);
+       /** Set the inzip name of an EmbeddedFile, which should be the name
+        *  of an actual embedded file on disk.
+        */
+       void setInzipName(std::string const & name);
 
-       /// filename in the zip file, usually the relative path
+       /// filename in the zip file, which is related to buffer temp directory.
        std::string inzipName() const { return inzip_name_; }
+
        /// embedded file, equals to temppath()/inzipName()
-       std::string embeddedFile(Buffer const * buf) const;
+       std::string embeddedFile() const;
        /// embeddedFile() or absFilename() depending on embedding status
-       std::string availableFile(Buffer const * buf) const;
+       /// and whether or not embedding is enabled.
+       FileName availableFile() const;
+       /// relative file name or inzipName()
+       std::string latexFilename(std::string const & buffer_path) 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(); }
+       /// clear all isnets that associated with this file.
+       void clearInsets() const { inset_list_.clear(); }
 
        /// embedding status of this file
        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
-       // maybe obsolete. In Buffer::updateEmbeddedFiles, the EmbeddedFiles is first
-       // invalidated (c.f. invalidate()), and all insets are asked to register
-       // embedded files. In this way, EmbeddedFileList will be refreshed, with
-       // status setting untouched.
-       bool valid() const { return valid_; }
-       void validate() { valid_ = true; }
-       void invalidate();
+       /// set embedding status. 
+       void setEmbed(bool embed);
+
+       /// whether or not embedding is enabled for the current file
+       /**
+        * An embedded file needs to know the temp path of a buffer to know
+        * where its embedded copy is. This has to be stored within EmbeddedFile
+        * because this class is often used when Buffer is unavailable. However,
+        * when an embedded file is copied to another buffer, temp_path_ has
+        * to be updated and file copying may be needed.
+        */
+       bool isEnabled() const { return !temp_path_.empty(); }
+       /// enable embedding of this file
+       void enable(bool enabled, Buffer const * buf, bool updateFile);
 
        /// extract file, does not change embedding status
-       bool extract(Buffer const * buf) const;
+       bool extract() const;
        /// update embedded file from external file, does not change embedding status
-       bool updateFromExternalFile(Buffer const * buf) const;
-
+       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
+       /// embedded file instead of external file.
+       void updateInsets() const;
+
+       /// Check readability of availableFile
+       bool isReadableFile() const;
+       /// Calculate checksum of availableFile
+       unsigned long checksum() const;
+
+       // calculate inzip_name_ from filename
+       std::string calcInzipName(std::string const & buffer_path);
+       // move an embedded disk file with an existing inzip_name_ to 
+       // a calculated inzip_name_, if they differ.
+       void syncInzipFile(std::string const & buffer_path);
 private:
        /// filename in zip file
        std::string inzip_name_;
        /// the status of this docfile
        bool embedded_;
-       ///
-       bool valid_;
-       /// Current position of the item, used to locate the files. Because one
+       /// Insets that contains this file item. Because a 
        /// file item can be referred by several Insets, a vector is used.
-       std::vector<Inset const *> inset_list_;
+       mutable std::vector<Inset const *> inset_list_;
+       /// Embedded file needs to know whether enbedding is enabled,
+       /// and where is the lyx temporary directory. Such information can
+       /// be retrived from a buffer, but a buffer is not always available when
+       /// an EmbeddedFile is used.
+       std::string temp_path_;
 };
 
 
-class EmbeddedFiles {
-public:
-       typedef std::vector<EmbeddedFile> EmbeddedFileList;
-public:
-       ///
-       EmbeddedFiles(Buffer * buffer = 0) : file_list_(), buffer_(buffer) {}
-       ///
-       ~EmbeddedFiles() {}
+bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
+bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
 
-       /// return buffer params embedded flag
-       bool enabled() const;
+
+class EmbeddedFileList : public std::vector<EmbeddedFile> {
+public:
        /// 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. 
-       /* \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
-        */
-       void registerFile(std::string const & filename, bool embed = false,
-               Inset const * inset = 0,
-               std::string const & inzipName = std::string());
+       void enable(bool enabled, Buffer & buffer, bool updateFile);
 
+       /// add a file item.
+       /** \param file Embedded file to add
+        *  \param inset Inset pointer
+        */
+       void registerFile(EmbeddedFile const & file, Inset const * inset,
+               Buffer const & buffer);
+       /// returns a pointer to the Embedded file representing this object,
+       /// or null if not found. The filename should be absolute.
+       const_iterator findFile(std::string const & filename) const;
+       iterator findFile(std::string const & filename);
+
+       /// validate embedded fies after a file is read.
+       void validate(Buffer const & buffer);
        /// scan the buffer and get a list of EmbeddedFile
-       void update();
-
+       void update(Buffer const & buffer);
        /// write a zip file
-       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;
-       ///
-       bool readManifest(Lexer & lex, ErrorList & errorList);
-       void writeManifest(std::ostream & os) const;
-private:
-       /// 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_;
+       bool writeFile(support::DocFileName const & filename, Buffer const & buffer);
 };
 
 } // namespace lyx