2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file GraphicsCache.cpp
|
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
|
|
|
|
*
|
2003-08-23 00:17:00 +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
|
|
|
#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
|
|
|
|
2008-06-06 11:34:49 +00:00
|
|
|
#include "Format.h"
|
|
|
|
|
2008-06-06 12:29:58 +00:00
|
|
|
#include "frontends/Application.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2002-06-25 15:59:10 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2004-11-20 09:19:52 +00:00
|
|
|
#include <map>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2003-09-05 03:10:30 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace graphics {
|
2002-02-27 09:59:52 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2007-12-12 19:28:07 +00:00
|
|
|
typedef map<FileName, Cache::ItemPtr> CacheType;
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class Cache::Impl {
|
|
|
|
public:
|
2002-06-28 11:22:56 +00:00
|
|
|
///
|
|
|
|
CacheType cache;
|
|
|
|
};
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2014-07-05 13:20:54 +00:00
|
|
|
// FIXME THREAD
|
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()
|
2003-09-21 16:02:54 +00:00
|
|
|
: pimpl_(new Impl)
|
2002-06-28 11:22:56 +00:00
|
|
|
{}
|
2000-07-31 16:39:50 +00:00
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::~Cache()
|
2007-11-21 23:47:47 +00:00
|
|
|
{
|
|
|
|
delete pimpl_;
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2008-06-06 11:34:49 +00:00
|
|
|
vector<string> const & Cache::loadableFormats() const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2008-06-06 11:34:49 +00:00
|
|
|
static vector<string> fmts;
|
|
|
|
|
|
|
|
if (!fmts.empty())
|
|
|
|
return fmts;
|
|
|
|
|
|
|
|
// The formats recognised by LyX
|
|
|
|
Formats::const_iterator begin = formats.begin();
|
|
|
|
Formats::const_iterator end = formats.end();
|
|
|
|
|
|
|
|
// The formats natively loadable.
|
2008-06-06 12:29:58 +00:00
|
|
|
vector<string> nformat = frontend::loadableImageFormats();
|
2008-06-06 11:34:49 +00:00
|
|
|
|
|
|
|
vector<string>::const_iterator it = nformat.begin();
|
|
|
|
for (; it != nformat.end(); ++it) {
|
|
|
|
for (Formats::const_iterator fit = begin; fit != end; ++fit) {
|
|
|
|
if (fit->extension() == *it) {
|
|
|
|
fmts.push_back(fit->name());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lyxerr.debugging()) {
|
|
|
|
LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
|
|
|
|
|
|
|
|
vector<string>::const_iterator fbegin = fmts.begin();
|
|
|
|
vector<string>::const_iterator fend = fmts.end();
|
|
|
|
for (vector<string>::const_iterator fit = fbegin; fit != fend; ++fit) {
|
|
|
|
if (fit != fbegin)
|
|
|
|
LYXERR(Debug::GRAPHICS, ", ");
|
|
|
|
LYXERR(Debug::GRAPHICS, *fit);
|
|
|
|
}
|
|
|
|
LYXERR(Debug::GRAPHICS, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmts;
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Cache::add(FileName const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
// Is the file in the cache already?
|
|
|
|
if (inCache(file)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
|
|
|
|
<< "The file is already in the cache.");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Cache::remove(FileName 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
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool Cache::inCache(FileName 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
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
Cache::ItemPtr const Cache::item(FileName 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
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|