Kill CameraButtonRow and use the QtQuick Row component
[harmattan/cameraplus] / src / dbusservice.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "dbusservice.h"
22 #include <QDBusInterface>
23 #include "platformsettings.h"
24 #if defined(QT4)
25 #include <QDeclarativeInfo>
26 #elif defined(QT5)
27 #include <QQmlInfo>
28 #endif
29 #include <QDBusPendingCall>
30
31 // We cannot use ADBusInterface because it will try to introspect the service upon
32 // creation. This will lead to services being launched when we create the interfaces.
33 DbusService::DbusService(QObject *parent) :
34   QObject(parent), m_settings(0), m_enabled(false) {
35
36 }
37
38 DbusService::~DbusService() {
39
40 }
41
42 PlatformSettings *DbusService::settings() const {
43   return m_settings;
44 }
45
46 void DbusService::setSettings(PlatformSettings *settings) {
47   if (m_settings != settings) {
48     m_settings = settings;
49
50     init();
51
52     emit settingsChanged();
53   }
54 }
55
56 void DbusService::setName(const QString& name) {
57   if (m_name != name) {
58     m_name = name;
59
60     init();
61
62     emit nameChanged();
63   }
64 }
65
66 bool DbusService::isEnabled() const {
67   return m_enabled;
68 }
69
70 void DbusService::init() {
71   if (m_name.isEmpty() || !m_settings) {
72     return;
73   }
74
75   PlatformSettings::Service service = m_settings->service(m_name);
76
77   if (service.m_type != "dbus") {
78     qmlInfo(this) << "Unknown service type" << service.m_type;
79     return;
80   }
81
82   if (!service.m_enabled) {
83     qmlInfo(this) << "Not initializing a disabled service";
84     return;
85   }
86
87   m_path = service.m_path;
88   m_interface = service.m_interface;
89   m_dest = service.m_dest;
90
91   QStringList methods = service.m_method.split('|');
92   foreach (const QString& method, methods) {
93     QStringList parts = method.split(':');
94     if (parts.size() != 2) {
95       qmlInfo(this) << "Cannot parse dbus method description" << method;
96       continue;
97     }
98
99     QString id = parts[0].trimmed();
100     QString val = parts[1].trimmed();
101     if (id.isEmpty() || val.isEmpty()) {
102       qmlInfo(this) << "Empty id or value";
103       continue;
104     }
105
106     m_methods[id] = val;
107   }
108
109   m_enabled = service.m_enabled;
110   emit isEnabledChanged();
111 }
112
113 bool DbusService::asyncCall(const QString& method, const QVariant& arg) {
114   if (!m_settings) {
115     qmlInfo(this) << "Empty settings.";
116     return false;
117   }
118
119   if (!m_enabled) {
120     qmlInfo(this) << "Service is not enabled.";
121     return false;
122   }
123
124   if (m_dest.isEmpty() || m_path.isEmpty() || m_interface.isEmpty()) {
125     qmlInfo(this) << "Unable to construct DBus method call.";
126     return false;
127   }
128
129   if (method.isEmpty()) {
130     qmlInfo(this) << "Empty method call";
131     return false;
132   }
133
134   if (!m_methods.contains(method)) {
135     qmlInfo(this) << "Unknown method call ID" << method;
136     return false;
137   }
138
139   QString call = m_methods[method];
140
141   QDBusMessage msg = QDBusMessage::createMethodCall(m_dest, m_path, m_interface, call);
142   msg.setAutoStartService(true);
143   if (arg.isValid()) {
144     msg.setArguments(QList<QVariant>() << arg);
145   }
146
147   QDBusConnection conn = QDBusConnection::sessionBus();
148   if (!conn.isConnected()) {
149     qmlInfo(this) << "Unable to connect to DBus session bus.";
150     return false;
151   }
152
153   conn.asyncCall(msg);
154
155   return true;
156 }