2007-08-30 20:46:42 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file EmbeddedFiles.h
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Bo Peng
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EMBEDDEDFILES_H
|
|
|
|
#define EMBEDDEDFILES_H
|
|
|
|
|
|
|
|
#include "support/FileName.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
This file, and the embedding dialog implemented in src/frontends, implements
|
|
|
|
an 'Embedded Files' feature of lyx.
|
|
|
|
|
|
|
|
|
|
|
|
Expected features:
|
|
|
|
=========================
|
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
1. Bundled .lyx file can embed graphics, listings, bib file etc. The bundle
|
|
|
|
format is used when Document->Save in bundled format is selected.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
2. Embedded file.lyx file is a zip file, with content.lyx and embedded files.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
3. The embedding status of embedded files can be set at the inset level,
|
|
|
|
or from Document->Settings->Embedded Files.
|
|
|
|
|
|
|
|
4. Extra files such as .cls and .layout can be embedded from Document->
|
|
|
|
Settings->Embedded Files->Extra 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.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
Overall, this feature allows two ways of editing a .lyx file
|
|
|
|
|
|
|
|
a. The continuous use of the pure-text .lyx file format with external
|
|
|
|
files. This is the default file format, and allows external editing
|
|
|
|
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
|
2007-09-07 03:02:24 +00:00
|
|
|
easily.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
Format a anb b can be converted easily, by packing/unpacking a .lyx file.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
Implementation:
|
|
|
|
======================
|
|
|
|
|
|
|
|
1. An EmbeddedFiles class is implemented to keep the embedded files (
|
|
|
|
class EmbeddedFile). (c.f. src/EmbeddedFiles.[h|cpp])
|
|
|
|
|
|
|
|
2. When a file is saved, it is scanned for embedded files. (c.f.
|
|
|
|
EmbeddedFiles::update(), Inset::registerEmbeddedFiles()).
|
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
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.
|
2007-08-30 20:46:42 +00:00
|
|
|
If embedding is disabled, file.lyx is saved in the usual pure-text form.
|
2007-09-11 14:23:12 +00:00
|
|
|
(c.f. Buffer::writeFile(), EmbeddedFiles::writeFile())
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
4. When a lyx file.lyx file is opened, if it is a zip file, it is
|
2007-10-30 22:21:48 +00:00
|
|
|
decompressed to tmppath() and tmppath()/content.lyx is read as usual.
|
2007-08-30 20:46:42 +00:00
|
|
|
(c.f. bool Buffer::readFile())
|
|
|
|
|
2007-10-30 22:21:48 +00:00
|
|
|
5. A menu item Document -> Save in bundled format is provided to pack/unpack
|
|
|
|
a .lyx file.
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-09-07 03:02:24 +00:00
|
|
|
6. If embedding of a .lyx file with embedded files is disabled, all its
|
2007-08-30 20:46:42 +00:00
|
|
|
embedded files are copied to their respective external filenames. This
|
|
|
|
is why external filename will exist even if a file is at "EMBEDDED" status.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
class Buffer;
|
2007-11-03 17:37:37 +00:00
|
|
|
class Inset;
|
2007-09-10 03:54:02 +00:00
|
|
|
class Lexer;
|
|
|
|
class ErrorList;
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
class EmbeddedFile : public support::DocFileName
|
|
|
|
{
|
|
|
|
public:
|
2007-09-15 18:31:32 +00:00
|
|
|
EmbeddedFile() {};
|
|
|
|
|
2007-08-30 20:46:42 +00:00
|
|
|
EmbeddedFile(std::string const & file, std::string const & inzip_name,
|
2007-09-09 07:07:13 +00:00
|
|
|
bool embedded, Inset const * inset);
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
/// filename in the zip file, usually the relative path
|
|
|
|
std::string inzipName() const { return inzip_name_; }
|
2007-09-15 18:31:32 +00:00
|
|
|
void setInzipName(std::string name) { inzip_name_ = name; }
|
2007-08-30 20:46:42 +00:00
|
|
|
/// embedded file, equals to temppath()/inzipName()
|
|
|
|
std::string embeddedFile(Buffer const * buf) const;
|
2007-09-07 03:02:24 +00:00
|
|
|
/// embeddedFile() or absFilename() depending on embedding status
|
2007-09-01 20:08:16 +00:00
|
|
|
std::string availableFile(Buffer const * buf) const;
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-09-09 07:07:13 +00:00
|
|
|
/// 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;
|
2007-09-08 04:10:43 +00:00
|
|
|
/// 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.
|
2007-09-09 07:07:13 +00:00
|
|
|
int refCount() const { return inset_list_.size(); }
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
/// embedding status of this file
|
2007-09-07 03:02:24 +00:00
|
|
|
bool embedded() const { return embedded_; }
|
2007-09-08 04:10:43 +00:00
|
|
|
/// set embedding status. updateFromExternal() should be called before this
|
|
|
|
/// to copy or sync the embedded file with external one.
|
2007-09-08 20:06:31 +00:00
|
|
|
void setEmbed(bool embed) { embedded_ = embed; }
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-09-07 16:33:55 +00:00
|
|
|
/// extract file, does not change embedding status
|
2007-09-01 20:08:16 +00:00
|
|
|
bool extract(Buffer const * buf) const;
|
2007-09-07 16:33:55 +00:00
|
|
|
/// update embedded file from external file, does not change embedding status
|
2007-09-08 04:10:43 +00:00
|
|
|
bool updateFromExternalFile(Buffer const * buf) const;
|
2007-09-12 14:27:47 +00:00
|
|
|
///
|
|
|
|
/// 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;
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// filename in zip file
|
|
|
|
std::string inzip_name_;
|
|
|
|
/// the status of this docfile
|
2007-09-07 03:02:24 +00:00
|
|
|
bool embedded_;
|
2007-10-30 22:21:48 +00:00
|
|
|
/// Insets that contains this file item. Because a
|
2007-09-08 04:10:43 +00:00
|
|
|
/// file item can be referred by several Insets, a vector is used.
|
2007-09-09 07:07:13 +00:00
|
|
|
std::vector<Inset const *> inset_list_;
|
2007-08-30 20:46:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class EmbeddedFiles {
|
|
|
|
public:
|
|
|
|
typedef std::vector<EmbeddedFile> EmbeddedFileList;
|
|
|
|
public:
|
|
|
|
///
|
2007-09-11 21:27:57 +00:00
|
|
|
EmbeddedFiles(Buffer * buffer = 0) : file_list_(), buffer_(buffer) {}
|
2007-08-30 20:46:42 +00:00
|
|
|
///
|
|
|
|
~EmbeddedFiles() {}
|
|
|
|
|
|
|
|
/// return buffer params embedded flag
|
|
|
|
bool enabled() const;
|
2007-09-08 04:10:43 +00:00
|
|
|
/// set buffer params embedded flag. Files will be updated or extracted
|
|
|
|
/// if such an operation fails, enable will fail.
|
2007-09-11 21:27:57 +00:00
|
|
|
void enable(bool flag);
|
2007-09-08 04:10:43 +00:00
|
|
|
|
|
|
|
/// 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.
|
2007-09-10 03:54:02 +00:00
|
|
|
* \param inset Inset pointer
|
|
|
|
* \param inzipName suggested inzipname
|
2007-09-08 04:10:43 +00:00
|
|
|
*/
|
2007-09-13 05:30:44 +00:00
|
|
|
EmbeddedFile & registerFile(std::string const & filename, bool embed = false,
|
2007-09-11 21:27:57 +00:00
|
|
|
Inset const * inset = 0,
|
2007-09-10 03:54:02 +00:00
|
|
|
std::string const & inzipName = std::string());
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
/// scan the buffer and get a list of EmbeddedFile
|
|
|
|
void update();
|
|
|
|
|
|
|
|
/// write a zip file
|
2007-09-11 14:23:12 +00:00
|
|
|
bool writeFile(support::DocFileName const & filename);
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
void clear() { file_list_.clear(); }
|
|
|
|
|
2007-08-31 22:02:34 +00:00
|
|
|
///
|
|
|
|
EmbeddedFile & operator[](size_t idx) { return *(file_list_.begin() + idx); }
|
|
|
|
EmbeddedFile const & operator[](size_t idx) const { return *(file_list_.begin() + idx); }
|
2007-08-30 20:46:42 +00:00
|
|
|
///
|
|
|
|
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(); }
|
2007-09-05 17:50:07 +00:00
|
|
|
// try to locate filename, using either absFilename() or embeddedFile()
|
|
|
|
EmbeddedFileList::const_iterator find(std::string filename) const;
|
2007-09-08 04:10:43 +00:00
|
|
|
/// extract all file items, used when disable embedding
|
|
|
|
bool extract() const;
|
|
|
|
/// update all files from external, used when enable embedding
|
|
|
|
bool updateFromExternalFile() const;
|
2007-08-30 20:46:42 +00:00
|
|
|
///
|
2007-09-12 14:27:47 +00:00
|
|
|
/// update all insets to use embedded files when embedding status is changed
|
|
|
|
void updateInsets() const;
|
2007-08-30 20:46:42 +00:00
|
|
|
private:
|
2007-09-10 03:54:02 +00:00
|
|
|
/// get a unique inzip name, a suggestion can be given.
|
|
|
|
std::string const getInzipName(std::string const & name, std::string const & inzipName);
|
2007-08-30 20:46:42 +00:00
|
|
|
/// list of embedded files
|
|
|
|
EmbeddedFileList file_list_;
|
|
|
|
///
|
|
|
|
Buffer * buffer_;
|
|
|
|
};
|
|
|
|
|
2007-09-11 21:27:57 +00:00
|
|
|
} // namespace lyx
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
#endif
|