Debian Codegraph @

qt5/quickphrase-editor/model.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2012~2012 by CSSlayer *
3 * [email protected] *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 * *
18 ***************************************************************************/
19
20 #include <QApplication>
21 #include <QFile>
22 #include <QFutureWatcher>
23 #include <QtConcurrentRun>
24 #include <qtemporaryfile.h>
25
26 #include "common.h"
27 #include "editor.h"
28 #include "filelistmodel.h"
29 #include "model.h"
30 #include <fcitx-config/xdg.h>
31
32 namespace fcitx {
33
34 typedef QPair<QString, QString> ItemType;
35
36 QuickPhraseModel::QuickPhraseModel(QObject *parent)
37 : QAbstractTableModel(parent), m_needSave(false), m_futureWatcher(0) {}
38
39 QuickPhraseModel::~QuickPhraseModel() {}
40
41 bool QuickPhraseModel::needSave() { return m_needSave; }
42
43 QVariant QuickPhraseModel::headerData(int section, Qt::Orientation orientation,
44 int role) const {
45 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
46 if (section == 0)
47 return _("Keyword");
48 else if (section == 1)
49 return _("Phrase");
50 }
51 return QVariant();
52 }
53
54 int QuickPhraseModel::rowCount(const QModelIndex &parent) const {
55 Q_UNUSED(parent);
56 return m_list.count();
57 }
58
59 int QuickPhraseModel::columnCount(const QModelIndex &parent) const {
60 Q_UNUSED(parent);
61 return 2;
62 }
63
64 QVariant QuickPhraseModel::data(const QModelIndex &index, int role) const {
65 do {
66 if ((role == Qt::DisplayRole || role == Qt::EditRole) &&
67 index.row() < m_list.count()) {
68 if (index.column() == 0) {
69 return m_list[index.row()].first;
70 } else if (index.column() == 1) {
71 return m_list[index.row()].second;
72 }
73 }
74 } while (0);
75 return QVariant();
76 }
77
78 void QuickPhraseModel::addItem(const QString &macro, const QString &word) {
79 beginInsertRows(QModelIndex(), m_list.size(), m_list.size());
80 m_list.append(QPair<QString, QString>(macro, word));
81 endInsertRows();
82 setNeedSave(true);
83 }
84
85 void QuickPhraseModel::deleteItem(int row) {
86 if (row >= m_list.count())
87 return;
88 QPair<QString, QString> item = m_list.at(row);
89 QString key = item.first;
90 beginRemoveRows(QModelIndex(), row, row);
91 m_list.removeAt(row);
92 endRemoveRows();
93 setNeedSave(true);
94 }
95
96 void QuickPhraseModel::deleteAllItem() {
97 if (m_list.count())
98 setNeedSave(true);
99 beginResetModel();
100 m_list.clear();
101 endResetModel();
102 }
103
104 Qt::ItemFlags QuickPhraseModel::flags(const QModelIndex &index) const {
105 if (!index.isValid())
106 return {};
107
108 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
109 }
110
111 bool QuickPhraseModel::setData(const QModelIndex &index, const QVariant &value,
112 int role) {
113 if (role != Qt::EditRole)
114 return false;
115
116 if (index.column() == 0) {
117 m_list[index.row()].first = value.toString();
118
119 Q_EMIT dataChanged(index, index);
120 setNeedSave(true);
121 return true;
122 } else if (index.column() == 1) {
123 m_list[index.row()].second = value.toString();
124
125 Q_EMIT dataChanged(index, index);
126 setNeedSave(true);
127 return true;
128 } else
129 return false;
130 }
131
132 void QuickPhraseModel::load(const QString &file, bool append) {
133 if (m_futureWatcher) {
134 return;
135 }
136
137 beginResetModel();
138 if (!append) {
139 m_list.clear();
140 setNeedSave(false);
141 } else
142 setNeedSave(true);
143 m_futureWatcher = new QFutureWatcher<QStringPairList>(this);
144 m_futureWatcher->setFuture(QtConcurrent::run<QStringPairList>(
145 this, &QuickPhraseModel::parse, file));
146 connect(m_futureWatcher, SIGNAL(finished()), this, SLOT(loadFinished()));
147 }
148
149 QStringPairList QuickPhraseModel::parse(const QString &file) {
150 QByteArray fileNameArray = file.toLocal8Bit();
151 QStringPairList list;
152
153 do {
154 FILE *fp =
155 FcitxXDGGetFileWithPrefix("", fileNameArray.constData(), "r", NULL);
156 if (!fp)
157 break;
158
159 QFile file;
160 if (!file.open(fp, QFile::ReadOnly)) {
161 fclose(fp);
162 break;
163 }
164 QByteArray line;
165 while (!(line = file.readLine()).isNull()) {
166 QString s = QString::fromUtf8(line);
167 s = s.simplified();
168 if (s.isEmpty())
169 continue;
170 QString key = s.section(" ", 0, 0, QString::SectionSkipEmpty);
171 QString value = s.section(" ", 1, -1, QString::SectionSkipEmpty);
172 if (key.isEmpty() || value.isEmpty())
173 continue;
174 list.append(QPair<QString, QString>(key, value));
175 }
176
177 file.close();
178 fclose(fp);
179 } while (0);
180
181 return list;
182 }
183
184 void QuickPhraseModel::loadFinished() {
185 m_list.append(m_futureWatcher->future().result());
186 endResetModel();
187 m_futureWatcher->deleteLater();
188 m_futureWatcher = 0;
189 }
190
191 QFutureWatcher<bool> *QuickPhraseModel::save(const QString &file) {
192 QFutureWatcher<bool> *futureWatcher = new QFutureWatcher<bool>(this);
193 futureWatcher->setFuture(QtConcurrent::run<bool>(
194 this, &QuickPhraseModel::saveData, file, m_list));
195 connect(futureWatcher, SIGNAL(finished()), this, SLOT(saveFinished()));
196 return futureWatcher;
197 }
198
199 void QuickPhraseModel::saveData(QTextStream &dev) {
200 for (int i = 0; i < m_list.size(); i++) {
201 dev << m_list[i].first << "\t" << m_list[i].second << "\n";
202 }
203 }
204
205 void QuickPhraseModel::loadData(QTextStream &stream) {
206 beginResetModel();
207 m_list.clear();
208 setNeedSave(true);
209 QString s;
210 while (!(s = stream.readLine()).isNull()) {
211 s = s.simplified();
212 if (s.isEmpty())
213 continue;
214 QString key = s.section(" ", 0, 0, QString::SectionSkipEmpty);
215 QString value = s.section(" ", 1, -1, QString::SectionSkipEmpty);
216 if (key.isEmpty() || value.isEmpty())
217 continue;
218 m_list.append(QPair<QString, QString>(key, value));
219 }
220 endResetModel();
221 }
222
223 bool QuickPhraseModel::saveData(const QString &file,
224 const QStringPairList &list) {
225 char *name = NULL;
226 QByteArray filenameArray = file.toLocal8Bit();
227 FcitxXDGMakeDirUser(QUICK_PHRASE_CONFIG_DIR);
228 FcitxXDGGetFileUserWithPrefix("", filenameArray.constData(), NULL, &name);
229 QString fileName = QString::fromLocal8Bit(name);
230 QTemporaryFile tempFile(fileName);
231 free(name);
232 if (!tempFile.open()) {
233 return false;
234 }
235
236 for (int i = 0; i < list.size(); i++) {
237 tempFile.write(list[i].first.toUtf8());
238 tempFile.write("\t");
239 tempFile.write(list[i].second.toUtf8());
240 tempFile.write("\n");
241 }
242
243 tempFile.setAutoRemove(false);
244 QFile::remove(fileName);
245 if (!tempFile.rename(fileName)) {
246 tempFile.remove();
247 }
248
249 return true;
250 }
251
252 void QuickPhraseModel::saveFinished() {
253 QFutureWatcher<bool> *watcher =
254 static_cast<QFutureWatcher<bool> *>(sender());
255 QFuture<bool> future = watcher->future();
256 if (future.result()) {
257 setNeedSave(false);
258 }
259 watcher->deleteLater();
260 }
261
262 void QuickPhraseModel::setNeedSave(bool needSave) {
263 if (m_needSave != needSave) {
264 m_needSave = needSave;
265 Q_EMIT needSaveChanged(m_needSave);
266 }
267 }
268 } // namespace fcitx