]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCacheItem.C
small fix with footnote, use stringstream some more
[lyx.git] / src / graphics / GraphicsCacheItem.C
index bc0ba27fea6b638d58697354e0c184040a97b1e1..e531a7cb2efab8fd7769fc09bc847b81700cfe7d 100644 (file)
 
 #include "graphics/GraphicsCache.h"
 #include "graphics/GraphicsCacheItem.h"
-#include "graphics/GraphicsCacheItem_pimpl.h"
 #include "frontends/support/LyXImage.h"
-
-
-GraphicsCacheItem::GraphicsCacheItem()
-       : pimpl(new GraphicsCacheItem_pimpl)
+#include "graphics/ImageLoaderXPM.h"
+#include "converter.h"
+#include "support/filetools.h"
+#include "support/lyxlib.h"
+#include "lyx_gui_misc.h"
+#include "debug.h"
+#include "support/LAssert.h"
+#include "gettext.h"
+
+using std::endl;
+
+GraphicsCacheItem::GraphicsCacheItem(string const & filename)
+       : imageStatus_(GraphicsCacheItem::Loading)
 {
-       pimpl->refCount = 1;
+       filename_ = filename;
+       
+       bool success = convertImage(filename);
+       // For now we do it synchronously
+       if (success) 
+               imageConverted(0);
+       else
+               imageStatus_ = ErrorConverting;
 }
 
 
 GraphicsCacheItem::~GraphicsCacheItem()
-{
-       destroy();
-}
+{}
 
 
-bool
-GraphicsCacheItem::setFilename(string const & filename)
-{
-       filename_ = filename;
-       return pimpl->setFilename(filename);
-}
+GraphicsCacheItem::ImageStatus 
+GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
 
 
-GraphicsCacheItem::GraphicsCacheItem(GraphicsCacheItem const & gci)
-       : pimpl(0)
-{
-       // copy will set the actual value of the pimpl.
-       copy(gci);
-}
+LyXImage * 
+GraphicsCacheItem::getImage() const { return image_.get(); }
 
-GraphicsCacheItem &
-GraphicsCacheItem::operator=(GraphicsCacheItem const & gci)
-{
-       // Are we trying to copy the object onto itself.
-       if (this == &gci)
-               return *this;
 
-       // Destroy old copy 
-       destroy();
+void
+GraphicsCacheItem::imageConverted(int retval)
+{
+       lyxerr << "imageConverted, retval=" << retval << endl;
 
-       // And then copy new object.
-       copy(gci);
+       if (retval) {
+               lyxerr << "(GraphicsCacheItem::imageConverter) "
+                       "Error converting image." << endl;
+               imageStatus_ = GraphicsCacheItem::ErrorConverting;
+               return;
+       }
 
-       return *this;
+       // Do the actual image loading from file to memory.
+       loadImage();    
 }
 
-GraphicsCacheItem *
-GraphicsCacheItem::Clone() const
+
+namespace {
+
+string const findTargetFormat(string const & from)
 {
-       return new GraphicsCacheItem(*this);
+       typedef ImageLoader::FormatList FormatList;
+       FormatList formats = ImageLoaderXPM().loadableFormats();
+       lyx::Assert(formats.size() > 0); // There must be a format to load from.
+       
+       FormatList::const_iterator iter = formats.begin();
+       FormatList::const_iterator end  = formats.end();
+
+       for (; iter != end; ++iter) {
+               if (converters.IsReachable(from, *iter))
+                       break;
+       }
+       if (iter == end) {
+               // We do not know how to convert the image to something loadable.
+               lyxerr << "ERROR: Do not know how to convert image." << std::endl;
+
+               string const first(_("Cannot convert image to display format"));
+               string const second1(_("Need converter from "));
+               string const second2(_(" to "));
+               string const second(second1 + from + second2 + formats[0]);
+
+               WriteAlert(first, second);
+               
+               return string();
+       }
+
+       return (*iter);
 }
 
-void
-GraphicsCacheItem::copy(GraphicsCacheItem const & gci)
+} // anon namespace
+
+       
+bool
+GraphicsCacheItem::convertImage(string const & filename)
 {
-       pimpl = gci.pimpl;
-       ++(pimpl->refCount);
+       string const from = GetExtension(filename);
+       string const to = findTargetFormat(from);
+       if (to.empty()) 
+               return false;
+       
+       // 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.
+       // This tempfile is deleted in loadImage after it is loaded to memory.
+       tempfile = lyx::tempName(string(), temp);
+       // Remove the temp file, we only want the name...
+       lyx::unlink(tempfile);
+
+       converters.Convert(0, filename, tempfile, from, to);
+
+       return true;
 }
 
 
+// This function gets called from the callback after the image has been
+// converted successfully.
 void
-GraphicsCacheItem::destroy()
+GraphicsCacheItem::loadImage()
 {
-       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; temp = 0;
-                       }
-                       GraphicsCache * gc = GraphicsCache::getInstance();
-                       gc->removeFile(filename_);
-               }
+       lyxerr << "Loading XPM Image... ";
+
+       ImageLoaderXPM imageLoader;
+       if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
+               lyxerr << "Success." << endl;
+               image_.reset(imageLoader.getImage());
+               imageStatus_ = GraphicsCacheItem::Loaded;
+       } else {
+               lyxerr << "Loading " << tempfile << "Failed" << endl;
+               imageStatus_ = GraphicsCacheItem::ErrorReading;
        }
-}
 
-
-GraphicsCacheItem::ImageStatus 
-GraphicsCacheItem::getImageStatus() const { return pimpl->imageStatus_; }
-
-LyXImage * 
-GraphicsCacheItem::getImage() const { return pimpl->getImage(); }
+       // remove the xpm file now.
+       lyx::unlink(tempfile);
+       // and remove the reference to the filename.
+       tempfile = string();
+}