~canonical-platform-qa/ubuntu-system-settings-online-accounts/launch_fixture

« back to all changes in this revision

Viewing changes to src/request-manager.cpp

  • Committer: CI bot
  • Author(s): Alberto Mardegan
  • Date: 2014-05-30 13:30:01 UTC
  • mfrom: (107.1.13 master)
  • Revision ID: ps-jenkins@lists.canonical.com-20140530133001-an9lfy1dc1pfkifd
Release development branch

Features landing with this branch:
- Updating the ACL when applications are enabled/disabled in System Settings
- Write profile information in the XML files installed by click hooks
- Run tests with Python 3 autopilot.
- Merge signon-ui into online-accounts-ui 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical Ltd.
 
3
 *
 
4
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
5
 *
 
6
 * This file is part of online-accounts-ui
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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/>.
 
19
 */
 
20
 
 
21
#include "debug.h"
 
22
#include "request.h"
 
23
#include "request-manager.h"
 
24
 
 
25
#include <QQueue>
 
26
 
 
27
using namespace OnlineAccountsUi;
 
28
 
 
29
namespace OnlineAccountsUi {
 
30
 
 
31
static RequestManager *m_instance = 0;
 
32
 
 
33
typedef QQueue<Request*> RequestQueue;
 
34
 
 
35
class RequestManagerPrivate: public QObject
 
36
{
 
37
    Q_OBJECT
 
38
    Q_DECLARE_PUBLIC(RequestManager)
 
39
 
 
40
public:
 
41
    RequestManagerPrivate(RequestManager *service);
 
42
    ~RequestManagerPrivate();
 
43
 
 
44
    RequestQueue &queueForWindowId(WId windowId);
 
45
    void enqueue(Request *request);
 
46
    void runQueue(RequestQueue &queue);
 
47
 
 
48
private Q_SLOTS:
 
49
    void onRequestCompleted();
 
50
 
 
51
private:
 
52
    mutable RequestManager *q_ptr;
 
53
    /* each window Id has a different queue */
 
54
    QMap<WId,RequestQueue> m_requests;
 
55
};
 
56
 
 
57
} // namespace
 
58
 
 
59
RequestManagerPrivate::RequestManagerPrivate(RequestManager *service):
 
60
    QObject(service),
 
61
    q_ptr(service)
 
62
{
 
63
}
 
64
 
 
65
RequestManagerPrivate::~RequestManagerPrivate()
 
66
{
 
67
}
 
68
 
 
69
RequestQueue &RequestManagerPrivate::queueForWindowId(WId windowId)
 
70
{
 
71
    if (!m_requests.contains(windowId)) {
 
72
        RequestQueue queue;
 
73
        m_requests.insert(windowId, queue);
 
74
    }
 
75
    return m_requests[windowId];
 
76
}
 
77
 
 
78
void RequestManagerPrivate::enqueue(Request *request)
 
79
{
 
80
    Q_Q(RequestManager);
 
81
    bool wasIdle = q->isIdle();
 
82
 
 
83
    WId windowId = request->windowId();
 
84
 
 
85
    RequestQueue &queue = queueForWindowId(windowId);
 
86
    queue.enqueue(request);
 
87
 
 
88
    if (wasIdle) {
 
89
        Q_EMIT q->isIdleChanged();
 
90
    }
 
91
 
 
92
    runQueue(queue);
 
93
}
 
94
 
 
95
void RequestManagerPrivate::runQueue(RequestQueue &queue)
 
96
{
 
97
    Request *request = queue.head();
 
98
    DEBUG() << "Head:" << request;
 
99
 
 
100
    if (request->isInProgress()) {
 
101
        DEBUG() << "Already in progress";
 
102
        return; // Nothing to do
 
103
    }
 
104
 
 
105
    QObject::connect(request, SIGNAL(completed()),
 
106
                     this, SLOT(onRequestCompleted()));
 
107
    request->start();
 
108
}
 
109
 
 
110
void RequestManagerPrivate::onRequestCompleted()
 
111
{
 
112
    Q_Q(RequestManager);
 
113
 
 
114
    Request *request = qobject_cast<Request*>(sender());
 
115
    WId windowId = request->windowId();
 
116
 
 
117
    RequestQueue &queue = queueForWindowId(windowId);
 
118
    if (request != queue.head()) {
 
119
        qCritical("Completed request is not first in queue!");
 
120
        return;
 
121
    }
 
122
 
 
123
    queue.dequeue();
 
124
    request->deleteLater();
 
125
 
 
126
    if (queue.isEmpty()) {
 
127
        m_requests.remove(windowId);
 
128
    } else {
 
129
        /* start the next request */
 
130
        runQueue(queue);
 
131
    }
 
132
 
 
133
    if (q->isIdle()) {
 
134
        Q_EMIT q->isIdleChanged();
 
135
    }
 
136
}
 
137
 
 
138
RequestManager::RequestManager(QObject *parent):
 
139
    QObject(parent),
 
140
    d_ptr(new RequestManagerPrivate(this))
 
141
{
 
142
    if (m_instance == 0) {
 
143
        m_instance = this;
 
144
    } else {
 
145
        qWarning() << "Instantiating a second RequestManager!";
 
146
    }
 
147
}
 
148
 
 
149
RequestManager::~RequestManager()
 
150
{
 
151
}
 
152
 
 
153
RequestManager *RequestManager::instance()
 
154
{
 
155
    return m_instance;
 
156
}
 
157
 
 
158
void RequestManager::enqueue(Request *request)
 
159
{
 
160
    Q_D(RequestManager);
 
161
    d->enqueue(request);
 
162
}
 
163
 
 
164
bool RequestManager::isIdle() const
 
165
{
 
166
    Q_D(const RequestManager);
 
167
    return d->m_requests.isEmpty();
 
168
}
 
169
 
 
170
 
 
171
#include "request-manager.moc"