Skip to content

Commit 9de0de1

Browse files
authored
don't panic when context failed to setup (#51)
* don't panic when context failed to setup
1 parent a2f3421 commit 9de0de1

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

cmake/DebPack.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CPACK_PACKAGE_CONTACT "[email protected]")
2020
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Naoaki Iwakiri")
2121

2222
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libcskk (>= 0.8.0), fcitx5 (>=5.0.6)")
23+
set(CPACK_DEBIAN_PACKAGE_SUGGESTS "skkdic, skkdic-extra")
2324

2425
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
2526
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")

src/cskk.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,17 @@ void FcitxCskkEngine::save() {
5959
if (factory_.registered()) {
6060
instance_->inputContextManager().foreach ([this](InputContext *ic) {
6161
auto context = ic->propertyFor(&factory_);
62-
skk_context_save_dictionaries(context->context());
63-
return true;
62+
return context->saveDictionary();
6463
});
6564
}
6665
}
6766
void FcitxCskkEngine::activate(const InputMethodEntry &, InputContextEvent &) {}
6867
void FcitxCskkEngine::deactivate(const InputMethodEntry &entry,
6968
InputContextEvent &event) {
70-
FCITX_UNUSED(entry);
7169
reset(entry, event);
7270
}
73-
void FcitxCskkEngine::reset(const InputMethodEntry &entry,
71+
void FcitxCskkEngine::reset(const InputMethodEntry &,
7472
InputContextEvent &event) {
75-
FCITX_UNUSED(entry);
7673
CSKK_DEBUG() << "Reset";
7774
auto ic = event.inputContext();
7875
auto context = ic->propertyFor(&factory_);
@@ -229,7 +226,7 @@ KeyList FcitxCskkEngine::getSelectionKeys(
229226
std::string FcitxCskkEngine::subModeIconImpl(const InputMethodEntry &,
230227
InputContext &ic) {
231228
auto context = ic.propertyFor(&factory_);
232-
auto current_input_mode = skk_context_get_input_mode(context->context());
229+
auto current_input_mode = context->getInputMode();
233230
switch (current_input_mode) {
234231
case InputMode::Ascii:
235232
return "cskk-ascii";
@@ -241,9 +238,11 @@ std::string FcitxCskkEngine::subModeIconImpl(const InputMethodEntry &,
241238
return "cskk-katakana";
242239
case InputMode::Zenkaku:
243240
return "cskk-zenei";
241+
default:
242+
return "";
244243
}
245-
return "";
246244
}
245+
bool FcitxCskkEngine::isEngineReady() { return factory_.registered(); }
247246

248247
/*******************************************************************************
249248
* CskkContext
@@ -252,10 +251,17 @@ std::string FcitxCskkEngine::subModeIconImpl(const InputMethodEntry &,
252251
FcitxCskkContext::FcitxCskkContext(FcitxCskkEngine *engine, InputContext *ic)
253252
: context_(skk_context_new(nullptr, 0)), ic_(ic), engine_(engine) {
254253
CSKK_DEBUG() << "Cskk context new";
255-
skk_context_set_input_mode(context_, *engine_->config().inputMode);
254+
if (!context_) {
255+
// new context wasn't created
256+
CSKK_ERROR() << "Failed to create new cskk context";
257+
}
256258
}
257-
FcitxCskkContext::~FcitxCskkContext() = default;
259+
FcitxCskkContext::~FcitxCskkContext() { skk_free_context(context_); }
258260
void FcitxCskkContext::keyEvent(KeyEvent &keyEvent) {
261+
if (!context_) {
262+
CSKK_ERROR() << "CSKK Context is not setup. Ignored key.";
263+
return;
264+
}
259265
auto candidateList = std::dynamic_pointer_cast<FcitxCskkCandidateList>(
260266
ic_->inputPanel().candidateList());
261267
if (candidateList != nullptr && !candidateList->empty()) {
@@ -329,6 +335,10 @@ void FcitxCskkContext::reset() {
329335
updateUI();
330336
}
331337
void FcitxCskkContext::updateUI() {
338+
if (!context_) {
339+
CSKK_WARN() << "No context setup";
340+
return;
341+
}
332342
auto &inputPanel = ic_->inputPanel();
333343

334344
// Output
@@ -385,6 +395,10 @@ void FcitxCskkContext::updateUI() {
385395
}
386396
void FcitxCskkContext::applyConfig() {
387397
CSKK_DEBUG() << "apply config";
398+
if (!context_) {
399+
CSKK_WARN() << "No context setup. Ignoring config.";
400+
return;
401+
}
388402
auto &config = engine_->config();
389403

390404
skk_context_set_rule(context_, config.cskkRule->c_str());
@@ -398,6 +412,22 @@ void FcitxCskkContext::copyTo(InputContextProperty *context) {
398412
skk_context_set_input_mode(otherContext->context(),
399413
skk_context_get_input_mode(context_));
400414
}
415+
bool FcitxCskkContext::saveDictionary() {
416+
if (!context_) {
417+
CSKK_WARN() << "No cskk context setup. Ignored dictionary save.";
418+
return false;
419+
}
420+
skk_context_save_dictionaries(context_);
421+
// cskk v0.8 doesn't return value on save dict, return true for now.
422+
return true;
423+
}
424+
int FcitxCskkContext::getInputMode() {
425+
if (!context_) {
426+
CSKK_WARN() << "No cskk context setup. No inputmode.";
427+
return -1;
428+
}
429+
return skk_context_get_input_mode(context_);
430+
}
401431

402432
/*******************************************************************************
403433
* FcitxCskkFactory
@@ -408,7 +438,11 @@ AddonInstance *FcitxCskkFactory::create(AddonManager *manager) {
408438
CSKK_DEBUG() << "**** CSKK FcitxCskkFactory Create ****";
409439
registerDomain("fcitx5-cskk", FCITX_INSTALL_LOCALEDIR);
410440
auto engine = new FcitxCskkEngine(manager->instance());
411-
return engine;
441+
if (engine->isEngineReady()) {
442+
return engine;
443+
} else {
444+
return nullptr;
445+
}
412446
}
413447
}
414448
} // namespace fcitx

src/cskk.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class FcitxCskkEngine final : public InputMethodEngineV2 {
5959
static KeyList
6060
getSelectionKeys(CandidateSelectionKeys candidateSelectionKeys);
6161

62+
bool isEngineReady();
63+
6264
private:
6365
Instance *instance_;
6466
FactoryFor<FcitxCskkContext> factory_;
@@ -69,9 +71,6 @@ class FcitxCskkEngine final : public InputMethodEngineV2 {
6971
static const std::string config_file_path;
7072

7173
void loadDictionary();
72-
static std::string getXDGDataHome();
73-
static std::vector<std::string> getXDGDataDirs();
74-
7574
void freeDictionaries();
7675
};
7776

@@ -86,6 +85,13 @@ class FcitxCskkContext final : public InputContextProperty {
8685
void reset();
8786
void updateUI();
8887
void applyConfig();
88+
89+
bool saveDictionary();
90+
// value castable to InputMode
91+
int getInputMode();
92+
93+
// FIXME: Ideally, don't use this context() and prepare public function for
94+
// each usage to separate the responsibility.
8995
auto &context() { return context_; }
9096

9197
private:

src/log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
FCITX_DECLARE_LOG_CATEGORY(cskk_log);
2020
#define CSKK_DEBUG() FCITX_LOGC(cskk_log, Debug) << "\t**CSKK** "
2121
#define CSKK_WARN() FCITX_LOGC(cskk_log, Warn) << "\t**CSKK** "
22+
#define CSKK_ERROR() FCITX_LOGC(cskk_log, Error) << "\t**CSKK** "
2223

2324
#endif // FCITX5_CSKK_LOG_H

0 commit comments

Comments
 (0)