lyx_mirror/src/EmbeddedFiles.h
Bo Peng 48f9225e27 Embed: allow the use of embedded layout and class (.cls, .sty) files.
* src/EmbeddedFiles.h|cpp: add validate() function to validate loaded .lyx file.
	* src/LayoutFile.h|cpp: add Layout_Type parameter to addLayoutFile to load an embedded layout.
	* src/BufferParams.h|cpp: read layout from temp directory, then local directory.
	* src/frontends/qt4/GuiDocument.cpp: handle embedded layout type
	* src/LyXFunc.cpp: handle embedded layout when a layout is re-read.
	* src/Buffer.cpp: validate after a buffer is read. handle exceptions properly.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23667 a592a061-630c-0410-9148-cb99ea01b6c8
2008-03-11 18:31:38 +00:00

220 lines
7.6 KiB
C++

// -*- 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 <string>
#include <vector>
/**
This file, and the embedding dialog implemented in src/frontends, implements
an 'Embedded Files' feature of lyx.
Expected features:
=========================
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 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.
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
easily.
Format a and b can be converted easily, by packing/unpacking a .lyx file.
NOTE: With current implementation, files with absolute filenames (not in
or deeper under the current document directory) can not be embedded.
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()).
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 form.
(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() and tmppath()/content.lyx is read as usual.
(c.f. bool Buffer::readFile())
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.
*/
namespace lyx {
class Buffer;
class Inset;
class Lexer;
class ErrorList;
class EmbeddedFile : public support::DocFileName
{
public:
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. However, the embedded file will be renamed to the
* name set here when an EmbeddedFile is enabled. It is therefore
* safe to change the naming scheme here.
*
* 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. When an EmbeddedFile is enabled,
* this file will be renamed to the default inzipName if needed.
*/
void setInzipName(std::string const & name);
/// filename in the zip file, which is the relative path
std::string inzipName() const { return inzip_name_; }
/// embedded file, equals to temppath()/inzipName()
std::string embeddedFile() const;
/// embeddedFile() or absFilename() depending on embedding status
/// and whether or not embedding is enabled.
FileName availableFile() const;
///
std::string latexFilename(std::string const & buffer_path) const;
/// add an inset that refers to this file
void addInset(Inset const * inset);
void clearInsets() const { inset_list_.clear(); }
/// embedding status of this file
bool embedded() const { return embedded_; }
/// set embedding status.
void setEmbed(bool embed);
/// whether or not embedding is enabled in the current buffer
bool enabled() const { return temp_path_ != ""; }
/// enable embedding of this file
void enable(bool flag, Buffer const * buf, bool updateFile);
/// extract file, does not change embedding status
bool extract() const;
/// update embedded file from external file, does not change embedding status
bool updateFromExternalFile() const;
///
/// 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. To make sure inset pointers
/// are up to date, please make sure there is no modification to the
/// document between EmbeddedFiles::update() and this function.
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
// an calculated inzip_name_
void syncInzipFile(std::string const & buffer_path);
private:
/// filename in zip file
std::string inzip_name_;
/// the status of this docfile
bool embedded_;
/// Insets that contains this file item. Because a
/// file item can be referred by several Insets, a vector is used.
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_;
};
bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
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, 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);
/// validate embedded fies after a file is read.
void validate(Buffer const & buffer);
/// scan the buffer and get a list of EmbeddedFile
void update(Buffer const & buffer);
/// write a zip file
bool writeFile(support::DocFileName const & filename, Buffer const & buffer);
};
} // namespace lyx
#endif