| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2013-2018 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "filelistmodel.h" |
| 9 |
#include <QAbstractListModel> |
| 10 |
#include <QDebug> |
| 11 |
#include <QFile> |
| 12 |
#include <QObject> |
| 13 |
#include <QVariant> |
| 14 |
#include <Qt> |
| 15 |
#include <algorithm> |
| 16 |
#include <fcitx-utils/fs.h> |
| 17 |
#include <fcitx-utils/standardpaths.h> |
| 18 |
#include <fcitxqti18nhelper.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <filesystem> |
| 21 |
#include <iterator> |
| 22 |
#include <map> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
namespace fcitx { |
| 26 |
|
| 27 |
FileListModel::FileListModel(QObject *parent) : QAbstractListModel(parent) { |
| 28 |
loadFileList(); |
| 29 |
} |
| 30 |
|
| 31 |
FileListModel::~FileListModel() {} |
| 32 |
|
| 33 |
int FileListModel::rowCount(const QModelIndex & /*parent*/) const { |
| 34 |
return fileList_.size(); |
| 35 |
} |
| 36 |
|
| 37 |
QVariant FileListModel::data(const QModelIndex &index, int role) const { |
| 38 |
if (!index.isValid() || index.row() >= fileList_.size()) { |
| 39 |
return {}; |
| 40 |
} |
| 41 |
|
| 42 |
switch (role) { |
| 43 |
case Qt::DisplayRole: { |
| 44 |
auto name = fileList_[index.row()].first; |
| 45 |
|
| 46 |
if (name.endsWith(".dict")) { |
| 47 |
name = name.left(name.size() - 5); |
| 48 |
} |
| 49 |
return name; |
| 50 |
} |
| 51 |
case Qt::CheckStateRole: { |
| 52 |
return fileList_[index.row()].second ? Qt::Checked : Qt::Unchecked; |
| 53 |
} |
| 54 |
case Qt::UserRole: |
| 55 |
return fileList_[index.row()].first; |
| 56 |
default: |
| 57 |
break; |
| 58 |
} |
| 59 |
return QVariant(); |
| 60 |
} |
| 61 |
|
| 62 |
bool FileListModel::setData(const QModelIndex &index, const QVariant &value, |
| 63 |
int role) { |
| 64 |
if (!index.isValid() || index.row() >= fileList_.size()) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if (role == Qt::CheckStateRole) { |
| 69 |
if (fileList_[index.row()].second != value.toBool()) { |
| 70 |
fileList_[index.row()].second = value.toBool(); |
| 71 |
Q_EMIT changed(); |
| 72 |
return true; |
| 73 |
} |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
Qt::ItemFlags FileListModel::flags(const QModelIndex &index) const { |
| 79 |
if (!index.isValid() || index.row() >= fileList_.size()) { |
| 80 |
return {}; |
| 81 |
} |
| 82 |
|
| 83 |
return Qt::ItemIsUserCheckable | QAbstractListModel::flags(index); |
| 84 |
} |
| 85 |
|
| 86 |
void FileListModel::loadFileList() { |
| 87 |
beginResetModel(); |
| 88 |
fileList_.clear(); |
| 89 |
auto files = StandardPaths::global().locate(StandardPathsType::PkgData, |
| 90 |
"pinyin/dictionaries", |
| 91 |
pathfilter::extension(".dict")); |
| 92 |
std::map<std::string, bool> enableMap; |
| 93 |
for (const auto &file : files) { |
| 94 |
enableMap[file.first] = true; |
| 95 |
} |
| 96 |
auto disableFiles = StandardPaths::global().locate( |
| 97 |
StandardPathsType::PkgData, "pinyin/dictionaries", |
| 98 |
pathfilter::extension(".disable")); |
| 99 |
for (const auto &file : disableFiles) { |
| 100 |
// Remove .disable suffix. |
| 101 |
QString s = QString::fromStdU16String(file.first.u16string()); |
| 102 |
auto dictName = s.chopped(8).toStdU16String(); |
| 103 |
if (auto iter = enableMap.find(std::filesystem::path(dictName)); |
| 104 |
iter != enableMap.end()) { |
| 105 |
iter->second = false; |
| 106 |
} |
| 107 |
} |
| 108 |
for (const auto &file : enableMap) { |
| 109 |
fileList_.append({QString::fromStdString(file.first), file.second}); |
| 110 |
} |
| 111 |
|
| 112 |
endResetModel(); |
| 113 |
} |
| 114 |
|
| 115 |
int FileListModel::findFile(const QString &lastFileName) { |
| 116 |
auto iter = |
| 117 |
std::ranges::find_if(fileList_, [&lastFileName](const auto &item) { |
| 118 |
return item.first == lastFileName; |
| 119 |
}); |
| 120 |
if (iter == fileList_.end()) { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
return std::distance(fileList_.begin(), iter); |
| 124 |
} |
| 125 |
|
| 126 |
void FileListModel::save() { |
| 127 |
const auto baseDir = |
| 128 |
StandardPaths::global().userDirectory(StandardPathsType::PkgData) / |
| 129 |
"pinyin/dictionaries"; |
| 130 |
for (const auto &file : fileList_) { |
| 131 |
auto disableFilePath = |
| 132 |
baseDir / (file.first.toStdString() + ".disable"); |
| 133 |
QFile disableFile(QString::fromStdString(disableFilePath)); |
| 134 |
if (file.second) { |
| 135 |
disableFile.remove(); |
| 136 |
} else { |
| 137 |
if (fs::makePath(baseDir)) { |
| 138 |
disableFile.open(QIODevice::WriteOnly); |
| 139 |
disableFile.close(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
} // namespace fcitx |