1/*
2 This file is part of the KDE
3 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-only
6*/
7
8#include "kinterprocesslock.h"
9#include "kiotrashdebug.h"
10
11#include <QDBusConnectionInterface>
12#include <QEventLoop>
13
14class KInterProcessLockPrivate
15{
16 Q_DECLARE_PUBLIC(KInterProcessLock)
17 KInterProcessLock *const q_ptr;
18
19public:
20 KInterProcessLockPrivate(const QString &resource, KInterProcessLock *qq)
21 : q_ptr(qq)
22 , m_resource(resource)
23 {
24 m_serviceName = QStringLiteral("org.kde.private.lock-%1").arg(a: m_resource);
25
26 q_ptr->connect(sender: QDBusConnection::sessionBus().interface(), signal: &QDBusConnectionInterface::serviceRegistered, context: q_ptr, slot: [this](const QString &service) {
27 serviceRegistered(service);
28 });
29 }
30
31 ~KInterProcessLockPrivate()
32 {
33 }
34
35 void serviceRegistered(const QString &service)
36 {
37 if (service == m_serviceName) {
38 Q_EMIT q_ptr->lockGranted(lock: q_ptr);
39 }
40 }
41
42 QString m_resource;
43 QString m_serviceName;
44};
45
46KInterProcessLock::KInterProcessLock(const QString &resource)
47 : d_ptr(new KInterProcessLockPrivate(resource, this))
48{
49}
50
51KInterProcessLock::~KInterProcessLock()
52{
53 delete d_ptr;
54}
55
56QString KInterProcessLock::resource() const
57{
58 return d_ptr->m_resource;
59}
60
61void KInterProcessLock::lock()
62{
63 QDBusConnection::sessionBus().interface()->registerService(serviceName: d_ptr->m_serviceName,
64 qoption: QDBusConnectionInterface::QueueService,
65 roption: QDBusConnectionInterface::DontAllowReplacement);
66}
67
68void KInterProcessLock::unlock()
69{
70 QDBusConnection::sessionBus().interface()->unregisterService(serviceName: d_ptr->m_serviceName);
71}
72
73void KInterProcessLock::waitForLockGranted()
74{
75 QEventLoop loop;
76 connect(sender: this, signal: &KInterProcessLock::lockGranted, context: &loop, slot: &QEventLoop::quit);
77 loop.exec();
78}
79
80#include "moc_kinterprocesslock.cpp"
81

source code of kio/src/kioworkers/trash/kinterprocesslock.cpp