Debian Codegraph @

qt6/dbusaddons/fcitxqtwatcher.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #include "fcitxqtwatcher_p.h"
9 #include <QDBusConnection>
10 #include <QDBusConnectionInterface>
11 #include <QDBusServiceWatcher>
12
13 namespace fcitx {
14
15 FcitxQtWatcher::FcitxQtWatcher(QObject *parent)
16 : QObject(parent), d_ptr(new FcitxQtWatcherPrivate(this)) {}
17
18 FcitxQtWatcher::FcitxQtWatcher(const QDBusConnection &connection,
19 QObject *parent)
20 : FcitxQtWatcher(parent) {
21 setConnection(connection);
22 }
23
24 FcitxQtWatcher::~FcitxQtWatcher() { delete d_ptr; }
25
26 bool FcitxQtWatcher::availability() const {
27 Q_D(const FcitxQtWatcher);
28 return d->availability_;
29 }
30
31 void FcitxQtWatcher::setConnection(const QDBusConnection &connection) {
32 Q_D(FcitxQtWatcher);
33 return d->serviceWatcher_.setConnection(connection);
34 }
35
36 QDBusConnection FcitxQtWatcher::connection() const {
37 Q_D(const FcitxQtWatcher);
38 return d->serviceWatcher_.connection();
39 }
40
41 void FcitxQtWatcher::setWatchPortal(bool portal) {
42 Q_D(FcitxQtWatcher);
43 d->watchPortal_ = portal;
44 }
45
46 bool FcitxQtWatcher::watchPortal() const {
47 Q_D(const FcitxQtWatcher);
48 return d->watchPortal_;
49 }
50
51 QString FcitxQtWatcher::serviceName() const {
52 Q_D(const FcitxQtWatcher);
53 if (d->mainPresent_) {
54 return FCITX_MAIN_SERVICE_NAME;
55 }
56 if (d->portalPresent_) {
57 return FCITX_PORTAL_SERVICE_NAME;
58 }
59 return QString();
60 }
61
62 void FcitxQtWatcher::setAvailability(bool availability) {
63 Q_D(FcitxQtWatcher);
64 if (d->availability_ != availability) {
65 d->availability_ = availability;
66 Q_EMIT availabilityChanged(d->availability_);
67 }
68 }
69
70 void FcitxQtWatcher::watch() {
71 Q_D(FcitxQtWatcher);
72 if (d->watched_) {
73 return;
74 }
75
76 connect(&d->serviceWatcher_, &QDBusServiceWatcher::serviceOwnerChanged,
77 this, &FcitxQtWatcher::imChanged);
78 d->serviceWatcher_.addWatchedService(FCITX_MAIN_SERVICE_NAME);
79 if (d->watchPortal_) {
80 d->serviceWatcher_.addWatchedService(FCITX_PORTAL_SERVICE_NAME);
81 }
82
83 if (connection().interface()->isServiceRegistered(
84 FCITX_MAIN_SERVICE_NAME)) {
85 d->mainPresent_ = true;
86 }
87 if (d->watchPortal_ && connection().interface()->isServiceRegistered(
88 FCITX_PORTAL_SERVICE_NAME)) {
89 d->portalPresent_ = true;
90 }
91
92 updateAvailability();
93
94 d->watched_ = true;
95 }
96
97 void FcitxQtWatcher::unwatch() {
98 Q_D(FcitxQtWatcher);
99 if (!d->watched_) {
100 return;
101 }
102 disconnect(&d->serviceWatcher_, &QDBusServiceWatcher::serviceOwnerChanged,
103 this, &FcitxQtWatcher::imChanged);
104 d->mainPresent_ = false;
105 d->portalPresent_ = false;
106 d->watched_ = false;
107 updateAvailability();
108 }
109
110 bool FcitxQtWatcher::isWatching() const {
111 Q_D(const FcitxQtWatcher);
112 return d->watched_;
113 }
114
115 void FcitxQtWatcher::imChanged(const QString &service, const QString &,
116 const QString &newOwner) {
117 Q_D(FcitxQtWatcher);
118 if (service == FCITX_MAIN_SERVICE_NAME) {
119 if (!newOwner.isEmpty()) {
120 d->mainPresent_ = true;
121 } else {
122 d->mainPresent_ = false;
123 }
124 } else if (service == FCITX_PORTAL_SERVICE_NAME) {
125 if (!newOwner.isEmpty()) {
126 d->portalPresent_ = true;
127 } else {
128 d->portalPresent_ = false;
129 }
130 }
131
132 updateAvailability();
133 }
134
135 void FcitxQtWatcher::updateAvailability() {
136 Q_D(FcitxQtWatcher);
137 setAvailability(d->mainPresent_ || d->portalPresent_);
138 }
139 } // namespace fcitx