File indexing completed on 2024-04-28 11:35:28
0001 /* 0002 This file is part of the KDE 0003 0004 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 SPDX-FileCopyrightText: 2011 Kevin Ottens <ervin@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0008 */ 0009 0010 #include "kdbusinterprocesslock.h" 0011 0012 #include <QDBusConnectionInterface> 0013 #include <QEventLoop> 0014 0015 class KDBusInterProcessLockPrivate 0016 { 0017 public: 0018 KDBusInterProcessLockPrivate(const QString &resource, KDBusInterProcessLock *parent) 0019 : m_resource(resource) 0020 , m_parent(parent) 0021 { 0022 m_serviceName = QStringLiteral("org.kde.private.lock-%1").arg(m_resource); 0023 0024 m_parent->connect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceRegistered, m_parent, [this](const QString &service) { 0025 serviceRegistered(service); 0026 }); 0027 } 0028 0029 ~KDBusInterProcessLockPrivate() 0030 { 0031 } 0032 0033 void serviceRegistered(const QString &service) 0034 { 0035 if (service == m_serviceName) { 0036 Q_EMIT m_parent->lockGranted(m_parent); 0037 } 0038 } 0039 0040 QString m_resource; 0041 QString m_serviceName; 0042 KDBusInterProcessLock *m_parent; 0043 }; 0044 0045 KDBusInterProcessLock::KDBusInterProcessLock(const QString &resource) 0046 : d(new KDBusInterProcessLockPrivate(resource, this)) 0047 { 0048 } 0049 0050 KDBusInterProcessLock::~KDBusInterProcessLock() = default; 0051 0052 QString KDBusInterProcessLock::resource() const 0053 { 0054 return d->m_resource; 0055 } 0056 0057 void KDBusInterProcessLock::lock() 0058 { 0059 QDBusConnection::sessionBus().interface()->registerService(d->m_serviceName, 0060 QDBusConnectionInterface::QueueService, 0061 QDBusConnectionInterface::DontAllowReplacement); 0062 } 0063 0064 void KDBusInterProcessLock::unlock() 0065 { 0066 QDBusConnection::sessionBus().interface()->unregisterService(d->m_serviceName); 0067 } 0068 0069 void KDBusInterProcessLock::waitForLockGranted() 0070 { 0071 QEventLoop loop; 0072 connect(this, &KDBusInterProcessLock::lockGranted, &loop, &QEventLoop::quit); 0073 loop.exec(); 0074 } 0075 0076 #include "moc_kdbusinterprocesslock.cpp"