| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _CLOUDPINYIN_CLOUDPINYIN_H_ |
| 8 |
#define _CLOUDPINYIN_CLOUDPINYIN_H_ |
| 9 |
|
| 10 |
#include "cloudpinyin_public.h" |
| 11 |
#include "fetch.h" |
| 12 |
#include "lrucache.h" |
| 13 |
#include <fcitx-config/configuration.h> |
| 14 |
#include <fcitx-config/enum.h> |
| 15 |
#include <fcitx-config/iniparser.h> |
| 16 |
#include <fcitx-utils/eventdispatcher.h> |
| 17 |
#include <fcitx-utils/i18n.h> |
| 18 |
#include <fcitx-utils/misc.h> |
| 19 |
#include <fcitx-utils/trackableobject.h> |
| 20 |
#include <fcitx/addonfactory.h> |
| 21 |
#include <fcitx/addoninstance.h> |
| 22 |
#include <fcitx/instance.h> |
| 23 |
|
| 24 |
FCITX_CONFIG_ENUM(CloudPinyinBackend, Google, GoogleCN, Baidu); |
| 25 |
FCITX_CONFIGURATION( |
| 26 |
CloudPinyinConfig, |
| 27 |
fcitx::Option<fcitx::KeyList> toggleKey{ |
| 28 |
this, |
| 29 |
"Toggle Key", |
| 30 |
_("Toggle Key"), |
| 31 |
{fcitx::Key("Control+Alt+Shift+C")}}; |
| 32 |
fcitx::Option<int> minimumLength{this, "MinimumPinyinLength", |
| 33 |
_("Minimum Pinyin Length"), 4}; |
| 34 |
fcitx::Option<CloudPinyinBackend> backend{this, "Backend", _("Backend"), |
| 35 |
CloudPinyinBackend::GoogleCN}; |
| 36 |
fcitx::OptionWithAnnotation<std::string, fcitx::ToolTipAnnotation> proxy{ |
| 37 |
this, |
| 38 |
"Proxy", |
| 39 |
_("Proxy"), |
| 40 |
"", |
| 41 |
{}, |
| 42 |
{}, |
| 43 |
{_("The proxy format must be the one that is supported by cURL. " |
| 44 |
"Usually it is in the format of [scheme]://[host]:[port], e.g. " |
| 45 |
"http://localhost:1080.")}};); |
| 46 |
|
| 47 |
class Backend { |
| 48 |
public: |
| 49 |
FCITX_NODISCARD virtual bool prepareRequest(CurlQueue *queue, |
| 50 |
const std::string &pinyin) = 0; |
| 51 |
virtual std::string parseResult(CurlQueue *queue) = 0; |
| 52 |
virtual ~Backend() = default; |
| 53 |
}; |
| 54 |
|
| 55 |
class CloudPinyin : public fcitx::AddonInstance, |
| 56 |
public fcitx::TrackableObject<CloudPinyin> { |
| 57 |
public: |
| 58 |
CloudPinyin(fcitx::AddonManager *manager); |
| 59 |
~CloudPinyin() override; |
| 60 |
|
| 61 |
void reloadConfig() override; |
| 62 |
const fcitx::Configuration *getConfig() const override { return &config_; } |
| 63 |
void setConfig(const fcitx::RawConfig &config) override { |
| 64 |
config_.load(config, true); |
| 65 |
fcitx::safeSaveAsIni(config_, "conf/cloudpinyin.conf"); |
| 66 |
} |
| 67 |
|
| 68 |
void request(const std::string &pinyin, CloudPinyinCallback callback); |
| 69 |
const fcitx::KeyList &toggleKey() const { |
| 70 |
return config_.toggleKey.value(); |
| 71 |
} |
| 72 |
void resetError() { |
| 73 |
errorCount_ = 0; |
| 74 |
resetError_->setEnabled(false); |
| 75 |
} |
| 76 |
|
| 77 |
void notifyFinished(); |
| 78 |
|
| 79 |
private: |
| 80 |
FCITX_ADDON_EXPORT_FUNCTION(CloudPinyin, request); |
| 81 |
FCITX_ADDON_EXPORT_FUNCTION(CloudPinyin, toggleKey); |
| 82 |
FCITX_ADDON_EXPORT_FUNCTION(CloudPinyin, resetError); |
| 83 |
std::unique_ptr<FetchThread> thread_; |
| 84 |
fcitx::EventLoop *eventLoop_; |
| 85 |
fcitx::EventDispatcher &dispatcher_; |
| 86 |
std::unique_ptr<fcitx::EventSourceIO> event_; |
| 87 |
std::unique_ptr<fcitx::EventSourceTime> resetError_; |
| 88 |
LRUCache<std::string, std::string> cache_{2048}; |
| 89 |
std::unordered_map<CloudPinyinBackend, std::unique_ptr<Backend>, |
| 90 |
fcitx::EnumHash> |
| 91 |
backends_; |
| 92 |
CloudPinyinConfig config_; |
| 93 |
int errorCount_ = 0; |
| 94 |
}; |
| 95 |
|
| 96 |
class CloudPinyinFactory : public fcitx::AddonFactory { |
| 97 |
public: |
| 98 |
fcitx::AddonInstance *create(fcitx::AddonManager *manager) override { |
| 99 |
return new CloudPinyin(manager); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
#endif // _CLOUDPINYIN_CLOUDPINYIN_H_ |