diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/function/CacheManager.cpp | 66 | ||||
-rw-r--r-- | src/core/function/CacheManager.h | 45 | ||||
-rw-r--r-- | src/core/function/gpg/GpgAdvancedOperator.cpp | 2 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 59 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.h | 11 |
5 files changed, 181 insertions, 2 deletions
diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp new file mode 100644 index 00000000..a20b8003 --- /dev/null +++ b/src/core/function/CacheManager.cpp @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2021 Saturneric + * + * 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 "CacheManager.h" + +#include <algorithm> + +#include "function/DataObjectOperator.h" +#include "nlohmann/json_fwd.hpp" +#include "spdlog/spdlog.h" + +void GpgFrontend::CacheManager::SaveCache(std::string key, + const nlohmann::json &value) { + auto stored_data = + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( + "__cache_data_list"); + + // get cache data list from file system + nlohmann::json cache_data_list; + if (stored_data.has_value()) { + cache_data_list = std::move(stored_data.value()); + } + + if (!cache_data_list.is_array()) { + cache_data_list.clear(); + } + + if (GpgFrontend::DataObjectOperator::GetInstance() + .SaveDataObj(key, value) + .empty()) { + return; + } + + if (std::find(cache_data_list.begin(), cache_data_list.end(), key) == + cache_data_list.end()) { + cache_data_list.push_back(key); + } + + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( + "__cache_data_list", cache_data_list); +} diff --git a/src/core/function/CacheManager.h b/src/core/function/CacheManager.h new file mode 100644 index 00000000..e489182f --- /dev/null +++ b/src/core/function/CacheManager.h @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2021 Saturneric + * + * 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 + * + */ + +#ifndef GPGFRONTEND_CACHEMANAGER_H +#define GPGFRONTEND_CACHEMANAGER_H + +namespace GpgFrontend { + +class CacheManager { + public: + static void SaveCache(std::string key, const nlohmann::json &value); + + static nlohmann::json LoadCache(std::string name); + + static void ClearAllCache(); +}; + +} // namespace GpgFrontend + +#endif diff --git a/src/core/function/gpg/GpgAdvancedOperator.cpp b/src/core/function/gpg/GpgAdvancedOperator.cpp index dc48410e..3cbee45d 100644 --- a/src/core/function/gpg/GpgAdvancedOperator.cpp +++ b/src/core/function/gpg/GpgAdvancedOperator.cpp @@ -32,6 +32,7 @@ #include "GpgAdvancedOperator.h" #include "core/function/gpg/GpgCommandExecutor.h" +#include "spdlog/spdlog.h" GpgFrontend::GpgAdvancedOperator::GpgAdvancedOperator(int channel) : SingletonFunctionObject(channel) {} @@ -42,6 +43,7 @@ bool GpgFrontend::GpgAdvancedOperator::ClearGpgPasswordCache() { ctx_.GetInfo().GpgConfPath, {"--reload", "gpg-agent"}, [&](int exit_code, const std::string &p_out, const std::string &p_err) { if (exit_code == 0) { + SPDLOG_DEBUG("gpgconf reload exit code: {}", exit_code); success = true; } }); diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index d8c4605d..5d10ece9 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -29,6 +29,11 @@ #include "ui/widgets/TextEdit.h" #include <boost/format.hpp> +#include <string> +#include <tuple> +#include <vector> + +#include "spdlog/spdlog.h" namespace GpgFrontend::UI { @@ -61,6 +66,8 @@ void TextEdit::SlotNewTab() { 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); } void TextEdit::slotNewHelpTab(const QString& title, const QString& path) const { @@ -88,6 +95,9 @@ void TextEdit::SlotOpenFile(const QString& path) { connect(page->GetTextPage()->document(), &QTextDocument::modificationChanged, this, &TextEdit::SlotShowModified); + // connect to cache recovery fucntion + connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged, + this, &TextEdit::slot_save_status_to_cache_for_revovery); QApplication::setOverrideCursor(Qt::WaitCursor); auto index = tab_widget_->addTab(page, stripped_name(path)); @@ -449,9 +459,17 @@ void TextEdit::SlotPrint() { #endif } -void TextEdit::SlotShowModified() const { +void TextEdit::SlotShowModified(bool changed) const { + // get current tab int index = tab_widget_->currentIndex(); QString title = tab_widget_->tabText(index); + + // if changed + if (!changed) { + tab_widget_->setTabText(index, title.remove(0, 2)); + return; + } + // if doc is modified now, add leading * to title, // otherwise remove the leading * from the title if (CurTextPage()->GetTextPage()->document()->isModified()) { @@ -580,4 +598,43 @@ void TextEdit::slot_file_page_path_changed(const QString& path) const { tab_widget_->setTabText(index, mPath); } +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; + + int tab_count = tab_widget_->count(); + SPDLOG_DEBUG("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++) { + auto* target_page = + qobject_cast<PlainTextEditorPage*>(tab_widget_->widget(i)); + + // if this page is no textedit, there should be nothing to save + if (target_page == nullptr) { + continue; + } + + auto* document = target_page->GetTextPage()->document(); + 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; + } + + auto raw_text = document->toRawText().toStdString(); + SPDLOG_DEBUG("unsaved page index: {}, tab title: {} tab content: {}", i, + tab_title, raw_text); + unsaved_pages.push_back({i, tab_title, raw_text}); + } +} + } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/TextEdit.h b/src/ui/widgets/TextEdit.h index 6398cf34..2e2f949d 100644 --- a/src/ui/widgets/TextEdit.h +++ b/src/ui/widgets/TextEdit.h @@ -172,7 +172,7 @@ class TextEdit : public QWidget { * @details put a * in front of current tabs title, if current textedit is * modified */ - void SlotShowModified() const; + void SlotShowModified(bool) const; /** * @details close the current tab and decrease TabWidget->count by \a 1 @@ -193,6 +193,8 @@ class TextEdit : public QWidget { void SlotSwitchTabDown() const; private: + uint text_page_data_modified_count_ = 0; ///< + /** * @details return just a filename stripped of a whole path * @@ -221,6 +223,13 @@ class TextEdit : public QWidget { */ void slot_remove_tab(int index); + /** + * @brief + * + * @param index + */ + void slot_save_status_to_cache_for_revovery(); + public slots: /** |