1 // Aseprite
2 // Copyright (C) 2001-2015  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_THUMBNAIL_GENERATOR_H_INCLUDED
8 #define APP_THUMBNAIL_GENERATOR_H_INCLUDED
9 #pragma once
10 
11 #include "base/mutex.h"
12 #include "base/unique_ptr.h"
13 
14 #include <vector>
15 
16 namespace base {
17   class thread;
18 }
19 
20 namespace app {
21   class IFileItem;
22 
23   class ThumbnailGenerator {
24   public:
25     enum WorkerStatus { WithoutWorker, WorkingOnThumbnail, ThumbnailIsDone };
26 
27     static ThumbnailGenerator* instance();
28 
29     // Generate a thumbnail for the given file-item.  It must be called
30     // from the GUI thread.
31     void addWorkerToGenerateThumbnail(IFileItem* fileitem);
32 
33     // Returns the status of the worker that is generating the thumbnail
34     // for the given file.
35     WorkerStatus getWorkerStatus(IFileItem* fileitem, double& progress);
36 
37     // Checks the status of workers. If there are workers that already
38     // done its job, we've to destroy them. This function must be called
39     // from the GUI thread (because a thread is joint to it).
40     // Returns true if there are workers generating thumbnails.
41     bool checkWorkers();
42 
43     // Stops all workers generating thumbnails. This is an non-blocking
44     // operation. The cancelation of all workers is done in a background
45     // thread.
46     void stopAllWorkers();
47 
48   private:
49     void stopAllWorkersBackground();
50 
51     class Worker;
52     typedef std::vector<Worker*> WorkerList;
53 
54     WorkerList m_workers;
55     base::mutex m_workersAccess;
56     base::UniquePtr<base::thread> m_stopThread;
57   };
58 } // namespace app
59 
60 #endif
61