]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.cpp
Avoid duplicate generation of the same preview
[lyx.git] / src / graphics / GraphicsCacheItem.cpp
1 /**
2  * \file GraphicsCacheItem.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Herbert Voß
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GraphicsCacheItem.h"
16
17 #include "Buffer.h"
18 #include "GraphicsCache.h"
19 #include "GraphicsConverter.h"
20 #include "GraphicsImage.h"
21
22 #include "ConverterCache.h"
23 #include "Format.h"
24
25 #include "support/debug.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/FileMonitor.h"
29 #include "support/lassert.h"
30 #include "support/unique_ptr.h"
31
32 #include "support/TempFile.h"
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 namespace graphics {
40
41 class CacheItem::Impl {
42 public:
43
44         ///
45         Impl(FileName const & file, FileName const & doc_file);
46
47         void startMonitor();
48
49         /**
50          *  If no file conversion is needed, then tryDisplayFormat() calls
51          *  loadImage() directly.
52          * \return true if a conversion is necessary and no error occurred.
53          */
54         bool tryDisplayFormat(FileName & filename, string & from);
55
56         /** Start the image conversion process, checking first that it is
57          *  necessary. If it is necessary, then a conversion task is started.
58          *  CacheItem asumes that the conversion is asynchronous and so
59          *  passes a Signal to the converting routine. When the conversion
60          *  is finished, this Signal is emitted, returning the converted
61          *  file to this->imageConverted.
62          *
63          *  convertToDisplayFormat() will set the loading status flag as
64          *  approriate through calls to setStatus().
65          */
66         void convertToDisplayFormat();
67
68         /** Load the image into memory. This is called either from
69          *  convertToDisplayFormat() direct or from imageConverted().
70          */
71         bool loadImage();
72
73         /** Get a notification when the image conversion is done.
74          *  Connected to a signal on_finish_ which is passed to
75          *  Converter::convert.
76          */
77         void imageConverted(bool);
78
79         /** Sets the status of the loading process. Also notifies
80          *  listeners that the status has changed.
81          */
82         void setStatus(ImageStatus new_status);
83
84         /** Can be invoked directly by the user, but is also connected to the
85          *  FileMonitor and so is invoked when the file is changed
86          *  (if monitoring is taking place).
87          */
88         void startLoading();
89
90         /** If we are asked to load the file for a second or further time,
91          *  (because the file has changed), then we'll have to first reset
92          *  many of the variables below.
93          */
94         void reset();
95
96         /// The filename we refer too.
97         FileName const filename_;
98         /// The document filename this graphic item belongs to
99         FileName const doc_file_;
100         ///
101         ActiveFileMonitorPtr monitor_;
102
103         /// Is the file compressed?
104         bool zipped_;
105         /// If so, store the uncompressed file in this temporary file.
106         FileName unzipped_filename_;
107         /// The target format
108         string to_;
109         /// What file are we trying to load?
110         FileName file_to_load_;
111         /** Should we delete the file after loading? True if the file is
112          *  the result of a conversion process.
113          */
114         bool remove_loaded_file_;
115
116         /// The image and its loading status.
117         std::shared_ptr<Image> image_;
118         ///
119         ImageStatus status_;
120
121         /// This signal is emitted when the image loading status changes.
122         signals2::signal<void()> statusChanged;
123
124         ///
125         unique_ptr<Converter> converter_;
126 };
127
128
129 CacheItem::CacheItem(FileName const & file, FileName const & doc_file)
130   : pimpl_(new Impl(file,doc_file))
131 {}
132
133
134 CacheItem::~CacheItem()
135 {
136         delete pimpl_;
137 }
138
139
140 FileName const & CacheItem::filename() const
141 {
142         return pimpl_->filename_;
143 }
144
145
146 bool CacheItem::tryDisplayFormat() const
147 {
148         if (pimpl_->status_ != WaitingToLoad)
149                 pimpl_->reset();
150         FileName filename;
151         string from;
152         bool const conversion_needed = pimpl_->tryDisplayFormat(filename, from);
153         bool const success = status() == Loaded && !conversion_needed;
154         if (!success)
155                 pimpl_->reset();
156         return success;
157 }
158
159
160 void CacheItem::startLoading() const
161 {
162         pimpl_->startLoading();
163 }
164
165
166 void CacheItem::startMonitoring() const
167 {
168         pimpl_->startMonitor();
169 }
170
171
172 bool CacheItem::monitoring() const
173 {
174         return (bool)pimpl_->monitor_;
175 }
176
177
178 void CacheItem::checkModifiedAsync() const
179 {
180         if (!pimpl_->monitor_)
181                 return;
182         pimpl_->monitor_->checkModifiedAsync();
183 }
184
185
186 Image const * CacheItem::image() const
187 {
188         return pimpl_->image_.get();
189 }
190
191
192 ImageStatus CacheItem::status() const
193 {
194         return pimpl_->status_;
195 }
196
197
198 signals2::connection CacheItem::connect(slot_type const & slot) const
199 {
200         return pimpl_->statusChanged.connect(slot);
201 }
202
203
204 //------------------------------
205 // Implementation details follow
206 //------------------------------
207
208
209 CacheItem::Impl::Impl(FileName const & file, FileName const & doc_file)
210         : filename_(file), doc_file_(doc_file),
211           zipped_(false),
212           remove_loaded_file_(false),
213           status_(WaitingToLoad)
214 {}
215
216
217 void CacheItem::Impl::startMonitor()
218 {
219         if (monitor_)
220                 return;
221         monitor_ = FileSystemWatcher::activeMonitor(filename_);
222         // Disconnected at the same time as this is destroyed.
223         monitor_->connect([=](bool /* exists */){ startLoading(); });
224 }
225
226
227 void CacheItem::Impl::startLoading()
228 {
229         if (status_ != WaitingToLoad)
230                 reset();
231
232         convertToDisplayFormat();
233 }
234
235
236 void CacheItem::Impl::reset()
237 {
238         zipped_ = false;
239         if (!unzipped_filename_.empty())
240                 unzipped_filename_.removeFile();
241         unzipped_filename_.erase();
242
243         if (remove_loaded_file_ && !file_to_load_.empty())
244                 file_to_load_.removeFile();
245         remove_loaded_file_ = false;
246         file_to_load_.erase();
247         to_.erase();
248
249         if (image_)
250                 image_.reset();
251
252         status_ = WaitingToLoad;
253
254         if (converter_)
255                 converter_.reset();
256 }
257
258
259 void CacheItem::Impl::setStatus(ImageStatus new_status)
260 {
261         if (status_ == new_status)
262                 return;
263
264         status_ = new_status;
265         statusChanged();
266 }
267
268
269 void CacheItem::Impl::imageConverted(bool success)
270 {
271         string const text = success ? "succeeded" : "failed";
272         LYXERR(Debug::GRAPHICS, "Image conversion " << text << '.');
273
274         file_to_load_ = converter_ ? FileName(converter_->convertedFile())
275                                        : FileName();
276         converter_.reset();
277
278         success = !file_to_load_.empty() && file_to_load_.isReadableFile();
279
280         if (!success) {
281                 LYXERR(Debug::GRAPHICS, "Unable to find converted file!");
282                 setStatus(ErrorConverting);
283
284                 if (zipped_)
285                         unzipped_filename_.removeFile();
286
287                 return;
288         }
289
290         // Add the converted file to the file cache
291         ConverterCache::get().add(filename_, to_, file_to_load_);
292
293         setStatus(loadImage() ? Loaded : ErrorLoading);
294 }
295
296
297 // This function gets called from the callback after the image has been
298 // converted successfully.
299 bool CacheItem::Impl::loadImage()
300 {
301         LYXERR(Debug::GRAPHICS, "Loading image.");
302
303         image_.reset(newImage());
304
305         bool success = image_->load(file_to_load_);
306         string const text = success ? "succeeded" : "failed";
307         LYXERR(Debug::GRAPHICS, "Image loading " << text << '.');
308
309         // Clean up after loading.
310         if (zipped_)
311                 unzipped_filename_.removeFile();
312
313         if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
314                 file_to_load_.removeFile();
315
316         return success;
317 }
318
319
320 typedef vector<string> FormatList;
321
322 static string const findTargetFormat(FormatList const & format_list, string const & from)
323 {
324          // There must be a format to load from.
325         LASSERT(!theFormats().empty(), return string());
326
327         // Use the standard converter if we don't know the format to load
328         // from.
329         if (from.empty())
330                 return string("ppm");
331
332         // First ascertain if we can load directly with no conversion
333         FormatList::const_iterator it  = format_list.begin();
334         FormatList::const_iterator end = format_list.end();
335         for (; it != end; ++it) {
336                 if (from == *it)
337                         return *it;
338         }
339
340         // So, we have to convert to a loadable format. Can we?
341         it = format_list.begin();
342         for (; it != end; ++it) {
343                 if (lyx::graphics::Converter::isReachable(from, *it))
344                         return *it;
345                 else
346                         LYXERR(Debug::GRAPHICS, "Unable to convert from " << from
347                                 << " to " << *it);
348         }
349
350         // Failed! so we have to try to convert it to PPM format
351         // with the standard converter
352         return string("ppm");
353 }
354
355
356 bool CacheItem::Impl::tryDisplayFormat(FileName & filename, string & from)
357 {
358         // First, check that the file exists!
359         filename_.refresh();
360         if (!filename_.isReadableFile()) {
361                 if (status_ != ErrorNoFile) {
362                         status_ = ErrorNoFile;
363                         LYXERR(Debug::GRAPHICS, "\tThe file is not readable");
364                 }
365                 return false;
366         }
367
368         zipped_ = theFormats().isZippedFile(filename_);
369         if (zipped_) {
370                 string tempname = unzippedFileName(filename_.toFilesystemEncoding());
371                 string const ext = getExtension(tempname);
372                 tempname = changeExtension(tempname, "") + "-XXXXXX";
373                 if (!ext.empty())
374                         tempname = addExtension(tempname, ext);
375                 TempFile tempfile(tempname);
376                 tempfile.setAutoRemove(false);
377                 unzipped_filename_ = tempfile.name();
378                 if (unzipped_filename_.empty()) {
379                         status_ = ErrorConverting;
380                         LYXERR(Debug::GRAPHICS, "\tCould not create temporary file.");
381                         return false;
382                 }
383                 filename = unzipFile(filename_, unzipped_filename_.toFilesystemEncoding());
384         } else {
385                 filename = filename_;
386         }
387
388         docstring const displayed_filename = makeDisplayPath(filename_.absFileName());
389         LYXERR(Debug::GRAPHICS, "[CacheItem::Impl::convertToDisplayFormat]\n"
390                 << "\tAttempting to convert image file: " << filename
391                 << "\n\twith displayed filename: " << to_utf8(displayed_filename));
392
393         from = theFormats().getFormatFromFile(filename);
394         if (from.empty()) {
395                 status_ = ErrorConverting;
396                 LYXERR(Debug::GRAPHICS, "\tCould not determine file format.");
397         }
398         LYXERR(Debug::GRAPHICS, "\n\tThe file contains " << from << " format data.");
399         to_ = findTargetFormat(Cache::get().loadableFormats(), from);
400
401         if (from == to_) {
402                 // No conversion needed!
403                 LYXERR(Debug::GRAPHICS, "\tNo conversion needed (from == to)!");
404                 file_to_load_ = filename;
405                 status_ = loadImage() ? Loaded : ErrorLoading;
406                 return false;
407         }
408
409         if (ConverterCache::get().inCache(filename, to_)) {
410                 LYXERR(Debug::GRAPHICS, "\tNo conversion needed (file in file cache)!");
411                 file_to_load_ = ConverterCache::get().cacheName(filename, to_);
412                 status_ = loadImage() ? Loaded : ErrorLoading;
413                 return false;
414         }
415         return true;
416 }
417
418
419 void CacheItem::Impl::convertToDisplayFormat()
420 {
421         LYXERR(Debug::GRAPHICS, "\tConverting it to " << to_ << " format.");
422
423         // Make a local copy in case we unzip it
424         FileName filename;
425         string from;
426         if (!tryDisplayFormat(filename, from)) {
427                 // The image status has changed, tell it to the outside world.
428                 statusChanged();
429                 return;
430         }
431
432         // We will need a conversion, tell it to the outside world.
433         setStatus(Converting);
434
435         // Add some stuff to create a uniquely named temporary file.
436         // This file is deleted in loadImage after it is loaded into memory.
437         TempFile tempfile("CacheItem");
438         tempfile.setAutoRemove(false);
439         FileName const to_file_base = tempfile.name();
440         remove_loaded_file_ = true;
441
442         // Connect a signal to this->imageConverted and pass this signal to
443         // the graphics converter so that we can load the modified file
444         // on completion of the conversion process.
445         converter_ = make_unique<Converter>(doc_file_, filename,
446                                             to_file_base.absFileName(),
447                                             from, to_);
448         // Connection is closed at the same time as *this is destroyed.
449         converter_->connect([this](bool success){
450                         imageConverted(success);
451                 });
452         converter_->startConversion();
453 }
454
455 } // namespace graphics
456 } // namespace lyx