| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2018-2018 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "pinyindictmanager.h" |
| 8 |
#include "config.h" |
| 9 |
#include "filelistmodel.h" |
| 10 |
#include "pipeline.h" |
| 11 |
#include "processrunner.h" |
| 12 |
#include "renamefile.h" |
| 13 |
#include <QAction> |
| 14 |
#include <QDesktopServices> |
| 15 |
#include <QDialog> |
| 16 |
#include <QDir> |
| 17 |
#include <QFileDialog> |
| 18 |
#include <QFileInfo> |
| 19 |
#include <QInputDialog> |
| 20 |
#include <QLineEdit> |
| 21 |
#include <QMenu> |
| 22 |
#include <QMessageBox> |
| 23 |
#include <QMetaObject> |
| 24 |
#include <QPushButton> |
| 25 |
#include <QString> |
| 26 |
#include <QStringList> |
| 27 |
#include <QTemporaryFile> |
| 28 |
#include <fcitx-utils/fs.h> |
| 29 |
#include <fcitx-utils/i18n.h> |
| 30 |
#include <fcitx-utils/standardpaths.h> |
| 31 |
#include <fcitx-utils/stringutils.h> |
| 32 |
#include <fcitxqtconfiguiwidget.h> |
| 33 |
#include <qnamespace.h> |
| 34 |
#include <string> |
| 35 |
|
| 36 |
#ifdef ENABLE_BROWSER |
| 37 |
#include "browserdialog.h" |
| 38 |
#include "filedownloader.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
namespace fcitx { |
| 42 |
|
| 43 |
PinyinDictManager::PinyinDictManager(QWidget *parent) |
| 44 |
: FcitxQtConfigUIWidget(parent), model_(new FileListModel(this)), |
| 45 |
pipeline_(new Pipeline(this)) { |
| 46 |
setupUi(this); |
| 47 |
|
| 48 |
auto *menu = new QMenu(this); |
| 49 |
importFromFileAction_ = new QAction(_("From &File"), this); |
| 50 |
importFromSogou_ = new QAction(_("From &Sogou Cell Dictionary File"), this); |
| 51 |
importFromSogouOnline_ = |
| 52 |
new QAction(_("&Browse Sogou Cell Dictionary Online"), this); |
| 53 |
menu->addAction(importFromFileAction_); |
| 54 |
menu->addAction(importFromSogou_); |
| 55 |
menu->addAction(importFromSogouOnline_); |
| 56 |
importButton_->setMenu(menu); |
| 57 |
|
| 58 |
menu = new QMenu(this); |
| 59 |
clearUserDictAction_ = new QAction(_("&Clear User Data"), this); |
| 60 |
clearAllDataAction_ = new QAction(_("Clear &All Data"), this); |
| 61 |
menu->addAction(clearUserDictAction_); |
| 62 |
menu->addAction(clearAllDataAction_); |
| 63 |
clearDictButton_->setMenu(menu); |
| 64 |
|
| 65 |
listView_->setModel(model_); |
| 66 |
|
| 67 |
connect(importFromFileAction_, &QAction::triggered, this, |
| 68 |
&PinyinDictManager::importFromFile); |
| 69 |
connect(importFromSogou_, &QAction::triggered, this, |
| 70 |
&PinyinDictManager::importFromSogou); |
| 71 |
connect(importFromSogouOnline_, &QAction::triggered, this, |
| 72 |
&PinyinDictManager::importFromSogouOnline); |
| 73 |
connect(clearUserDictAction_, &QAction::triggered, this, |
| 74 |
&PinyinDictManager::clearUserDict); |
| 75 |
connect(clearAllDataAction_, &QAction::triggered, this, |
| 76 |
&PinyinDictManager::clearAllDict); |
| 77 |
|
| 78 |
connect(removeButton_, &QPushButton::clicked, this, |
| 79 |
&PinyinDictManager::removeDict); |
| 80 |
connect(removeAllButton_, &QPushButton::clicked, this, |
| 81 |
&PinyinDictManager::removeAllDict); |
| 82 |
connect(openDirectoryButton_, &QPushButton::clicked, this, |
| 83 |
&PinyinDictManager::openUserDirectory); |
| 84 |
|
| 85 |
connect(pipeline_, &Pipeline::finished, this, [this]() { |
| 86 |
setEnabled(true); |
| 87 |
reload(); |
| 88 |
}); |
| 89 |
|
| 90 |
model_->loadFileList(); |
| 91 |
connect(model_, &FileListModel::changed, this, [this]() { changed(true); }); |
| 92 |
} |
| 93 |
|
| 94 |
void PinyinDictManager::load() {} |
| 95 |
|
| 96 |
void PinyinDictManager::save() { |
| 97 |
QMetaObject::invokeMethod( |
| 98 |
this, |
| 99 |
[this]() { |
| 100 |
model_->save(); |
| 101 |
Q_EMIT changed(false); |
| 102 |
Q_EMIT saveFinished(); |
| 103 |
}, |
| 104 |
Qt::QueuedConnection); |
| 105 |
} |
| 106 |
|
| 107 |
QString PinyinDictManager::title() { return _("Pinyin dictionary manager"); } |
| 108 |
|
| 109 |
bool PinyinDictManager::asyncSave() { return true; } |
| 110 |
|
| 111 |
QString PinyinDictManager::confirmImportFileName(const QString &defaultName) { |
| 112 |
bool ok; |
| 113 |
auto importName = QInputDialog::getText( |
| 114 |
this, _("Input Dictionary Name"), _("New Dictionary Name:"), |
| 115 |
QLineEdit::Normal, defaultName, &ok); |
| 116 |
if (!ok) { |
| 117 |
return QString(); |
| 118 |
} |
| 119 |
return importName; |
| 120 |
} |
| 121 |
|
| 122 |
QString PinyinDictManager::prepareDirectory() { |
| 123 |
auto directory = |
| 124 |
StandardPaths::global().userDirectory(StandardPathsType::PkgData) / |
| 125 |
"pinyin/dictionaries"; |
| 126 |
if (!fs::makePath(directory)) { |
| 127 |
QMessageBox::warning(this, _("Failed to create directory"), |
| 128 |
_("Create directory failed. Please check the " |
| 129 |
"permission or disk space.")); |
| 130 |
return ""; |
| 131 |
} |
| 132 |
return QString::fromStdU16String(directory.u16string()); |
| 133 |
} |
| 134 |
|
| 135 |
QString PinyinDictManager::prepareTempFile(const QString &tempFileTemplate) { |
| 136 |
QTemporaryFile tempFile(tempFileTemplate); |
| 137 |
if (!tempFile.open()) { |
| 138 |
QMessageBox::warning(this, _("Failed to create temp file"), |
| 139 |
_("Creating temp file failed. Please check the " |
| 140 |
"permission or disk space.")); |
| 141 |
return QString(); |
| 142 |
} |
| 143 |
tempFile.setAutoRemove(false); |
| 144 |
return tempFile.fileName(); |
| 145 |
} |
| 146 |
|
| 147 |
QString PinyinDictManager::checkOverwriteFile(const QString &dirName, |
| 148 |
const QString &importName) { |
| 149 |
QDir dir(dirName); |
| 150 |
auto fullname = dir.filePath(importName + ".dict"); |
| 151 |
|
| 152 |
if (QFile::exists(fullname)) { |
| 153 |
auto button = QMessageBox::warning( |
| 154 |
this, _("Dictionary already exists"), |
| 155 |
QString(_("%1 already exists, do you want to overwrite this " |
| 156 |
"dictionary?")) |
| 157 |
.arg(importName), |
| 158 |
QMessageBox::Yes | QMessageBox::No, QMessageBox::No); |
| 159 |
if (button == QMessageBox::No) { |
| 160 |
return QString(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return fullname; |
| 165 |
} |
| 166 |
|
| 167 |
void PinyinDictManager::reload() { |
| 168 |
model_->loadFileList(); |
| 169 |
saveSubConfig("fcitx://config/addon/pinyin/dictmanager"); |
| 170 |
} |
| 171 |
|
| 172 |
void PinyinDictManager::importFromFile() { |
| 173 |
QString name = |
| 174 |
QFileDialog::getOpenFileName(this, _("Select Dictionary File")); |
| 175 |
if (name.isEmpty()) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
QFileInfo info(name); |
| 180 |
QString importName = info.fileName(); |
| 181 |
|
| 182 |
if (importName.endsWith(".txt")) { |
| 183 |
importName = importName.left(importName.size() - 4); |
| 184 |
} |
| 185 |
|
| 186 |
importName = confirmImportFileName(importName); |
| 187 |
if (importName.isEmpty()) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
auto directory = prepareDirectory(); |
| 192 |
if (directory.isEmpty()) { |
| 193 |
return; |
| 194 |
} |
| 195 |
QDir dir(directory); |
| 196 |
auto fullname = dir.filePath(importName + ".dict"); |
| 197 |
auto tempFile = prepareTempFile(fullname + "_XXXXXX"); |
| 198 |
if (tempFile.isEmpty()) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
setEnabled(false); |
| 203 |
pipeline_->reset(); |
| 204 |
auto *runner = new ProcessRunner( |
| 205 |
"libime_pinyindict", QStringList() << info.filePath() << tempFile, |
| 206 |
tempFile); |
| 207 |
auto *rename = new RenameFile(tempFile, fullname); |
| 208 |
pipeline_->addJob(runner); |
| 209 |
pipeline_->addJob(rename); |
| 210 |
pipeline_->start(); |
| 211 |
} |
| 212 |
|
| 213 |
void PinyinDictManager::importFromSogou() { |
| 214 |
QString name = QFileDialog::getOpenFileName( |
| 215 |
this, _("Select scel file"), QString(), _("Scel file (*.scel)")); |
| 216 |
if (name.isEmpty()) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
QFileInfo info(name); |
| 221 |
QString importName = info.fileName(); |
| 222 |
if (importName.endsWith(".scel")) { |
| 223 |
importName = importName.left(importName.size() - 5); |
| 224 |
} |
| 225 |
|
| 226 |
importName = confirmImportFileName(importName); |
| 227 |
if (importName.isEmpty()) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
auto directory = prepareDirectory(); |
| 232 |
if (directory.isEmpty()) { |
| 233 |
return; |
| 234 |
} |
| 235 |
auto runtimeDirectory = |
| 236 |
StandardPaths::global().userDirectory(StandardPathsType::Runtime); |
| 237 |
if (runtimeDirectory.empty()) { |
| 238 |
QMessageBox::warning(this, _("Failed to get runtime directory"), |
| 239 |
_("Create directory failed. Please check the " |
| 240 |
"permission or disk space.")); |
| 241 |
return; |
| 242 |
} |
| 243 |
auto fullname = checkOverwriteFile(directory, importName); |
| 244 |
|
| 245 |
if (fullname.isEmpty()) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
auto tempFile = prepareTempFile(fullname + "_XXXXXX"); |
| 250 |
|
| 251 |
QDir runtimeDir(runtimeDirectory); |
| 252 |
auto txtFile = prepareTempFile(runtimeDir.filePath("scel_txt_XXXXXX")); |
| 253 |
if (tempFile.isEmpty() || txtFile.isEmpty()) { |
| 254 |
if (!tempFile.isEmpty()) { |
| 255 |
QFile::remove(tempFile); |
| 256 |
} |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
setEnabled(false); |
| 261 |
pipeline_->reset(); |
| 262 |
auto *scelrunner = new ProcessRunner( |
| 263 |
"scel2org5", |
| 264 |
QStringList() << info.absoluteFilePath() << "-o" << txtFile, txtFile); |
| 265 |
pipeline_->addJob(scelrunner); |
| 266 |
auto *dictrunner = new ProcessRunner( |
| 267 |
"libime_pinyindict", QStringList() << txtFile << tempFile, tempFile); |
| 268 |
pipeline_->addJob(dictrunner); |
| 269 |
auto *rename = new RenameFile(tempFile, fullname); |
| 270 |
pipeline_->addJob(rename); |
| 271 |
pipeline_->start(); |
| 272 |
} |
| 273 |
|
| 274 |
void PinyinDictManager::importFromSogouOnline() { |
| 275 |
#ifdef ENABLE_BROWSER |
| 276 |
BrowserDialog dialog(this); |
| 277 |
int result = dialog.exec(); |
| 278 |
if (result != QDialog::Accepted) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
QString importName = dialog.name(); |
| 283 |
|
| 284 |
importName = confirmImportFileName(importName); |
| 285 |
if (importName.isEmpty()) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
auto directory = prepareDirectory(); |
| 290 |
if (directory.isEmpty()) { |
| 291 |
return; |
| 292 |
} |
| 293 |
auto runtimeDirectory = |
| 294 |
StandardPaths::global().userDirectory(StandardPathsType::Runtime); |
| 295 |
if (runtimeDirectory.empty()) { |
| 296 |
QMessageBox::warning(this, _("Failed to get runtime directory"), |
| 297 |
_("Create directory failed. Please check the " |
| 298 |
"permission or disk space.")); |
| 299 |
return; |
| 300 |
} |
| 301 |
auto fullname = checkOverwriteFile(directory, importName); |
| 302 |
|
| 303 |
if (fullname.isEmpty()) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
QDir runtimeDir(runtimeDirectory); |
| 308 |
auto tempFile = prepareTempFile(fullname + "_XXXXXX"); |
| 309 |
auto txtFile = prepareTempFile(runtimeDir.filePath("scel_txt_XXXXXX")); |
| 310 |
auto scelFile = prepareTempFile(runtimeDir.filePath("scel_XXXXXX")); |
| 311 |
QStringList list; |
| 312 |
list << tempFile << txtFile << scelFile; |
| 313 |
for (const auto &file : list) { |
| 314 |
if (file.isEmpty()) { |
| 315 |
for (const auto &file : list) { |
| 316 |
if (!file.isEmpty()) { |
| 317 |
QFile::remove(file); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
setEnabled(false); |
| 326 |
pipeline_->reset(); |
| 327 |
auto *fileDownloader = new FileDownloader(dialog.url(), scelFile); |
| 328 |
pipeline_->addJob(fileDownloader); |
| 329 |
auto *scelrunner = new ProcessRunner( |
| 330 |
"scel2org5", QStringList() << scelFile << "-o" << txtFile, txtFile); |
| 331 |
pipeline_->addJob(scelrunner); |
| 332 |
auto *dictrunner = new ProcessRunner( |
| 333 |
"libime_pinyindict", QStringList() << txtFile << tempFile, tempFile); |
| 334 |
pipeline_->addJob(dictrunner); |
| 335 |
auto *rename = new RenameFile(tempFile, fullname); |
| 336 |
pipeline_->addJob(rename); |
| 337 |
pipeline_->start(); |
| 338 |
#else |
| 339 |
QDesktopServices::openUrl(QUrl("https://pinyin.sogou.com/dict/")); |
| 340 |
#endif |
| 341 |
} |
| 342 |
|
| 343 |
void PinyinDictManager::removeAllDict() { |
| 344 |
int ret = QMessageBox::question( |
| 345 |
this, _("Confirm deletion"), |
| 346 |
QString::fromUtf8(_("Are you sure to delete all dictionaries?")), |
| 347 |
QMessageBox::Ok | QMessageBox::Cancel); |
| 348 |
|
| 349 |
if (ret != QMessageBox::Ok) { |
| 350 |
return; |
| 351 |
} |
| 352 |
for (int i = 0; i < model_->rowCount(); i++) { |
| 353 |
QModelIndex index = model_->index(i); |
| 354 |
|
| 355 |
std::string fileName = |
| 356 |
index.data(Qt::UserRole).toString().toLocal8Bit().constData(); |
| 357 |
auto fullPath = StandardPaths::global().locate( |
| 358 |
StandardPathsType::PkgData, "pinyin/dictionaries/" + fileName); |
| 359 |
QFile::remove(fullPath); |
| 360 |
} |
| 361 |
reload(); |
| 362 |
} |
| 363 |
|
| 364 |
void PinyinDictManager::removeDict() { |
| 365 |
QModelIndex index = listView_->currentIndex(); |
| 366 |
if (!index.isValid()) { |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
QString curName = index.data(Qt::DisplayRole).toString(); |
| 371 |
std::string fileName = |
| 372 |
index.data(Qt::UserRole).toString().toLocal8Bit().constData(); |
| 373 |
auto fullPath = StandardPaths::global().locate( |
| 374 |
StandardPathsType::PkgData, "pinyin/dictionaries/" + fileName); |
| 375 |
|
| 376 |
int ret = QMessageBox::question( |
| 377 |
this, _("Confirm deletion"), |
| 378 |
QString::fromUtf8(_("Are you sure to delete %1?")).arg(curName), |
| 379 |
QMessageBox::Ok | QMessageBox::Cancel); |
| 380 |
|
| 381 |
if (ret == QMessageBox::Ok) { |
| 382 |
bool ok = QFile::remove(fullPath); |
| 383 |
if (!ok) { |
| 384 |
QMessageBox::warning( |
| 385 |
this, _("File Operation Failed"), |
| 386 |
QString::fromUtf8(_("Error while deleting %1.")).arg(curName)); |
| 387 |
} else { |
| 388 |
reload(); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
void PinyinDictManager::clearUserDict() { |
| 394 |
Q_EMIT saveSubConfig("fcitx://config/addon/pinyin/clearuserdict"); |
| 395 |
} |
| 396 |
|
| 397 |
void PinyinDictManager::clearAllDict() { |
| 398 |
Q_EMIT saveSubConfig("fcitx://config/addon/pinyin/clearalldict"); |
| 399 |
} |
| 400 |
|
| 401 |
void PinyinDictManager::openUserDirectory() { |
| 402 |
QString path = prepareDirectory(); |
| 403 |
if (path.isEmpty()) { |
| 404 |
return; |
| 405 |
} |
| 406 |
QDesktopServices::openUrl(QUrl::fromLocalFile(path)); |
| 407 |
} |
| 408 |
|
| 409 |
} // namespace fcitx |