| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "model.h" |
| 8 |
#include "filelistmodel.h" |
| 9 |
#include <QAbstractItemModel> |
| 10 |
#include <QApplication> |
| 11 |
#include <QFile> |
| 12 |
#include <QFuture> |
| 13 |
#include <QFutureWatcher> |
| 14 |
#include <QObject> |
| 15 |
#include <QString> |
| 16 |
#include <QTextStream> |
| 17 |
#include <Qt> |
| 18 |
#include <QtConcurrentRun> |
| 19 |
#include <fcitx-utils/fs.h> |
| 20 |
#include <fcitx-utils/i18n.h> |
| 21 |
#include <fcitx-utils/macros.h> |
| 22 |
#include <fcitx-utils/standardpaths.h> |
| 23 |
#include <fcitx-utils/stringutils.h> |
| 24 |
#include <fcitx-utils/utf8.h> |
| 25 |
#include <optional> |
| 26 |
#include <string> |
| 27 |
#include <string_view> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
namespace fcitx { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
std::optional<std::pair<std::string, std::string>> |
| 35 |
parseLine(const std::string &strBuf) { |
| 36 |
auto text = stringutils::trimView(strBuf); |
| 37 |
if (text.empty()) { |
| 38 |
return std::nullopt; |
| 39 |
} |
| 40 |
if (!utf8::validate(text)) { |
| 41 |
return std::nullopt; |
| 42 |
} |
| 43 |
|
| 44 |
auto pos = text.find_first_of(FCITX_WHITESPACE); |
| 45 |
if (pos == std::string::npos) { |
| 46 |
return std::nullopt; |
| 47 |
} |
| 48 |
|
| 49 |
auto word = text.find_first_not_of(FCITX_WHITESPACE, pos); |
| 50 |
if (word == std::string::npos) { |
| 51 |
return std::nullopt; |
| 52 |
} |
| 53 |
|
| 54 |
std::string key(text.begin(), text.begin() + pos); |
| 55 |
auto wordString = |
| 56 |
stringutils::unescapeForValue(std::string_view(text).substr(word)); |
| 57 |
if (!wordString) { |
| 58 |
return std::nullopt; |
| 59 |
} |
| 60 |
|
| 61 |
return std::make_pair(key, *wordString); |
| 62 |
} |
| 63 |
|
| 64 |
QString escapeValue(const QString &v) { |
| 65 |
return QString::fromStdString(stringutils::escapeForValue(v.toStdString())); |
| 66 |
} |
| 67 |
|
| 68 |
} // namespace |
| 69 |
|
| 70 |
using ItemType = std::pair<QString, QString>; |
| 71 |
|
| 72 |
QuickPhraseModel::QuickPhraseModel(QObject *parent) |
| 73 |
: QAbstractTableModel(parent), needSave_(false), futureWatcher_(0) {} |
| 74 |
|
| 75 |
QuickPhraseModel::~QuickPhraseModel() {} |
| 76 |
|
| 77 |
bool QuickPhraseModel::needSave() const { return needSave_; } |
| 78 |
|
| 79 |
QVariant QuickPhraseModel::headerData(int section, Qt::Orientation orientation, |
| 80 |
int role) const { |
| 81 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 82 |
if (section == 0) { |
| 83 |
return _("Keyword"); |
| 84 |
} |
| 85 |
if (section == 1) { |
| 86 |
return _("Phrase"); |
| 87 |
} |
| 88 |
} |
| 89 |
return {}; |
| 90 |
} |
| 91 |
|
| 92 |
int QuickPhraseModel::rowCount(const QModelIndex &parent) const { |
| 93 |
Q_UNUSED(parent); |
| 94 |
return list_.count(); |
| 95 |
} |
| 96 |
|
| 97 |
int QuickPhraseModel::columnCount(const QModelIndex &parent) const { |
| 98 |
Q_UNUSED(parent); |
| 99 |
return 2; |
| 100 |
} |
| 101 |
|
| 102 |
QVariant QuickPhraseModel::data(const QModelIndex &index, int role) const { |
| 103 |
do { |
| 104 |
if ((role == Qt::DisplayRole || role == Qt::EditRole) && |
| 105 |
index.row() < list_.count()) { |
| 106 |
if (index.column() == 0) { |
| 107 |
return list_[index.row()].first; |
| 108 |
} |
| 109 |
if (index.column() == 1) { |
| 110 |
return list_[index.row()].second; |
| 111 |
} |
| 112 |
} |
| 113 |
} while (0); |
| 114 |
return QVariant(); |
| 115 |
} |
| 116 |
|
| 117 |
void QuickPhraseModel::addItem(const QString ¯o, const QString &word) { |
| 118 |
beginInsertRows(QModelIndex(), list_.size(), list_.size()); |
| 119 |
list_.append({macro, word}); |
| 120 |
endInsertRows(); |
| 121 |
setNeedSave(true); |
| 122 |
} |
| 123 |
|
| 124 |
void QuickPhraseModel::deleteItem(int row) { |
| 125 |
if (row >= list_.count()) { |
| 126 |
return; |
| 127 |
} |
| 128 |
auto item = list_.at(row); |
| 129 |
QString key = item.first; |
| 130 |
beginRemoveRows(QModelIndex(), row, row); |
| 131 |
list_.removeAt(row); |
| 132 |
endRemoveRows(); |
| 133 |
setNeedSave(true); |
| 134 |
} |
| 135 |
|
| 136 |
void QuickPhraseModel::deleteAllItem() { |
| 137 |
if (list_.count()) { |
| 138 |
setNeedSave(true); |
| 139 |
} |
| 140 |
beginResetModel(); |
| 141 |
list_.clear(); |
| 142 |
endResetModel(); |
| 143 |
} |
| 144 |
|
| 145 |
Qt::ItemFlags QuickPhraseModel::flags(const QModelIndex &index) const { |
| 146 |
if (!index.isValid()) { |
| 147 |
return {}; |
| 148 |
} |
| 149 |
|
| 150 |
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
| 151 |
} |
| 152 |
|
| 153 |
bool QuickPhraseModel::setData(const QModelIndex &index, const QVariant &value, |
| 154 |
int role) { |
| 155 |
if (role != Qt::EditRole) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
if (index.column() == 0) { |
| 160 |
list_[index.row()].first = value.toString(); |
| 161 |
|
| 162 |
Q_EMIT dataChanged(index, index); |
| 163 |
setNeedSave(true); |
| 164 |
return true; |
| 165 |
} |
| 166 |
if (index.column() == 1) { |
| 167 |
list_[index.row()].second = value.toString(); |
| 168 |
|
| 169 |
Q_EMIT dataChanged(index, index); |
| 170 |
setNeedSave(true); |
| 171 |
return true; |
| 172 |
} |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
void QuickPhraseModel::load(const QString &file, bool append) { |
| 177 |
if (futureWatcher_) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
beginResetModel(); |
| 182 |
if (!append) { |
| 183 |
list_.clear(); |
| 184 |
setNeedSave(false); |
| 185 |
} else { |
| 186 |
setNeedSave(true); |
| 187 |
} |
| 188 |
futureWatcher_ = new QFutureWatcher<QStringPairList>(this); |
| 189 |
futureWatcher_->setFuture( |
| 190 |
QtConcurrent::run([this, file]() { return parse(file); })); |
| 191 |
connect(futureWatcher_, &QFutureWatcherBase::finished, this, |
| 192 |
&QuickPhraseModel::loadFinished); |
| 193 |
} |
| 194 |
|
| 195 |
QStringPairList QuickPhraseModel::parse(const QString &file) { |
| 196 |
QStringPairList list; |
| 197 |
|
| 198 |
do { |
| 199 |
auto fp = fcitx::StandardPaths::global().open( |
| 200 |
fcitx::StandardPathsType::PkgData, file.toStdString()); |
| 201 |
if (!fp.isValid()) { |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
QFile file; |
| 206 |
if (!file.open(fp.fd(), QFile::ReadOnly)) { |
| 207 |
break; |
| 208 |
} |
| 209 |
QByteArray line; |
| 210 |
while (!(line = file.readLine()).isNull()) { |
| 211 |
auto l = line.toStdString(); |
| 212 |
auto parsed = parseLine(l); |
| 213 |
if (!parsed) { |
| 214 |
continue; |
| 215 |
} |
| 216 |
auto [key, value] = *parsed; |
| 217 |
if (key.empty() || value.empty()) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
list_.append( |
| 221 |
{QString::fromStdString(key), QString::fromStdString(value)}); |
| 222 |
} |
| 223 |
|
| 224 |
file.close(); |
| 225 |
} while (false); |
| 226 |
|
| 227 |
return list; |
| 228 |
} |
| 229 |
|
| 230 |
void QuickPhraseModel::loadFinished() { |
| 231 |
list_.append(futureWatcher_->future().result()); |
| 232 |
endResetModel(); |
| 233 |
futureWatcher_->deleteLater(); |
| 234 |
futureWatcher_ = 0; |
| 235 |
} |
| 236 |
|
| 237 |
QFutureWatcher<bool> *QuickPhraseModel::save(const QString &file) { |
| 238 |
auto *futureWatcher = new QFutureWatcher<bool>(this); |
| 239 |
futureWatcher->setFuture(QtConcurrent::run( |
| 240 |
[this, file, list = list_]() { return saveData(file, list); })); |
| 241 |
connect(futureWatcher, &QFutureWatcherBase::finished, this, |
| 242 |
&QuickPhraseModel::saveFinished); |
| 243 |
return futureWatcher; |
| 244 |
} |
| 245 |
|
| 246 |
void QuickPhraseModel::saveDataToStream(QTextStream &dev) { |
| 247 |
for (const auto &item : list_) { |
| 248 |
dev << item.first << "\t" << escapeValue(item.second) << "\n"; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void QuickPhraseModel::loadData(QTextStream &stream) { |
| 253 |
beginResetModel(); |
| 254 |
list_.clear(); |
| 255 |
setNeedSave(true); |
| 256 |
QString s; |
| 257 |
while (!(s = stream.readLine()).isNull()) { |
| 258 |
auto line = s.toStdString(); |
| 259 |
auto parsed = parseLine(line); |
| 260 |
if (!parsed) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
auto [key, value] = *parsed; |
| 264 |
if (key.empty() || value.empty()) { |
| 265 |
continue; |
| 266 |
} |
| 267 |
list_.append( |
| 268 |
{QString::fromStdString(key), QString::fromStdString(value)}); |
| 269 |
} |
| 270 |
endResetModel(); |
| 271 |
} |
| 272 |
|
| 273 |
bool QuickPhraseModel::saveData(const QString &file, |
| 274 |
const QStringPairList &list) { |
| 275 |
fs::makePath( |
| 276 |
StandardPaths::global().userDirectory(StandardPathsType::PkgData) / |
| 277 |
QUICK_PHRASE_CONFIG_DIR); |
| 278 |
return StandardPaths::global().safeSave( |
| 279 |
StandardPathsType::PkgData, file.toStdString(), [&list](int fd) { |
| 280 |
QFile tempFile; |
| 281 |
if (!tempFile.open(fd, QIODevice::WriteOnly)) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
for (const auto &item : list) { |
| 285 |
tempFile.write(item.first.toUtf8()); |
| 286 |
tempFile.write("\t"); |
| 287 |
tempFile.write(escapeValue(item.second).toUtf8()); |
| 288 |
tempFile.write("\n"); |
| 289 |
} |
| 290 |
tempFile.close(); |
| 291 |
return true; |
| 292 |
}); |
| 293 |
} |
| 294 |
|
| 295 |
void QuickPhraseModel::saveFinished() { |
| 296 |
QFutureWatcher<bool> *watcher = |
| 297 |
static_cast<QFutureWatcher<bool> *>(sender()); |
| 298 |
QFuture<bool> future = watcher->future(); |
| 299 |
if (future.result()) { |
| 300 |
setNeedSave(false); |
| 301 |
} |
| 302 |
watcher->deleteLater(); |
| 303 |
} |
| 304 |
|
| 305 |
void QuickPhraseModel::setNeedSave(bool needSave) { |
| 306 |
if (needSave_ != needSave) { |
| 307 |
needSave_ = needSave; |
| 308 |
Q_EMIT needSaveChanged(needSave_); |
| 309 |
} |
| 310 |
} |
| 311 |
} // namespace fcitx |