"Initial commit to Gerrit"
[profile/ivi/mlite.git] / mremoteaction.cpp
1 /***************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (directui@nokia.com)
6 **
7 ** .
8 **
9 ** If you have questions regarding the use of this file, please contact
10 ** Nokia at directui@nokia.com.
11 **
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
16 ** of this file.
17 **
18 ****************************************************************************/
19
20 #include "mremoteaction.h"
21 #include "mremoteaction_p.h"
22
23 #include <QDBusInterface>
24 #include <QDBusPendingCall>
25 #include <QBuffer>
26
27 MRemoteActionPrivate::MRemoteActionPrivate() :
28     MActionPrivate()
29 {
30 }
31
32 MRemoteActionPrivate::~MRemoteActionPrivate()
33 {
34 }
35
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)
37 {
38     Q_D(MRemoteAction);
39
40     d->serviceName = serviceName;
41     d->objectPath = objectPath;
42     d->interface = interface;
43     d->methodName = methodName;
44     d->arguments = arguments;
45
46     connect(this, SIGNAL(triggered()), this, SLOT(call()));
47 }
48
49 MRemoteAction::MRemoteAction(const QString &string, QObject *parent) : MAction(*new MRemoteActionPrivate, parent)
50 {
51     fromString(string);
52
53     connect(this, SIGNAL(triggered()), this, SLOT(call()));
54 }
55
56 MRemoteAction::MRemoteAction(MRemoteActionPrivate &dd, QObject *parent) : MAction(dd, parent)
57 {
58     connect(this, SIGNAL(triggered()), this, SLOT(call()));
59 }
60
61 MRemoteAction::~MRemoteAction()
62 {
63 }
64
65 QString MRemoteAction::toString() const
66 {
67     Q_D(const MRemoteAction);
68
69     QString s;
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);
75
76         foreach(const QVariant & arg, d->arguments) {
77             // Serialize the QVariant into a QBuffer
78             QBuffer buffer;
79             buffer.open(QIODevice::ReadWrite);
80             QDataStream stream(&buffer);
81             stream << arg;
82             buffer.close();
83
84             // Encode the contents of the QBuffer in Base64
85             s.append(' ');
86             s.append(buffer.buffer().toBase64().data());
87         }
88     }
89
90     return s;
91 }
92
93 void MRemoteAction::fromString(const QString &string)
94 {
95     Q_D(MRemoteAction);
96
97     QStringList l = string.split(' ');
98
99     if (l.count() > 3) {
100         d->serviceName = l.at(0);
101         d->objectPath = l.at(1);
102         d->interface = l.at(2);
103         d->methodName = l.at(3);
104     }
105
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);
112         QVariant arg;
113         stream >> arg;
114         buffer.close();
115
116         d->arguments.append(arg);
117     }
118 }
119
120 MRemoteAction::MRemoteAction(const MRemoteAction &action) : MAction(*new MRemoteActionPrivate, action.parent())
121 {
122     fromString(action.toString());
123
124     connect(this, SIGNAL(triggered()), this, SLOT(call()));
125 }
126
127 void MRemoteAction::call()
128 {
129     Q_D(MRemoteAction);
130
131     QDBusInterface interface(d->serviceName, d->objectPath, d->interface.toAscii());
132     if (interface.isValid())
133     {
134         interface.asyncCallWithArgumentList(d->methodName, d->arguments);
135     }
136 }