#pragma implementation
#endif
-#include "GraphicsCacheItem.h"
+#include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "graphics/GraphicsCacheItem_pimpl.h"
+#include "frontends/support/LyXImage.h"
-#include "graphics/XPM_Renderer.h"
-#include "support/filetools.h"
-#include "debug.h"
-#include "support/LAssert.h"
-#include <unistd.h> // unlink
-
-#include <map>
-
-#include FORMS_H_LOCATION
-
-using std::endl;
GraphicsCacheItem::GraphicsCacheItem()
- : height_(-1), width_(-1), imageStatus_(Loading),
- pixmap_(0), renderer(0)
-{}
+ : pimpl(new GraphicsCacheItem_pimpl)
+{
+ pimpl->refCount = 1;
+}
+
GraphicsCacheItem::~GraphicsCacheItem()
{
- if (imageStatus_ == Loaded) {
- XFreePixmap(fl_display, pixmap_);
- }
-
- delete renderer;
+ destroy();
}
+
bool
GraphicsCacheItem::setFilename(string const & filename)
{
- imageStatus_ = Loading;
-
- renderer = new XPM_Renderer();
- if (renderXPM(filename))
- return true;
-
- return false;
+ filename_ = filename;
+ return pimpl->setFilename(filename);
}
-/*** Callback method ***/
-
-typedef map<string, GraphicsCacheItem*> CallbackMap;
-static CallbackMap callbackMap;
-void
-callback(string cmd, int retval)
+GraphicsCacheItem::GraphicsCacheItem(GraphicsCacheItem const & gci)
{
- lyxerr << "callback, cmd="<<cmd<<", retval="<<retval<<endl;
-
- GraphicsCacheItem * item = callbackMap[cmd];
- callbackMap.erase(cmd);
-
- item->imageConverted(retval);
+ pimpl = 0;
+ copy(gci);
}
-void
-GraphicsCacheItem::imageConverted(int retval)
+GraphicsCacheItem &
+GraphicsCacheItem::operator=(GraphicsCacheItem const & gci)
{
- lyxerr << "imageConverted, retval="<<retval<<endl;
+ // Are we trying to copy the object onto itself.
+ if (this == &gci)
+ return *this;
- if (retval) {
- imageStatus_ = ErrorConverting;
- return;
- }
+ // Destroy old copy
+ destroy();
+
+ // And then copy new object.
+ copy(gci);
- // Do the actual image loading from XPM to memory.
- loadXPMImage();
+ return *this;
}
-/**********************/
+GraphicsCacheItem *
+GraphicsCacheItem::Clone() const
+{
+ return new GraphicsCacheItem(*this);
+}
-bool
-GraphicsCacheItem::renderXPM(string const & filename)
+void
+GraphicsCacheItem::copy(GraphicsCacheItem const & gci)
{
- // Create the command to do the conversion, this depends on ImageMagicks
- // convert program.
- string command = "convert ";
- command += filename;
- command += " XPM:";
-
- // Take only the filename part of the file, without path or extension.
- string temp = OnlyFilename(filename);
- temp = ChangeExtension(filename , string());
-
- // Add some stuff to have it a unique temp file.
- xpmfile = TmpFileName(string(), temp);
- xpmfile = ChangeExtension(xpmfile, ".xpm");
-
- command += xpmfile;
-
- // Set the callback mapping to point to us.
- callbackMap[command] = this;
-
- // Run the convertor.
- // There is a problem with running it asyncronously, it doesn't return
- // to call the callback, so until the Systemcalls mechanism is fixed
- // I use the syncronous method.
- lyxerr << "Launching convert to xpm, command="<<command<<endl;
-// syscall.startscript(Systemcalls::DontWait, command, &callback);
- syscall.startscript(Systemcalls::Wait, command, &callback);
-
- return true;
+ pimpl = gci.pimpl;
+ ++(pimpl->refCount);
}
-// This function gets called from the callback after the image has been
-// converted successfully.
+
void
-GraphicsCacheItem::loadXPMImage()
+GraphicsCacheItem::destroy()
{
- if (! renderer->setFilename(xpmfile)) {
- return;
+ if (pimpl) {
+ --(pimpl->refCount);
+ if (pimpl->refCount == 0) {
+ { // We are deleting the pimpl but we want to mark it deleted
+ // even before it is deleted.
+ GraphicsCacheItem_pimpl * temp = pimpl;
+ pimpl = 0;
+ delete temp;
+ }
+ GraphicsCache * gc = GraphicsCache::getInstance();
+ gc->removeFile(filename_);
+ }
}
+}
- if (renderer->renderImage()) {
- pixmap_ = renderer->getPixmap();
- width_ = renderer->getWidth();
- height_ = renderer->getHeight();
- imageStatus_ = Loaded;
- } else {
- imageStatus_ = ErrorReading;
- }
- imageDone.emit();
+GraphicsCacheItem::ImageStatus
+GraphicsCacheItem::getImageStatus() const { return pimpl->imageStatus_; }
- // remove the xpm file now.
- ::unlink(xpmfile.c_str());
- // and remove the reference to the filename.
- xpmfile = string();
-}
+
+int
+GraphicsCacheItem::getHeight() const { return pimpl->height_; }
+
+
+int
+GraphicsCacheItem::getWidth() const { return pimpl->width_; }
+
+LyXImage *
+GraphicsCacheItem::getImage() const { return pimpl->pixmap_; }