File: fcitxqtinputcontextproxy_p.h

package info (click to toggle)
fcitx5-qt 5.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,340 kB
  • sloc: cpp: 11,697; xml: 243; ansic: 46; makefile: 14; sh: 9
file content (218 lines) | stat: -rw-r--r-- 7,792 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#ifndef _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_
#define _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_

#include "fcitxqtinputcontextproxy.h"
#include "fcitxqtinputcontextproxyimpl.h"
#include "fcitxqtinputmethodproxy.h"
#include "fcitxqtwatcher.h"
#include <QDBusServiceWatcher>

namespace fcitx {

class FcitxQtInputContextProxyPrivate {
public:
    FcitxQtInputContextProxyPrivate(FcitxQtWatcher *watcher,
                                    FcitxQtInputContextProxy *q)
        : q_ptr(q), fcitxWatcher_(watcher), watcher_(q) {
        registerFcitxQtDBusTypes();
        QObject::connect(fcitxWatcher_, SIGNAL(availabilityChanged(bool)), q,
                         SLOT(availabilityChanged()));
        watcher_.setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
        QObject::connect(&watcher_, SIGNAL(serviceUnregistered(QString)), q,
                         SLOT(serviceUnregistered()));
        availabilityChanged();
    }

    ~FcitxQtInputContextProxyPrivate() {
        if (isValid()) {
            icproxy_->DestroyIC();
        }
    }

    bool isValid() const { return (icproxy_ && icproxy_->isValid()); }

    void serviceUnregistered() {
        cleanUp();
        availabilityChanged();
    }

    void availabilityChanged() {
        QTimer::singleShot(100, q_ptr, SLOT(recheck()));
    }

    void recheck() {
        if (!isValid() && fcitxWatcher_->availability()) {
            createInputContext();
        }
        if (!fcitxWatcher_->availability()) {
            cleanUp();
        }
    }

    void cleanUp() {
        auto services = watcher_.watchedServices();
        for (const auto &service : services) {
            watcher_.removeWatchedService(service);
        }

        delete improxy_;
        improxy_ = nullptr;
        delete icproxy_;
        icproxy_ = nullptr;
        delete createInputContextWatcher_;
        createInputContextWatcher_ = nullptr;
        delete introspectWatcher_;
        introspectWatcher_ = nullptr;
        supportInvokeAction_ = false;
    }

    void createInputContext() {
        Q_Q(FcitxQtInputContextProxy);
        if (!fcitxWatcher_->availability()) {
            return;
        }

        cleanUp();

        auto service = fcitxWatcher_->serviceName();
        auto connection = fcitxWatcher_->connection();

        auto owner = connection.interface()->serviceOwner(service);
        if (!owner.isValid()) {
            return;
        }

        watcher_.setConnection(connection);
        watcher_.setWatchedServices(QStringList() << owner);
        // Avoid race, query again.
        if (!connection.interface()->isServiceRegistered(owner)) {
            cleanUp();
            return;
        }

        QFileInfo info(QCoreApplication::applicationFilePath());
        portal_ = true;
        improxy_ = new FcitxQtInputMethodProxy(
            owner, "/org/freedesktop/portal/inputmethod", connection, q);
        FcitxQtStringKeyValueList list;
        FcitxQtStringKeyValue arg;
        arg.setKey("program");
        arg.setValue(info.fileName());
        list << arg;
        if (!display_.isEmpty()) {
            FcitxQtStringKeyValue arg2;
            arg2.setKey("display");
            arg2.setValue(display_);
            list << arg2;
        }
        // Qt has good support for showing virtual keyboard, so we should
        // disable the default behavior supported by fcitx5
        FcitxQtStringKeyValue clientControlVirtualkeyboardShow;
        clientControlVirtualkeyboardShow.setKey(
            "clientControlVirtualkeyboardShow");
        clientControlVirtualkeyboardShow.setValue("true");
        list << clientControlVirtualkeyboardShow;
        // Qt has poor support for hiding virtual keyboard, so we should enable
        // the default behavior supported by fcitx5
        FcitxQtStringKeyValue clientControlVirtualkeyboardHide;
        clientControlVirtualkeyboardHide.setKey(
            "clientControlVirtualkeyboardHide");
        clientControlVirtualkeyboardHide.setValue("false");
        list << clientControlVirtualkeyboardHide;

        auto result = improxy_->CreateInputContext(list);
        createInputContextWatcher_ = new QDBusPendingCallWatcher(result);
        QObject::connect(createInputContextWatcher_,
                         SIGNAL(finished(QDBusPendingCallWatcher *)), q,
                         SLOT(createInputContextFinished()));
    }

    void createInputContextFinished() {
        Q_Q(FcitxQtInputContextProxy);
        if (createInputContextWatcher_->isError()) {
            cleanUp();
            return;
        }

        QDBusPendingReply<QDBusObjectPath, QByteArray> reply(
            *createInputContextWatcher_);
        icproxy_ = new FcitxQtInputContextProxyImpl(improxy_->service(),
                                                    reply.value().path(),
                                                    improxy_->connection(), q);
        QObject::connect(icproxy_, SIGNAL(CommitString(QString)), q,
                         SIGNAL(commitString(QString)));
        QObject::connect(icproxy_, SIGNAL(CurrentIM(QString, QString, QString)),
                         q, SIGNAL(currentIM(QString, QString, QString)));
        QObject::connect(
            icproxy_, SIGNAL(ForwardKey(unsigned int, unsigned int, bool)), q,
            SIGNAL(forwardKey(unsigned int, unsigned int, bool)));
        QObject::connect(
            icproxy_,
            SIGNAL(UpdateFormattedPreedit(FcitxQtFormattedPreeditList, int)), q,
            SIGNAL(updateFormattedPreedit(FcitxQtFormattedPreeditList, int)));
        QObject::connect(icproxy_,
                         SIGNAL(DeleteSurroundingText(int, unsigned int)), q,
                         SIGNAL(deleteSurroundingText(int, unsigned int)));
        QObject::connect(icproxy_, SIGNAL(NotifyFocusOut()), q,
                         SIGNAL(notifyFocusOut()));

        delete createInputContextWatcher_;
        createInputContextWatcher_ = nullptr;
        Q_EMIT q->inputContextCreated(reply.argumentAt<1>());

        introspect();
    }

    void introspect() {
        Q_Q(FcitxQtInputContextProxy);
        if (introspectWatcher_) {
            delete introspectWatcher_;
            introspectWatcher_ = nullptr;
        }
        QDBusMessage call = QDBusMessage::createMethodCall(
            icproxy_->service(), icproxy_->path(),
            "org.freedesktop.DBus.Introspectable", "Introspect");

        introspectWatcher_ = new QDBusPendingCallWatcher(
            fcitxWatcher_->connection().asyncCall(call));
        QObject::connect(introspectWatcher_,
                         SIGNAL(finished(QDBusPendingCallWatcher *)), q,
                         SLOT(introspectFinished()));
    }

    void introspectFinished() {
        if (introspectWatcher_->isFinished() &&
            !introspectWatcher_->isError()) {
            QDBusPendingReply<QString> reply = *introspectWatcher_;

            if (reply.value().contains("InvokeAction")) {
                supportInvokeAction_ = true;
            }
        }
        delete introspectWatcher_;
        introspectWatcher_ = nullptr;
    }

    FcitxQtInputContextProxy *q_ptr;
    Q_DECLARE_PUBLIC(FcitxQtInputContextProxy);

    FcitxQtWatcher *fcitxWatcher_;
    QDBusServiceWatcher watcher_;
    FcitxQtInputMethodProxy *improxy_ = nullptr;
    FcitxQtInputContextProxyImpl *icproxy_ = nullptr;
    bool supportInvokeAction_ = false;
    QDBusPendingCallWatcher *createInputContextWatcher_ = nullptr;
    QDBusPendingCallWatcher *introspectWatcher_ = nullptr;
    QString display_;
    bool portal_ = false;
};
} // namespace fcitx

#endif // _DBUSADDONS_FCITXQTINPUTCONTEXTPROXY_P_H_