]> git.lyx.org Git - lyx.git/blob - src/EmbeddedFiles.h
Showing a message box after embedding status is changed.
[lyx.git] / src / EmbeddedFiles.h
1 // -*- C++ -*-
2 /**
3  * \file EmbeddedFiles.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #ifndef EMBEDDEDFILES_H
14 #define EMBEDDEDFILES_H
15
16 #include "support/FileName.h"
17
18 #include <string>
19 #include <vector>
20
21 /**
22
23 This file, and the embedding dialog implemented in src/frontends, implements
24 an 'Embedded Files' feature of lyx.
25
26
27 Expected features:
28 =========================
29
30 1. Bundled .lyx file can embed graphics, listings, bib file etc. The bundle
31 format is used when Document->Save in bundled format is selected.
32
33 2. Embedded file.lyx file is a zip file, with content.lyx and embedded files. 
34
35 3. The embedding status of embedded files can be set at the inset level,
36 or from Document->Settings->Embedded Files.
37
38 4. Extra files such as .cls and .layout can be embedded from Document->
39 Settings->Embedded Files->Extra Files.
40
41 5. When Document->Save in bundled format is selected, all embedded files
42 become bundled. Changes to the external version of this file does not
43 affect the output of the .lyx file.
44
45 6. When Document->Save in bundled format is unchecked, all embedded files
46 are copied to their original locations.
47
48 Overall, this feature allows two ways of editing a .lyx file
49
50 a. The continuous use of the pure-text .lyx file format with external
51 files. This is the default file format, and allows external editing
52 of .lyx file and better use of version control systems.
53
54 b. The embedded way. Figures etc are inserted to .lyx file and will
55 be embedded. These embedded files can be viewed or edited through
56 the embedding dialog. This file can be shared with others more
57 easily. 
58
59 Format a anb b can be converted easily, by packing/unpacking a .lyx file.
60
61 Implementation:
62 ======================
63
64 1. An EmbeddedFiles class is implemented to keep the embedded files (
65 class EmbeddedFile). (c.f. src/EmbeddedFiles.[h|cpp])
66
67 2. When a file is saved, it is scanned for embedded files. (c.f.
68 EmbeddedFiles::update(), Inset::registerEmbeddedFiles()).
69
70 3. When a lyx file file.lyx is saved, it is save to tmppath()/content.lyx
71 first. Embedded files are compressed along with content.lyx.
72 If embedding is disabled, file.lyx is saved in the usual pure-text form.
73 (c.f. Buffer::writeFile(), EmbeddedFiles::writeFile())
74
75 4. When a lyx file.lyx file is opened, if it is a zip file, it is
76 decompressed to tmppath() and tmppath()/content.lyx is read as usual.
77 (c.f. bool Buffer::readFile())
78
79 5. A menu item Document -> Save in bundled format is provided to pack/unpack
80 a .lyx file.
81
82 6. If embedding of a .lyx file with embedded files is disabled, all its
83 embedded files are copied to their respective external filenames. This
84 is why external filename will exist even if a file is at "EMBEDDED" status.
85
86 */
87
88 namespace lyx {
89
90 class Buffer;
91 class Inset;
92 class Lexer;
93 class ErrorList;
94
95 class EmbeddedFile : public support::DocFileName
96 {
97 public:
98         EmbeddedFile() {};
99
100         EmbeddedFile(std::string const & file, std::string const & inzip_name,
101                 bool embedded, Inset const * inset);
102
103         /// filename in the zip file, usually the relative path
104         std::string inzipName() const { return inzip_name_; }
105         void setInzipName(std::string name) { inzip_name_ = name; }
106         /// embedded file, equals to temppath()/inzipName()
107         std::string embeddedFile(Buffer const * buf) const;
108         /// embeddedFile() or absFilename() depending on embedding status
109         std::string availableFile(Buffer const * buf) const;
110
111         /// add an inset that refers to this file
112         void addInset(Inset const * inset);
113         Inset const * inset(int idx) const;
114         /// save the location of this inset as bookmark so that
115         /// it can be located using LFUN_BOOKMARK_GOTO
116         void saveBookmark(Buffer const * buf, int idx) const;
117         /// Number of Insets this file item is referred
118         /// If refCount() == 0, this file must be manually inserted.
119         /// This fact is used by the update() function to skip updating
120         /// such items.
121         int refCount() const { return inset_list_.size(); }
122
123         /// embedding status of this file
124         bool embedded() const { return embedded_; }
125         /// set embedding status. updateFromExternal() should be called before this
126         /// to copy or sync the embedded file with external one.
127         void setEmbed(bool embed) { embedded_ = embed; }
128
129         /// extract file, does not change embedding status
130         bool extract(Buffer const * buf) const;
131         /// update embedded file from external file, does not change embedding status
132         bool updateFromExternalFile(Buffer const * buf) const;
133         ///
134         /// After the embedding status is changed, update all insets related
135         /// to this file item.
136         /// Because inset pointers may not be up to date, EmbeddedFiles::update()
137         /// would better be called before this function is called.
138         void updateInsets(Buffer const * buf) const;
139
140 private:
141         /// filename in zip file
142         std::string inzip_name_;
143         /// the status of this docfile
144         bool embedded_;
145         /// Insets that contains this file item. Because a 
146         /// file item can be referred by several Insets, a vector is used.
147         std::vector<Inset const *> inset_list_;
148 };
149
150
151 class EmbeddedFiles {
152 public:
153         typedef std::vector<EmbeddedFile> EmbeddedFileList;
154 public:
155         ///
156         EmbeddedFiles(Buffer * buffer = 0) : file_list_(), buffer_(buffer) {}
157         ///
158         ~EmbeddedFiles() {}
159
160         /// return buffer params embedded flag
161         bool enabled() const;
162         /// set buffer params embedded flag. Files will be updated or extracted
163         /// if such an operation fails, enable will fail.
164         void enable(bool flag);
165
166         /// add a file item. 
167         /* \param filename filename to add
168          * \param embed embedding status. For a new file item, this is always true.
169          *    If the file already exists, this parameter is ignored.
170          * \param inset Inset pointer
171          * \param inzipName suggested inzipname
172          */
173         EmbeddedFile & registerFile(std::string const & filename, bool embed = false,
174                 Inset const * inset = 0,
175                 std::string const & inzipName = std::string());
176
177         /// scan the buffer and get a list of EmbeddedFile
178         void update();
179
180         /// write a zip file
181         bool writeFile(support::DocFileName const & filename);
182
183         void clear() { file_list_.clear(); }
184
185         ///
186         EmbeddedFile & operator[](size_t idx) { return *(file_list_.begin() + idx); }
187         EmbeddedFile const & operator[](size_t idx) const { return *(file_list_.begin() + idx); }
188         ///
189         EmbeddedFileList::iterator begin() { return file_list_.begin(); }
190         EmbeddedFileList::iterator end() { return file_list_.end(); }
191         EmbeddedFileList::const_iterator begin() const { return file_list_.begin(); }
192         EmbeddedFileList::const_iterator end() const { return file_list_.end(); }
193         // try to locate filename, using either absFilename() or embeddedFile()
194         EmbeddedFileList::const_iterator find(std::string filename) const;
195         /// extract all file items, used when disable embedding
196         bool extractAll() const;
197         /// update all files from external, used when enable embedding
198         bool updateFromExternalFile() const;
199         ///
200         /// update all insets to use embedded files when embedding status is changed
201         void updateInsets() const;
202 private:
203         /// get a unique inzip name, a suggestion can be given.
204         std::string const getInzipName(std::string const & name, std::string const & inzipName);
205         /// list of embedded files
206         EmbeddedFileList file_list_;
207         ///
208         Buffer * buffer_;
209 };
210
211 } // namespace lyx
212
213 #endif