diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/dialog/settings/SettingsGeneral.cpp | 20 | ||||
-rw-r--r-- | src/ui/main_window/GeneralMainWindow.cpp | 4 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.cpp | 59 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.h | 16 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 51 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowUI.cpp | 6 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 76 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.h | 6 |
8 files changed, 224 insertions, 14 deletions
diff --git a/src/ui/dialog/settings/SettingsGeneral.cpp b/src/ui/dialog/settings/SettingsGeneral.cpp index c967b8fd..719e5de8 100644 --- a/src/ui/dialog/settings/SettingsGeneral.cpp +++ b/src/ui/dialog/settings/SettingsGeneral.cpp @@ -48,6 +48,9 @@ GeneralTab::GeneralTab(QWidget* parent) _("Save checked private keys on exit and restore them on next start.")); ui_->clearGpgPasswordCacheCheckBox->setText( _("Clear gpg password cache when closing GpgFrontend.")); + ui_->restoreTextEditorPageCheckBox->setText( + _("Automatically restore unsaved Text Editor pages after an application " + "crash.")); ui_->importConfirmationBox->setTitle(_("Operation")); ui_->longerKeyExpirationDateCheckBox->setText( @@ -98,6 +101,15 @@ void GeneralTab::SetSettings() { } try { + bool restore_text_editor_page = + settings.lookup("general.restore_text_editor_page"); + if (restore_text_editor_page) + ui_->restoreTextEditorPageCheckBox->setCheckState(Qt::Checked); + } catch (...) { + SPDLOG_ERROR("setting operation error: restore_text_editor_page"); + } + + try { bool longer_expiration_date = settings.lookup("general.longer_expiration_date"); SPDLOG_DEBUG("longer_expiration_date: {}", longer_expiration_date); @@ -170,6 +182,14 @@ void GeneralTab::ApplySettings() { ui_->saveCheckedKeysCheckBox->isChecked(); } + if (!general.exists("restore_text_editor_page")) + general.add("restore_text_editor_page", libconfig::Setting::TypeBoolean) = + ui_->restoreTextEditorPageCheckBox->isChecked(); + else { + general["restore_text_editor_page"] = + ui_->restoreTextEditorPageCheckBox->isChecked(); + } + #ifdef MULTI_LANG_SUPPORT if (!general.exists("lang")) general.add("lang", libconfig::Setting::TypeBoolean) = diff --git a/src/ui/main_window/GeneralMainWindow.cpp b/src/ui/main_window/GeneralMainWindow.cpp index 66255a08..0acedec6 100644 --- a/src/ui/main_window/GeneralMainWindow.cpp +++ b/src/ui/main_window/GeneralMainWindow.cpp @@ -41,7 +41,9 @@ GpgFrontend::UI::GeneralMainWindow::GeneralMainWindow(std::string name, GpgFrontend::UI::GeneralMainWindow::~GeneralMainWindow() = default; void GpgFrontend::UI::GeneralMainWindow::closeEvent(QCloseEvent *event) { + SPDLOG_DEBUG("main window close event caught, event type: {}", event->type()); slot_save_settings(); + QMainWindow::closeEvent(event); } @@ -51,6 +53,7 @@ void GpgFrontend::UI::GeneralMainWindow::slot_restore_settings() noexcept { std::string window_state = general_windows_state.Check( "window_state", saveState().toBase64().toStdString()); + SPDLOG_DEBUG("restore main window state: {}", window_state); // state sets pos & size of dock-widgets this->restoreState( @@ -133,6 +136,7 @@ void GpgFrontend::UI::GeneralMainWindow::slot_restore_settings() noexcept { void GpgFrontend::UI::GeneralMainWindow::slot_save_settings() noexcept { try { + SPDLOG_DEBUG("save main window state, name: {}", name_); SettingsObject general_windows_state(name_ + "_state"); // window position and size diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp index c31abeff..b07ad309 100644 --- a/src/ui/main_window/MainWindow.cpp +++ b/src/ui/main_window/MainWindow.cpp @@ -29,8 +29,12 @@ #include "MainWindow.h" #include "core/GpgConstants.h" +#include "core/function/CacheManager.h" #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgAdvancedOperator.h" +#include "main_window/GeneralMainWindow.h" +#include "nlohmann/json_fwd.hpp" +#include "spdlog/spdlog.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/struct/SettingsObject.h" @@ -85,6 +89,9 @@ void MainWindow::Init() noexcept { connect(this, &MainWindow::SignalUIRefresh, SignalStation::GetInstance(), &SignalStation::SignalUIRefresh); + connect(this, &MainWindow::SignalKeyDatabaseRefresh, + SignalStation::GetInstance(), + &SignalStation::SignalKeyDatabaseRefresh); connect(edit_->tab_widget_, &QTabWidget::currentChanged, this, &MainWindow::slot_disable_tab_actions); @@ -102,8 +109,10 @@ void MainWindow::Init() noexcept { m_key_list_->AddMenuAction(copy_mail_address_to_clipboard_act_); m_key_list_->AddMenuAction(copy_key_default_uid_to_clipboard_act_); m_key_list_->AddMenuAction(copy_key_id_to_clipboard_act_); + m_key_list_->AddMenuAction(set_owner_trust_of_key_act_); m_key_list_->AddMenuAction(add_key_2_favourtie_act_); m_key_list_->AddMenuAction(remove_key_from_favourtie_act_); + m_key_list_->AddSeparator(); m_key_list_->AddMenuAction(show_key_details_act_); @@ -163,6 +172,9 @@ void MainWindow::Init() noexcept { } }); + // recover unsaved page from cache if it exists + recover_editor_unsaved_pages_from_cache(); + } catch (...) { SPDLOG_ERROR(_("Critical error occur while loading GpgFrontend.")); QMessageBox::critical(nullptr, _("Loading Failed"), @@ -243,6 +255,40 @@ void MainWindow::restore_settings() { SPDLOG_DEBUG("settings restored"); } +void MainWindow::recover_editor_unsaved_pages_from_cache() { + auto unsaved_page_array = + CacheManager::GetInstance().LoadCache("editor_unsaved_pages"); + + if (!unsaved_page_array.is_array() || unsaved_page_array.empty()) { + return; + } + + SPDLOG_DEBUG("plan ot recover unsaved page from cache, page array: {}", + unsaved_page_array.dump()); + + bool first = true; + + for (auto &unsaved_page_json : unsaved_page_array) { + if (!unsaved_page_json.contains("title") || + !unsaved_page_json.contains("content")) { + continue; + } + std::string title = unsaved_page_json["title"]; + std::string content = unsaved_page_json["content"]; + + SPDLOG_DEBUG( + "recovering unsaved page from cache, page title: {}, content size", + title, content.size()); + + if (first) { + edit_->SlotCloseTab(); + first = false; + } + + edit_->SlotNewTabWithContent(title, content); + } +} + void MainWindow::save_settings() { bool save_key_checked = GlobalSettingStation::GetInstance().LookupSettings( "general.save_key_checked", false); @@ -285,8 +331,17 @@ void MainWindow::closeEvent(QCloseEvent *event) { event->ignore(); } - // clear password from memory - // GpgContext::GetInstance().clearPasswordCache(); + if (event->isAccepted()) { + // clear cache of unsaved page + CacheManager::GetInstance().SaveCache("editor_unsaved_pages", + nlohmann::json::array(), true); + + // clear password from memory + // GpgContext::GetInstance().clearPasswordCache(); + + // call parent + GeneralMainWindow::closeEvent(event); + } } } // namespace GpgFrontend::UI diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h index 670db23a..42f9daf3 100644 --- a/src/ui/main_window/MainWindow.h +++ b/src/ui/main_window/MainWindow.h @@ -102,6 +102,11 @@ class MainWindow : public GeneralMainWindow { */ void SignalUIRefresh(); + /** + * @brief + */ + void SignalKeyDatabaseRefresh(); + public slots: /** @@ -322,6 +327,11 @@ class MainWindow : public GeneralMainWindow { */ void slot_remove_key_from_favourite(); + /** + * @details + */ + void slot_set_owner_trust_level_of_key(); + private: /** * @details Create actions for the main-menu and the context-menu of the @@ -365,6 +375,11 @@ class MainWindow : public GeneralMainWindow { void restore_settings(); /** + * @details + */ + void recover_editor_unsaved_pages_from_cache(); + + /** * @details Save settings to ini-file. */ void save_settings(); @@ -438,6 +453,7 @@ class MainWindow : public GeneralMainWindow { QAction* add_key_2_favourtie_act_{}; ///< QAction* remove_key_from_favourtie_act_{}; ///< + QAction* set_owner_trust_of_key_act_{}; ///< QAction* open_key_management_act_{}; ///< Action to open key management QAction* copy_act_{}; ///< Action to copy text diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index fc0218b9..1adf2d4e 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -39,6 +39,7 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" +#include "core/function/gpg/GpgKeyManager.h" #include "dialog/SignersPicker.h" #include "spdlog/spdlog.h" #include "ui/UserInterfaceUtils.h" @@ -776,6 +777,56 @@ void MainWindow::refresh_keys_from_key_server() { dialog->SlotImport(key_ids); } +void MainWindow::slot_set_owner_trust_level_of_key() { + auto key_ids = m_key_list_->GetSelected(); + if (key_ids->empty()) return; + + auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); + + QStringList items; + + items << _("Unknown") << _("Undefined") << _("Never") << _("Marginal") + << _("Full") << _("Ultimate"); + bool ok; + QString item = QInputDialog::getItem(this, _("Modify Owner Trust Level"), + _("Trust for the Key Pair:"), items, + key.GetOwnerTrustLevel(), false, &ok); + + if (ok && !item.isEmpty()) { + SPDLOG_DEBUG("selected policy: {}", item.toStdString()); + int trust_level = 0; // Unknown Level + if (item == _("Ultimate")) { + trust_level = 5; + } else if (item == _("Full")) { + trust_level = 4; + } else if (item == _("Marginal")) { + trust_level = 3; + } else if (item == _("Never")) { + trust_level = 2; + } else if (item == _("Undefined")) { + trust_level = 1; + } + + if (trust_level == 0) { + QMessageBox::warning( + this, _("Warning"), + QString(_("Owner Trust Level cannot set to Unknown level, automately " + "changing it into Undefined level."))); + trust_level = 1; + } + + bool status = + GpgKeyManager::GetInstance().SetOwnerTrustLevel(key, trust_level); + if (!status) { + QMessageBox::critical(this, _("Failed"), + QString(_("Modify Owner Trust Level failed."))); + } else { + // update key database and refresh ui + emit SignalKeyDatabaseRefresh(); + } + } +} + void MainWindow::upload_key_to_server() { auto key_ids = m_key_list_->GetSelected(); auto* dialog = new KeyUploadDialog(key_ids, this); diff --git a/src/ui/main_window/MainWindowUI.cpp b/src/ui/main_window/MainWindowUI.cpp index 760561eb..d1a3cb68 100644 --- a/src/ui/main_window/MainWindowUI.cpp +++ b/src/ui/main_window/MainWindowUI.cpp @@ -433,6 +433,12 @@ void MainWindow::create_actions() { connect(remove_key_from_favourtie_act_, &QAction::triggered, this, &MainWindow::slot_remove_key_from_favourite); + set_owner_trust_of_key_act_ = new QAction(_("Set Owner Trust Level"), this); + set_owner_trust_of_key_act_->setToolTip(_("Set Owner Trust Level")); + set_owner_trust_of_key_act_->setData(QVariant("set_owner_trust_level")); + connect(set_owner_trust_of_key_act_, &QAction::triggered, this, + &MainWindow::slot_set_owner_trust_level_of_key); + /* Key-Shortcuts for Tab-Switchung-Action */ switch_tab_up_act_ = new QAction(this); diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index d22c091a..7af4d5f8 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -33,6 +33,9 @@ #include <tuple> #include <vector> +#include "core/function/CacheManager.h" +#include "core/function/GlobalSettingStation.h" +#include "nlohmann/json_fwd.hpp" #include "spdlog/spdlog.h" namespace GpgFrontend::UI { @@ -70,6 +73,33 @@ void TextEdit::SlotNewTab() { this, &TextEdit::slot_save_status_to_cache_for_revovery); } +void TextEdit::SlotNewTabWithContent(std::string title, + const std::string& content) { + QString header = _("untitled") + QString::number(++count_page_) + ".txt"; + if (!title.empty()) { + // modify title + if (!title.empty() && title[0] == '*') { + title.erase(0, 1); + } + // set title + header = QString::fromStdString(title); + } + + auto* page = new PlainTextEditorPage(); + auto index = tab_widget_->addTab(page, header); + tab_widget_->setTabIcon(index, QIcon(":file.png")); + tab_widget_->setCurrentIndex(tab_widget_->count() - 1); + page->GetTextPage()->setFocus(); + connect(page->GetTextPage()->document(), &QTextDocument::modificationChanged, + this, &TextEdit::SlotShowModified); + connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged, + this, &TextEdit::slot_save_status_to_cache_for_revovery); + + // set content with modified status + page->GetTextPage()->document()->setPlainText( + QString::fromStdString(content)); +} + void TextEdit::slotNewHelpTab(const QString& title, const QString& path) const { auto* page = new HelpPage(path); tab_widget_->addTab(page, title); @@ -599,15 +629,29 @@ void TextEdit::slot_file_page_path_changed(const QString& path) const { } void TextEdit::slot_save_status_to_cache_for_revovery() { - SPDLOG_DEBUG("catch text page modified event, count: {}", - text_page_data_modified_count_); - if (this->text_page_data_modified_count_++ % 3 != 0) return; + if (this->text_page_data_modified_count_++ % 8 != 0) return; + + auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); + bool restore_text_editor_page = false; + try { + restore_text_editor_page = + settings.lookup("general.restore_text_editor_page"); + } catch (...) { + SPDLOG_ERROR("setting operation error: restore_text_editor_page"); + } + + if (!restore_text_editor_page) { + SPDLOG_DEBUG("restore_text_editor_page is false, ignoring..."); + return; + } -#ifdef DEBUG int tab_count = tab_widget_->count(); - SPDLOG_DEBUG("current tabs count {}", tab_count); + SPDLOG_DEBUG( + "restore_text_editor_page is true, pan to save pages, current tabs " + "count: " + "{}", + tab_count); - std::vector<std::pair<int, std::string>> saved_pages; std::vector<std::tuple<int, std::string, std::string>> unsaved_pages; for (int i = 0; i < tab_count; i++) { @@ -623,11 +667,6 @@ void TextEdit::slot_save_status_to_cache_for_revovery() { auto tab_title = tab_widget_->tabText(i).toStdString(); if (!target_page->ReadDone() || !target_page->isEnabled() || !document->isModified()) { - auto file_path = target_page->GetFilePath().toStdString(); - SPDLOG_DEBUG("saved page index: {}, tab title: {} tab file path: {}", i, - tab_title, file_path); - - saved_pages.push_back({i, file_path}); continue; } @@ -636,7 +675,20 @@ void TextEdit::slot_save_status_to_cache_for_revovery() { tab_title, raw_text.size()); unsaved_pages.push_back({i, tab_title, raw_text}); } -#endif + + nlohmann::json unsaved_page_array = nlohmann::json::array(); + for (const auto& page : unsaved_pages) { + nlohmann::json page_json; + page_json["index"] = std::get<0>(page); + page_json["title"] = std::get<1>(page); + page_json["content"] = std::get<2>(page); + + unsaved_page_array.push_back(page_json); + } + + SPDLOG_DEBUG("unsaved page json array: {}", unsaved_page_array.dump()); + CacheManager::GetInstance().SaveCache("editor_unsaved_pages", + unsaved_page_array); } } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/TextEdit.h b/src/ui/widgets/TextEdit.h index 2e2f949d..f69bda4c 100644 --- a/src/ui/widgets/TextEdit.h +++ b/src/ui/widgets/TextEdit.h @@ -151,6 +151,12 @@ class TextEdit : public QWidget { void SlotNewTab(); /** + * @details + * + */ + void SlotNewTabWithContent(std::string title, const std::string& content); + + /** * @details Adds a new tab with opening file by path */ void SlotOpenFile(const QString& path); |