diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/function/CharsetOperator.cpp | 46 | ||||
-rw-r--r-- | src/core/function/CharsetOperator.h | 44 | ||||
-rw-r--r-- | src/pinentry/qti18n.cpp | 108 | ||||
-rw-r--r-- | src/ui/dialog/settings/SettingsDialog.cpp | 3 | ||||
-rw-r--r-- | src/ui/widgets/PlainTextEditorPage.cpp | 102 | ||||
-rw-r--r-- | src/ui/widgets/PlainTextEditorPage.h | 30 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 19 |
7 files changed, 66 insertions, 286 deletions
diff --git a/src/core/function/CharsetOperator.cpp b/src/core/function/CharsetOperator.cpp deleted file mode 100644 index 023fcd10..00000000 --- a/src/core/function/CharsetOperator.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * This file is part of GpgFrontend. - * - * GpgFrontend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GpgFrontend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. - * - * The initial version of the source code is inherited from - * the gpg4usb project, which is under GPL-3.0-or-later. - * - * All the source code of GpgFrontend was modified and released by - * Saturneric <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "core/function/CharsetOperator.h" - -#include "core/utils/LogUtils.h" - -namespace GpgFrontend { - -auto CharsetOperator::Detect(const QString &buffer) - -> CharsetOperator::CharsetInfo { - // TODO - return {"", "", 0}; -} - -auto CharsetOperator::Convert2Utf8(const QString &buffer, QString &out_buffer, - QString from_charset_name) -> bool { - // TODO -} - -} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/CharsetOperator.h b/src/core/function/CharsetOperator.h deleted file mode 100644 index c005f8b6..00000000 --- a/src/core/function/CharsetOperator.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * This file is part of GpgFrontend. - * - * GpgFrontend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GpgFrontend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. - * - * The initial version of the source code is inherited from - * the gpg4usb project, which is under GPL-3.0-or-later. - * - * All the source code of GpgFrontend was modified and released by - * Saturneric <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#pragma once - -#include "core/GpgFrontendCore.h" - -namespace GpgFrontend { - -class GPGFRONTEND_CORE_EXPORT CharsetOperator { - public: - using CharsetInfo = std::tuple<QString, QString, int>; - - static auto Detect(const QString &buffer) -> CharsetInfo; - - static auto Convert2Utf8(const QString &buffer, QString &out_buffer, - QString from_charset_name) -> bool; -}; -} // namespace GpgFrontend diff --git a/src/pinentry/qti18n.cpp b/src/pinentry/qti18n.cpp index 3556d153..49c27dd3 100644 --- a/src/pinentry/qti18n.cpp +++ b/src/pinentry/qti18n.cpp @@ -20,72 +20,74 @@ * SPDX-License-Identifier: GPL-2.0+ */ -#include <QDebug> #include <QCoreApplication> +#include <QDebug> #include <QLibraryInfo> #include <QLocale> #include <QTranslator> -static bool loadCatalog(const QString &catalog, const QLocale &locale) -{ - auto translator = new QTranslator(QCoreApplication::instance()); +static bool loadCatalog(const QString &catalog, const QLocale &locale) { + auto translator = new QTranslator(QCoreApplication::instance()); - if (!translator->load(locale, catalog, QString(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { - qDebug() << "Loading the" << catalog << "catalog failed for locale" << locale; - delete translator; - return false; - } - QCoreApplication::instance()->installTranslator(translator); - return true; + if (!translator->load(locale, catalog, QString(), + QLibraryInfo::path(QLibraryInfo::TranslationsPath))) { + qDebug() << "Loading the" << catalog << "catalog failed for locale" + << locale; + delete translator; + return false; + } + QCoreApplication::instance()->installTranslator(translator); + return true; } -static bool loadCatalog(const QString &catalog, const QLocale &locale, const QLocale &fallbackLocale) -{ - // try to load the catalog for locale - if (loadCatalog(catalog, locale)) { - return true; - } - // if this fails, then try the fallback locale (if it's different from locale) - if (fallbackLocale != locale) { - return loadCatalog(catalog, fallbackLocale); - } - return false; +static bool loadCatalog(const QString &catalog, const QLocale &locale, + const QLocale &fallbackLocale) { + // try to load the catalog for locale + if (loadCatalog(catalog, locale)) { + return true; + } + // if this fails, then try the fallback locale (if it's different from locale) + if (fallbackLocale != locale) { + return loadCatalog(catalog, fallbackLocale); + } + return false; } -// load global Qt translation, needed in KDE e.g. by lots of builtin dialogs (QColorDialog, QFontDialog) that we use -static void loadTranslation(const QString &localeName, const QString &fallbackLocaleName) -{ - const QLocale locale{localeName}; - const QLocale fallbackLocale{fallbackLocaleName}; - // first, try to load the qt_ meta catalog - if (loadCatalog(QStringLiteral("qt_"), locale, fallbackLocale)) { - return; - } - // if loading the meta catalog failed, then try loading the four catalogs - // it depends on, i.e. qtbase, qtscript, qtmultimedia, qtxmlpatterns, separately - const auto catalogs = { - QStringLiteral("qtbase_"), - /* QStringLiteral("qtscript_"), - QStringLiteral("qtmultimedia_"), - QStringLiteral("qtxmlpatterns_"), */ - }; - for (const auto &catalog : catalogs) { - loadCatalog(catalog, locale, fallbackLocale); - } +// load global Qt translation, needed in KDE e.g. by lots of builtin dialogs +// (QColorDialog, QFontDialog) that we use +static void loadTranslation(const QString &localeName, + const QString &fallbackLocaleName) { + const QLocale locale{localeName}; + const QLocale fallbackLocale{fallbackLocaleName}; + // first, try to load the qt_ meta catalog + if (loadCatalog(QStringLiteral("qt_"), locale, fallbackLocale)) { + return; + } + // if loading the meta catalog failed, then try loading the four catalogs + // it depends on, i.e. qtbase, qtscript, qtmultimedia, qtxmlpatterns, + // separately + const auto catalogs = { + QStringLiteral("qtbase_"), + /* QStringLiteral("qtscript_"), + QStringLiteral("qtmultimedia_"), + QStringLiteral("qtxmlpatterns_"), */ + }; + for (const auto &catalog : catalogs) { + loadCatalog(catalog, locale, fallbackLocale); + } } -static void load() -{ - // The way Qt translation system handles plural forms makes it necessary to - // have a translation file which contains only plural forms for `en`. That's - // why we load the `en` translation unconditionally, then load the - // translation for the current locale to overload it. - loadCatalog(QStringLiteral("qt_"), QLocale{QStringLiteral("en")}); +static void load() { + // The way Qt translation system handles plural forms makes it necessary to + // have a translation file which contains only plural forms for `en`. That's + // why we load the `en` translation unconditionally, then load the + // translation for the current locale to overload it. + loadCatalog(QStringLiteral("qt_"), QLocale{QStringLiteral("en")}); - const QLocale locale = QLocale::system(); - if (locale.name() != QStringLiteral("en")) { - loadTranslation(locale.name(), locale.bcp47Name()); - } + const QLocale locale = QLocale::system(); + if (locale.name() != QStringLiteral("en")) { + loadTranslation(locale.name(), locale.bcp47Name()); + } } Q_COREAPP_STARTUP_FUNCTION(load) diff --git a/src/ui/dialog/settings/SettingsDialog.cpp b/src/ui/dialog/settings/SettingsDialog.cpp index c69fb071..d0e90cd9 100644 --- a/src/ui/dialog/settings/SettingsDialog.cpp +++ b/src/ui/dialog/settings/SettingsDialog.cpp @@ -128,7 +128,6 @@ void SettingsDialog::SlotAccept() { auto SettingsDialog::ListLanguages() -> QHash<QString, QString> { QHash<QString, QString> languages; - languages.insert(QString(), _("System Default")); auto locale_path = GlobalSettingStation::GetInstance().GetLocaleDir(); @@ -142,7 +141,7 @@ auto SettingsDialog::ListLanguages() -> QHash<QString, QString> { if (locale == "." || locale == "..") continue; QLocale const q_locale(locale); - if (q_locale.nativeCountryName().isEmpty()) continue; + if (q_locale.nativeTerritoryName().isEmpty()) continue; auto language = q_locale.nativeLanguageName() + " (" + locale + ")"; languages.insert(locale, language); diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp index 1a6978fe..0426ed50 100644 --- a/src/ui/widgets/PlainTextEditorPage.cpp +++ b/src/ui/widgets/PlainTextEditorPage.cpp @@ -30,9 +30,7 @@ #include <utility> -#include "core/function/CharsetOperator.h" #include "core/thread/FileReadTask.h" -#include "core/thread/Task.h" #include "core/thread/TaskRunnerGetter.h" #include "ui/struct/SettingsObject.h" #include "ui/struct/settings/AppearanceSO.h" @@ -56,8 +54,8 @@ PlainTextEditorPage::PlainTextEditorPage(QString file_path, QWidget *parent) this->setAttribute(Qt::WA_DeleteOnClose); this->ui_->characterLabel->setText(_("0 character")); - this->ui_->lfLabel->setText(_("lf")); - this->ui_->encodingLabel->setText(_("utf-8")); + this->ui_->lfLabel->setHidden(true); + this->ui_->encodingLabel->setText("Unicode"); connect(ui_->textPage, &QPlainTextEdit::textChanged, this, [=]() { // if file is loading @@ -84,18 +82,8 @@ const QString &PlainTextEditorPage::GetFilePath() const { QPlainTextEdit *PlainTextEditorPage::GetTextPage() { return ui_->textPage; } -bool PlainTextEditorPage::WillCharsetChange() const { - // detect if the line-ending will change - if (is_crlf_) return true; - - // detect if the charset of the file will change - return charset_name_ != "UTF-8" && charset_name_ != "ISO-8859-1"; -} - void PlainTextEditorPage::NotifyFileSaved() { this->is_crlf_ = false; - this->charset_confidence_ = 100; - this->charset_name_ = "UTF-8"; this->ui_->lfLabel->setText(_("lf")); this->ui_->encodingLabel->setText(_("UTF-8")); @@ -181,7 +169,6 @@ void PlainTextEditorPage::ReadFile() { [=]() { read_task->SignalTaskShouldEnd(0); }); connect(read_task, &FileReadTask::SignalFileBytesReadEnd, this, [=]() { // set the UI - if (!binary_mode_) text_page->setReadOnly(false); this->read_done_ = true; this->ui_->textPage->setEnabled(true); text_page->document()->setModified(false); @@ -205,90 +192,15 @@ void PlainTextEditorPage::slot_insert_text(QByteArray bytes_data) { GF_UI_LOG_TRACE("inserting data read to editor, data size: {}", bytes_data.size()); read_bytes_ += bytes_data.size(); - // If binary format is detected, the entire file is converted to binary - // format for display. - bool if_last_binary_mode = binary_mode_; - if (!binary_mode_ && !read_done_) { - detect_encoding(bytes_data); - } - if (binary_mode_) { - // change formery displayed text to binary format - if (if_last_binary_mode != binary_mode_) { - auto text_buffer = ui_->textPage->document()->toRawText().toLocal8Bit(); - ui_->textPage->clear(); - this->GetTextPage()->insertPlainText(BinaryToString(text_buffer)); - this->ui_->lfLabel->setText("None"); - } + // insert the text to the text page + this->GetTextPage()->insertPlainText(bytes_data); - // insert new data - this->GetTextPage()->insertPlainText(BinaryToString(bytes_data)); + auto text = this->GetTextPage()->toPlainText(); + auto str = QString(_("%1 character(s)")).arg(text.size()); + this->ui_->characterLabel->setText(str); - // update the size of the file - auto str = QString(_("%1 byte(s)")).arg(read_bytes_); - this->ui_->characterLabel->setText(str); - } else { - // detect crlf/lf line ending - detect_cr_lf(bytes_data); - - // when reding from a text file - // try convert the any of thetext to utf8 - QString utf8_data; - if (!read_done_ && charset_confidence_ > 25) { - CharsetOperator::Convert2Utf8(bytes_data, utf8_data, charset_name_); - } else { - // when editing a text file, do nothing. - utf8_data = bytes_data; - } - - // insert the text to the text page - this->GetTextPage()->insertPlainText(utf8_data); - - auto text = this->GetTextPage()->toPlainText(); - auto str = QString(_("%1 character(s)")).arg(text.size()); - this->ui_->characterLabel->setText(str); - } QTimer::singleShot(25, this, &PlainTextEditorPage::SignalUIBytesDisplayed); } -void PlainTextEditorPage::detect_encoding(const QString &data) { - // skip the binary data to avoid the false detection of the encoding - if (binary_mode_) return; - - // detect the encoding - auto charset = CharsetOperator::Detect(data); - this->charset_name_ = std::get<0>(charset); - this->language_name_ = std::get<1>(charset); - this->charset_confidence_ = std::get<2>(charset); - - // probably there is no need to detect the encoding again - if (this->charset_confidence_ < 10) { - binary_mode_ = true; - } - - if (binary_mode_) { - // hide the line ending label, when the file is binary - this->ui_->lfLabel->setHidden(true); - this->ui_->encodingLabel->setText(_("binary")); - } else { - ui_->encodingLabel->setText(this->charset_name_); - } -} - -void PlainTextEditorPage::detect_cr_lf(const QString &data) { - if (binary_mode_) { - return; - } - - // if contain crlf, set the label to crlf - if (is_crlf_) return; - - if (data.contains("\r\n")) { - this->ui_->lfLabel->setText("crlf"); - is_crlf_ = true; - } else { - this->ui_->lfLabel->setText("lf"); - } -} - } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/PlainTextEditorPage.h b/src/ui/widgets/PlainTextEditorPage.h index 64724d7c..702dec1d 100644 --- a/src/ui/widgets/PlainTextEditorPage.h +++ b/src/ui/widgets/PlainTextEditorPage.h @@ -100,12 +100,6 @@ class PlainTextEditorPage : public QWidget { [[nodiscard]] bool ReadDone() const { return this->read_done_; } /** - * @brief detect if the charset of the file will change - * - */ - bool WillCharsetChange() const; - - /** * @brief notify the user that the file has been saved. * */ @@ -123,27 +117,9 @@ class PlainTextEditorPage : public QWidget { std::shared_ptr<Ui_PlainTextEditor> ui_; ///< QString full_file_path_; ///< The path to the file handled in the tab bool sign_marked_{}; ///< true, if the signed header is marked, false if not - bool read_done_ = false; ///< - bool binary_mode_ = false; ///< - size_t read_bytes_ = 0; ///< - QString charset_name_; ///< - QString language_name_; ///< - int32_t charset_confidence_{}; ///< - bool is_crlf_ = false; ///< - - /** - * @brief - * - * @param data - */ - void detect_encoding(const QString& data); - - /** - * @brief - * - * @param data - */ - void detect_cr_lf(const QString& data); + bool read_done_ = false; ///< + size_t read_bytes_ = 0; ///< + bool is_crlf_ = false; ///< private slots: diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index 0c5fb631..1f1f0242 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -183,26 +183,7 @@ bool TextEdit::save_file(const QString& fileName) { PlainTextEditorPage* page = SlotCurPageTextEdit(); if (page == nullptr) return false; - if (page->WillCharsetChange()) { - auto result = QMessageBox::warning( - this, _("Save"), - QString("<p>") + - _("After saving, the encoding of the current file will be " - "converted to UTF-8 and the line endings will be changed to " - "LF. ") + - "</p>" + "<p>" + - _("If this is not the result you expect, please use \"save " - "as\".") + - "</p>", - QMessageBox::Save | QMessageBox::Cancel, QMessageBox::Cancel); - - if (result == QMessageBox::Cancel) { - return false; - } - } - QFile file(fileName); - if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream output_stream(&file); QApplication::setOverrideCursor(Qt::WaitCursor); |