Changeset 14352


Ignore:
Timestamp:
Dec 1, 2014, 9:05:44 PM (8 years ago)
Author:
mikedld
Message:

Create QDBusConnection after QApplication has been initialized

Doing otherwise causes Qt warning "QDBusConnection: session D-Bus
connection created before QCoreApplication. Application may misbehave."
Also, don't try to do anything D-Bus-related if connection fails.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/app.cc

    r14337 r14352  
    8585MyApp :: MyApp (int& argc, char ** argv):
    8686  QApplication (argc, argv),
    87   myLastFullUpdateTime (0)
     87  myLastFullUpdateTime (0),
     88  myPrefs(nullptr),
     89  mySession(nullptr),
     90  myModel(nullptr),
     91  myWindow(nullptr),
     92  myWatchDir(nullptr)
    8893{
    8994  const QString MY_CONFIG_NAME = QString::fromUtf8 ("transmission");
     
    134139        }
    135140    }
     141
     142  // try to delegate the work to an existing copy of Transmission
     143  // before starting ourselves...
     144  QDBusConnection bus = QDBusConnection::sessionBus ();
     145  if (bus.isConnected ())
     146  {
     147    bool delegated = false;
     148    for (int i=0, n=filenames.size (); i<n; ++i)
     149      {
     150        QDBusMessage request = QDBusMessage::createMethodCall (DBUS_SERVICE,
     151                                                               DBUS_OBJECT_PATH,
     152                                                               DBUS_INTERFACE,
     153                                                               QString::fromUtf8 ("AddMetainfo"));
     154        QList<QVariant> arguments;
     155        AddData a (filenames[i]);
     156        switch (a.type)
     157          {
     158            case AddData::URL:      arguments.push_back (a.url.toString ()); break;
     159            case AddData::MAGNET:   arguments.push_back (a.magnet); break;
     160            case AddData::FILENAME: arguments.push_back (a.toBase64 ().constData ()); break;
     161            case AddData::METAINFO: arguments.push_back (a.toBase64 ().constData ()); break;
     162            default:                break;
     163          }
     164        request.setArguments (arguments);
     165
     166        QDBusMessage response = bus.call (request);
     167        //std::cerr << qPrintable (response.errorName ()) << std::endl;
     168        //std::cerr << qPrintable (response.errorMessage ()) << std::endl;
     169        arguments = response.arguments ();
     170        delegated |= (arguments.size ()==1) && arguments[0].toBool ();
     171      }
     172
     173    if (delegated)
     174      {
     175        QTimer::singleShot (0, this, SLOT (quit ()));
     176        return;
     177      }
     178  }
    136179
    137180  // set the fallback config dir
     
    250293
    251294  // register as the dbus handler for Transmission
    252   new TrDBusAdaptor (this);
    253   QDBusConnection bus = QDBusConnection::sessionBus ();
    254   if (!bus.registerService (DBUS_SERVICE))
    255     std::cerr << "couldn't register " << qPrintable (DBUS_SERVICE) << std::endl;
    256   if (!bus.registerObject (DBUS_OBJECT_PATH, this))
    257     std::cerr << "couldn't register " << qPrintable (DBUS_OBJECT_PATH) << std::endl;
     295  if (bus.isConnected ())
     296    {
     297      new TrDBusAdaptor (this);
     298      if (!bus.registerService (DBUS_SERVICE))
     299        std::cerr << "couldn't register " << qPrintable (DBUS_SERVICE) << std::endl;
     300      if (!bus.registerObject (DBUS_OBJECT_PATH, this))
     301        std::cerr << "couldn't register " << qPrintable (DBUS_OBJECT_PATH) << std::endl;
     302    }
    258303}
    259304
     
    337382MyApp :: ~MyApp ()
    338383{
    339   const QRect mainwinRect (myWindow->geometry ());
     384  if (myPrefs != nullptr && myWindow != nullptr)
     385    {
     386      const QRect mainwinRect (myWindow->geometry ());
     387      myPrefs->set (Prefs :: MAIN_WINDOW_HEIGHT, std::max (100, mainwinRect.height ()));
     388      myPrefs->set (Prefs :: MAIN_WINDOW_WIDTH, std::max (100, mainwinRect.width ()));
     389      myPrefs->set (Prefs :: MAIN_WINDOW_X, mainwinRect.x ());
     390      myPrefs->set (Prefs :: MAIN_WINDOW_Y, mainwinRect.y ());
     391    }
     392
    340393  delete myWatchDir;
    341394  delete myWindow;
    342395  delete myModel;
    343396  delete mySession;
    344 
    345   myPrefs->set (Prefs :: MAIN_WINDOW_HEIGHT, std::max (100, mainwinRect.height ()));
    346   myPrefs->set (Prefs :: MAIN_WINDOW_WIDTH, std::max (100, mainwinRect.width ()));
    347   myPrefs->set (Prefs :: MAIN_WINDOW_X, mainwinRect.x ());
    348   myPrefs->set (Prefs :: MAIN_WINDOW_Y, mainwinRect.y ());
    349397  delete myPrefs;
    350398}
     
    490538main (int argc, char * argv[])
    491539{
    492   // find .torrents, URLs, magnet links, etc in the command-line args
    493   int c;
    494   QStringList addme;
    495   const char * optarg;
    496   char ** argvv;
    497 
    498540#ifdef _WIN32
    499541  tr_win32_make_args_utf8 (&argc, &argv);
    500542#endif
    501543
    502   argvv = argv;
    503   while ( (c = tr_getopt (getUsage (), argc, (const char **)argvv, opts, &optarg)))
    504     if (c == TR_OPT_UNK)
    505       addme.append (optarg);
    506 
    507   // try to delegate the work to an existing copy of Transmission
    508   // before starting ourselves...
    509   bool delegated = false;
    510   QDBusConnection bus = QDBusConnection::sessionBus ();
    511   for (int i=0, n=addme.size (); i<n; ++i)
    512     {
    513       QDBusMessage request = QDBusMessage::createMethodCall (DBUS_SERVICE,
    514                                                              DBUS_OBJECT_PATH,
    515                                                              DBUS_INTERFACE,
    516                                                              QString::fromUtf8 ("AddMetainfo"));
    517       QList<QVariant> arguments;
    518       AddData a (addme[i]);
    519       switch (a.type)
    520         {
    521           case AddData::URL:      arguments.push_back (a.url.toString ()); break;
    522           case AddData::MAGNET:   arguments.push_back (a.magnet); break;
    523           case AddData::FILENAME: arguments.push_back (a.toBase64 ().constData ()); break;
    524           case AddData::METAINFO: arguments.push_back (a.toBase64 ().constData ()); break;
    525           default:                break;
    526         }
    527       request.setArguments (arguments);
    528 
    529       QDBusMessage response = bus.call (request);
    530       //std::cerr << qPrintable (response.errorName ()) << std::endl;
    531       //std::cerr << qPrintable (response.errorMessage ()) << std::endl;
    532       arguments = response.arguments ();
    533       delegated |= (arguments.size ()==1) && arguments[0].toBool ();
    534     }
    535 
    536   if (delegated)
    537     return 0;
    538 
    539   tr_optind = 1;
    540544  MyApp app (argc, argv);
    541545  return app.exec ();
Note: See TracChangeset for help on using the changeset viewer.