]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.C
Whitespace.
[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 LoaderQueue & LoaderQueue::get()
27 {
28         static LoaderQueue singleton;
29         return singleton;
30 }
31
32
33 void LoaderQueue::loadNext()
34 {
35         emptyBucket();
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                 if(cache_queue_.front()->status() == WaitingToLoad)
42                         cache_queue_.front()->startLoading();
43                 cache_set_.erase(cache_queue_.front());
44                 cache_queue_.pop_front();
45         }
46         if (cache_queue_.size() || bucket_.size()) {
47                 startLoader();
48         } else {
49                 stopLoader();
50         }
51 }
52
53
54 void LoaderQueue::setPriority(int numimages , int millisecs)
55 {
56         s_numimages_ = numimages;
57         s_millisecs_ = millisecs;
58         lyxerr[Debug::GRAPHICS] << "LoaderQueue:  priority set to "
59                                 << s_numimages_ << " images at a time, "
60                                 << s_millisecs_ << " milliseconds between calls"
61                                 << endl;
62 }
63
64
65 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
66                              running_(false)
67 {
68         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
69 }
70
71
72 void LoaderQueue::emptyBucket()
73 {
74         lyxerr[Debug::GRAPHICS] << "LoaderQueue: emptying bucket"
75                                 << endl;
76         while (! bucket_.empty()) {
77                 addToQueue(bucket_.front());
78                 bucket_.pop();
79         }
80 }
81
82
83 void LoaderQueue::startLoader()
84 {
85         lyxerr[Debug::GRAPHICS] << "LoaderQueue: waking up" << endl;
86         running_ = true ;
87         timer.setTimeout(s_millisecs_);
88         timer.start();
89 }
90
91
92 void LoaderQueue::stopLoader()
93 {
94         timer.stop();
95         running_ = false ;
96         lyxerr[Debug::GRAPHICS] << "LoaderQueue: I'm going to sleep" << endl;
97 }
98
99
100 bool LoaderQueue::running() const
101 {
102         return running_ ;
103 }
104
105
106 void LoaderQueue::touch(Cache::ItemPtr const & item)
107 {
108         if (! running_)
109                 startLoader();
110         bucket_.push(item);
111 }
112
113
114 void LoaderQueue::addToQueue(Cache::ItemPtr const & item)
115 {
116         if (! cache_set_.insert(item).second) {
117                 list<Cache::ItemPtr>::iterator
118                         it = cache_queue_.begin();
119                 list<Cache::ItemPtr>::iterator
120                         end = cache_queue_.end();
121
122                 it = std::find(it, end, item);
123                 if (it != end)
124                         cache_queue_.erase(it);
125         }
126         cache_queue_.push_front(item);
127 }
128
129
130 } // namespace grfx