Skip to content

Commit 0ad9377

Browse files
authored
Merge pull request #82 from yamader/fix-load-dict
fix SkkEngine::loadDictionary() envvar behavior
2 parents 2942316 + 3981e7d commit 0ad9377

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

gui/adddictdialog.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <QFileDialog>
1616
#include <QLineEdit>
1717
#include <QPushButton>
18-
#include <fcitx-utils/standardpath.h>
18+
#include <fcitx-utils/standardpaths.h>
1919
#include <fcitxqti18nhelper.h>
2020

2121
#define FCITX_CONFIG_DIR "$FCITX_CONFIG_DIR"
@@ -95,17 +95,17 @@ void AddDictDialog::browseClicked() {
9595
path = QFileDialog::getOpenFileName(this, _("Select Dictionary File"),
9696
info.path());
9797
} else {
98-
auto fcitxBasePath = stringutils::joinPath(
99-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
100-
"cskk");
98+
auto fcitxBasePath =
99+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
100+
"cskk";
101101
fs::makePath(fcitxBasePath);
102102
QString fcitxConfigBasePath =
103103
QDir::cleanPath(QString::fromStdString(fcitxBasePath));
104104

105105
if (path.isEmpty()) {
106-
auto baseDataPath = stringutils::joinPath(
107-
StandardPath::global().userDirectory(StandardPath::Type::Data),
108-
"fcitx5-cskk");
106+
auto baseDataPath =
107+
StandardPaths::global().userDirectory(StandardPathsType::Data) /
108+
"fcitx5-cskk";
109109
fs::makePath(baseDataPath);
110110
QString basePath = QDir::cleanPath(QString::fromStdString(baseDataPath));
111111
path = basePath;

gui/dictmodel.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <QStringList>
1313
#include <QTemporaryFile>
1414
#include <QtGlobal>
15-
#include <fcitx-utils/standardpath.h>
15+
#include <fcitx-utils/standardpaths.h>
1616
#include <fcntl.h>
1717
#include <iostream>
1818

@@ -23,16 +23,16 @@ const std::string config_path = "cskk/dictionary_list";
2323
SkkDictModel::SkkDictModel(QObject *parent) : QAbstractListModel(parent) {}
2424

2525
void SkkDictModel::defaults() {
26-
auto path = StandardPath::fcitxPath("pkgdatadir", config_path.c_str());
27-
QFile f(path.data());
26+
auto path = StandardPaths::fcitxPath("pkgdatadir", config_path.c_str());
27+
QFile f(path);
2828
if (f.open(QIODevice::ReadOnly)) {
2929
load(f);
3030
}
3131
}
3232

3333
void SkkDictModel::load() {
34-
auto file = StandardPath::global().open(StandardPath::Type::PkgData,
35-
config_path, O_RDONLY);
34+
auto file =
35+
StandardPaths::global().open(StandardPathsType::PkgData, config_path);
3636
if (file.fd() < 0) {
3737
return;
3838
}
@@ -61,8 +61,8 @@ void SkkDictModel::load(QFile &file) {
6161
}
6262

6363
bool SkkDictModel::save() {
64-
return StandardPath::global().safeSave(
65-
StandardPath::Type::PkgData, config_path, [this](int fd) {
64+
return StandardPaths::global().safeSave(
65+
StandardPathsType::PkgData, config_path, [this](int fd) {
6666
QFile tempFile;
6767
if (!tempFile.open(fd, QIODevice::WriteOnly)) {
6868
return false;

gui/dictwidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "dictmodel.h"
1111
#include <QItemSelectionModel>
1212
#include <fcitx-utils/fs.h>
13-
#include <fcitx-utils/standardpath.h>
13+
#include <fcitx-utils/standardpaths.h>
1414
#include <fcitx-utils/stringutils.h>
1515
#include <fcitxqti18nhelper.h>
1616

@@ -21,9 +21,9 @@ SkkDictWidget::SkkDictWidget(QWidget *parent)
2121
m_ui(std::make_unique<Ui::SkkDictWidget>()) {
2222
m_ui->setupUi(this);
2323
m_dictModel = new SkkDictModel(this);
24-
auto fcitxBasePath = stringutils::joinPath(
25-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
26-
"cskk");
24+
auto fcitxBasePath =
25+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
26+
"cskk";
2727
fs::makePath(fcitxBasePath);
2828

2929
m_dictModel->load();

src/cskk.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <fcitx-utils/key.h>
2727
#include <fcitx-utils/keysym.h>
2828
#include <fcitx-utils/log.h>
29-
#include <fcitx-utils/standardpath.h>
29+
#include <fcitx-utils/standardpaths.h>
3030
#include <fcitx-utils/stringutils.h>
3131
#include <fcitx-utils/textformatflags.h>
3232
#include <fcitx/addoninstance.h>
@@ -131,8 +131,8 @@ typedef enum _FcitxSkkDictType { FSDT_Invalid, FSDT_File } FcitxSkkDictType;
131131
void FcitxCskkEngine::loadDictionary() {
132132
freeDictionaries();
133133

134-
auto dict_config_file = StandardPath::global().open(
135-
StandardPath::Type::PkgData, "cskk/dictionary_list", O_RDONLY);
134+
auto dict_config_file = StandardPaths::global().open(
135+
StandardPathsType::PkgData, "cskk/dictionary_list");
136136

137137
if (!dict_config_file.isValid()) {
138138
return;
@@ -201,26 +201,29 @@ void FcitxCskkEngine::loadDictionary() {
201201
CSKK_WARN() << "Invalid dictionary path or mode. Ignored";
202202
continue;
203203
}
204+
205+
constexpr char configDir[] = "$FCITX_CONFIG_DIR/";
206+
constexpr auto var_len = sizeof(configDir) - 1;
207+
std::string realpath = path;
208+
if (stringutils::startsWith(path, configDir)) {
209+
realpath =
210+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
211+
path.substr(var_len);
212+
}
213+
204214
if (mode == 1) {
205215
// readonly mode
206-
auto *dict = skk_file_dict_new(path.c_str(), encoding.c_str(), complete);
216+
auto *dict =
217+
skk_file_dict_new(realpath.c_str(), encoding.c_str(), complete);
207218
if (dict) {
208-
CSKK_DEBUG() << "Adding file dict: " << path
219+
CSKK_DEBUG() << "Adding file dict: " << realpath
209220
<< " complete:" << complete;
210221
dictionaries_.emplace_back(dict);
211222
} else {
212-
CSKK_WARN() << "Static dictionary load error. Ignored: " << path;
223+
CSKK_WARN() << "Static dictionary load error. Ignored: " << realpath;
213224
}
214225
} else {
215226
// read/write mode
216-
constexpr char configDir[] = "$FCITX_CONFIG_DIR/";
217-
constexpr auto var_len = sizeof(configDir) - 1;
218-
std::string realpath = path;
219-
if (stringutils::startsWith(path, configDir)) {
220-
realpath = stringutils::joinPath(
221-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
222-
path.substr(var_len));
223-
}
224227
auto *userdict =
225228
skk_user_dict_new(realpath.c_str(), encoding.c_str(), complete);
226229
if (userdict) {
@@ -404,7 +407,8 @@ void FcitxCskkContext::updateUI() {
404407

405408
// Preedit
406409
uint32_t stateStackLen;
407-
auto *preeditDetail = skk_context_get_preedit_detail(context_, &stateStackLen);
410+
auto *preeditDetail =
411+
skk_context_get_preedit_detail(context_, &stateStackLen);
408412
auto [mainPreedit, supplementPreedit] =
409413
FcitxCskkContext::formatPreedit(preeditDetail, stateStackLen);
410414
skk_free_preedit_detail(preeditDetail, stateStackLen);
@@ -427,8 +431,7 @@ void FcitxCskkContext::updateUI() {
427431
std::make_unique<FcitxCskkCandidateList>(engine_, ic_));
428432
} else {
429433
// Sync UI with actual data
430-
currentCandidateList->setCursorPosition(
431-
currentCursorPosition);
434+
currentCandidateList->setCursorPosition(currentCursorPosition);
432435
}
433436

434437
} else {

0 commit comments

Comments
 (0)