]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
small fix with footnote, use stringstream some more
[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 "converter.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "lyx_gui_misc.h"
26 #include "debug.h"
27 #include "support/LAssert.h"
28 #include "gettext.h"
29
30 using std::endl;
31
32 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
33         : imageStatus_(GraphicsCacheItem::Loading)
34 {
35         filename_ = filename;
36         
37         bool success = convertImage(filename);
38         // For now we do it synchronously
39         if (success) 
40                 imageConverted(0);
41         else
42                 imageStatus_ = ErrorConverting;
43 }
44
45
46 GraphicsCacheItem::~GraphicsCacheItem()
47 {}
48
49
50 GraphicsCacheItem::ImageStatus 
51 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
52
53
54 LyXImage * 
55 GraphicsCacheItem::getImage() const { return image_.get(); }
56
57
58 void
59 GraphicsCacheItem::imageConverted(int retval)
60 {
61         lyxerr << "imageConverted, retval=" << retval << endl;
62
63         if (retval) {
64                 lyxerr << "(GraphicsCacheItem::imageConverter) "
65                         "Error converting image." << endl;
66                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
67                 return;
68         }
69
70         // Do the actual image loading from file to memory.
71         loadImage();    
72 }
73
74
75 namespace {
76
77 string const findTargetFormat(string const & from)
78 {
79         typedef ImageLoader::FormatList FormatList;
80         FormatList formats = ImageLoaderXPM().loadableFormats();
81         lyx::Assert(formats.size() > 0); // There must be a format to load from.
82         
83         FormatList::const_iterator iter = formats.begin();
84         FormatList::const_iterator end  = formats.end();
85
86         for (; iter != end; ++iter) {
87                 if (converters.IsReachable(from, *iter))
88                         break;
89         }
90         if (iter == end) {
91                 // We do not know how to convert the image to something loadable.
92                 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
93
94                 string const first(_("Cannot convert image to display format"));
95                 string const second1(_("Need converter from "));
96                 string const second2(_(" to "));
97                 string const second(second1 + from + second2 + formats[0]);
98
99                 WriteAlert(first, second);
100                 
101                 return string();
102         }
103
104         return (*iter);
105 }
106
107 } // anon namespace
108
109         
110 bool
111 GraphicsCacheItem::convertImage(string const & filename)
112 {
113         string const from = GetExtension(filename);
114         string const to = findTargetFormat(from);
115         if (to.empty()) 
116                 return false;
117         
118         // Take only the filename part of the file, without path or extension.
119         string temp = OnlyFilename(filename);
120         temp = ChangeExtension(filename, string());
121         
122         // Add some stuff to have it a unique temp file.
123         // This tempfile is deleted in loadImage after it is loaded to memory.
124         tempfile = lyx::tempName(string(), temp);
125         // Remove the temp file, we only want the name...
126         lyx::unlink(tempfile);
127
128         converters.Convert(0, filename, tempfile, from, to);
129
130         return true;
131 }
132
133
134 // This function gets called from the callback after the image has been
135 // converted successfully.
136 void
137 GraphicsCacheItem::loadImage()
138 {
139         lyxerr << "Loading XPM Image... ";
140
141         ImageLoaderXPM imageLoader;
142         if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
143                 lyxerr << "Success." << endl;
144                 image_.reset(imageLoader.getImage());
145                 imageStatus_ = GraphicsCacheItem::Loaded;
146         } else {
147                 lyxerr << "Loading " << tempfile << "Failed" << endl;
148                 imageStatus_ = GraphicsCacheItem::ErrorReading;
149         }
150
151         // remove the xpm file now.
152         lyx::unlink(tempfile);
153         // and remove the reference to the filename.
154         tempfile = string();
155 }