forked from Panorama/panorama
/
handleexecutionqueue.cpp
77 lines (69 loc) · 1.44 KB
/
handleexecutionqueue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "handleexecutionqueue.h"
HandleExecutionQueue::HandleExecutionQueue(QObject* parent) :
QObject(parent), handles(), maxExecuting(DEFAULT_MAX_EXECUTING),
currentlyExecuting(0)
{
}
bool HandleExecutionQueue::enqueue(QPndman::Handle* handle)
{
connect(handle, SIGNAL(done()), this, SLOT(handleReady()));
connect(handle, SIGNAL(cancelled()), this, SLOT(handleReady()));
connect(handle, SIGNAL(error(QString)), this, SLOT(handleReady()));
if(currentlyExecuting < maxExecuting)
{
execute(handle);
return false;
}
else
{
handles.enqueue(handle);
return true;
}
}
int HandleExecutionQueue::getMaxExecuting() const
{
return maxExecuting;
}
void HandleExecutionQueue::setMaxExecuting(int value)
{
if(maxExecuting != value)
{
maxExecuting = value;
emit maxExecutingChanged();
}
}
int HandleExecutionQueue::getCurrentlyExecuting() const
{
return currentlyExecuting;
}
void HandleExecutionQueue::handleReady()
{
QPndman::Handle* queuedHandle = 0;
foreach(QPndman::Handle* handle, handles)
{
if(handle->getCancelled())
{
queuedHandle = handle;
break;
}
}
if(queuedHandle == 0)
{
--currentlyExecuting;
}
else
{
handles.removeOne(queuedHandle);
}
if(currentlyExecuting < maxExecuting && !handles.empty())
{
execute(handles.dequeue());
}
}
void HandleExecutionQueue::execute(QPndman::Handle* handle)
{
if(handle->execute())
{
++currentlyExecuting;
}
}