2
* Copyright (C) 2013 Canonical Ltd.
4
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
6
* This file is part of online-accounts-ui
8
* This program is free software: you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License version 3, as published
10
* by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranties of
14
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
* PURPOSE. See the GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "request-manager.h"
27
using namespace OnlineAccountsUi;
29
namespace OnlineAccountsUi {
31
static RequestManager *m_instance = 0;
33
typedef QQueue<Request*> RequestQueue;
35
class RequestManagerPrivate: public QObject
38
Q_DECLARE_PUBLIC(RequestManager)
41
RequestManagerPrivate(RequestManager *service);
42
~RequestManagerPrivate();
44
RequestQueue &queueForWindowId(WId windowId);
45
void enqueue(Request *request);
46
void runQueue(RequestQueue &queue);
49
void onRequestCompleted();
52
mutable RequestManager *q_ptr;
53
/* each window Id has a different queue */
54
QMap<WId,RequestQueue> m_requests;
59
RequestManagerPrivate::RequestManagerPrivate(RequestManager *service):
65
RequestManagerPrivate::~RequestManagerPrivate()
69
RequestQueue &RequestManagerPrivate::queueForWindowId(WId windowId)
71
if (!m_requests.contains(windowId)) {
73
m_requests.insert(windowId, queue);
75
return m_requests[windowId];
78
void RequestManagerPrivate::enqueue(Request *request)
81
bool wasIdle = q->isIdle();
83
WId windowId = request->windowId();
85
RequestQueue &queue = queueForWindowId(windowId);
86
queue.enqueue(request);
89
Q_EMIT q->isIdleChanged();
95
void RequestManagerPrivate::runQueue(RequestQueue &queue)
97
Request *request = queue.head();
98
DEBUG() << "Head:" << request;
100
if (request->isInProgress()) {
101
DEBUG() << "Already in progress";
102
return; // Nothing to do
105
QObject::connect(request, SIGNAL(completed()),
106
this, SLOT(onRequestCompleted()));
110
void RequestManagerPrivate::onRequestCompleted()
114
Request *request = qobject_cast<Request*>(sender());
115
WId windowId = request->windowId();
117
RequestQueue &queue = queueForWindowId(windowId);
118
if (request != queue.head()) {
119
qCritical("Completed request is not first in queue!");
124
request->deleteLater();
126
if (queue.isEmpty()) {
127
m_requests.remove(windowId);
129
/* start the next request */
134
Q_EMIT q->isIdleChanged();
138
RequestManager::RequestManager(QObject *parent):
140
d_ptr(new RequestManagerPrivate(this))
142
if (m_instance == 0) {
145
qWarning() << "Instantiating a second RequestManager!";
149
RequestManager::~RequestManager()
153
RequestManager *RequestManager::instance()
158
void RequestManager::enqueue(Request *request)
164
bool RequestManager::isIdle() const
166
Q_D(const RequestManager);
167
return d->m_requests.isEmpty();
171
#include "request-manager.moc"