source: trunk/qt/DBusInteropHelper.cc @ 14621

Last change on this file since 14621 was 14621, checked in by mikedld, 7 years ago

Refactor DBus IPC to allow for further extensibility

  • Property svn:keywords set to Date Rev Author Id
File size: 1.6 KB
Line 
1/*
2 * This file Copyright (C) 2015 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: DBusInteropHelper.cc 14621 2015-12-16 17:57:05Z mikedld $
8 */
9
10#include <iostream>
11
12#include <QDBusConnection>
13#include <QDBusMessage>
14#include <QDBusReply>
15#include <QString>
16#include <QVariant>
17
18#include "DBusInteropHelper.h"
19#include "InteropObject.h"
20
21namespace
22{
23  const QLatin1String DBUS_SERVICE ("com.transmissionbt.Transmission");
24  const QLatin1String DBUS_OBJECT_PATH ("/com/transmissionbt/Transmission");
25  const QLatin1String DBUS_INTERFACE ("com.transmissionbt.Transmission");
26}
27
28bool
29DBusInteropHelper::isConnected () const
30{
31  return !QDBusConnection::sessionBus ().isConnected ();
32}
33
34QVariant
35DBusInteropHelper::addMetainfo (const QString& metainfo)
36{
37  QDBusMessage request = QDBusMessage::createMethodCall (DBUS_SERVICE, DBUS_OBJECT_PATH,
38                                                         DBUS_INTERFACE, QLatin1String ("AddMetainfo"));
39  request.setArguments (QVariantList () << metainfo);
40
41  const QDBusReply<bool> response = QDBusConnection::sessionBus ().call (request);
42  return response.isValid () ? QVariant (response.value ()) : QVariant ();
43}
44
45void
46DBusInteropHelper::registerObject (QObject * parent)
47{
48  QDBusConnection bus = QDBusConnection::sessionBus ();
49  if (!bus.isConnected ())
50    return;
51
52  if (!bus.registerService (DBUS_SERVICE))
53    std::cerr << "couldn't register " << qPrintable (DBUS_SERVICE) << std::endl;
54  if (!bus.registerObject (DBUS_OBJECT_PATH, new InteropObject (parent), QDBusConnection::ExportAllSlots))
55    std::cerr << "couldn't register " << qPrintable (DBUS_OBJECT_PATH) << std::endl;
56}
Note: See TracBrowser for help on using the repository browser.