|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2013~2020 CSSlayer <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "adddictdialog.h" |
| 9 | +#include "skk_dict_config.h" |
| 10 | +#include <QDebug> |
| 11 | +#include <QFileDialog> |
| 12 | +#include <fcitx-utils/standardpath.h> |
| 13 | +#include <fcitxqti18nhelper.h> |
| 14 | + |
| 15 | +#define FCITX_CONFIG_DIR "$FCITX_CONFIG_DIR" |
| 16 | + |
| 17 | +namespace fcitx { |
| 18 | + |
| 19 | +enum DictType { DictType_Static, DictType_User }; |
| 20 | + |
| 21 | +AddDictDialog::AddDictDialog(QWidget *parent) |
| 22 | + : QDialog(parent), m_ui(std::make_unique<Ui::AddDictDialog>()) { |
| 23 | + m_ui->setupUi(this); |
| 24 | + m_ui->typeComboBox->addItem(_("System")); |
| 25 | + m_ui->typeComboBox->addItem(_("User")); |
| 26 | + m_ui->typeComboBox->setCurrentIndex(1); |
| 27 | + |
| 28 | + indexChanged(0); |
| 29 | + |
| 30 | + connect(m_ui->browseButton, &QPushButton::clicked, this, |
| 31 | + &AddDictDialog::browseClicked); |
| 32 | + connect(m_ui->typeComboBox, |
| 33 | + QOverload<int>::of(&QComboBox::currentIndexChanged), this, |
| 34 | + &AddDictDialog::indexChanged); |
| 35 | + connect(m_ui->urlLineEdit, &QLineEdit::textChanged, this, |
| 36 | + &AddDictDialog::validate); |
| 37 | +} |
| 38 | + |
| 39 | +QMap<QString, QString> AddDictDialog::dictionary() { |
| 40 | + int idx = m_ui->typeComboBox->currentIndex(); |
| 41 | + idx = idx < 0 ? 0 : idx; |
| 42 | + idx = idx > 1 ? 0 : idx; |
| 43 | + |
| 44 | + QMap<QString, QString> dict; |
| 45 | + const char *type[] = {"readonly", "readwrite"}; |
| 46 | + |
| 47 | + dict["type"] = "file"; |
| 48 | + dict["file"] = m_ui->urlLineEdit->text(); |
| 49 | + dict["mode"] = type[idx]; |
| 50 | + dict["encoding"] = m_ui->encodingEdit->text(); |
| 51 | + |
| 52 | + return dict; |
| 53 | +} |
| 54 | + |
| 55 | +void AddDictDialog::indexChanged([[maybe_unused]] int _idx) { |
| 56 | + validate(); |
| 57 | +} |
| 58 | + |
| 59 | +void AddDictDialog::validate() { |
| 60 | + const auto index = m_ui->typeComboBox->currentIndex(); |
| 61 | + bool valid = true; |
| 62 | + switch (index) { |
| 63 | + case DictType_Static: |
| 64 | + case DictType_User: |
| 65 | + if (m_ui->urlLineEdit->text().isEmpty() || m_ui->encodingEdit->text().isEmpty()) { |
| 66 | + valid = false; |
| 67 | + } |
| 68 | + break; |
| 69 | + } |
| 70 | + m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); |
| 71 | +} |
| 72 | + |
| 73 | +void AddDictDialog::browseClicked() { |
| 74 | + QString path = m_ui->urlLineEdit->text(); |
| 75 | + if (m_ui->typeComboBox->currentIndex() == DictType_Static) { |
| 76 | + QString dir; |
| 77 | + if (path.isEmpty()) { |
| 78 | + path = SKK_DICT_DEFAULT_PATH; |
| 79 | + } |
| 80 | + QFileInfo info(path); |
| 81 | + path = QFileDialog::getOpenFileName(this, _("Select Dictionary File"), |
| 82 | + info.path()); |
| 83 | + } else { |
| 84 | + auto fcitxBasePath = stringutils::joinPath( |
| 85 | + StandardPath::global().userDirectory(StandardPath::Type::PkgData), |
| 86 | + "cskk"); |
| 87 | + fs::makePath(fcitxBasePath); |
| 88 | + QString fcitxConfigBasePath = |
| 89 | + QDir::cleanPath(QString::fromStdString(fcitxBasePath)); |
| 90 | + |
| 91 | + if (path.isEmpty()) { |
| 92 | + auto baseDataPath = stringutils::joinPath( |
| 93 | + StandardPath::global().userDirectory(StandardPath::Type::Data), "fcitx5-cskk"); |
| 94 | + fs::makePath(baseDataPath); |
| 95 | + QString basePath = QDir::cleanPath(QString::fromStdString(baseDataPath)); |
| 96 | + path = basePath; |
| 97 | + } else if (path.startsWith(FCITX_CONFIG_DIR "/")) { |
| 98 | + |
| 99 | + QDir dir(fcitxConfigBasePath); |
| 100 | + path = dir.filePath(path.mid(strlen(FCITX_CONFIG_DIR) + 1)); |
| 101 | + } |
| 102 | + path = |
| 103 | + QFileDialog::getOpenFileName(this, _("Select Dictionary File"), path); |
| 104 | + if (path.startsWith(fcitxConfigBasePath + "/")) { |
| 105 | + path = FCITX_CONFIG_DIR + path.mid(fcitxConfigBasePath.length(), -1); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (!path.isEmpty()) { |
| 110 | + m_ui->urlLineEdit->setText(path); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace fcitx |
0 commit comments