File: model.cpp

package info (click to toggle)
fcitx5-qt 5.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,340 kB
  • sloc: cpp: 11,697; xml: 243; ansic: 46; makefile: 14; sh: 9
file content (311 lines) | stat: -rw-r--r-- 8,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */
#include "model.h"
#include "filelistmodel.h"
#include <QAbstractItemModel>
#include <QApplication>
#include <QFile>
#include <QFuture>
#include <QFutureWatcher>
#include <QObject>
#include <QString>
#include <QTextStream>
#include <Qt>
#include <QtConcurrentRun>
#include <fcitx-utils/fs.h>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/standardpaths.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/utf8.h>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

namespace fcitx {

namespace {

std::optional<std::pair<std::string, std::string>>
parseLine(const std::string &strBuf) {
    auto text = stringutils::trimView(strBuf);
    if (text.empty()) {
        return std::nullopt;
    }
    if (!utf8::validate(text)) {
        return std::nullopt;
    }

    auto pos = text.find_first_of(FCITX_WHITESPACE);
    if (pos == std::string::npos) {
        return std::nullopt;
    }

    auto word = text.find_first_not_of(FCITX_WHITESPACE, pos);
    if (word == std::string::npos) {
        return std::nullopt;
    }

    std::string key(text.begin(), text.begin() + pos);
    auto wordString =
        stringutils::unescapeForValue(std::string_view(text).substr(word));
    if (!wordString) {
        return std::nullopt;
    }

    return std::make_pair(key, *wordString);
}

QString escapeValue(const QString &v) {
    return QString::fromStdString(stringutils::escapeForValue(v.toStdString()));
}

} // namespace

using ItemType = std::pair<QString, QString>;

QuickPhraseModel::QuickPhraseModel(QObject *parent)
    : QAbstractTableModel(parent), needSave_(false), futureWatcher_(0) {}

QuickPhraseModel::~QuickPhraseModel() {}

bool QuickPhraseModel::needSave() const { return needSave_; }

QVariant QuickPhraseModel::headerData(int section, Qt::Orientation orientation,
                                      int role) const {
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if (section == 0) {
            return _("Keyword");
        }
        if (section == 1) {
            return _("Phrase");
        }
    }
    return {};
}

int QuickPhraseModel::rowCount(const QModelIndex &parent) const {
    Q_UNUSED(parent);
    return list_.count();
}

int QuickPhraseModel::columnCount(const QModelIndex &parent) const {
    Q_UNUSED(parent);
    return 2;
}

QVariant QuickPhraseModel::data(const QModelIndex &index, int role) const {
    do {
        if ((role == Qt::DisplayRole || role == Qt::EditRole) &&
            index.row() < list_.count()) {
            if (index.column() == 0) {
                return list_[index.row()].first;
            }
            if (index.column() == 1) {
                return list_[index.row()].second;
            }
        }
    } while (0);
    return QVariant();
}

void QuickPhraseModel::addItem(const QString &macro, const QString &word) {
    beginInsertRows(QModelIndex(), list_.size(), list_.size());
    list_.append({macro, word});
    endInsertRows();
    setNeedSave(true);
}

void QuickPhraseModel::deleteItem(int row) {
    if (row >= list_.count()) {
        return;
    }
    auto item = list_.at(row);
    QString key = item.first;
    beginRemoveRows(QModelIndex(), row, row);
    list_.removeAt(row);
    endRemoveRows();
    setNeedSave(true);
}

void QuickPhraseModel::deleteAllItem() {
    if (list_.count()) {
        setNeedSave(true);
    }
    beginResetModel();
    list_.clear();
    endResetModel();
}

Qt::ItemFlags QuickPhraseModel::flags(const QModelIndex &index) const {
    if (!index.isValid()) {
        return {};
    }

    return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

bool QuickPhraseModel::setData(const QModelIndex &index, const QVariant &value,
                               int role) {
    if (role != Qt::EditRole) {
        return false;
    }

    if (index.column() == 0) {
        list_[index.row()].first = value.toString();

        Q_EMIT dataChanged(index, index);
        setNeedSave(true);
        return true;
    }
    if (index.column() == 1) {
        list_[index.row()].second = value.toString();

        Q_EMIT dataChanged(index, index);
        setNeedSave(true);
        return true;
    }
    return false;
}

void QuickPhraseModel::load(const QString &file, bool append) {
    if (futureWatcher_) {
        return;
    }

    beginResetModel();
    if (!append) {
        list_.clear();
        setNeedSave(false);
    } else {
        setNeedSave(true);
    }
    futureWatcher_ = new QFutureWatcher<QStringPairList>(this);
    futureWatcher_->setFuture(
        QtConcurrent::run([this, file]() { return parse(file); }));
    connect(futureWatcher_, &QFutureWatcherBase::finished, this,
            &QuickPhraseModel::loadFinished);
}

QStringPairList QuickPhraseModel::parse(const QString &file) {
    QStringPairList list;

    do {
        auto fp = fcitx::StandardPaths::global().open(
            fcitx::StandardPathsType::PkgData, file.toStdString());
        if (!fp.isValid()) {
            break;
        }

        QFile file;
        if (!file.open(fp.fd(), QFile::ReadOnly)) {
            break;
        }
        QByteArray line;
        while (!(line = file.readLine()).isNull()) {
            auto l = line.toStdString();
            auto parsed = parseLine(l);
            if (!parsed) {
                continue;
            }
            auto [key, value] = *parsed;
            if (key.empty() || value.empty()) {
                continue;
            }
            list_.append(
                {QString::fromStdString(key), QString::fromStdString(value)});
        }

        file.close();
    } while (false);

    return list;
}

void QuickPhraseModel::loadFinished() {
    list_.append(futureWatcher_->future().result());
    endResetModel();
    futureWatcher_->deleteLater();
    futureWatcher_ = 0;
}

QFutureWatcher<bool> *QuickPhraseModel::save(const QString &file) {
    auto *futureWatcher = new QFutureWatcher<bool>(this);
    futureWatcher->setFuture(QtConcurrent::run(
        [this, file, list = list_]() { return saveData(file, list); }));
    connect(futureWatcher, &QFutureWatcherBase::finished, this,
            &QuickPhraseModel::saveFinished);
    return futureWatcher;
}

void QuickPhraseModel::saveDataToStream(QTextStream &dev) {
    for (const auto &item : list_) {
        dev << item.first << "\t" << escapeValue(item.second) << "\n";
    }
}

void QuickPhraseModel::loadData(QTextStream &stream) {
    beginResetModel();
    list_.clear();
    setNeedSave(true);
    QString s;
    while (!(s = stream.readLine()).isNull()) {
        auto line = s.toStdString();
        auto parsed = parseLine(line);
        if (!parsed) {
            continue;
        }
        auto [key, value] = *parsed;
        if (key.empty() || value.empty()) {
            continue;
        }
        list_.append(
            {QString::fromStdString(key), QString::fromStdString(value)});
    }
    endResetModel();
}

bool QuickPhraseModel::saveData(const QString &file,
                                const QStringPairList &list) {
    fs::makePath(
        StandardPaths::global().userDirectory(StandardPathsType::PkgData) /
        QUICK_PHRASE_CONFIG_DIR);
    return StandardPaths::global().safeSave(
        StandardPathsType::PkgData, file.toStdString(), [&list](int fd) {
            QFile tempFile;
            if (!tempFile.open(fd, QIODevice::WriteOnly)) {
                return false;
            }
            for (const auto &item : list) {
                tempFile.write(item.first.toUtf8());
                tempFile.write("\t");
                tempFile.write(escapeValue(item.second).toUtf8());
                tempFile.write("\n");
            }
            tempFile.close();
            return true;
        });
}

void QuickPhraseModel::saveFinished() {
    QFutureWatcher<bool> *watcher =
        static_cast<QFutureWatcher<bool> *>(sender());
    QFuture<bool> future = watcher->future();
    if (future.result()) {
        setNeedSave(false);
    }
    watcher->deleteLater();
}

void QuickPhraseModel::setNeedSave(bool needSave) {
    if (needSave_ != needSave) {
        needSave_ = needSave;
        Q_EMIT needSaveChanged(needSave_);
    }
}
} // namespace fcitx