]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
use anon namespace, somewhat better comp. handling of minipages, not quite there yet
[lyx.git] / src / graphics / GraphicsCacheItem.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "graphics/GraphicsCache.h"
19 #include "graphics/GraphicsCacheItem.h"
20 #include "frontends/support/LyXImage.h"
21 #include "graphics/ImageLoaderXPM.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/syscall.h"
25
26 #include "debug.h"
27
28 using std::endl;
29
30 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
31         : imageStatus_(GraphicsCacheItem::Loading)
32 {
33         filename_ = filename;
34         
35         renderXPM(filename);
36         // For now we do it synchronously
37         imageConverted(0);
38 }
39
40
41 GraphicsCacheItem::~GraphicsCacheItem()
42 {}
43
44
45 GraphicsCacheItem::ImageStatus 
46 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
47
48
49 LyXImage * 
50 GraphicsCacheItem::getImage() const { return image_.get(); }
51
52
53 void
54 GraphicsCacheItem::imageConverted(int retval)
55 {
56         lyxerr << "imageConverted, retval=" << retval << endl;
57
58         if (retval) {
59                 lyxerr << "(GraphicsCacheItem::imageConverter) "
60                         "Error converting image." << endl;
61                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
62                 return;
63         }
64
65         // Do the actual image loading from XPM to memory.
66         loadXPMImage(); 
67 }
68
69         
70 bool
71 GraphicsCacheItem::renderXPM(string const & filename)
72 {
73         // Create the command to do the conversion, this depends on ImageMagicks
74         // convert program.
75         string command = "convert ";
76         command += filename;
77         command += " XPM:";
78
79         // Take only the filename part of the file, without path or extension.
80         string temp = OnlyFilename(filename);
81         temp = ChangeExtension(filename, string());
82         
83         // Add some stuff to have it a unique temp file.
84         // This tempfile is deleted in loadXPMImage after it is loaded to memory.
85         tempfile = lyx::tempName(string(), temp);
86         // Remove the temp file, we only want the name...
87         lyx::unlink(tempfile);
88         tempfile = ChangeExtension(tempfile, ".xpm");   
89         
90         command += tempfile;
91
92         // Run the convertor.
93         lyxerr << "Launching convert to xpm, command=" << command << endl;
94         Systemcalls syscall;
95         syscall.startscript(Systemcalls::Wait, command);
96
97         return true;
98 }
99
100
101 // This function gets called from the callback after the image has been
102 // converted successfully.
103 void
104 GraphicsCacheItem::loadXPMImage()
105 {
106         lyxerr << "Loading XPM Image... ";
107
108         ImageLoaderXPM imageLoader;
109         if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
110                 lyxerr << "Success." << endl;
111                 image_.reset(imageLoader.getImage());
112                 imageStatus_ = GraphicsCacheItem::Loaded;
113         } else {
114                 lyxerr << "Loading " << tempfile << "Failed" << endl;
115                 imageStatus_ = GraphicsCacheItem::ErrorReading;
116         }
117
118         // remove the xpm file now.
119         lyx::unlink(tempfile);
120         // and remove the reference to the filename.
121         tempfile = string();
122 }