| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2023 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _FCITX4INPUTCONTEXTPROXY_P_H_ |
| 8 |
#define _FCITX4INPUTCONTEXTPROXY_P_H_ |
| 9 |
|
| 10 |
#include "fcitx4inputcontextproxy.h" |
| 11 |
#include "fcitx4inputcontextproxyimpl.h" |
| 12 |
#include "fcitx4inputmethodproxy.h" |
| 13 |
#include "fcitx4watcher.h" |
| 14 |
#include <QDBusServiceWatcher> |
| 15 |
#include <cstddef> |
| 16 |
#include <unistd.h> |
| 17 |
|
| 18 |
namespace fcitx { |
| 19 |
|
| 20 |
class Fcitx4InputContextProxyPrivate { |
| 21 |
public: |
| 22 |
Fcitx4InputContextProxyPrivate(Fcitx4Watcher *watcher, |
| 23 |
Fcitx4InputContextProxy *q) |
| 24 |
: q_ptr(q), fcitxWatcher_(watcher), watcher_(q) { |
| 25 |
registerFcitxQtDBusTypes(); |
| 26 |
QObject::connect(fcitxWatcher_, &Fcitx4Watcher::availabilityChanged, q, |
| 27 |
[this]() { availabilityChanged(); }); |
| 28 |
watcher_.setWatchMode(QDBusServiceWatcher::WatchForUnregistration); |
| 29 |
QObject::connect(&watcher_, &QDBusServiceWatcher::serviceUnregistered, |
| 30 |
q, [this]() { |
| 31 |
cleanUp(); |
| 32 |
availabilityChanged(); |
| 33 |
}); |
| 34 |
availabilityChanged(); |
| 35 |
} |
| 36 |
|
| 37 |
~Fcitx4InputContextProxyPrivate() { |
| 38 |
if (isValid()) { |
| 39 |
icproxy_->DestroyIC(); |
| 40 |
} |
| 41 |
cleanUp(); |
| 42 |
} |
| 43 |
|
| 44 |
bool isValid() const { return (icproxy_ && icproxy_->isValid()); } |
| 45 |
|
| 46 |
void availabilityChanged() { |
| 47 |
QTimer::singleShot(100, q_ptr, [this]() { recheck(); }); |
| 48 |
} |
| 49 |
|
| 50 |
void recheck() { |
| 51 |
if (!isValid() && fcitxWatcher_->availability()) { |
| 52 |
createInputContext(); |
| 53 |
} |
| 54 |
if (!fcitxWatcher_->availability()) { |
| 55 |
cleanUp(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void cleanUp() { |
| 60 |
auto services = watcher_.watchedServices(); |
| 61 |
for (const auto &service : services) { |
| 62 |
watcher_.removeWatchedService(service); |
| 63 |
} |
| 64 |
|
| 65 |
delete improxy_; |
| 66 |
improxy_ = nullptr; |
| 67 |
delete icproxy_; |
| 68 |
icproxy_ = nullptr; |
| 69 |
delete createInputContextWatcher_; |
| 70 |
createInputContextWatcher_ = nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
void createInputContext() { |
| 74 |
Q_Q(Fcitx4InputContextProxy); |
| 75 |
if (!fcitxWatcher_->availability()) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
cleanUp(); |
| 80 |
|
| 81 |
auto service = fcitxWatcher_->service(); |
| 82 |
auto connection = fcitxWatcher_->connection(); |
| 83 |
|
| 84 |
auto owner = connection.interface()->serviceOwner(service); |
| 85 |
if (!owner.isValid()) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
watcher_.setConnection(connection); |
| 90 |
watcher_.setWatchedServices(QStringList() << owner); |
| 91 |
// Avoid race, query again. |
| 92 |
if (!connection.interface()->isServiceRegistered(owner)) { |
| 93 |
cleanUp(); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
QFileInfo info(QCoreApplication::applicationFilePath()); |
| 98 |
improxy_ = |
| 99 |
new Fcitx4InputMethodProxy(owner, "/inputmethod", connection, q); |
| 100 |
|
| 101 |
auto result = improxy_->CreateICv3(info.fileName(), getpid()); |
| 102 |
createInputContextWatcher_ = new QDBusPendingCallWatcher(result); |
| 103 |
QObject::connect(createInputContextWatcher_, |
| 104 |
&QDBusPendingCallWatcher::finished, q, |
| 105 |
[this]() { createInputContextFinished(); }); |
| 106 |
} |
| 107 |
|
| 108 |
void createInputContextFinished() { |
| 109 |
Q_Q(Fcitx4InputContextProxy); |
| 110 |
if (createInputContextWatcher_->isError()) { |
| 111 |
cleanUp(); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
QDBusPendingReply<int, bool, unsigned int, unsigned int, unsigned int, |
| 116 |
unsigned int> |
| 117 |
reply(*createInputContextWatcher_); |
| 118 |
|
| 119 |
QString path = QStringLiteral("/inputcontext_%1").arg(reply.value()); |
| 120 |
icproxy_ = new Fcitx4InputContextProxyImpl(improxy_->service(), path, |
| 121 |
improxy_->connection(), q); |
| 122 |
QObject::connect(icproxy_, &Fcitx4InputContextProxyImpl::CommitString, |
| 123 |
q, &Fcitx4InputContextProxy::commitString); |
| 124 |
QObject::connect(icproxy_, &Fcitx4InputContextProxyImpl::CurrentIM, q, |
| 125 |
&Fcitx4InputContextProxy::currentIM); |
| 126 |
QObject::connect(icproxy_, |
| 127 |
&Fcitx4InputContextProxyImpl::DeleteSurroundingText, q, |
| 128 |
&Fcitx4InputContextProxy::deleteSurroundingText); |
| 129 |
QObject::connect(icproxy_, &Fcitx4InputContextProxyImpl::ForwardKey, q, |
| 130 |
&Fcitx4InputContextProxy::forwardKey); |
| 131 |
QObject::connect(icproxy_, |
| 132 |
&Fcitx4InputContextProxyImpl::UpdateFormattedPreedit, |
| 133 |
q, &Fcitx4InputContextProxy::updateFormattedPreedit); |
| 134 |
|
| 135 |
delete createInputContextWatcher_; |
| 136 |
createInputContextWatcher_ = nullptr; |
| 137 |
Q_EMIT q->inputContextCreated(); |
| 138 |
} |
| 139 |
|
| 140 |
Fcitx4InputContextProxy *q_ptr; |
| 141 |
Q_DECLARE_PUBLIC(Fcitx4InputContextProxy); |
| 142 |
|
| 143 |
Fcitx4Watcher *fcitxWatcher_; |
| 144 |
QDBusServiceWatcher watcher_; |
| 145 |
Fcitx4InputMethodProxy *improxy_ = nullptr; |
| 146 |
Fcitx4InputContextProxyImpl *icproxy_ = nullptr; |
| 147 |
QDBusPendingCallWatcher *createInputContextWatcher_ = nullptr; |
| 148 |
QString display_; |
| 149 |
}; |
| 150 |
} // namespace fcitx |
| 151 |
|
| 152 |
#endif // _FCITX4INPUTCONTEXTPROXY_P_H_ |