LeechCraft  0.6.70-15082-g543737046d
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  * 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 
12 namespace LC
13 {
14 namespace 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 
45  void QueueManager::Clear ()
46  {
47  Queue_.clear ();
48  }
49 
50  void QueueManager::Pause ()
51  {
52  Paused_ = true;
53  ReqTimer_->stop ();
54  }
55 
56  bool QueueManager::IsPaused () const
57  {
58  return Paused_;
59  }
60 
61  void QueueManager::Resume ()
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 }
LC::Util::QueuePriority
QueuePriority
The priority of the action in the queue.
Definition: queuemanager.h:27
queuemanager.h
LC
Definition: constants.h:14
LC::Util::oral::sph::f
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:952
LC::Util::QueueManager::QueueManager
QueueManager(int timeout, QObject *parent=nullptr)
Creates a queue manager with the given timeout.
Definition: queuemanager.cpp:28