File indexing completed on 2025-02-02 03:49:25
0001 /* 0002 This file is part of the KDE 0003 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-only 0006 */ 0007 0008 #include "kinterprocesslock.h" 0009 #include "kiotrashdebug.h" 0010 0011 #include <QDBusConnectionInterface> 0012 #include <QEventLoop> 0013 0014 class KInterProcessLockPrivate 0015 { 0016 Q_DECLARE_PUBLIC(KInterProcessLock) 0017 KInterProcessLock *const q_ptr; 0018 0019 public: 0020 KInterProcessLockPrivate(const QString &resource, KInterProcessLock *qq) 0021 : q_ptr(qq) 0022 , m_resource(resource) 0023 { 0024 m_serviceName = QStringLiteral("org.kde.private.lock-%1").arg(m_resource); 0025 0026 q_ptr->connect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceRegistered, q_ptr, [this](const QString &service) { 0027 serviceRegistered(service); 0028 }); 0029 } 0030 0031 ~KInterProcessLockPrivate() 0032 { 0033 } 0034 0035 void serviceRegistered(const QString &service) 0036 { 0037 if (service == m_serviceName) { 0038 Q_EMIT q_ptr->lockGranted(q_ptr); 0039 } 0040 } 0041 0042 QString m_resource; 0043 QString m_serviceName; 0044 }; 0045 0046 KInterProcessLock::KInterProcessLock(const QString &resource) 0047 : d_ptr(new KInterProcessLockPrivate(resource, this)) 0048 { 0049 } 0050 0051 KInterProcessLock::~KInterProcessLock() 0052 { 0053 delete d_ptr; 0054 } 0055 0056 QString KInterProcessLock::resource() const 0057 { 0058 return d_ptr->m_resource; 0059 } 0060 0061 void KInterProcessLock::lock() 0062 { 0063 QDBusConnection::sessionBus().interface()->registerService(d_ptr->m_serviceName, 0064 QDBusConnectionInterface::QueueService, 0065 QDBusConnectionInterface::DontAllowReplacement); 0066 } 0067 0068 void KInterProcessLock::unlock() 0069 { 0070 QDBusConnection::sessionBus().interface()->unregisterService(d_ptr->m_serviceName); 0071 } 0072 0073 void KInterProcessLock::waitForLockGranted() 0074 { 0075 QEventLoop loop; 0076 connect(this, &KInterProcessLock::lockGranted, &loop, &QEventLoop::quit); 0077 loop.exec(); 0078 } 0079 0080 #include "moc_kinterprocesslock.cpp"