2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
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.
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.
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
21 #include "dbusservice.h"
22 #include <QDBusInterface>
23 #include "platformsettings.h"
25 #include <QDeclarativeInfo>
29 #include <QDBusPendingCall>
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) {
38 DbusService::~DbusService() {
42 PlatformSettings *DbusService::settings() const {
46 void DbusService::setSettings(PlatformSettings *settings) {
47 if (m_settings != settings) {
48 m_settings = settings;
52 emit settingsChanged();
56 void DbusService::setName(const QString& name) {
66 bool DbusService::isEnabled() const {
70 void DbusService::init() {
71 if (m_name.isEmpty() || !m_settings) {
75 PlatformSettings::Service service = m_settings->service(m_name);
77 if (service.m_type != "dbus") {
78 qmlInfo(this) << "Unknown service type" << service.m_type;
82 if (!service.m_enabled) {
83 qmlInfo(this) << "Not initializing a disabled service";
87 m_path = service.m_path;
88 m_interface = service.m_interface;
89 m_dest = service.m_dest;
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;
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";
109 m_enabled = service.m_enabled;
110 emit isEnabledChanged();
113 bool DbusService::asyncCall(const QString& method, const QVariant& arg) {
115 qmlInfo(this) << "Empty settings.";
120 qmlInfo(this) << "Service is not enabled.";
124 if (m_dest.isEmpty() || m_path.isEmpty() || m_interface.isEmpty()) {
125 qmlInfo(this) << "Unable to construct DBus method call.";
129 if (method.isEmpty()) {
130 qmlInfo(this) << "Empty method call";
134 if (!m_methods.contains(method)) {
135 qmlInfo(this) << "Unknown method call ID" << method;
139 QString call = m_methods[method];
141 QDBusMessage msg = QDBusMessage::createMethodCall(m_dest, m_path, m_interface, call);
142 msg.setAutoStartService(true);
144 msg.setArguments(QList<QVariant>() << arg);
147 QDBusConnection conn = QDBusConnection::sessionBus();
148 if (!conn.isConnected()) {
149 qmlInfo(this) << "Unable to connect to DBus session bus.";