LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
queuemanager.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "queuemanager.h"
10#include <QTimer>
11
12namespace LC
13{
14namespace Util
15{
16 QueueManager::QueueManager (int timeout, QObject *parent)
17 : QObject (parent)
18 , Timeout_ (timeout)
19 , ReqTimer_ (new QTimer (this))
20 , Paused_ (false)
21 {
22 ReqTimer_->setSingleShot (true);
23 connect (ReqTimer_,
24 SIGNAL (timeout ()),
25 this,
26 SLOT (exec ()));
27 }
28
29 void QueueManager::Schedule (std::function<void ()> f, QObject *dep, QueuePriority prio)
30 {
31 const auto& now = QDateTime::currentDateTime ();
32
33 if (prio == QueuePriority::High)
34 Queue_.prepend ({ f, dep ? OptionalTracker_t { dep } : OptionalTracker_t () });
35 else
36 Queue_.append ({ f, dep ? OptionalTracker_t { dep } : OptionalTracker_t () });
37
38 const auto diff = LastRequest_.msecsTo (now);
39 if (diff >= Timeout_)
40 exec ();
41 else if (Queue_.size () == 1)
42 ReqTimer_->start (Timeout_ - diff);
43 }
44
46 {
47 Queue_.clear ();
48 }
49
51 {
52 Paused_ = true;
53 ReqTimer_->stop ();
54 }
55
57 {
58 return Paused_;
59 }
60
62 {
63 Paused_ = false;
64 ReqTimer_->start (Timeout_);
65 }
66
67 void QueueManager::exec ()
68 {
69 if (Queue_.isEmpty ())
70 return;
71
72 if (Paused_)
73 return;
74
75 const auto& pair = Queue_.takeFirst ();
76 if (pair.second && !*pair.second)
77 {
78 exec ();
79 return;
80 }
81
82 pair.first ();
83 LastRequest_ = QDateTime::currentDateTime ();
84
85 if (!Queue_.isEmpty ())
86 ReqTimer_->start (Timeout_);
87 }
88}
89}
void Resume()
Continues the queue rotation.
bool IsPaused() const
Checks if the queue is paused.
void Clear()
Clears the queue.
void Pause()
Pauses the queue rotation.
void Schedule(std::function< void()> functor, QObject *dependent=nullptr, QueuePriority prio=QueuePriority::Normal)
Adds the given functor.
QueueManager(int timeout, QObject *parent=nullptr)
Creates a queue manager with the given timeout.
QueuePriority
The priority of the action in the queue.
Definition: queuemanager.h:28
@ High
Higher priority.
Definition: constants.h:15