| 1 | /* |
| 2 | * chewing-editor: Chewing userphrase editor |
| 3 | * Copyright (C) 2014 Chewing Development Team |
| 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 2 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 along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include "UserphraseModel.h" |
| 21 | |
| 22 | #include <QDebug> |
| 23 | #include <algorithm> |
| 24 | #include <functional> |
| 25 | |
| 26 | static void logger(void *data, int level, const char *fmt, ...) |
| 27 | { |
| 28 | va_list ap; |
| 29 | |
| 30 | va_start(ap, fmt); |
| 31 | size_t len = vsnprintf(NULL, 0, fmt, ap); |
| 32 | va_end(ap); |
| 33 | |
| 34 | std::vector<char> buffer(len + 1); |
| 35 | |
| 36 | va_start(ap, fmt); |
| 37 | vsnprintf(&buffer[0], buffer.size(), fmt, ap); |
| 38 | va_end(ap); |
| 39 | |
| 40 | if (buffer[len - 1] == '\n') { |
| 41 | buffer[len - 1] = 0; |
| 42 | } |
| 43 | |
| 44 | if (level <= CHEWING_LOG_INFO) { |
| 45 | qDebug() << &buffer[0]; |
| 46 | } else if (level <= CHEWING_LOG_WARN) { |
| 47 | qWarning() << &buffer[0]; |
| 48 | } else if (level <= CHEWING_LOG_ERROR) { |
| 49 | qCritical() << &buffer[0]; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | UserphraseModel::UserphraseModel(QObject *parent, const char *path) |
| 54 | :QAbstractListModel{parent} |
| 55 | ,ctx_{chewing_new2(nullptr, path, logger, nullptr), chewing_delete} |
| 56 | ,userphrase_{} |
| 57 | ,addresult_(1) |
| 58 | { |
| 59 | if (!ctx_) { |
| 60 | // FIXME: Report error here |
| 61 | qCritical() << "chewing_new2() fails"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int UserphraseModel::rowCount(const QModelIndex &parent) const |
| 66 | { |
| 67 | return userphrase_.size(); |
| 68 | } |
| 69 | |
| 70 | QVariant UserphraseModel::data(const QModelIndex &index, int role) const |
| 71 | { |
| 72 | switch (role) { |
| 73 | case Qt::DisplayRole: |
| 74 | return userphrase_[index.row()].display_; |
| 75 | case Qt::WhatsThisRole: // FIXME: Provide "What's This?" |
| 76 | break; |
| 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | return QVariant(); |
| 82 | } |
| 83 | |
| 84 | void UserphraseModel::remove(QModelIndexList indexList) |
| 85 | { |
| 86 | if (indexList.empty()) { |
| 87 | qDebug() << "indexList is empty"; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | std::sort(indexList.begin(), indexList.end(), [] (const QModelIndex& a, const QModelIndex& b) { |
| 92 | // QModelIndex provides operator< but no operator> |
| 93 | return !(a < b); |
| 94 | }); |
| 95 | |
| 96 | // XXX: indexList is in revsrsed order, so first is actual last, and vice |
| 97 | // verse. |
| 98 | auto first = indexList.last().row(); |
| 99 | auto last = indexList.first().row(); |
| 100 | |
| 101 | emit beginRemoveRows(indexList.first().parent(), first, last); |
| 102 | |
| 103 | foreach(auto item, indexList) { |
| 104 | auto index = item.row(); |
| 105 | |
| 106 | qDebug() << userphrase_[index].phrase_ |
| 107 | << userphrase_[index].bopomofo_; |
| 108 | auto ret = chewing_userphrase_remove( |
| 109 | ctx_.get(), |
| 110 | userphrase_[index].phrase_.toUtf8().constData(), |
| 111 | userphrase_[index].bopomofo_.toUtf8().constData()); |
| 112 | if (ret > 0) { |
| 113 | removerecord_.push_back(Userphrase{userphrase_[index]}); |
| 114 | // FIXME: std::vector::erase is an inefficient operation. |
| 115 | userphrase_.erase(userphrase_.begin() + index); |
| 116 | } else { |
| 117 | qWarning() << "chewing_userphrase_remove() returns" << ret; |
| 118 | } |
| 119 | // FIXME: Handle chewing_userphrase_remove fails. |
| 120 | } |
| 121 | |
| 122 | emit endRemoveRows(); |
| 123 | |
| 124 | emit removePhraseCompleted(indexList.size()); |
| 125 | } |
| 126 | |
| 127 | void UserphraseModel::refresh() |
| 128 | { |
| 129 | UserphraseSet userphrase; |
| 130 | |
| 131 | std::vector<char> phrase; |
| 132 | unsigned int phrase_len; |
| 133 | |
| 134 | std::vector<char> bopomofo; |
| 135 | unsigned int bopomofo_len; |
| 136 | |
| 137 | chewing_userphrase_enumerate(ctx_.get()); |
| 138 | while (chewing_userphrase_has_next(ctx_.get(), &phrase_len, &bopomofo_len)) { |
| 139 | phrase.resize(phrase_len); |
| 140 | bopomofo.resize(bopomofo_len); |
| 141 | |
| 142 | int ret = chewing_userphrase_get(ctx_.get(), |
| 143 | &phrase[0], phrase.size(), |
| 144 | &bopomofo[0], bopomofo.size()); |
| 145 | if (ret == -1) { |
| 146 | qWarning() << "chewing_userphrase_get() returns" << ret; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | qDebug() << "Get userphrase:" << &phrase[0] << &bopomofo[0]; |
| 151 | userphrase.insert(Userphrase{ |
| 152 | QString::fromUtf8(&phrase[0]), |
| 153 | QString::fromUtf8(&bopomofo[0]) |
| 154 | }); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | emit beginResetModel(); |
| 159 | userphrase_.swap(userphrase); |
| 160 | emit endResetModel(); |
| 161 | |
| 162 | qDebug() << "Total userphrase" << userphrase_.size(); |
| 163 | emit refreshCompleted(userphrase_.size()); |
| 164 | } |
| 165 | |
| 166 | void UserphraseModel::importUserphrase(std::shared_ptr<UserphraseImporter> importer) |
| 167 | { |
| 168 | size_t old_count = userphrase_.size(); |
| 169 | |
| 170 | if (!importer.get()->isSupportedFormat()) { |
| 171 | emit importCompleted(false, importer.get()->getPath(), 0, old_count); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | auto result = importer.get()->getUserphraseSet(); |
| 176 | |
| 177 | for (auto& i: result) { |
| 178 | add(i.phrase_, i.bopomofo_); |
| 179 | } |
| 180 | |
| 181 | size_t new_count = userphrase_.size(); |
| 182 | |
| 183 | emit importCompleted(true, importer.get()->getPath(), new_count - old_count, new_count); |
| 184 | } |
| 185 | |
| 186 | void UserphraseModel::exportUserphrase(std::shared_ptr<UserphraseExporter> exporter) |
| 187 | { |
| 188 | size_t exported = userphrase_.size(); |
| 189 | |
| 190 | for (auto& i: userphrase_) { |
| 191 | exporter.get()->addUserphrase(i.phrase_, i.bopomofo_); |
| 192 | } |
| 193 | |
| 194 | bool result = exporter.get()->save(); |
| 195 | |
| 196 | emit exportCompleted(result, exporter.get()->getPath(), exported); |
| 197 | } |
| 198 | |
| 199 | QString UserphraseModel::checkBopomofo(const QString &bopomofo) const |
| 200 | { |
| 201 | QString replaceBopomofo = bopomofo; |
| 202 | replaceBopomofo.replace(QString::fromUtf8("一"),QString::fromUtf8("ㄧ")); |
| 203 | replaceBopomofo.replace(QString::fromUtf8("丫"),QString::fromUtf8("ㄚ")); |
| 204 | |
| 205 | return replaceBopomofo; |
| 206 | } |
| 207 | |
| 208 | void UserphraseModel::add(const QString &phrase, const QString &bopomofo) |
| 209 | { |
| 210 | QString replaceBopomofo = checkBopomofo(bopomofo); |
| 211 | auto ret = chewing_userphrase_add( |
| 212 | ctx_.get(), |
| 213 | phrase.toUtf8().constData(), |
| 214 | replaceBopomofo.toUtf8().constData()); |
| 215 | |
| 216 | addresult_ = ret; |
| 217 | |
| 218 | if (ret > 0) { |
| 219 | emit beginResetModel(); |
| 220 | userphrase_.insert(Userphrase{ |
| 221 | phrase, |
| 222 | bopomofo |
| 223 | }); |
| 224 | emit endResetModel(); |
| 225 | emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]); |
| 226 | } else { |
| 227 | qWarning() << "chewing_userphrase_add() returns" << ret; |
| 228 | refresh(); |
| 229 | emit addNewPhraseFailed(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | const Userphrase *UserphraseModel::getUserphrase(const QModelIndex& idx) |
| 234 | { |
| 235 | return &userphrase_[idx.row()]; |
| 236 | } |
| 237 | |
| 238 | void UserphraseModel::undo() |
| 239 | { |
| 240 | if (!removerecord_.empty()) { |
| 241 | auto last = removerecord_.end() - 1; |
| 242 | const QString phrase = last->display_; |
| 243 | add(last->phrase_, last->bopomofo_); |
| 244 | removerecord_.erase(last); |
| 245 | emit undoCompleted(phrase); |
| 246 | } |
| 247 | } |