Debian Codegraph @

qt4/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_,
77 SIGNAL(serviceOwnerChanged(QString, QString, QString)), this,
78 SLOT(imChanged(QString, QString, QString)));
79 d->serviceWatcher_.addWatchedService(FCITX_MAIN_SERVICE_NAME);
80 if (d->watchPortal_) {
81 d->serviceWatcher_.addWatchedService(FCITX_PORTAL_SERVICE_NAME);
82 }
83
84 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(
85 FCITX_MAIN_SERVICE_NAME)) {
86 d->mainPresent_ = true;
87 }
88 if (d->watchPortal_ &&
89 QDBusConnection::sessionBus().interface()->isServiceRegistered(
90 FCITX_PORTAL_SERVICE_NAME)) {
91 d->portalPresent_ = true;
92 }
93
94 updateAvailability();
95
96 d->watched_ = true;
97 }
98
99 void FcitxQtWatcher::unwatch() {
100 Q_D(FcitxQtWatcher);
101 if (!d->watched_) {
102 return;
103 }
104 disconnect(&d->serviceWatcher_,
105 SIGNAL(serviceOwnerChanged(QString, QString, QString)), this,
106 SLOT(imChanged(QString, QString, QString)));
107 d->mainPresent_ = false;
108 d->portalPresent_ = false;
109 d->watched_ = false;
110 updateAvailability();
111 }
112
113 bool FcitxQtWatcher::isWatching() const {
114 Q_D(const FcitxQtWatcher);
115 return d->watched_;
116 }
117
118 void FcitxQtWatcher::imChanged(const QString &service, const QString &,
119 const QString &newOwner) {
120 Q_D(FcitxQtWatcher);
121 if (service == FCITX_MAIN_SERVICE_NAME) {
122 if (!newOwner.isEmpty()) {
123 d->mainPresent_ = true;
124 } else {
125 d->mainPresent_ = false;
126 }
127 } else if (service == FCITX_PORTAL_SERVICE_NAME) {
128 if (!newOwner.isEmpty()) {
129 d->portalPresent_ = true;
130 } else {
131 d->portalPresent_ = false;
132 }
133 }
134
135 updateAvailability();
136 }
137
138 void FcitxQtWatcher::updateAvailability() {
139 Q_D(FcitxQtWatcher);
140 setAvailability(d->mainPresent_ || d->portalPresent_);
141 }
142 } // namespace fcitx