aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-02-13 06:54:44 +0000
committerSaturneric <[email protected]>2022-02-13 06:54:44 +0000
commitb991fea4912c63170c79ccdc586e3d3bc4297715 (patch)
tree8d1e1c2acc55442a52caf7f8814a689d9f7ba37b
parent<fix>(ci): Set Git's file line endings to LF (diff)
downloadGpgFrontend-b991fea4912c63170c79ccdc586e3d3bc4297715.tar.gz
GpgFrontend-b991fea4912c63170c79ccdc586e3d3bc4297715.zip
<feat, refactor>(src): Enable top encryption action menu in file mode
1. Adjust the names of some functions 2. Enable top encryption action menu in file mode
-rw-r--r--src/main.cpp2
-rw-r--r--src/ui/main_window/MainWindow.cpp4
-rw-r--r--src/ui/main_window/MainWindow.h23
-rw-r--r--src/ui/main_window/MainWindowSlotFunction.cpp31
-rw-r--r--src/ui/main_window/MainWindowSlotUI.cpp33
-rw-r--r--src/ui/widgets/FilePage.cpp63
6 files changed, 132 insertions, 24 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 9af7c2ea..3f72be7f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -218,7 +218,7 @@ int main(int argc, char* argv[]) {
// create main window and show it
auto main_window = std::make_unique<GpgFrontend::UI::MainWindow>();
- main_window->init();
+ main_window->Init();
main_window->show();
return_from_event_loop_code = QApplication::exec();
#ifdef RELEASE
diff --git a/src/ui/main_window/MainWindow.cpp b/src/ui/main_window/MainWindow.cpp
index 66c240a9..53fce2ca 100644
--- a/src/ui/main_window/MainWindow.cpp
+++ b/src/ui/main_window/MainWindow.cpp
@@ -43,7 +43,7 @@ MainWindow::MainWindow() {
this->setWindowTitle(qApp->applicationName());
}
-void MainWindow::init() noexcept {
+void MainWindow::Init() noexcept {
try {
/* get path where app was started */
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
@@ -117,7 +117,7 @@ void MainWindow::init() noexcept {
slot_start_wizard();
}
- emit Loaded();
+ emit SignalLoaded();
// if not prohibit update checking
if (!prohibit_update_checking_) {
diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h
index cfae16ab..5cc9a15a 100644
--- a/src/ui/main_window/MainWindow.h
+++ b/src/ui/main_window/MainWindow.h
@@ -53,6 +53,19 @@ class MainWindow : public QMainWindow {
Q_OBJECT
public:
+
+ struct CryptoMenu{
+ using OperationType = unsigned int;
+
+ static constexpr OperationType None = 0;
+ static constexpr OperationType Encrypt = 1 << 0;
+ static constexpr OperationType Sign = 1 << 1;
+ static constexpr OperationType Decrypt = 1 << 2;
+ static constexpr OperationType Verify = 1 << 3;
+ static constexpr OperationType EncryptAndSign = 1 << 4;
+ static constexpr OperationType DecryptAndVerify = 1 << 5;
+ };
+
/**
* @brief
*
@@ -62,14 +75,20 @@ class MainWindow : public QMainWindow {
/**
* @details ONLY Called from main()
*/
- void init() noexcept;
+ void Init() noexcept;
+
+ /**
+ * @details refresh and enable specify crypto-menu actions.
+ */
+ void SetCryptoMenuStatus(CryptoMenu::OperationType type);
signals:
/**
* @brief
*/
- void Loaded();
+ void SignalLoaded();
+
public slots:
diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp
index 6286ed49..9a05cdc6 100644
--- a/src/ui/main_window/MainWindowSlotFunction.cpp
+++ b/src/ui/main_window/MainWindowSlotFunction.cpp
@@ -39,12 +39,12 @@
#include "ui/mail/SendMailDialog.h"
#endif
+#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgBasicOperator.h"
#include "core/function/gpg/GpgKeyGetter.h"
#include "core/function/gpg/GpgKeyImportExporter.h"
#include "ui/UserInterfaceUtils.h"
#include "ui/help/AboutDialog.h"
-#include "core/function/GlobalSettingStation.h"
#include "ui/widgets/SignersPicker.h"
namespace GpgFrontend::UI {
@@ -52,7 +52,10 @@ namespace GpgFrontend::UI {
* Encrypt Entry(Text & File)
*/
void MainWindow::slot_encrypt() {
- if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
+ if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
+ if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileEncrypt();
+ return;
+ }
auto key_ids = m_key_list_->GetChecked();
@@ -191,7 +194,10 @@ void MainWindow::slot_sign() {
}
void MainWindow::slot_decrypt() {
- if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
+ if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
+ if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileDecrypt();
+ return;
+ }
auto decrypted = std::make_unique<ByteArray>();
QByteArray text = edit_->CurTextPage()->GetTextPage()->toPlainText().toUtf8();
@@ -243,7 +249,10 @@ void MainWindow::slot_find() {
}
void MainWindow::slot_verify() {
- if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
+ if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
+ if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileVerify();
+ return;
+ }
auto text = edit_->CurTextPage()->GetTextPage()->toPlainText().toUtf8();
// TODO(Saturneric) PreventNoDataErr
@@ -278,7 +287,11 @@ void MainWindow::slot_verify() {
}
void MainWindow::slot_encrypt_sign() {
- if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
+ if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
+ if (edit_->SlotCurPageFileTreeView() != nullptr)
+ this->SlotFileEncryptSign();
+ return;
+ }
auto key_ids = m_key_list_->GetChecked();
@@ -390,7 +403,11 @@ void MainWindow::slot_encrypt_sign() {
}
void MainWindow::slot_decrypt_verify() {
- if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) return;
+ if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
+ if (edit_->SlotCurPageFileTreeView() != nullptr)
+ this->SlotFileDecryptVerify();
+ return;
+ }
QString plainText = edit_->CurTextPage()->GetTextPage()->toPlainText();
@@ -429,7 +446,7 @@ void MainWindow::slot_decrypt_verify() {
try {
auto buffer = text.toStdString();
error = GpgBasicOperator::GetInstance().DecryptVerify(buffer, decrypted,
- d_result, v_result);
+ d_result, v_result);
} catch (const std::runtime_error& e) {
if_error = true;
}
diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp
index 42dd3be7..9d81b11c 100644
--- a/src/ui/main_window/MainWindowSlotUI.cpp
+++ b/src/ui/main_window/MainWindowSlotUI.cpp
@@ -188,4 +188,37 @@ void MainWindow::SlotSetRestartNeeded(bool needed) {
bool MainWindow::get_restart_needed() const { return this->restart_needed_; }
+void MainWindow::SetCryptoMenuStatus(
+ MainWindow::CryptoMenu::OperationType type) {
+ LOG(INFO) << "SetCryptoMenuStatus" << type;
+
+ // refresh status to disable all
+ verify_act_->setDisabled(true);
+ sign_act_->setDisabled(true);
+ encrypt_act_->setDisabled(true);
+ encrypt_sign_act_->setDisabled(true);
+ decrypt_act_->setDisabled(true);
+ decrypt_verify_act_->setDisabled(true);
+
+ // enable according to type
+ if (type & MainWindow::CryptoMenu::Verify) {
+ verify_act_->setDisabled(false);
+ }
+ if (type & MainWindow::CryptoMenu::Sign) {
+ sign_act_->setDisabled(false);
+ }
+ if (type & MainWindow::CryptoMenu::Encrypt) {
+ encrypt_act_->setDisabled(false);
+ }
+ if (type & MainWindow::CryptoMenu::EncryptAndSign) {
+ encrypt_sign_act_->setDisabled(false);
+ }
+ if (type & MainWindow::CryptoMenu::Decrypt) {
+ decrypt_act_->setDisabled(false);
+ }
+ if (type & MainWindow::CryptoMenu::DecryptAndVerify) {
+ decrypt_verify_act_->setDisabled(false);
+ }
+}
+
} // namespace GpgFrontend::UI
diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp
index 8f562f85..8c22affd 100644
--- a/src/ui/widgets/FilePage.cpp
+++ b/src/ui/widgets/FilePage.cpp
@@ -101,6 +101,45 @@ void FilePage::slot_file_tree_view_item_clicked(const QModelIndex& index) {
dir_model_->fileInfo(index).absoluteFilePath().toStdString());
m_path_ = selected_path_;
LOG(INFO) << "selected path" << selected_path_;
+
+ selected_path_ = std::filesystem::path(selected_path_);
+ MainWindow::CryptoMenu::OperationType operation_type = MainWindow::CryptoMenu::None;
+
+ if (index.isValid()) {
+ QFileInfo info(QString::fromStdString(selected_path_.string()));
+
+
+ if(info.isFile() && (info.suffix() != "gpg" &&
+ info.suffix() != "sig" &&
+ info.suffix() != "asc")) {
+ operation_type |= MainWindow::CryptoMenu::Encrypt;
+ }
+
+ if(info.isFile() && (info.suffix() != "gpg" && info.suffix() != "sig" &&
+ info.suffix() != "asc")){
+ operation_type |= MainWindow::CryptoMenu::EncryptAndSign;
+ }
+
+ if(info.isFile() && (info.suffix() == "gpg" || info.suffix() == "asc")) {
+ operation_type |= MainWindow::CryptoMenu::Decrypt;
+ operation_type |= MainWindow::CryptoMenu::DecryptAndVerify;
+ }
+
+ if(info.isFile() && (info.suffix() != "gpg" &&
+ info.suffix() != "sig" &&
+ info.suffix() != "asc")){
+ operation_type |= MainWindow::CryptoMenu::Sign;
+ }
+
+ if(info.isFile() && (info.suffix() == "sig" ||
+ info.suffix() == "gpg" ||
+ info.suffix() == "asc")) {
+ operation_type |= MainWindow::CryptoMenu::Verify;
+ }
+ }
+
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SetCryptoMenuStatus(operation_type);
}
void FilePage::slot_up_level() {
@@ -297,10 +336,10 @@ void FilePage::slot_open_item() {
}
} else {
if (info.isReadable()) {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
LOG(INFO) << "open item" << selected_path_;
auto qt_path = QString::fromStdString(selected_path_.string());
- if (mainWindow != nullptr) mainWindow->SlotOpenFile(qt_path);
+ if (main_window != nullptr) main_window->SlotOpenFile(qt_path);
} else {
QMessageBox::critical(this, _("Error"),
_("The file is unprivileged or unreachable."));
@@ -351,28 +390,28 @@ void FilePage::slot_delete_item() {
}
void FilePage::slot_encrypt_item() {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
- if (mainWindow != nullptr) mainWindow->SlotFileEncrypt();
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SlotFileEncrypt();
}
void FilePage::slot_encrypt_sign_item() {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
- if (mainWindow != nullptr) mainWindow->SlotFileEncryptSign();
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SlotFileEncryptSign();
}
void FilePage::slot_decrypt_item() {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
- if (mainWindow != nullptr) mainWindow->SlotFileDecryptVerify();
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SlotFileDecryptVerify();
}
void FilePage::slot_sign_item() {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
- if (mainWindow != nullptr) mainWindow->SlotFileSign();
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SlotFileSign();
}
void FilePage::slot_verify_item() {
- auto mainWindow = qobject_cast<MainWindow*>(first_parent_);
- if (mainWindow != nullptr) mainWindow->SlotFileVerify();
+ auto main_window = qobject_cast<MainWindow*>(first_parent_);
+ if (main_window != nullptr) main_window->SlotFileVerify();
}
void FilePage::slot_calculate_hash() {