]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
File monitoring is Go!
[lyx.git] / src / graphics / GraphicsCacheItem.C
1 /*
2  * \file GraphicsCacheItem.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Baruch Even <baruch.even@writeme.com>
7  * \author Herbert Voss <voss@lyx.org>
8  * \author Angus Leeming <leeming@lyx.org>
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsConverter.h"
20
21 #include "support/FileMonitor.h"
22
23 #include "debug.h"
24
25 #include "support/LAssert.h"
26 #include "support/filetools.h"
27
28 #include <boost/shared_ptr.hpp>
29 #include <boost/bind.hpp>
30 #include <boost/signals/trackable.hpp>
31
32 using std::endl;
33
34 namespace grfx {
35
36 struct CacheItem::Impl : public boost::signals::trackable {
37
38         ///
39         Impl(string const & file);
40
41         /** Start the image conversion process, checking first that it is
42          *  necessary. If it is necessary, then a conversion task is started.
43          *  CacheItem asumes that the conversion is asynchronous and so
44          *  passes a Signal to the converting routine. When the conversion
45          *  is finished, this Signal is emitted, returning the converted
46          *  file to this->imageConverted.
47          *
48          *  If no file conversion is needed, then convertToDisplayFormat() calls
49          *  loadImage() directly.
50          *
51          *  convertToDisplayFormat() will set the loading status flag as
52          *  approriate through calls to setStatus().
53          */
54         void convertToDisplayFormat();
55
56         /** Load the image into memory. This is called either from
57          *  convertToDisplayFormat() direct or from imageConverted().
58          */
59         void loadImage();
60
61         /** Get a notification when the image conversion is done.
62          *  Connected to a signal on_finish_ which is passed to
63          *  Converter::convert.
64          */
65         void imageConverted(bool);
66
67         /** Get a notification when the image loading is done.
68          *  Connected to a signal on_finish_ which is passed to
69          *  grfx::Image::loadImage.
70          */
71         void imageLoaded(bool);
72
73         /** Sets the status of the loading process. Also notifies
74          *  listeners that the status has changed.
75          */
76         void setStatus(ImageStatus new_status);
77
78         /** Can be invoked directly by the user, but is also connected to the
79          *  FileMonitor and so is invoked when the file is changed
80          *  (if monitoring is taking place).
81          */
82         void startLoading();
83
84         /** If we are asked to load the file for a second or further time,
85          *  (because the file has changed), then we'll have to first reset
86          *  many of the variables below.
87          */
88         void reset();
89
90         /// The filename we refer too.
91         string const filename_;
92         ///
93         FileMonitor const monitor_;
94
95         /// Is the file compressed?
96         bool zipped_;
97         /// If so, store the uncompressed file in this temporary file.
98         string unzipped_filename_;
99         /// What file are we trying to load?
100         string file_to_load_;
101         /** Should we delete the file after loading? True if the file is
102          *  the result of a conversion process.
103          */
104         bool remove_loaded_file_;
105
106         /// The image and its loading status.
107         boost::shared_ptr<Image> image_;
108         ///
109         ImageStatus status_;
110
111         /// This signal is emitted when the image loading status changes.
112         boost::signal0<void> statusChanged;
113
114         /// The connection to the signal Image::finishedLoading
115         boost::signals::connection cl_;
116
117         /// The connection of the signal ConvProcess::finishedConversion,
118         boost::signals::connection cc_;
119
120         ///
121         boost::scoped_ptr<Converter> converter_;
122 };
123
124
125 CacheItem::CacheItem(string const & file)
126         : pimpl_(new Impl(file))
127 {}
128
129
130 CacheItem::~CacheItem()
131 {}
132
133
134 string const & CacheItem::filename() const
135 {
136         return pimpl_->filename_;
137 }
138
139
140 void CacheItem::startLoading() const
141 {
142         pimpl_->startLoading();
143 }
144
145
146 void CacheItem::startMonitoring() const
147 {
148         if (!pimpl_->monitor_.monitoring())
149                 pimpl_->monitor_.start();
150 }
151
152
153 bool CacheItem::monitoring() const
154 {
155         return pimpl_->monitor_.monitoring();
156 }
157
158
159 unsigned long CacheItem::checksum() const
160 {
161         return pimpl_->monitor_.checksum();
162 }
163
164
165 Image const * CacheItem::image() const
166 {
167         return pimpl_->image_.get();
168 }
169
170
171 ImageStatus CacheItem::status() const
172 {
173         return pimpl_->status_;
174 }
175
176
177 boost::signals::connection CacheItem::connect(slot_type const & slot) const
178 {
179         return pimpl_->statusChanged.connect(slot);
180 }
181
182
183 //------------------------------
184 // Implementation details follow
185 //------------------------------
186
187
188 CacheItem::Impl::Impl(string const & file)
189         : filename_(file),
190           monitor_(file, 2000),
191           zipped_(false),
192           remove_loaded_file_(false),
193           status_(WaitingToLoad)
194 {
195         monitor_.connect(boost::bind(&Impl::startLoading, this));
196 }
197
198
199 void CacheItem::Impl::startLoading()
200 {
201         if (status_ != WaitingToLoad)
202                 reset();
203
204         convertToDisplayFormat();
205 }
206
207
208 void CacheItem::Impl::reset()
209 {
210         zipped_ = false;
211         if (!unzipped_filename_.empty())
212                 lyx::unlink(unzipped_filename_);
213         unzipped_filename_.clear();
214
215         if (remove_loaded_file_ && !file_to_load_.empty())
216                 lyx::unlink(file_to_load_);
217         remove_loaded_file_ = false;
218         file_to_load_.clear();
219
220         if (image_.get())
221                 image_.reset();
222
223         status_ = WaitingToLoad;
224
225         if (cl_.connected())
226                 cl_.disconnect();
227
228         if (cc_.connected())
229                 cc_.disconnect();
230
231         if (converter_.get())
232                 converter_.reset();
233 }
234
235
236 void CacheItem::Impl::setStatus(ImageStatus new_status)
237 {
238         if (status_ == new_status)
239                 return;
240
241         status_ = new_status;
242         statusChanged();
243 }
244
245
246 void CacheItem::Impl::imageConverted(bool success)
247 {
248         string const text = success ? "succeeded" : "failed";
249         lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
250
251         file_to_load_ = converter_.get() ?
252                 converter_->convertedFile() : string();
253         converter_.reset();
254         cc_.disconnect();
255
256         success = !file_to_load_.empty() && IsFileReadable(file_to_load_);
257         lyxerr[Debug::GRAPHICS] << "Unable to find converted file!" << endl;
258
259         if (!success) {
260                 setStatus(ErrorConverting);
261
262                 if (zipped_)
263                         lyx::unlink(unzipped_filename_);
264
265                 return;
266         }
267
268         loadImage();
269 }
270
271
272 // This function gets called from the callback after the image has been
273 // converted successfully.
274 void CacheItem::Impl::loadImage()
275 {
276         setStatus(Loading);
277         lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
278
279         image_ = Image::newImage();
280
281         cl_.disconnect();
282         cl_ = image_->finishedLoading.connect(
283                 boost::bind(&Impl::imageLoaded, this, _1));
284         image_->load(file_to_load_);
285 }
286
287
288 void CacheItem::Impl::imageLoaded(bool success)
289 {
290         string const text = success ? "succeeded" : "failed";
291         lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
292
293         // Clean up after loading.
294         if (zipped_)
295                 lyx::unlink(unzipped_filename_);
296
297         if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
298                 lyx::unlink(file_to_load_);
299
300         cl_.disconnect();
301
302         if (!success) {
303                 setStatus(ErrorLoading);
304                 return;
305         }
306
307         // Inform the outside world.
308         setStatus(Loaded);
309 }
310
311
312 } // namespace grfx
313
314
315 namespace {
316
317 string const findTargetFormat(string const & from)
318 {
319         typedef grfx::Image::FormatList FormatList;
320         FormatList const formats = grfx::Image::loadableFormats();
321
322         // There must be a format to load from.
323         lyx::Assert(!formats.empty());
324
325         // First ascertain if we can load directly with no conversion
326         FormatList::const_iterator it1 = formats.begin();
327         FormatList::const_iterator end = formats.end();
328         for (; it1 != end; ++it1) {
329                 if (from == *it1)
330                         return *it1;
331         }
332
333         // So, we have to convert to a loadable format. Can we?
334         FormatList::const_iterator it2 = formats.begin();
335         for (; it2 != end; ++it2) {
336                 if (grfx::Converter::isReachable(from, *it2))
337                         return *it2;
338         }
339
340         // Failed! so we have to try to convert it to XPM format
341         // with the standard converter
342         return string("xpm");
343 }
344
345 } // anon namespace
346
347
348 namespace grfx {
349
350 void CacheItem::Impl::convertToDisplayFormat()
351 {
352         setStatus(Converting);
353
354         // First, check that the file exists!
355         if (!IsFileReadable(filename_)) {
356                 if (status_ != ErrorNoFile) {
357                         setStatus(ErrorNoFile);
358                         lyxerr[Debug::GRAPHICS]
359                                 << "\tThe file is not readable" << endl;
360                 }
361                 return;
362         }
363
364         // Make a local copy in case we unzip it
365         string const filename = zippedFile(filename_) ?
366                 unzipFile(filename_) : filename_;
367         string const displayed_filename = MakeDisplayPath(filename_);
368         lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
369                 << "\tAttempting to convert image file: " << filename
370                 << "\n\twith displayed filename: " << displayed_filename
371                 << endl;
372
373         string from = getExtFromContents(filename);
374         lyxerr[Debug::GRAPHICS]
375                 << "\n\tThe file contains " << from << " format data." << endl;
376         string const to = findTargetFormat(from);
377
378         if (from == to) {
379                 // No conversion needed!
380                 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
381                 file_to_load_ = filename;
382                 loadImage();
383                 return;
384         }
385
386         lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
387         // Take only the filename part of the file, without path or extension.
388         string const temp = ChangeExtension(OnlyFilename(filename), string());
389
390         // Add some stuff to create a uniquely named temporary file.
391         // This file is deleted in loadImage after it is loaded into memory.
392         string const to_file_base = lyx::tempName(string(), temp);
393         remove_loaded_file_ = true;
394
395         // Remove the temp file, we only want the name...
396         lyx::unlink(to_file_base);
397
398         // Connect a signal to this->imageConverted and pass this signal to
399         // the graphics converter so that we can load the modified file
400         // on completion of the conversion process.
401         converter_.reset(new Converter(filename, to_file_base, from, to));
402         converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
403         converter_->startConversion();
404 }
405
406 } // namespace grfx