Skip to content

Commit f2093c2

Browse files
committed
Port to StandardPaths
1 parent 174fd92 commit f2093c2

File tree

7 files changed

+94
-71
lines changed

7 files changed

+94
-71
lines changed

CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ include(GNUInstallDirs)
1111
include(ECMUninstallTarget)
1212

1313
option(ENABLE_QT "Enable Qt for GUI configuration" On)
14-
option(USE_QT6 "Build against Qt6" On)
1514

1615
find_package(PkgConfig REQUIRED)
1716
find_package(Fcitx5Core ${REQUIRED_FCITX_VERSION} REQUIRED)
@@ -22,14 +21,9 @@ pkg_check_modules(JSonGlib IMPORTED_TARGET "json-glib-1.0" REQUIRED)
2221
pkg_check_modules(GObject2 IMPORTED_TARGET "gobject-2.0" REQUIRED)
2322

2423
if (ENABLE_QT)
25-
if (USE_QT6)
2624
set(QT_MAJOR_VERSION 6)
2725
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
28-
else()
29-
set(QT_MAJOR_VERSION 5)
30-
find_package(Qt5 5.7 REQUIRED COMPONENTS Core Gui Widgets)
31-
endif()
32-
find_package(Fcitx5Qt${QT_MAJOR_VERSION}WidgetsAddons REQUIRED)
26+
find_package(Fcitx5Qt${QT_MAJOR_VERSION}WidgetsAddons REQUIRED)
3327
endif ()
3428

3529
if (NOT SKK_DEFAULT_PATH)

gui/adddictdialog.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77

88
#include "adddictdialog.h"
99
#include "config.h"
10+
#include <QDialog>
11+
#include <QDir>
1012
#include <QFileDialog>
13+
#include <QFileInfo>
14+
#include <QMap>
15+
#include <QPushButton>
16+
#include <QString>
17+
#include <QWidget>
1118
#include <array>
1219
#include <fcitx-utils/i18n.h>
13-
#include <fcitx-utils/standardpath.h>
20+
#include <fcitx-utils/standardpaths.h>
1421
#include <fcitx-utils/stringutils.h>
1522

1623
#define FCITX_CONFIG_DIR_STR "$FCITX_CONFIG_DIR"
@@ -52,10 +59,10 @@ void AddDictDialog::browseClicked() {
5259
info.path());
5360
} else {
5461
constexpr char configDir[] = FCITX_CONFIG_DIR_STR "/";
55-
auto fcitxBasePath = stringutils::joinPath(
56-
StandardPath::global().userDirectory(StandardPath::Type::PkgData));
62+
auto fcitxBasePath =
63+
StandardPaths::global().userDirectory(StandardPathsType::PkgData);
5764
QString basePath =
58-
QDir::cleanPath(QString::fromLocal8Bit(fcitxBasePath.data()));
65+
QDir::cleanPath(QString::fromStdString(fcitxBasePath.string()));
5966
if (path.isEmpty()) {
6067
path = basePath;
6168
} else if (path.startsWith(configDir)) {

gui/dictmodel.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
*/
77

88
#include "dictmodel.h"
9+
#include <QAbstractListModel>
910
#include <QFile>
11+
#include <QIODevice>
12+
#include <QMap>
13+
#include <QObject>
1014
#include <QSet>
15+
#include <QString>
1116
#include <QStringList>
17+
#include <QVariant>
18+
#include <Qt>
1219
#include <QtGlobal>
13-
#include <fcitx-utils/standardpath.h>
14-
#include <fcntl.h>
20+
#include <fcitx-utils/standardpaths.h>
21+
1522
namespace fcitx {
1623

1724
DictModel::DictModel(QObject *parent) : QAbstractListModel(parent) {
@@ -23,17 +30,16 @@ DictModel::DictModel(QObject *parent) : QAbstractListModel(parent) {
2330
DictModel::~DictModel() {}
2431

2532
void DictModel::defaults() {
26-
auto path =
27-
StandardPath::global().fcitxPath("pkgdatadir", "kkc/dictionary_list");
28-
QFile f(path.data());
33+
auto path = StandardPaths::fcitxPath("pkgdatadir", "kkc/dictionary_list");
34+
QFile f(path);
2935
if (f.open(QIODevice::ReadOnly)) {
3036
load(f);
3137
}
3238
}
3339

3440
void DictModel::load() {
35-
auto file = StandardPath::global().open(StandardPath::Type::PkgData,
36-
"kkc/dictionary_list", O_RDONLY);
41+
auto file = StandardPaths::global().open(StandardPathsType::PkgData,
42+
"kkc/dictionary_list");
3743
if (file.fd() < 0) {
3844
return;
3945
}
@@ -60,7 +66,7 @@ void DictModel::load(QFile &file) {
6066

6167
bool failed = false;
6268
QMap<QString, QString> dict;
63-
Q_FOREACH (const QString &item, items) {
69+
for (const QString &item : items) {
6470
if (!item.contains('=')) {
6571
failed = true;
6672
break;
@@ -83,18 +89,18 @@ void DictModel::load(QFile &file) {
8389
}
8490

8591
bool DictModel::save() {
86-
return StandardPath::global().safeSave(
87-
StandardPath::Type::PkgData, "kkc/dictionary_list", [this](int fd) {
92+
return StandardPaths::global().safeSave(
93+
StandardPathsType::PkgData, "kkc/dictionary_list", [this](int fd) {
8894
QFile tempFile;
8995
if (!tempFile.open(fd, QIODevice::WriteOnly)) {
9096
return false;
9197
}
9298

93-
typedef QMap<QString, QString> DictType;
99+
using DictType = QMap<QString, QString>;
94100

95-
Q_FOREACH (const DictType &dict, dicts_) {
101+
for (const DictType &dict : dicts_) {
96102
bool first = true;
97-
Q_FOREACH (const QString &key, dict.keys()) {
103+
for (const QString &key : dict.keys()) {
98104
if (first) {
99105
first = false;
100106
} else {
@@ -135,28 +141,26 @@ bool DictModel::removeRows(int row, int count, const QModelIndex &parent) {
135141

136142
QVariant DictModel::data(const QModelIndex &index, int role) const {
137143
if (!index.isValid()) {
138-
return QVariant();
144+
return {};
139145
}
140146

141147
if (index.row() >= dicts_.size() || index.column() != 0) {
142-
return QVariant();
148+
return {};
143149
}
144150

145151
switch (role) {
146152
case Qt::DisplayRole:
147153
return dicts_[index.row()]["file"];
154+
default:
155+
return {};
148156
}
149-
return QVariant();
157+
return {};
150158
}
151159

152160
bool DictModel::moveUp(const QModelIndex &currentIndex) {
153161
if (currentIndex.row() > 0 && currentIndex.row() < dicts_.size()) {
154162
beginResetModel();
155-
#if (QT_VERSION < QT_VERSION_CHECK(5, 13, 0))
156-
dicts_.swap(currentIndex.row() - 1, currentIndex.row());
157-
#else
158163
dicts_.swapItemsAt(currentIndex.row() - 1, currentIndex.row());
159-
#endif
160164
endResetModel();
161165
return true;
162166
}
@@ -166,11 +170,7 @@ bool DictModel::moveUp(const QModelIndex &currentIndex) {
166170
bool DictModel::moveDown(const QModelIndex &currentIndex) {
167171
if (currentIndex.row() >= 0 && currentIndex.row() + 1 < dicts_.size()) {
168172
beginResetModel();
169-
#if (QT_VERSION < QT_VERSION_CHECK(5, 13, 0))
170-
dicts_.swap(currentIndex.row() + 1, currentIndex.row());
171-
#else
172173
dicts_.swapItemsAt(currentIndex.row() + 1, currentIndex.row());
173-
#endif
174174
endResetModel();
175175
return true;
176176
}

gui/shortcutmodel.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77

88
#include "shortcutmodel.h"
99
#include "common.h"
10+
#include <QAbstractTableModel>
11+
#include <QObject>
12+
#include <QVariant>
13+
#include <Qt>
1014
#include <fcitx-utils/i18n.h>
11-
#include <fcitx-utils/standardpath.h>
15+
#include <fcitx-utils/standardpaths.h>
1216
#include <fcitx-utils/stringutils.h>
17+
#include <glib.h>
1318
#include <libkkc/libkkc.h>
19+
#include <utility>
1420

1521
namespace fcitx {
1622

@@ -34,11 +40,11 @@ const char *modeName[] = {
3440

3541
QVariant ShortcutModel::data(const QModelIndex &index, int role) const {
3642
if (!index.isValid()) {
37-
return QVariant();
43+
return {};
3844
}
3945

4046
if (index.row() >= entries_.size() || index.column() >= 3) {
41-
return QVariant();
47+
return {};
4248
}
4349

4450
switch (role) {
@@ -50,10 +56,14 @@ QVariant ShortcutModel::data(const QModelIndex &index, int role) const {
5056
return entries_[index.row()].keyString();
5157
case 2:
5258
return entries_[index.row()].label();
59+
default:
60+
break;
5361
}
54-
return QVariant();
62+
break;
63+
default:
64+
break;
5565
}
56-
return QVariant();
66+
return {};
5767
}
5868

5969
QVariant ShortcutModel::headerData(int section, Qt::Orientation orientation,
@@ -71,7 +81,12 @@ QVariant ShortcutModel::headerData(int section, Qt::Orientation orientation,
7181
return _("Key");
7282
case 2:
7383
return _("Function");
84+
default:
85+
break;
7486
}
87+
break;
88+
default:
89+
break;
7590
}
7691
return QAbstractItemModel::headerData(section, orientation, role);
7792
}
@@ -90,12 +105,12 @@ void ShortcutModel::load(const QString &name) {
90105
return;
91106
}
92107

93-
std::string basePath = stringutils::joinPath(
94-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
95-
"kkc/rules");
108+
const auto basePath =
109+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
110+
"kkc/rules";
96111

97-
auto userRule = makeGObjectUnique(
98-
kkc_user_rule_new(ruleMeta, basePath.data(), "fcitx-kkc", nullptr));
112+
auto userRule = makeGObjectUnique(kkc_user_rule_new(
113+
ruleMeta, basePath.string().c_str(), "fcitx-kkc", nullptr));
99114
if (!userRule) {
100115
break;
101116
}
@@ -191,6 +206,6 @@ void ShortcutModel::setNeedSave(bool needSave) {
191206
}
192207
}
193208

194-
bool ShortcutModel::needSave() { return needSave_; }
209+
bool ShortcutModel::needSave() const { return needSave_; }
195210

196211
} // namespace fcitx

gui/shortcutmodel.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@
99

1010
#include "common.h"
1111
#include <QAbstractTableModel>
12+
#include <QList>
13+
#include <QObject>
14+
#include <QString>
15+
#include <QVariant>
16+
#include <Qt>
17+
#include <glib-object.h>
18+
#include <glib.h>
1219
#include <libkkc/libkkc.h>
13-
#include <memory>
20+
#include <utility>
1421

1522
namespace fcitx {
1623

1724
class ShortcutEntry {
1825
public:
19-
ShortcutEntry(const QString &command, KkcKeyEvent *event,
20-
const QString &label, KkcInputMode mode)
21-
: command_(command), event_(KKC_KEY_EVENT(g_object_ref(event))),
22-
label_(label), mode_(mode) {
26+
ShortcutEntry(QString command, KkcKeyEvent *event, QString label,
27+
KkcInputMode mode)
28+
: command_(std::move(command)),
29+
event_(KKC_KEY_EVENT(g_object_ref(event))), label_(std::move(label)),
30+
mode_(mode) {
2331
gchar *keystr = kkc_key_event_to_string(event_.get());
2432
keyString_ = QString::fromUtf8(keystr);
2533
g_free(keystr);
@@ -72,13 +80,13 @@ class ShortcutModel : public QAbstractTableModel {
7280
void remove(const QModelIndex &index);
7381
void load(const QString &name);
7482
void save();
75-
bool needSave();
83+
bool needSave() const;
7684

7785
Q_SIGNALS:
7886
void needSaveChanged(bool needSave);
7987

8088
private:
81-
void setNeedSave(bool arg1);
89+
void setNeedSave(bool needSave);
8290

8391
private:
8492
QList<ShortcutEntry> entries_;

gui/shortcutwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <QFile>
1414
#include <QMessageBox>
1515
#include <fcitx-utils/i18n.h>
16-
#include <fcitx-utils/standardpath.h>
16+
#include <fcitx-utils/standardpaths.h>
1717
#include <fcntl.h>
1818

1919
namespace fcitx {

src/kkc.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <fcitx-utils/log.h>
2323
#include <fcitx-utils/macros.h>
2424
#include <fcitx-utils/misc.h>
25-
#include <fcitx-utils/standardpath.h>
25+
#include <fcitx-utils/standardpaths.h>
2626
#include <fcitx-utils/stringutils.h>
2727
#include <fcitx-utils/textformatflags.h>
2828
#include <fcitx/action.h>
@@ -390,12 +390,12 @@ KkcEngine::KkcEngine(Instance *instance)
390390
#endif
391391
kkc_init();
392392

393-
fs::makePath(stringutils::joinPath(
394-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
395-
"kkc/dictionary"));
396-
fs::makePath(stringutils::joinPath(
397-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
398-
"kkc/rules"));
393+
fs::makePath(
394+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
395+
"kkc/dictionary");
396+
fs::makePath(
397+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
398+
"kkc/rules");
399399

400400
// We can only create kkc object here after we called kkc_init().
401401
model_.reset(kkc_language_model_load("sorted3", NULL));
@@ -590,8 +590,8 @@ void KkcEngine::updateInputMode(InputContext *ic) { modeAction_->update(ic); }
590590

591591
void KkcEngine::loadDictionary() {
592592
kkc_dictionary_list_clear(dictionaries_.get());
593-
auto file = StandardPath::global().open(StandardPath::Type::PkgData,
594-
"kkc/dictionary_list", O_RDONLY);
593+
auto file = StandardPaths::global().open(StandardPathsType::PkgData,
594+
"kkc/dictionary_list");
595595
if (!file.isValid()) {
596596
return;
597597
}
@@ -650,10 +650,9 @@ void KkcEngine::loadDictionary() {
650650
constexpr auto len = sizeof(configDir) - 1;
651651
std::string realpath = path;
652652
if (stringutils::startsWith(path, "$FCITX_CONFIG_DIR/")) {
653-
realpath =
654-
stringutils::joinPath(StandardPath::global().userDirectory(
655-
StandardPath::Type::PkgData),
656-
path.substr(len));
653+
realpath = StandardPaths::global().userDirectory(
654+
StandardPathsType::PkgData) /
655+
path.substr(len);
657656
}
658657

659658
auto userdict = makeGObjectUnique(reinterpret_cast<KkcDictionary *>(
@@ -675,11 +674,11 @@ void KkcEngine::loadRule() {
675674
if (!meta) {
676675
return;
677676
}
678-
std::string basePath = stringutils::joinPath(
679-
StandardPath::global().userDirectory(StandardPath::Type::PkgData),
680-
"kkc/rules");
677+
const auto basePath =
678+
StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
679+
"kkc/rules";
681680
userRule_.reset(
682-
kkc_user_rule_new(meta, basePath.c_str(), "fcitx-kkc", NULL));
681+
kkc_user_rule_new(meta, basePath.string().c_str(), "fcitx-kkc", NULL));
683682
}
684683

685684
std::string KkcEngine::subMode(const InputMethodEntry & /*entry*/,

0 commit comments

Comments
 (0)