1 /***************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (directui@nokia.com)
9 ** If you have questions regarding the use of this file, please contact
10 ** Nokia at directui@nokia.com.
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License version 2.1 as published by the Free Software Foundation
15 ** and appearing in the file LICENSE.LGPL included in the packaging
18 ****************************************************************************/
20 #include "mremoteaction.h"
21 #include "mremoteaction_p.h"
23 #include <QDBusInterface>
24 #include <QDBusPendingCall>
27 MRemoteActionPrivate::MRemoteActionPrivate() :
32 MRemoteActionPrivate::~MRemoteActionPrivate()
36 MRemoteAction::MRemoteAction(const QString &serviceName, const QString &objectPath, const QString &interface, const QString &methodName, const QList<QVariant> &arguments, QObject *parent) : MAction(*new MRemoteActionPrivate, parent)
40 d->serviceName = serviceName;
41 d->objectPath = objectPath;
42 d->interface = interface;
43 d->methodName = methodName;
44 d->arguments = arguments;
46 connect(this, SIGNAL(triggered()), this, SLOT(call()));
49 MRemoteAction::MRemoteAction(const QString &string, QObject *parent) : MAction(*new MRemoteActionPrivate, parent)
53 connect(this, SIGNAL(triggered()), this, SLOT(call()));
56 MRemoteAction::MRemoteAction(MRemoteActionPrivate &dd, QObject *parent) : MAction(dd, parent)
58 connect(this, SIGNAL(triggered()), this, SLOT(call()));
61 MRemoteAction::~MRemoteAction()
65 QString MRemoteAction::toString() const
67 Q_D(const MRemoteAction);
70 if (!d->serviceName.isEmpty() && !d->objectPath.isEmpty() && !d->interface.isEmpty() && !d->methodName.isEmpty()) {
71 s.append(d->serviceName).append(' ');
72 s.append(d->objectPath).append(' ');
73 s.append(d->interface).append(' ');
74 s.append(d->methodName);
76 foreach(const QVariant & arg, d->arguments) {
77 // Serialize the QVariant into a QBuffer
79 buffer.open(QIODevice::ReadWrite);
80 QDataStream stream(&buffer);
84 // Encode the contents of the QBuffer in Base64
86 s.append(buffer.buffer().toBase64().data());
93 void MRemoteAction::fromString(const QString &string)
97 QStringList l = string.split(' ');
100 d->serviceName = l.at(0);
101 d->objectPath = l.at(1);
102 d->interface = l.at(2);
103 d->methodName = l.at(3);
106 const int count = l.count();
107 for (int i = 4; i < count; ++i) {
108 QByteArray byteArray = QByteArray::fromBase64(l.at(i).toAscii());
109 QBuffer buffer(&byteArray);
110 buffer.open(QIODevice::ReadOnly);
111 QDataStream stream(&buffer);
116 d->arguments.append(arg);
120 MRemoteAction::MRemoteAction(const MRemoteAction &action) : MAction(*new MRemoteActionPrivate, action.parent())
122 fromString(action.toString());
124 connect(this, SIGNAL(triggered()), this, SLOT(call()));
127 void MRemoteAction::call()
131 QDBusInterface interface(d->serviceName, d->objectPath, d->interface.toAscii());
132 if (interface.isValid())
134 interface.asyncCallWithArgumentList(d->methodName, d->arguments);