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

« back to all changes in this revision

Viewing changes to src/signonui-service.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
 * This file is part of online-accounts-ui
 
3
 *
 
4
 * Copyright (C) 2011-2014 Canonical Ltd.
 
5
 *
 
6
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
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-handler.h"
 
24
#include "request-manager.h"
 
25
#include "signonui-request.h"
 
26
#include "signonui-service.h"
 
27
#include "browser-request.h"
 
28
 
 
29
#include <QDBusArgument>
 
30
#include <QtQml>
 
31
#include <SignOn/uisessiondata_priv.h>
 
32
 
 
33
using namespace SignOnUi;
 
34
 
 
35
namespace SignOnUi {
 
36
 
 
37
static QVariant dbusValueToVariant(const QDBusArgument &argument)
 
38
{
 
39
    QVariant ret;
 
40
 
 
41
    /* Note: this function should operate recursively, but it doesn't. */
 
42
    if (argument.currentType() == QDBusArgument::MapType) {
 
43
        /* Assume that all maps are a{sv} */
 
44
        ret = qdbus_cast<QVariantMap>(argument);
 
45
    } else {
 
46
        /* We don't know how to handle other types */
 
47
        ret = argument.asVariant();
 
48
    }
 
49
    return ret;
 
50
}
 
51
 
 
52
static QVariantMap expandDBusArguments(const QVariantMap &dbusMap)
 
53
{
 
54
    QVariantMap map;
 
55
    QMapIterator<QString, QVariant> it(dbusMap);
 
56
    while (it.hasNext()) {
 
57
        it.next();
 
58
        if (qstrcmp(it.value().typeName(), "QDBusArgument") == 0) {
 
59
            QDBusArgument dbusValue = it.value().value<QDBusArgument>();
 
60
            map.insert(it.key(), dbusValueToVariant(dbusValue));
 
61
        } else {
 
62
            map.insert(it.key(), it.value());
 
63
        }
 
64
    }
 
65
    return map;
 
66
}
 
67
 
 
68
class ServicePrivate: public QObject
 
69
{
 
70
    Q_OBJECT
 
71
    Q_DECLARE_PUBLIC(Service)
 
72
 
 
73
public:
 
74
    ServicePrivate(Service *service);
 
75
    ~ServicePrivate();
 
76
 
 
77
    void cancelUiRequest(const QString &requestId);
 
78
    void removeIdentityData(quint32 id);
 
79
 
 
80
private:
 
81
    mutable Service *q_ptr;
 
82
};
 
83
 
 
84
} // namespace
 
85
 
 
86
ServicePrivate::ServicePrivate(Service *service):
 
87
    QObject(service),
 
88
    q_ptr(service)
 
89
{
 
90
}
 
91
 
 
92
ServicePrivate::~ServicePrivate()
 
93
{
 
94
}
 
95
 
 
96
void ServicePrivate::cancelUiRequest(const QString &requestId)
 
97
{
 
98
    QVariantMap match;
 
99
    match.insert(SSOUI_KEY_REQUESTID, requestId);
 
100
    OnlineAccountsUi::Request *request =
 
101
        OnlineAccountsUi::Request::find(match);
 
102
 
 
103
    DEBUG() << "Cancelling request" << request;
 
104
    if (request != 0) {
 
105
        request->cancel();
 
106
    }
 
107
}
 
108
 
 
109
void ServicePrivate::removeIdentityData(quint32 id)
 
110
{
 
111
    /* Remove any data associated with the given identity. */
 
112
 
 
113
    /* The BrowserRequest class creates a directory to store the cookies */
 
114
    BrowserRequest::removeIdentityData(id);
 
115
}
 
116
 
 
117
Service::Service(QObject *parent):
 
118
    QObject(parent),
 
119
    d_ptr(new ServicePrivate(this))
 
120
{
 
121
    qmlRegisterType<SignOnUi::RequestHandler>("Ubuntu.OnlineAccounts.Plugin",
 
122
                                              1, 0, "RequestHandler");
 
123
}
 
124
 
 
125
Service::~Service()
 
126
{
 
127
}
 
128
 
 
129
QVariantMap Service::queryDialog(const QVariantMap &parameters)
 
130
{
 
131
    QVariantMap cleanParameters = expandDBusArguments(parameters);
 
132
    DEBUG() << "Got request:" << cleanParameters;
 
133
 
 
134
    /* The following line tells QtDBus not to generate a reply now */
 
135
    setDelayedReply(true);
 
136
 
 
137
    OnlineAccountsUi::Request *request =
 
138
        OnlineAccountsUi::Request::newRequest(connection(),
 
139
                                              message(),
 
140
                                              cleanParameters,
 
141
                                              this);
 
142
 
 
143
    /* Check if a RequestHandler has been setup to handle this request. If
 
144
     * so, bing the request object to the handler and start the request
 
145
     * immediately. */
 
146
    SignOnUi::Request *signonRequest =
 
147
        qobject_cast<SignOnUi::Request*>(request);
 
148
    Q_ASSERT(signonRequest != 0);
 
149
 
 
150
    RequestHandler *handler = RequestHandler::findMatching(cleanParameters);
 
151
    if (handler != 0) {
 
152
        DEBUG() << "Found RequestHandler!";
 
153
        signonRequest->setHandler(handler);
 
154
        QObject::connect(signonRequest, SIGNAL(completed()),
 
155
                         signonRequest, SLOT(deleteLater()));
 
156
        signonRequest->start();
 
157
    } else {
 
158
        // proceed normally
 
159
        OnlineAccountsUi::RequestManager::instance()->enqueue(request);
 
160
    }
 
161
 
 
162
    return QVariantMap();
 
163
}
 
164
 
 
165
QVariantMap Service::refreshDialog(const QVariantMap &newParameters)
 
166
{
 
167
    QVariantMap cleanParameters = expandDBusArguments(newParameters);
 
168
    QString requestId = Request::id(cleanParameters);
 
169
    // TODO find the request and update it
 
170
 
 
171
    /* The following line tells QtDBus not to generate a reply now */
 
172
    setDelayedReply(true);
 
173
    return QVariantMap();
 
174
}
 
175
 
 
176
void Service::cancelUiRequest(const QString &requestId)
 
177
{
 
178
    Q_D(Service);
 
179
    d->cancelUiRequest(requestId);
 
180
}
 
181
 
 
182
void Service::removeIdentityData(quint32 id)
 
183
{
 
184
    Q_D(Service);
 
185
    d->removeIdentityData(id);
 
186
}
 
187
 
 
188
#include "signonui-service.moc"