2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2002-02-27 09:59:52 +00:00
|
|
|
* \file GraphicsCache.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-09-05 11:31:30 +00:00
|
|
|
* \author Baruch Even
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2002-09-05 14:10:50 +00:00
|
|
|
* Full author contact details are available in file CREDITS
|
2002-02-27 09:59:52 +00:00
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "GraphicsCache.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "GraphicsCacheItem.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "GraphicsImage.h"
|
2002-06-25 15:59:10 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2002-06-12 09:47:10 +00:00
|
|
|
#include "frontends/lyx_gui.h"
|
2002-02-27 17:27:59 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
#include <map>
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
namespace grfx {
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/** The cache contains one item per file, so use a map to find the
|
|
|
|
* cache item quickly by filename.
|
|
|
|
*/
|
|
|
|
typedef std::map<string, Cache::ItemPtr> CacheType;
|
|
|
|
|
|
|
|
struct Cache::Impl {
|
|
|
|
///
|
|
|
|
CacheType cache;
|
|
|
|
};
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache & Cache::get()
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
|
|
|
// Now return the cache
|
2002-06-28 11:22:56 +00:00
|
|
|
static Cache singleton;
|
2000-09-14 17:53:12 +00:00
|
|
|
return singleton;
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::Cache()
|
|
|
|
: pimpl_(new Impl())
|
|
|
|
{}
|
2000-07-31 16:39:50 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::~Cache()
|
|
|
|
{}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
std::vector<string> Cache::loadableFormats() const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
return Image::loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Cache::add(string const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
if (!AbsolutePath(file)) {
|
2002-06-28 11:22:56 +00:00
|
|
|
lyxerr << "Cache::add(" << file << "):\n"
|
2002-06-25 15:59:10 +00:00
|
|
|
<< "The file must be have an absolute path."
|
|
|
|
<< std::endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
// Is the file in the cache already?
|
|
|
|
if (inCache(file)) {
|
2002-06-28 11:22:56 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
|
2002-06-25 15:59:10 +00:00
|
|
|
<< "The file is already in the cache."
|
|
|
|
<< std::endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
2002-06-25 15:59:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->cache[file] = ItemPtr(new CacheItem(file));
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Cache::remove(string const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
CacheType::iterator it = pimpl_->cache.find(file);
|
|
|
|
if (it == pimpl_->cache.end())
|
2002-06-25 15:59:10 +00:00
|
|
|
return;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
ItemPtr & item = it->second;
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
if (item.use_count() == 1) {
|
|
|
|
// The graphics file is in the cache, but nothing else
|
|
|
|
// references it.
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->cache.erase(it);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
bool Cache::inCache(string const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
return pimpl_->cache.find(file) != pimpl_->cache.end();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::ItemPtr const Cache::item(string const & file) const
|
2002-04-11 17:40:44 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
CacheType::const_iterator it = pimpl_->cache.find(file);
|
|
|
|
if (it == pimpl_->cache.end())
|
|
|
|
return ItemPtr();
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
return it->second;
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
} // namespace grfx
|