LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "queuemanager.h"
31 #include <QTimer>
32 
33 namespace LC
34 {
35 namespace Util
36 {
37  QueueManager::QueueManager (int timeout, QObject *parent)
38  : QObject (parent)
39  , Timeout_ (timeout)
40  , ReqTimer_ (new QTimer (this))
41  , Paused_ (false)
42  {
43  ReqTimer_->setSingleShot (true);
44  connect (ReqTimer_,
45  SIGNAL (timeout ()),
46  this,
47  SLOT (exec ()));
48  }
49 
50  void QueueManager::Schedule (std::function<void ()> f, QObject *dep, QueuePriority prio)
51  {
52  const auto& now = QDateTime::currentDateTime ();
53 
54  if (prio == QueuePriority::High)
55  Queue_.prepend ({ f, dep ? OptionalTracker_t { dep } : OptionalTracker_t () });
56  else
57  Queue_.append ({ f, dep ? OptionalTracker_t { dep } : OptionalTracker_t () });
58 
59  const auto diff = LastRequest_.msecsTo (now);
60  if (diff >= Timeout_)
61  exec ();
62  else if (Queue_.size () == 1)
63  ReqTimer_->start (Timeout_ - diff);
64  }
65 
67  {
68  Queue_.clear ();
69  }
70 
72  {
73  Paused_ = true;
74  ReqTimer_->stop ();
75  }
76 
77  bool QueueManager::IsPaused () const
78  {
79  return Paused_;
80  }
81 
83  {
84  Paused_ = false;
85  ReqTimer_->start (Timeout_);
86  }
87 
88  void QueueManager::exec ()
89  {
90  if (Queue_.isEmpty ())
91  return;
92 
93  if (Paused_)
94  return;
95 
96  const auto& pair = Queue_.takeFirst ();
97  if (pair.second && !*pair.second)
98  {
99  exec ();
100  return;
101  }
102 
103  pair.first ();
104  LastRequest_ = QDateTime::currentDateTime ();
105 
106  if (!Queue_.isEmpty ())
107  ReqTimer_->start (Timeout_);
108  }
109 }
110 }
QueuePriority
The priority of the action in the queue.
Definition: queuemanager.h:48
void Clear()
Clears the queue.
bool IsPaused() const
Checks if the queue is paused.
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:955
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.
void Resume()
Continues the queue rotation.
Definition: constants.h:35