/
service.cpp
186 lines (162 loc) · 5.97 KB
/
service.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2010 Nokia Corporation.
*
* Contact: Marius Vollmer <marius.vollmer@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "service.h"
#include "internal.h"
#include <MDesktopEntry>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QStringList>
#include <QDBusPendingCallWatcher>
#define MAPPER_SERVICENAME "com.nokia.MServiceFw"
#define MAPPER_PATH "/"
#define MAPPER_INTERFACE "com.nokia.MServiceFwIf"
using namespace ContentAction::Internal;
namespace ContentAction
{
ServiceFwPrivate::ServiceFwPrivate(QSharedPointer<MDesktopEntry> desktopEntry,
const QStringList& params)
: DefaultPrivate(desktopEntry, params),
serviceFwMethod(desktopEntry->value(XMaemoMethodKey))
{
}
void ServiceFwPrivate::trigger(bool wait) const
{
QString method;
QDBusInterface *proxy = resolver().implementorForAction(serviceFwMethod, method);
if (!proxy)
return;
QDBusPendingCallWatcher watcher(proxy->asyncCall(method, params));
if (wait) {
watcher.waitForFinished();
if (watcher.isError()) {
LCA_WARNING << "error reply from service implementor"
<< watcher.error().message()
<< "when trying to call" << serviceFwMethod
<< "on" << proxy->service();
}
}
}
ServiceResolver& resolver()
{
static ServiceResolver resolver;
return resolver;
}
ServiceResolver::ServiceResolver()
{
QDBusConnection conn = QDBusConnection::sessionBus();
conn.connect(MAPPER_SERVICENAME,
MAPPER_PATH,
MAPPER_INTERFACE,
"serviceAvailable",
this,
SLOT(onServiceAvailable(QString,QString)));
conn.connect(MAPPER_SERVICENAME,
MAPPER_PATH,
MAPPER_INTERFACE,
"serviceUnavailable",
this,
SLOT(onServiceUnavailable(QString,QString)));
}
ServiceResolver::~ServiceResolver()
{
Q_FOREACH (QDBusInterface* proxy, proxies)
delete proxy;
proxies.clear();
}
/// A slot connected to the serviceAvailable signal from Meego service mapper.
void ServiceResolver::onServiceAvailable(QString implementor, QString interface)
{
Q_UNUSED(implementor)
// Now a service become available for an interface, but we cannot know
// wheter it is now the *preferred* implementor or not. We cannot do
// anything else but clear our understanging about who's the preferred
// implementor of the interface.
// Remove the old implementor (and its proxy)
if (resolved.contains(interface)) {
QString oldImplementor = resolved.take(interface);
if (resolved.keys(oldImplementor).isEmpty())
// The old implementor implemented only this interface; the proxy
// can be deleted.
delete proxies.take(oldImplementor);
}
}
/// A slot connected to the serviceUnavailable signal from Meego service mapper.
void ServiceResolver::onServiceUnavailable(QString implementor)
{
// Check which interfaces now become unusable
QStringList interfaces = resolved.keys(implementor);
Q_FOREACH (const QString& interface, interfaces)
resolved.remove(interface);
if (proxies.contains(implementor))
delete proxies.take(implementor);
}
/// Returns the name of the current implementor of an interface. If an error
/// occurs (e.g., we cannot connect to the Meego service mapper), returns an
/// empty string.
QString ServiceResolver::implementorName(const QString& interface)
{
if (resolved.contains(interface))
return resolved[interface];
// A blocking call
QDBusMessage message =
QDBusMessage::createMethodCall(MAPPER_SERVICENAME, MAPPER_PATH,
MAPPER_INTERFACE, "serviceName");
message.setArguments(QVariantList() << interface);
QDBusMessage reply = QDBusConnection::sessionBus().call(message);
if (reply.type() != QDBusMessage::ReplyMessage || reply.arguments().size() == 0) {
LCA_WARNING << "invalid reply from service mapper" << reply.errorName();
return "";
}
QString service = reply.arguments()[0].toString();
if (service.size() == 0) {
// don't insert to the "resolved" map
return "";
}
resolved.insert(interface, service);
return service;
}
/// Returns the proxy object for the current implementor of the given
/// interface.
QDBusInterface* ServiceResolver::implementor(const QString& interface)
{
QString name = implementorName(interface);
if (!proxies.contains(name)) {
proxies.insert(name, new QDBusInterface(name, "/", interface, QDBusConnection::sessionBus()));
}
return proxies[name];
}
// Splits action to interface.method and looks up the proxy for the interface.
// Returns the method name in \a method.
QDBusInterface *ServiceResolver::implementorForAction(const QString& action,
QString& method)
{
// Get the service fw interface from the action name
int dotIx = action.lastIndexOf(".");
if (dotIx < 1) {
LCA_WARNING << "invalid action name" << action;
return 0;
}
// Action, e.g., "com.nokia.video-interface.play"
QString interface = action.left(dotIx);
method = action.right(action.size() - dotIx - 1);
return implementor(interface);
}
}