Strip mode settings are now fully customizable and integrated
[qcomicbook-oblomov] / src / imgloader.cpp
1 /*
2  * This file is a part of QComicBook.
3  *
4  * Copyright (C) 2005-2006 Pawel Stolowski <yogin@linux.bydg.org>
5  *
6  * QComicBook is free software; you can redestribute it and/or modify it
7  * under terms of GNU General Public License by Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY. See GPL for more details.
11  */
12
13 #include "imgloader.h"
14 #include "imgdirsink.h"
15 #include <qimage.h>
16
17 using namespace QComicBook;
18
19 ImgLoaderThread::ImgLoaderThread(): QThread(), prio(QThread::LowPriority), sink(NULL), stopped(false)
20 {
21 }
22
23 ImgLoaderThread::~ImgLoaderThread()
24 {
25 }
26
27 void ImgLoaderThread::setPriority(QThread::Priority p)
28 {
29         mtx.lock();
30         prio = p;
31         mtx.unlock();
32 }
33
34 void ImgLoaderThread::setSink(ImgDirSink *sink)
35 {
36         mtx.lock();
37         this->sink = sink;
38         mtx.unlock();
39 }
40
41 void ImgLoaderThread::request(int page)
42 {
43         mtx.lock();
44         if (requests.contains(page))
45         {
46                 mtx.unlock();
47                 return;
48         }
49         requests.append(page);
50         if (!running() && !stopped)
51         {
52                 mtx.unlock();
53                 start(prio);
54         }
55         else
56                 mtx.unlock();
57 }
58
59 void ImgLoaderThread::request(int first, int n)
60 {
61         mtx.lock();
62         const int last = first + n;
63         for (int i=first; i<last; i++)
64                 if (requests.contains(i) == 0)
65                         requests.append(i);
66         if (!running() && !stopped)
67         {
68                 mtx.unlock();
69                 start(prio);
70         }
71         else
72                 mtx.unlock();
73 }
74
75 void ImgLoaderThread::stop()
76 {
77         mtx.lock();
78         stopped = true;
79         mtx.unlock();
80 }
81
82 void ImgLoaderThread::run()
83 {
84         for (;;)
85         {
86                 mtx.lock();
87                 if (stopped || requests.empty())
88                 {
89                         mtx.unlock();
90                         break;
91                 }
92                 
93                 const int n = requests.first();
94                 requests.pop_front();
95                 if (sink)
96                 {
97                         int result;
98                         sink->getImage(n, result);
99                 }
100                 mtx.unlock();
101         }
102 }
103