Music Hub  ..
A session-wide music playback service
dbus_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2021-2022 UBports Foundation.
3  *
4  * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "dbus_utils.h"
20 
21 #include <QDBusPendingCall>
22 #include <QDBusPendingCallWatcher>
23 #include <QDBusPendingReply>
24 #include <QDebug>
25 #include <QEventLoop>
26 
27 using namespace lomiri::MediaHub;
28 
29 /* For compatibility with the old client library, some of this library methods
30  * are blocking. However we also want to continue iterating the main loop,
31  * because we update the property cache when the D-Bus signals arrive (and they
32  * will arrive just before the D-Bus call returns).
33  */
34 bool DBusUtils::waitForFinished(const QDBusPendingCall &call)
35 {
36  auto watcher = new QDBusPendingCallWatcher(call);
37  QEventLoop loop;
38  QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
39  [&loop](QDBusPendingCallWatcher *call) {
40  call->deleteLater();
41  QDBusPendingReply<void> reply(*call);
42  if (reply.isError()) {
43  qWarning() << Q_FUNC_INFO << reply.error();
44  loop.exit(1);
45  } else {
46  loop.exit(0);
47  }
48  });
49  int rc = loop.exec(QEventLoop::ExcludeUserInputEvents);
50  return rc == 0;
51 }
lomiri::MediaHub
Definition: dbus_utils.h:25
lomiri::MediaHub::DBusUtils::waitForFinished
static bool waitForFinished(const QDBusPendingCall &call)
Definition: dbus_utils.cpp:34
dbus_utils.h