Changeset 14621


Ignore:
Timestamp:
Dec 16, 2015, 5:57:05 PM (7 years ago)
Author:
mikedld
Message:

Refactor DBus IPC to allow for further extensibility

Location:
trunk/qt
Files:
2 added
2 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/qt/Application.cc

    r14603 r14621  
    1212
    1313#include <QDBusConnection>
    14 #include <QDBusConnectionInterface>
    15 #include <QDBusError>
    1614#include <QDBusMessage>
     15#include <QDBusReply>
    1716#include <QIcon>
    1817#include <QLibraryInfo>
     
    2928#include "AddData.h"
    3029#include "Application.h"
    31 #include "DBusAdaptor.h"
     30#include "DBusInteropHelper.h"
    3231#include "Formatter.h"
    3332#include "MainWindow.h"
     
    4039namespace
    4140{
    42   const QString DBUS_SERVICE     = QString::fromUtf8 ("com.transmissionbt.Transmission" );
    43   const QString DBUS_OBJECT_PATH = QString::fromUtf8 ("/com/transmissionbt/Transmission");
    44   const QString DBUS_INTERFACE   = QString::fromUtf8 ("com.transmissionbt.Transmission" );
    45 
    4641  const QLatin1String MY_CONFIG_NAME ("transmission");
    4742  const QLatin1String MY_READABLE_NAME ("transmission-qt");
     
    163158  // try to delegate the work to an existing copy of Transmission
    164159  // before starting ourselves...
    165   QDBusConnection bus = QDBusConnection::sessionBus ();
    166   if (bus.isConnected ())
    167   {
    168     bool delegated = false;
    169     for (const QString& filename: filenames)
    170       {
    171         QDBusMessage request = QDBusMessage::createMethodCall (DBUS_SERVICE,
    172                                                                DBUS_OBJECT_PATH,
    173                                                                DBUS_INTERFACE,
    174                                                                QString::fromUtf8 ("AddMetainfo"));
    175         QList<QVariant> arguments;
    176         AddData a (filename);
    177         switch (a.type)
    178           {
    179             case AddData::URL:      arguments.push_back (a.url.toString ()); break;
    180             case AddData::MAGNET:   arguments.push_back (a.magnet); break;
    181             case AddData::FILENAME: arguments.push_back (QString::fromLatin1 (a.toBase64 ())); break;
    182             case AddData::METAINFO: arguments.push_back (QString::fromLatin1 (a.toBase64 ())); break;
    183             default:                break;
    184           }
    185         request.setArguments (arguments);
    186 
    187         QDBusMessage response = bus.call (request);
    188         //std::cerr << qPrintable (response.errorName ()) << std::endl;
    189         //std::cerr << qPrintable (response.errorMessage ()) << std::endl;
    190         arguments = response.arguments ();
    191         delegated |= (arguments.size ()==1) && arguments[0].toBool ();
    192       }
    193 
    194     if (delegated)
    195       {
    196         quitLater ();
    197         return;
    198       }
    199   }
     160  DBusInteropHelper interopClient;
     161  if (interopClient.isConnected ())
     162    {
     163      bool delegated = false;
     164      for (const QString& filename: filenames)
     165        {
     166          QString metainfo;
     167
     168          AddData a (filename);
     169          switch (a.type)
     170            {
     171              case AddData::URL:      metainfo = a.url.toString (); break;
     172              case AddData::MAGNET:   metainfo = a.magnet; break;
     173              case AddData::FILENAME: metainfo = QString::fromLatin1 (a.toBase64 ()); break;
     174              case AddData::METAINFO: metainfo = QString::fromLatin1 (a.toBase64 ()); break;
     175              default:                break;
     176            }
     177
     178          if (metainfo.isEmpty ())
     179            continue;
     180
     181          const QVariant result = interopClient.addMetainfo (metainfo);
     182          if (result.isValid () && result.toBool ())
     183            delegated = true;
     184        }
     185
     186      if (delegated)
     187        {
     188          quitLater ();
     189          return;
     190        }
     191    }
    200192
    201193  // set the fallback config dir
     
    303295    addTorrent (filename);
    304296
    305   // register as the dbus handler for Transmission
    306   if (bus.isConnected ())
    307     {
    308       new DBusAdaptor (this);
    309       if (!bus.registerService (DBUS_SERVICE))
    310         std::cerr << "couldn't register " << qPrintable (DBUS_SERVICE) << std::endl;
    311       if (!bus.registerObject (DBUS_OBJECT_PATH, this))
    312         std::cerr << "couldn't register " << qPrintable (DBUS_OBJECT_PATH) << std::endl;
    313     }
     297  DBusInteropHelper::registerObject (this);
    314298}
    315299
     
    559543Application::notifyApp (const QString& title, const QString& body) const
    560544{
     545  const QLatin1String dbusServiceName ("org.freedesktop.Notifications");
     546  const QLatin1String dbusInterfaceName ("org.freedesktop.Notifications");
     547  const QLatin1String dbusPath ("/org/freedesktop/Notifications");
     548
    561549  QDBusConnection bus = QDBusConnection::sessionBus ();
    562   if (!bus.isConnected ())
    563     {
    564       myWindow->trayIcon ().showMessage (title, body);
    565       return true;
    566     }
    567 
    568   const QString dbusServiceName   = QString::fromUtf8 ("org.freedesktop.Notifications");
    569   const QString dbusInterfaceName = QString::fromUtf8 ("org.freedesktop.Notifications");
    570   const QString dbusPath          = QString::fromUtf8 ("/org/freedesktop/Notifications");
    571 
    572   QDBusMessage m = QDBusMessage::createMethodCall (dbusServiceName, dbusPath, dbusInterfaceName, QString::fromUtf8 ("Notify"));
    573   QList<QVariant> args;
    574   args.append (QString::fromUtf8 ("Transmission")); // app_name
    575   args.append (0U);                                   // replaces_id
    576   args.append (QString::fromUtf8 ("transmission")); // icon
    577   args.append (title);                                // summary
    578   args.append (body);                                 // body
    579   args.append (QStringList ());                       // actions - unused for plain passive popups
    580   args.append (QVariantMap ());                       // hints - unused atm
    581   args.append (static_cast<int32_t> (-1));            // use the default timeout period
    582   m.setArguments (args);
    583   QDBusMessage replyMsg = bus.call (m);
    584   //std::cerr << qPrintable (replyMsg.errorName ()) << std::endl;
    585   //std::cerr << qPrintable (replyMsg.errorMessage ()) << std::endl;
    586   return (replyMsg.type () == QDBusMessage::ReplyMessage) && !replyMsg.arguments ().isEmpty ();
     550  if (bus.isConnected ())
     551    {
     552      QDBusMessage m = QDBusMessage::createMethodCall (dbusServiceName, dbusPath, dbusInterfaceName, QLatin1String ("Notify"));
     553      QVariantList args;
     554      args.append (QLatin1String ("Transmission")); // app_name
     555      args.append (0U);                             // replaces_id
     556      args.append (QLatin1String ("transmission")); // icon
     557      args.append (title);                          // summary
     558      args.append (body);                           // body
     559      args.append (QStringList ());                 // actions - unused for plain passive popups
     560      args.append (QVariantMap ());                 // hints - unused atm
     561      args.append (static_cast<int32_t> (-1));      // use the default timeout period
     562      m.setArguments (args);
     563      const QDBusReply<quint32> replyMsg = bus.call (m);
     564      if (replyMsg.isValid () && replyMsg.value () > 0)
     565        return true;
     566    }
     567
     568  myWindow->trayIcon ().showMessage (title, body);
     569  return true;
    587570}
    588571
  • trunk/qt/CMakeLists.txt

    r14616 r14621  
    3030    Application.cc
    3131    ColumnResizer.cc
    32     DBusAdaptor.cc
     32    DBusInteropHelper.cc
    3333    DetailsDialog.cc
    3434    FaviconCache.cc
     
    4545    FreeSpaceLabel.cc
    4646    IconToolButton.cc
     47    InteropObject.cc
    4748    LicenseDialog.cc
    4849    MainWindow.cc
     
    7879    ColumnResizer.h
    7980    CustomVariantType.h
    80     DBusAdaptor.h
     81    DBusInteropHelper.h
    8182    DetailsDialog.h
    8283    FaviconCache.h
     
    9394    FreeSpaceLabel.h
    9495    IconToolButton.h
     96    InteropObject.h
    9597    LicenseDialog.h
    9698    MainWindow.h
  • trunk/qt/InteropObject.cc

    r14620 r14621  
    1010#include "AddData.h"
    1111#include "Application.h"
    12 #include "DBusAdaptor.h"
     12#include "InteropObject.h"
    1313
    14 DBusAdaptor::DBusAdaptor (Application* app):
    15   QDBusAbstractAdaptor (app),
    16   myApp (app)
     14InteropObject::InteropObject (QObject * parent):
     15  QObject (parent)
    1716{
    1817}
    1918
    2019bool
    21 DBusAdaptor::PresentWindow ()
     20InteropObject::PresentWindow ()
    2221{
    23   myApp->raise ();
     22  qApp->raise ();
    2423  return true;
    2524}
    2625
    2726bool
    28 DBusAdaptor::AddMetainfo (const QString& key)
     27InteropObject::AddMetainfo (const QString& metainfo)
    2928{
    30   AddData addme (key);
     29  AddData addme (metainfo);
    3130
    3231  if (addme.type != addme.NONE)
    33     myApp->addTorrent (addme);
     32    qApp->addTorrent (addme);
    3433
    3534  return true;
  • trunk/qt/InteropObject.h

    r14620 r14621  
    88 */
    99
    10 #ifndef QTR_DBUS_ADAPTOR_H
    11 #define QTR_DBUS_ADAPTOR_H
     10#ifndef QTR_INTEROP_OBJECT_H
     11#define QTR_INTEROP_OBJECT_H
    1212
    13 #include <QDBusAbstractAdaptor>
     13#include <QObject>
    1414
    15 class Application;
    16 
    17 class DBusAdaptor: public QDBusAbstractAdaptor
     15class InteropObject: public QObject
    1816{
    1917    Q_OBJECT
     
    2119
    2220  public:
    23     DBusAdaptor (Application *);
    24     virtual ~DBusAdaptor () {}
     21    InteropObject (QObject * parent = nullptr);
    2522
    2623  public slots:
    2724    bool PresentWindow ();
    28     bool AddMetainfo (const QString&);
    29 
    30   private:
    31     Application * myApp;
     25    bool AddMetainfo (const QString& metainfo);
    3226};
    3327
    34 #endif // QTR_DBUS_ADAPTOR_H
     28#endif // QTR_INTEROP_OBJECT_H
Note: See TracChangeset for help on using the changeset viewer.