]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.C
Store an InsetBase & in MailInset.
[lyx.git] / src / graphics / LoaderQueue.C
1 /**
2  * \file LoaderQueue.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include "LoaderQueue.h"
12
13 #include "debug.h"
14
15 #include <boost/bind.hpp>
16
17 using std::endl;
18 using std::list;
19
20
21 namespace grfx {
22
23 int LoaderQueue::s_numimages_ = 5;
24 int LoaderQueue::s_millisecs_ = 500;
25
26
27 LoaderQueue & LoaderQueue::get()
28 {
29         static LoaderQueue singleton;
30         return singleton;
31 }
32
33
34 void LoaderQueue::loadNext()
35 {
36         lyxerr[Debug::GRAPHICS] << "LoaderQueue: "
37                                 << cache_queue_.size()
38                                 << " items in the queue" << endl;
39         int counter = s_numimages_;
40         while (cache_queue_.size() && counter--) {
41                 Cache::ItemPtr ptr = cache_queue_.front();
42                 cache_set_.erase(ptr);
43                 cache_queue_.pop_front();
44                 if (ptr->status() == WaitingToLoad)
45                         ptr->startLoading();
46         }
47         if (cache_queue_.size()) {
48                 startLoader();
49         } else {
50                 stopLoader();
51         }
52 }
53
54
55 void LoaderQueue::setPriority(int numimages , int millisecs)
56 {
57         s_numimages_ = numimages;
58         s_millisecs_ = millisecs;
59         lyxerr[Debug::GRAPHICS] << "LoaderQueue:  priority set to "
60                                 << s_numimages_ << " images at a time, "
61                                 << s_millisecs_ << " milliseconds between calls"
62                                 << endl;
63 }
64
65
66 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
67                              running_(false)
68 {
69         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
70 }
71
72
73 void LoaderQueue::startLoader()
74 {
75         lyxerr[Debug::GRAPHICS] << "LoaderQueue: waking up" << endl;
76         running_ = true ;
77         timer.setTimeout(s_millisecs_);
78         timer.start();
79 }
80
81
82 void LoaderQueue::stopLoader()
83 {
84         timer.stop();
85         running_ = false ;
86         lyxerr[Debug::GRAPHICS] << "LoaderQueue: I'm going to sleep" << endl;
87 }
88
89
90 bool LoaderQueue::running() const
91 {
92         return running_ ;
93 }
94
95
96 void LoaderQueue::touch(Cache::ItemPtr const & item)
97 {
98         if (! cache_set_.insert(item).second) {
99                 list<Cache::ItemPtr>::iterator
100                         it = cache_queue_.begin();
101                 list<Cache::ItemPtr>::iterator
102                         end = cache_queue_.end();
103
104                 it = std::find(it, end, item);
105                 if (it != end)
106                         cache_queue_.erase(it);
107         }
108         cache_queue_.push_front(item);
109         if (!running_)
110                 startLoader();
111 }
112
113
114 } // namespace grfx