Debian Codegraph @

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