| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |
| 9 |
#define _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |
| 10 |
|
| 11 |
#include "fcitxqtinputcontextproxy.h" |
| 12 |
#include "fcitxqtinputcontextproxyimpl.h" |
| 13 |
#include "fcitxqtinputmethodproxy.h" |
| 14 |
#include "fcitxqtwatcher.h" |
| 15 |
#include <QDBusServiceWatcher> |
| 16 |
|
| 17 |
namespace fcitx { |
| 18 |
|
| 19 |
class FcitxQtInputContextProxyPrivate { |
| 20 |
public: |
| 21 |
FcitxQtInputContextProxyPrivate(FcitxQtWatcher *watcher, |
| 22 |
FcitxQtInputContextProxy *q) |
| 23 |
: q_ptr(q), fcitxWatcher_(watcher), watcher_(q) { |
| 24 |
registerFcitxQtDBusTypes(); |
| 25 |
QObject::connect(fcitxWatcher_, SIGNAL(availabilityChanged(bool)), q, |
| 26 |
SLOT(availabilityChanged())); |
| 27 |
watcher_.setWatchMode(QDBusServiceWatcher::WatchForUnregistration); |
| 28 |
QObject::connect(&watcher_, SIGNAL(serviceUnregistered(QString)), q, |
| 29 |
SLOT(serviceUnregistered())); |
| 30 |
availabilityChanged(); |
| 31 |
} |
| 32 |
|
| 33 |
~FcitxQtInputContextProxyPrivate() { |
| 34 |
if (isValid()) { |
| 35 |
icproxy_->DestroyIC(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
bool isValid() const { return (icproxy_ && icproxy_->isValid()); } |
| 40 |
|
| 41 |
void serviceUnregistered() { |
| 42 |
cleanUp(); |
| 43 |
availabilityChanged(); |
| 44 |
} |
| 45 |
|
| 46 |
void availabilityChanged() { |
| 47 |
QTimer::singleShot(100, q_ptr, SLOT(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 |
delete introspectWatcher_; |
| 72 |
introspectWatcher_ = nullptr; |
| 73 |
supportInvokeAction_ = false; |
| 74 |
} |
| 75 |
|
| 76 |
void createInputContext() { |
| 77 |
Q_Q(FcitxQtInputContextProxy); |
| 78 |
if (!fcitxWatcher_->availability()) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
cleanUp(); |
| 83 |
|
| 84 |
auto service = fcitxWatcher_->serviceName(); |
| 85 |
auto connection = fcitxWatcher_->connection(); |
| 86 |
|
| 87 |
auto owner = connection.interface()->serviceOwner(service); |
| 88 |
if (!owner.isValid()) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
watcher_.setConnection(connection); |
| 93 |
watcher_.setWatchedServices(QStringList() << owner); |
| 94 |
// Avoid race, query again. |
| 95 |
if (!connection.interface()->isServiceRegistered(owner)) { |
| 96 |
cleanUp(); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
QFileInfo info(QCoreApplication::applicationFilePath()); |
| 101 |
portal_ = true; |
| 102 |
improxy_ = new FcitxQtInputMethodProxy( |
| 103 |
owner, "/org/freedesktop/portal/inputmethod", connection, q); |
| 104 |
FcitxQtStringKeyValueList list; |
| 105 |
FcitxQtStringKeyValue arg; |
| 106 |
arg.setKey("program"); |
| 107 |
arg.setValue(info.fileName()); |
| 108 |
list << arg; |
| 109 |
if (!display_.isEmpty()) { |
| 110 |
FcitxQtStringKeyValue arg2; |
| 111 |
arg2.setKey("display"); |
| 112 |
arg2.setValue(display_); |
| 113 |
list << arg2; |
| 114 |
} |
| 115 |
// Qt has good support for showing virtual keyboard, so we should |
| 116 |
// disable the default behavior supported by fcitx5 |
| 117 |
FcitxQtStringKeyValue clientControlVirtualkeyboardShow; |
| 118 |
clientControlVirtualkeyboardShow.setKey( |
| 119 |
"clientControlVirtualkeyboardShow"); |
| 120 |
clientControlVirtualkeyboardShow.setValue("true"); |
| 121 |
list << clientControlVirtualkeyboardShow; |
| 122 |
// Qt has poor support for hiding virtual keyboard, so we should enable |
| 123 |
// the default behavior supported by fcitx5 |
| 124 |
FcitxQtStringKeyValue clientControlVirtualkeyboardHide; |
| 125 |
clientControlVirtualkeyboardHide.setKey( |
| 126 |
"clientControlVirtualkeyboardHide"); |
| 127 |
clientControlVirtualkeyboardHide.setValue("false"); |
| 128 |
list << clientControlVirtualkeyboardHide; |
| 129 |
|
| 130 |
auto result = improxy_->CreateInputContext(list); |
| 131 |
createInputContextWatcher_ = new QDBusPendingCallWatcher(result); |
| 132 |
QObject::connect(createInputContextWatcher_, |
| 133 |
SIGNAL(finished(QDBusPendingCallWatcher *)), q, |
| 134 |
SLOT(createInputContextFinished())); |
| 135 |
} |
| 136 |
|
| 137 |
void createInputContextFinished() { |
| 138 |
Q_Q(FcitxQtInputContextProxy); |
| 139 |
if (createInputContextWatcher_->isError()) { |
| 140 |
cleanUp(); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
QDBusPendingReply<QDBusObjectPath, QByteArray> reply( |
| 145 |
*createInputContextWatcher_); |
| 146 |
icproxy_ = new FcitxQtInputContextProxyImpl(improxy_->service(), |
| 147 |
reply.value().path(), |
| 148 |
improxy_->connection(), q); |
| 149 |
QObject::connect(icproxy_, SIGNAL(CommitString(QString)), q, |
| 150 |
SIGNAL(commitString(QString))); |
| 151 |
QObject::connect(icproxy_, SIGNAL(CurrentIM(QString, QString, QString)), |
| 152 |
q, SIGNAL(currentIM(QString, QString, QString))); |
| 153 |
QObject::connect( |
| 154 |
icproxy_, SIGNAL(ForwardKey(unsigned int, unsigned int, bool)), q, |
| 155 |
SIGNAL(forwardKey(unsigned int, unsigned int, bool))); |
| 156 |
QObject::connect( |
| 157 |
icproxy_, |
| 158 |
SIGNAL(UpdateFormattedPreedit(FcitxQtFormattedPreeditList, int)), q, |
| 159 |
SIGNAL(updateFormattedPreedit(FcitxQtFormattedPreeditList, int))); |
| 160 |
QObject::connect(icproxy_, |
| 161 |
SIGNAL(DeleteSurroundingText(int, unsigned int)), q, |
| 162 |
SIGNAL(deleteSurroundingText(int, unsigned int))); |
| 163 |
QObject::connect(icproxy_, SIGNAL(NotifyFocusOut()), q, |
| 164 |
SIGNAL(notifyFocusOut())); |
| 165 |
|
| 166 |
delete createInputContextWatcher_; |
| 167 |
createInputContextWatcher_ = nullptr; |
| 168 |
Q_EMIT q->inputContextCreated(reply.argumentAt<1>()); |
| 169 |
|
| 170 |
introspect(); |
| 171 |
} |
| 172 |
|
| 173 |
void introspect() { |
| 174 |
Q_Q(FcitxQtInputContextProxy); |
| 175 |
if (introspectWatcher_) { |
| 176 |
delete introspectWatcher_; |
| 177 |
introspectWatcher_ = nullptr; |
| 178 |
} |
| 179 |
QDBusMessage call = QDBusMessage::createMethodCall( |
| 180 |
icproxy_->service(), icproxy_->path(), |
| 181 |
"org.freedesktop.DBus.Introspectable", "Introspect"); |
| 182 |
|
| 183 |
introspectWatcher_ = new QDBusPendingCallWatcher( |
| 184 |
fcitxWatcher_->connection().asyncCall(call)); |
| 185 |
QObject::connect(introspectWatcher_, |
| 186 |
SIGNAL(finished(QDBusPendingCallWatcher *)), q, |
| 187 |
SLOT(introspectFinished())); |
| 188 |
} |
| 189 |
|
| 190 |
void introspectFinished() { |
| 191 |
if (introspectWatcher_->isFinished() && |
| 192 |
!introspectWatcher_->isError()) { |
| 193 |
QDBusPendingReply<QString> reply = *introspectWatcher_; |
| 194 |
|
| 195 |
if (reply.value().contains("InvokeAction")) { |
| 196 |
supportInvokeAction_ = true; |
| 197 |
} |
| 198 |
} |
| 199 |
delete introspectWatcher_; |
| 200 |
introspectWatcher_ = nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
FcitxQtInputContextProxy *q_ptr; |
| 204 |
Q_DECLARE_PUBLIC(FcitxQtInputContextProxy); |
| 205 |
|
| 206 |
FcitxQtWatcher *fcitxWatcher_; |
| 207 |
QDBusServiceWatcher watcher_; |
| 208 |
FcitxQtInputMethodProxy *improxy_ = nullptr; |
| 209 |
FcitxQtInputContextProxyImpl *icproxy_ = nullptr; |
| 210 |
bool supportInvokeAction_ = false; |
| 211 |
QDBusPendingCallWatcher *createInputContextWatcher_ = nullptr; |
| 212 |
QDBusPendingCallWatcher *introspectWatcher_ = nullptr; |
| 213 |
QString display_; |
| 214 |
bool portal_ = false; |
| 215 |
}; |
| 216 |
} // namespace fcitx |
| 217 |
|
| 218 |
#endif // _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_ |