scramble email addresses
[lyx.git] / src / graphics / LoaderQueue.cpp
blob6038dbe4b933d676704f2b3374d87006979a9eef
1 /**
2 * \file LoaderQueue.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "LoaderQueue.h"
14 #include "GraphicsCacheItem.h"
16 #include "debug.h"
18 #include <boost/bind.hpp>
20 using std::endl;
21 using std::list;
23 namespace lyx {
24 namespace graphics {
26 int LoaderQueue::s_numimages_ = 5;
27 int LoaderQueue::s_millisecs_ = 500;
30 LoaderQueue & LoaderQueue::get()
32 static LoaderQueue singleton;
33 return singleton;
37 void LoaderQueue::loadNext()
39 LYXERR(Debug::GRAPHICS) << "LoaderQueue: "
40 << cache_queue_.size()
41 << " items in the queue" << endl;
42 int counter = s_numimages_;
43 while (cache_queue_.size() && counter--) {
44 Cache::ItemPtr ptr = cache_queue_.front();
45 cache_set_.erase(ptr);
46 cache_queue_.pop_front();
47 if (ptr->status() == WaitingToLoad)
48 ptr->startLoading();
50 if (cache_queue_.size()) {
51 startLoader();
52 } else {
53 stopLoader();
58 void LoaderQueue::setPriority(int numimages , int millisecs)
60 s_numimages_ = numimages;
61 s_millisecs_ = millisecs;
62 LYXERR(Debug::GRAPHICS) << "LoaderQueue: priority set to "
63 << s_numimages_ << " images at a time, "
64 << s_millisecs_ << " milliseconds between calls"
65 << endl;
69 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
70 running_(false)
72 timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
76 void LoaderQueue::startLoader()
78 LYXERR(Debug::GRAPHICS) << "LoaderQueue: waking up" << endl;
79 running_ = true ;
80 timer.setTimeout(s_millisecs_);
81 timer.start();
85 void LoaderQueue::stopLoader()
87 timer.stop();
88 running_ = false ;
89 LYXERR(Debug::GRAPHICS) << "LoaderQueue: I'm going to sleep" << endl;
93 bool LoaderQueue::running() const
95 return running_ ;
99 void LoaderQueue::touch(Cache::ItemPtr const & item)
101 if (! cache_set_.insert(item).second) {
102 list<Cache::ItemPtr>::iterator
103 it = cache_queue_.begin();
104 list<Cache::ItemPtr>::iterator
105 end = cache_queue_.end();
107 it = std::find(it, end, item);
108 if (it != end)
109 cache_queue_.erase(it);
111 cache_queue_.push_front(item);
112 if (!running_)
113 startLoader();
117 } // namespace graphics
118 } // namespace lyx