]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.C
Alfredo's Loader Queue.
[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
24 LoaderQueue & LoaderQueue::get()
25 {
26         static LoaderQueue singleton;
27         return singleton;
28 }
29
30
31 void LoaderQueue::loadNext() 
32 {
33         emptyBucket();
34         lyxerr[Debug::GRAPHICS] << "LoaderQueue: "
35                                 << cache_queue_.size()
36                                 << " items in the queue" << endl; 
37         int counter = 10;
38         while (cache_queue_.size() && counter--) {
39                 if(cache_queue_.front()->status() == WaitingToLoad)
40                         cache_queue_.front()->startLoading();
41                 cache_set_.erase(cache_queue_.front());
42                 cache_queue_.pop_front();
43         }
44         if (cache_queue_.size() || bucket_.size()) {
45                 startLoader();
46         } else {
47                 stopLoader();
48         }
49 }
50
51
52 LoaderQueue::LoaderQueue() : timer(100, Timeout::ONETIME), 
53                              running_(false)
54 {
55         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
56 }
57
58         
59 void LoaderQueue::emptyBucket()
60 {
61         lyxerr[Debug::GRAPHICS] << "LoaderQueue: emptying bucket" 
62                                 << endl;
63         while (! bucket_.empty()) {
64                 addToQueue(bucket_.front());
65                 bucket_.pop();
66         }
67 }
68
69
70 void LoaderQueue::startLoader()
71 {
72         lyxerr[Debug::GRAPHICS] << "LoaderQueue: waking up" << endl;
73         running_ = true ;
74         timer.start();
75 }
76
77
78 void LoaderQueue::stopLoader()
79 {
80         timer.stop();
81         running_ = false ;
82         lyxerr[Debug::GRAPHICS] << "LoaderQueue: I'm going to sleep" << endl;
83 }
84
85
86 bool LoaderQueue::running() const
87 {
88         return running_ ;
89 }
90
91
92 void LoaderQueue::touch(Cache::ItemPtr const & item)
93 {
94         if (! running_)
95                 startLoader();
96         bucket_.push(item);
97 }
98
99
100 void LoaderQueue::addToQueue(Cache::ItemPtr const & item)
101 {
102         if (! cache_set_.insert(item).second) {
103                 list<Cache::ItemPtr>::iterator 
104                         it = cache_queue_.begin();
105                 list<Cache::ItemPtr>::iterator 
106                         end = cache_queue_.end();
107                 
108                 it = std::find(it, end, item);
109                 if (it != end)
110                         cache_queue_.erase(it);
111         } 
112         cache_queue_.push_front(item);
113 }
114
115
116 } // namespace grfx