diff options
author | saturneric <[email protected]> | 2024-01-19 12:10:17 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-01-19 12:10:17 +0000 |
commit | f22ceca734868a4cb946c232f661aad72da01ded (patch) | |
tree | 5d858753ca06a8dfed12a92f4762e579900cfb1f | |
parent | fix: slove discovered bugs (diff) | |
download | GpgFrontend-f22ceca734868a4cb946c232f661aad72da01ded.tar.gz GpgFrontend-f22ceca734868a4cb946c232f661aad72da01ded.zip |
fix: slove discovered faults and bugs
23 files changed, 207 insertions, 162 deletions
diff --git a/resource/lfs/locale/ts/GpgFrontend.zh_CN.ts b/resource/lfs/locale/ts/GpgFrontend.zh_CN.ts index 1773bd7b..75dabdf7 100644 --- a/resource/lfs/locale/ts/GpgFrontend.zh_CN.ts +++ b/resource/lfs/locale/ts/GpgFrontend.zh_CN.ts @@ -2047,7 +2047,7 @@ This will result in loss of all cached form positions, statuses, key servers, et <location filename="../../../../src/ui/main_window/KeyMgmt.cpp" line="272"/> <location filename="../../../../src/ui/main_window/KeyMgmt.cpp" line="303"/> <source>Export Key</source> - <translation type="unfinished"></translation> + <translation type="unfinished">导出密钥</translation> </message> <message> <location filename="../../../../src/ui/main_window/KeyMgmt.cpp" line="293"/> diff --git a/src/app.cpp b/src/app.cpp index 6d207373..b8b20300 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -59,7 +59,7 @@ auto StartApplication(const GFCxtWPtr& p_ctx) -> int { return -1; } - GF_MAIN_LOG_INFO("start running gui application"); + GF_MAIN_LOG_DEBUG("start running gui application"); /** * internationalisation. loop to restart main window diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 1790776e..7ce48ca4 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -331,7 +331,7 @@ void InitGpgFrontendCore(CoreInitArgs args) { QObject::tr("Gpg Key Detabase inilization failed")); }; } - GF_CORE_LOG_INFO( + GF_CORE_LOG_DEBUG( "basic env checking finished, " "including gpgme, ctx, and key infos"); Module::UpsertRTValue("core", "env.state.basic", 1); diff --git a/src/core/function/ArchiveFileOperator.cpp b/src/core/function/ArchiveFileOperator.cpp index 54c78628..f1345f87 100644 --- a/src/core/function/ArchiveFileOperator.cpp +++ b/src/core/function/ArchiveFileOperator.cpp @@ -158,11 +158,9 @@ void ArchiveFileOperator::NewArchive2DataExchanger( if (r > ARCHIVE_FAILED) { auto fd = open(archive_entry_sourcepath(entry), O_RDONLY); auto len = read(fd, buff.data(), buff.size()); - assert(len <= buff.size() && len > 0); while (len > 0) { archive_write_data(archive, buff.data(), len); len = read(fd, buff.data(), buff.size()); - assert(len <= buff.size() && len > 0); } close(fd); } @@ -179,6 +177,7 @@ void ArchiveFileOperator::NewArchive2DataExchanger( void ArchiveFileOperator::ExtractArchiveFromDataExchanger( std::shared_ptr<GFDataExchanger> ex, const QString &target_path, const OperationCallback &cb) { + GF_CORE_LOG_INFO("target path: {}", target_path); RunIOOperaAsync( [=](const DataObjectPtr &data_object) -> GFError { auto *archive = archive_read_new(); diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index e9ccdd05..6969c15a 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -59,9 +59,9 @@ class GlobalSettingStation::Impl { GF_CORE_LOG_INFO("app data path: {}", app_data_path_); GF_CORE_LOG_INFO("app log path: {}", app_log_path_); - GF_CORE_LOG_INFO("app log files total size: {}", GetLogFilesSize()); - GF_CORE_LOG_INFO("app data objects files total size: {}", - GetDataObjectsFilesSize()); + GF_CORE_LOG_DEBUG("app log files total size: {}", GetLogFilesSize()); + GF_CORE_LOG_DEBUG("app data objects files total size: {}", + GetDataObjectsFilesSize()); if (!QDir(app_data_path_).exists()) QDir(app_data_path_).mkpath("."); if (!QDir(app_log_path_).exists()) QDir(app_log_path_).mkpath("."); diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 94a08c76..5abf3159 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -452,6 +452,7 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, ArchiveFileOperator::ExtractArchiveFromDataExchanger( ex, out_path, [](GFError err, const DataObjectPtr&) { GF_CORE_LOG_DEBUG("extract archive from ex operation, err: {}", err); + GF_CORE_LOG_INFO("//////////////"); }); RunGpgOperaAsync( @@ -461,8 +462,10 @@ void GpgFileOpera::DecryptVerifyArchive(const QString& in_path, GpgData data_in(in_path, true); GpgData data_out(ex); + GF_CORE_LOG_INFO("-----------------------"); err = CheckGpgError( gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + GF_CORE_LOG_INFO("++++++++++++++++++++++++"); data_object->Swap({ GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), diff --git a/src/core/function/gpg/GpgKeyImportExporter.cpp b/src/core/function/gpg/GpgKeyImportExporter.cpp index 09b51359..d2c313dd 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.cpp +++ b/src/core/function/gpg/GpgKeyImportExporter.cpp @@ -138,4 +138,57 @@ void GpgKeyImportExporter::ExportKeys(const KeyArgsList& keys, bool secret, cb, "gpgme_op_export_keys", "2.1.0"); } +/** + * Export keys + * @param keys keys used + * @param outBuffer output byte array + * @return if success + */ +void GpgKeyImportExporter::ExportAllKeys(const KeyArgsList& keys, bool secret, + bool ascii, + const GpgOperationCallback& cb) const { + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + if (keys.empty()) return GPG_ERR_CANCELED; + + int mode = 0; + if (secret) mode |= GPGME_EXPORT_MODE_SECRET; + + std::vector<gpgme_key_t> keys_array(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + keys_array.emplace_back(nullptr); + + GpgData data_out; + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = gpgme_op_export_keys(ctx, keys_array.data(), mode, data_out); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {}; + + GF_CORE_LOG_DEBUG( + "operation of exporting keys finished, ascii: {}, read_bytes: {}", + ascii, gpgme_data_seek(data_out, 0, SEEK_END)); + auto buffer = data_out.Read2GFBuffer(); + + if (secret) { + int mode = 0; + mode |= GPGME_EXPORT_MODE_SECRET; + + GpgData data_out_secret; + auto err = gpgme_op_export_keys(ctx, keys_array.data(), mode, + data_out_secret); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {}; + + GF_CORE_LOG_DEBUG( + "operation of exporting secret keys finished, " + "ascii: {}, read_bytes: {}", + ascii, gpgme_data_seek(data_out_secret, 0, SEEK_END)); + buffer.Append(data_out_secret.Read2GFBuffer()); + } + + data_object->Swap({buffer}); + return err; + }, + cb, "gpgme_op_export_keys", "2.1.0"); +} + } // namespace GpgFrontend diff --git a/src/core/function/gpg/GpgKeyImportExporter.h b/src/core/function/gpg/GpgKeyImportExporter.h index 14b2b2bf..d0724f7b 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.h +++ b/src/core/function/gpg/GpgKeyImportExporter.h @@ -85,6 +85,17 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyImportExporter bool shortest, bool ssh_mode, const GpgOperationCallback& cb) const; + /** + * @brief + * + * @param keys + * @param secret + * @param ascii + * @param cb + */ + void ExportAllKeys(const KeyArgsList& keys, bool secret, bool ascii, + const GpgOperationCallback& cb) const; + private: GpgContext& ctx_; }; diff --git a/src/core/model/GFBuffer.cpp b/src/core/model/GFBuffer.cpp index 56015613..411a5725 100644 --- a/src/core/model/GFBuffer.cpp +++ b/src/core/model/GFBuffer.cpp @@ -30,37 +30,30 @@ namespace GpgFrontend { -GFBuffer::GFBuffer() - : buffer_(SecureCreateSharedObject<std::vector<std::byte>>()) {} +GFBuffer::GFBuffer() = default; -GFBuffer::GFBuffer(QByteArray buffer) - : buffer_(SecureCreateSharedObject<std::vector<std::byte>>()) { - std::transform(buffer.begin(), buffer.end(), std::back_inserter(*buffer_), - [](const char c) { return static_cast<std::byte>(c); }); -} +GFBuffer::GFBuffer(QByteArray buffer) : buffer_(std::move(buffer)) {} -GFBuffer::GFBuffer(QString str) - : buffer_(SecureCreateSharedObject<std::vector<std::byte>>()) { - std::transform( - str.begin(), str.end(), std::back_inserter(*buffer_), - [](const QChar c) { return static_cast<std::byte>(c.unicode()); }); -} +GFBuffer::GFBuffer(const QString& str) : buffer_(str.toUtf8()) {} auto GFBuffer::operator==(const GFBuffer& o) const -> bool { - return equal(buffer_->begin(), buffer_->end(), o.buffer_->begin()); + return buffer_ == o.buffer_; } -auto GFBuffer::Data() const -> std::byte* { return buffer_->data(); } +auto GFBuffer::Data() const -> const char* { return buffer_.constData(); } -void GFBuffer::Resize(size_t size) { buffer_->resize(size); } +void GFBuffer::Resize(ssize_t size) { buffer_.resize(size); } -auto GFBuffer::Size() const -> size_t { return buffer_->size(); } +auto GFBuffer::Size() const -> size_t { return buffer_.size(); } -auto GFBuffer::ConvertToQByteArray() const -> QByteArray { - return QByteArray::fromRawData(reinterpret_cast<const char*>(Data()), - static_cast<qsizetype>(Size())); -} +auto GFBuffer::ConvertToQByteArray() const -> QByteArray { return buffer_; } auto GFBuffer::Empty() const -> bool { return this->Size() == 0; } +void GFBuffer::Append(const GFBuffer& o) { buffer_.append(o.buffer_); } + +void GFBuffer::Append(const char* buffer, ssize_t size) { + buffer_.append(buffer, size); +} + } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GFBuffer.h b/src/core/model/GFBuffer.h index 381bcdf7..46189195 100644 --- a/src/core/model/GFBuffer.h +++ b/src/core/model/GFBuffer.h @@ -39,22 +39,26 @@ class GPGFRONTEND_CORE_EXPORT GFBuffer { explicit GFBuffer(QByteArray buffer); - explicit GFBuffer(QString str); + explicit GFBuffer(const QString& str); auto operator==(const GFBuffer& o) const -> bool; - [[nodiscard]] auto Data() const -> std::byte*; + [[nodiscard]] auto Data() const -> const char*; - void Resize(size_t size); + void Resize(ssize_t size); [[nodiscard]] auto Size() const -> size_t; [[nodiscard]] auto Empty() const -> bool; + void Append(const GFBuffer&); + + void Append(const char*, ssize_t); + [[nodiscard]] auto ConvertToQByteArray() const -> QByteArray; private: - std::shared_ptr<std::vector<std::byte>> buffer_; + QByteArray buffer_; }; } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgData.cpp b/src/core/model/GpgData.cpp index 6ab44994..ab188f01 100644 --- a/src/core/model/GpgData.cpp +++ b/src/core/model/GpgData.cpp @@ -48,6 +48,7 @@ auto GFWriteExCb(void* handle, const void* buffer, size_t size) -> ssize_t { } void GFReleaseExCb(void* handle) { + GF_CORE_LOG_INFO("$$$$$$$$$$$$$$$$$$$$$$$"); auto* ex = static_cast<GFDataExchanger*>(handle); ex->CloseWrite(); } @@ -137,13 +138,12 @@ auto GpgData::Read2GFBuffer() -> GFBuffer { const GpgError err = gpgme_err_code_from_errno(errno); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); } else { - std::array<std::byte, kBufferSize + 2> buf; + std::array<char, kBufferSize + 2> buf; while ((ret = gpgme_data_read(*this, buf.data(), kBufferSize)) > 0) { - const size_t size = out_buffer.Size(); - out_buffer.Resize(static_cast<int>(size + ret)); - memcpy(out_buffer.Data() + size, buf.data(), ret); + out_buffer.Append(buf.data(), ret); } + if (ret < 0) { const GpgError err = gpgme_err_code_from_errno(errno); assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 50f97334..9bc4f06b 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -163,7 +163,7 @@ class GlobalModuleContext::Impl { if (met_it == module_events_table_.end()) { module_events_table_[event] = std::unordered_set<ModuleIdentifier>(); met_it = module_events_table_.find(event); - GF_CORE_LOG_INFO("new event {} of module system created", event); + GF_CORE_LOG_DEBUG("new event {} of module system created", event); } auto& listeners_set = met_it->second; diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 75a412e7..db3513eb 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -143,6 +143,7 @@ auto SetExtensionOfOutputFileForArchive(const QString& path, GpgOperation opera, case kENCRYPT: case kENCRYPT_SIGN: extension = ".tar.asc"; + return path + extension; break; default: break; @@ -152,21 +153,15 @@ auto SetExtensionOfOutputFileForArchive(const QString& path, GpgOperation opera, case kENCRYPT: case kENCRYPT_SIGN: extension = ".tar.gpg"; + return path + extension; break; default: break; } } - if (!extension.isEmpty()) { - auto last_dot_index = path.lastIndexOf('.'); - if (last_dot_index != -1) { - return path.left(last_dot_index) + extension; - } - return path + extension; - } - - return path; // 如果没有匹配的操作,则返回原始路径 + auto file_info = QFileInfo(path); + return file_info.absolutePath() + "/" + file_info.baseName(); } } // namespace GpgFrontend diff --git a/src/ui/GpgFrontendUIInit.cpp b/src/ui/GpgFrontendUIInit.cpp index dd01bc9d..d696588d 100644 --- a/src/ui/GpgFrontendUIInit.cpp +++ b/src/ui/GpgFrontendUIInit.cpp @@ -221,7 +221,7 @@ void InitLocale() { if (translator->load(target_locale, QLatin1String(PROJECT_NAME), QLatin1String("."), QLatin1String(":/i18n"), QLatin1String(".qm"))) { - GF_UI_LOG_INFO("load target translation file done"); + GF_UI_LOG_DEBUG("load target translation file done"); QCoreApplication::installTranslator(translator); } } diff --git a/src/ui/dialog/settings/SettingsDialog.cpp b/src/ui/dialog/settings/SettingsDialog.cpp index a1ac1885..2af56906 100644 --- a/src/ui/dialog/settings/SettingsDialog.cpp +++ b/src/ui/dialog/settings/SettingsDialog.cpp @@ -63,8 +63,8 @@ SettingsDialog::SettingsDialog(QWidget* parent) &SettingsDialog::SlotAccept); connect(button_box_, &QDialogButtonBox::rejected, this, &SettingsDialog::reject); - mainLayout->addWidget(button_box_); - mainLayout->stretch(0); + main_layout->addWidget(button_box_); + main_layout->stretch(0); setWindowTitle(tr("Settings")); #else connect(this, &QDialog::finished, this, &SettingsDialog::SlotAccept); diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index 76fd4a9d..b7dff387 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -513,9 +513,10 @@ void MainWindow::SlotFileVerify(const QString& path) { if (result_analyse.GetStatus() == -2) { import_unknown_key_from_keyserver(this, result_analyse); } - if (result_analyse.GetStatus() >= 0) { - show_verify_details(this, info_board_, err, result); - } + // pause this feature + // if (result_analyse.GetStatus() >= 0) { + // show_verify_details(this, info_board_, err, result); + // } this->slot_refresh_current_file_view(); }); @@ -738,7 +739,6 @@ void MainWindow::SlotFileDecryptVerify(const QString& path) { } auto out_path = SetExtensionOfOutputFile(path, kDECRYPT_VERIFY, true); - check_result = TargetFilePreCheck(out_path, false); if (!std::get<0>(check_result)) { QMessageBox::critical(this, tr("Error"), @@ -786,9 +786,10 @@ void MainWindow::SlotFileDecryptVerify(const QString& path) { if (verify_result_analyse.GetStatus() == -2) { import_unknown_key_from_keyserver(this, verify_result_analyse); } - if (verify_result_analyse.GetStatus() >= 0) { - show_verify_details(this, info_board_, err, verify_result); - } + // pause this feature + // if (verify_result_analyse.GetStatus() >= 0) { + // show_verify_details(this, info_board_, err, verify_result); + // } this->slot_refresh_current_file_view(); }); @@ -805,7 +806,7 @@ void MainWindow::SlotArchiveDecryptVerify(const QString& path) { auto out_path = SetExtensionOfOutputFileForArchive(path, kDECRYPT_VERIFY, true); - + GF_UI_LOG_INFO("out_path: {}", out_path); check_result = TargetFilePreCheck(out_path, false); if (!std::get<0>(check_result)) { QMessageBox::critical(this, tr("Error"), @@ -828,6 +829,7 @@ void MainWindow::SlotArchiveDecryptVerify(const QString& path) { [=](const OperaWaitingHd& op_hd) { GpgFileOpera::GetInstance().DecryptVerifyArchive( path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) { + GF_CORE_LOG_INFO("****************************"); // stop waiting op_hd(); @@ -854,9 +856,10 @@ void MainWindow::SlotArchiveDecryptVerify(const QString& path) { if (verify_result_analyse.GetStatus() == -2) { import_unknown_key_from_keyserver(this, verify_result_analyse); } - if (verify_result_analyse.GetStatus() >= 0) { - show_verify_details(this, info_board_, err, verify_result); - } + // pause this feature + // if (verify_result_analyse.GetStatus() >= 0) { + // show_verify_details(this, info_board_, err, verify_result); + // } this->slot_refresh_current_file_view(); }); diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index dd7fa5c8..c86e7457 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -98,8 +98,12 @@ void MainWindow::slot_append_keys_create_datetime() { } auto create_datetime_format_str_local = - QLocale().toString(key.GetCreateTime()) + tr(" (Local Time) ") + "\n"; - edit_->SlotAppendText2CurTextPage(create_datetime_format_str_local); + QLocale().toString(key.GetCreateTime()) + " (" + tr("Localize") + ") " + + "\n"; + auto create_datetime_format_str = key.GetCreateTime().toString(Qt::ISODate) + + " (" + tr("UTC") + ") " + "\n "; + edit_->SlotAppendText2CurTextPage(create_datetime_format_str_local + + create_datetime_format_str); } void MainWindow::slot_append_keys_expire_datetime() { @@ -116,10 +120,13 @@ void MainWindow::slot_append_keys_expire_datetime() { return; } - auto create_datetime_format_str = - key.GetCreateTime().toString() + " (UTC) " + "\n"; + auto expire_datetime_format_str_local = + QLocale().toString(key.GetCreateTime()) + tr(" (Local Time) ") + "\n"; + auto expire_datetime_format_str = + key.GetCreateTime().toString(Qt::ISODate) + " (UTC) " + "\n"; - edit_->SlotAppendText2CurTextPage(create_datetime_format_str); + edit_->SlotAppendText2CurTextPage(expire_datetime_format_str_local + + expire_datetime_format_str); } void MainWindow::slot_append_keys_fingerprint() { diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index 1b39b606..78d6c271 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -158,16 +158,16 @@ void MainWindow::slot_cut_pgp_header() { } QString content = edit_->CurTextPage()->GetTextPage()->toPlainText(); - int start = content.indexOf(PGP_CRYPT_BEGIN); - int end = content.indexOf(PGP_CRYPT_END); + auto start = content.indexOf(PGP_CRYPT_BEGIN); + auto end = content.indexOf(PGP_CRYPT_END); if (start < 0 || end < 0) { return; } // remove head - int headEnd = content.indexOf("\n\n", start) + 2; - content.remove(start, headEnd - start); + auto head_end = content.indexOf("\n\n", start) + 2; + content.remove(start, head_end - start); // remove tail end = content.indexOf(PGP_CRYPT_END); diff --git a/src/ui/main_window/MainWindowUI.cpp b/src/ui/main_window/MainWindowUI.cpp index 662c673f..ab0df25f 100644 --- a/src/ui/main_window/MainWindowUI.cpp +++ b/src/ui/main_window/MainWindowUI.cpp @@ -254,7 +254,7 @@ void MainWindow::create_actions() { if (file_info.isFile()) { const QString extension = file_info.completeSuffix(); - if (extension == ".tar.gpg" || extension == ".tar.asc") { + if (extension == "tar.gpg" || extension == "tar.asc") { this->SlotArchiveDecryptVerify(path); } else { this->SlotFileDecryptVerify(path); diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index 27f4205f..c6260003 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -49,8 +49,7 @@ FilePage::FilePage(QWidget* parent, const QString& target_path) connect(this->ui_->newDirButton, &QPushButton::clicked, file_tree_view_, &FileTreeView::SlotMkdir); - ui_->pathEdit->setText( - QString::fromStdString(file_tree_view_->GetCurrentPath().u8string())); + ui_->pathEdit->setText(file_tree_view_->GetCurrentPath()); path_edit_completer_ = new QCompleter(this); path_complete_model_ = new QStringListModel(); @@ -150,16 +149,11 @@ FilePage::FilePage(QWidget* parent, const QString& target_path) } auto FilePage::GetSelected() const -> QString { - return QString::fromStdString(file_tree_view_->GetSelectedPath().string()); + return file_tree_view_->GetSelectedPath(); } void FilePage::SlotGoPath() { -#ifdef WINDOWS - std::filesystem::path target_path(ui_->pathEdit->text().toStdU16String()); -#else - std::filesystem::path target_path(ui_->pathEdit->text().toStdString()); -#endif - file_tree_view_->SlotGoPath(target_path); + file_tree_view_->SlotGoPath(ui_->pathEdit->text()); } void FilePage::keyPressEvent(QKeyEvent* event) { diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index f3556dc9..41cd9aff 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -43,7 +43,7 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) this->setModel(dir_model_); this->setColumnWidth(0, 320); this->sortByColumn(0, Qt::AscendingOrder); - current_path_ = std::filesystem::path(dir_model_->rootPath().toStdString()); + current_path_ = dir_model_->rootPath(); slot_create_popup_menu(); this->setContextMenuPolicy(Qt::CustomContextMenu); @@ -56,24 +56,27 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) void FileTreeView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { QTreeView::selectionChanged(selected, deselected); - + GF_UI_LOG_DEBUG( + "file tree view selected changed, selected: {}, deselected: {}", + selected.size(), deselected.size()); if (!selected.indexes().empty()) { - selected_path_ = dir_model_->fileInfo(selected.indexes().first()) - .filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view selected target path: {}", - selected_path_.u8string()); - emit SignalSelectedChanged(QString::fromStdString(selected_path_)); + selected_path_ = dir_model_->filePath(selected.indexes().first()); + GF_UI_LOG_DEBUG("file tree view selected target path: {}", selected_path_); + emit SignalSelectedChanged(selected_path_); } else { - selected_path_ = std::filesystem::path{}; + selected_path_ = {}; + if (!this->selectedIndexes().isEmpty()) { + selected_path_ = dir_model_->filePath(this->selectedIndexes().front()); + emit SignalSelectedChanged(selected_path_); + } } } -void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { +void FileTreeView::SlotGoPath(const QString& target_path) { auto file_info = QFileInfo(target_path); if (file_info.isDir() && file_info.isReadable() && file_info.isExecutable()) { - current_path_ = file_info.filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view set target path: {}", - current_path_.u8string()); + current_path_ = file_info.absoluteFilePath(); + GF_UI_LOG_DEBUG("file tree view set target path: {}", current_path_); this->setRootIndex(dir_model_->index(file_info.filePath())); dir_model_->setRootPath(file_info.filePath()); for (int i = 1; i < dir_model_->columnCount(); ++i) { @@ -84,7 +87,7 @@ void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { this, tr("Error"), tr("The path is not exists, unprivileged or unreachable.")); } - emit SignalPathChanged(QString::fromStdString(current_path_.u8string())); + emit SignalPathChanged(current_path_); } void FileTreeView::slot_file_tree_view_item_double_clicked( @@ -98,58 +101,51 @@ void FileTreeView::slot_file_tree_view_item_double_clicked( tr("The file is unprivileged or unreachable.")); } } else { - SlotGoPath(file_info.filesystemAbsoluteFilePath()); + SlotGoPath(file_info.absoluteFilePath()); } } void FileTreeView::SlotUpLevel() { QModelIndex const current_root = this->rootIndex(); - auto target_path = - dir_model_->fileInfo(current_root).filesystemAbsoluteFilePath(); - if (target_path.has_parent_path() && !target_path.parent_path().empty()) { - target_path = target_path.parent_path(); - GF_UI_LOG_DEBUG("file tree view go parent path: {}", - target_path.u8string()); + auto target_path = dir_model_->fileInfo(current_root).absoluteFilePath(); + if (auto parent_path = QDir(target_path); parent_path.cdUp()) { + target_path = parent_path.absolutePath(); + GF_UI_LOG_DEBUG("file tree view go parent path: {}", target_path); this->SlotGoPath(target_path); } current_path_ = target_path; } -auto FileTreeView::GetCurrentPath() -> std::filesystem::path { - return current_path_; -} +auto FileTreeView::GetCurrentPath() -> QString { return current_path_; } void FileTreeView::SlotShowSystemFile(bool on) { auto filters = on ? dir_model_->filter() | QDir::System : dir_model_->filter() & ~QDir::System; dir_model_->setFilter(filters); - dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); + dir_model_->setRootPath(current_path_); } void FileTreeView::SlotShowHiddenFile(bool on) { auto filters = on ? dir_model_->filter() | QDir::Hidden : dir_model_->filter() & ~QDir::Hidden; dir_model_->setFilter(filters); - dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); + dir_model_->setRootPath(current_path_); } -auto FileTreeView::GetPathByClickPoint(const QPoint& point) - -> std::filesystem::path { +auto FileTreeView::GetPathByClickPoint(const QPoint& point) -> QString { auto const index = this->indexAt(point); if (!index.isValid()) { return {}; } - auto index_path = dir_model_->fileInfo(index).filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view right click on: {}", index_path.string()); + auto index_path = dir_model_->fileInfo(index).absoluteFilePath(); + GF_UI_LOG_DEBUG("file tree view right click on: {}", index_path); return index_path; } -auto FileTreeView::GetSelectedPath() -> std::filesystem::path { - return selected_path_; -} +auto FileTreeView::GetSelectedPath() -> QString { return selected_path_; } auto FileTreeView::SlotDeleteSelectedItem() -> void { QModelIndex const index = this->currentIndex(); @@ -224,7 +220,7 @@ void FileTreeView::SlotTouch() { } void FileTreeView::SlotTouchBelowAtSelectedItem() { - std::filesystem::path root_path(selected_path_); + auto root_path(selected_path_); QString new_file_name; bool ok; @@ -232,12 +228,9 @@ void FileTreeView::SlotTouchBelowAtSelectedItem() { this, tr("Create Empty File"), tr("Filename (you can given extension)"), QLineEdit::Normal, new_file_name, &ok); if (ok && !new_file_name.isEmpty()) { -#ifdef WINDOWS - auto file_path = root_path / new_file_name.toStdU16String(); -#else - auto file_path = root_path / new_file_name.toStdString(); -#endif - QFile new_file(file_path.u8string().c_str()); + auto file_path = root_path + "/" + new_file_name; + + QFile new_file(file_path); if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { QMessageBox::critical(this, tr("Error"), tr("Unable to create the file.")); @@ -260,40 +253,33 @@ void FileTreeView::keyPressEvent(QKeyEvent* event) { } void FileTreeView::SlotOpenSelectedItemBySystemApplication() { - QFileInfo const info(QString::fromStdString(selected_path_.u8string())); - auto q_selected_path = QString::fromStdString(selected_path_.u8string()); + QFileInfo const info(selected_path_); if (info.isDir()) { const auto file_path = info.filePath().toUtf8().toStdString(); - QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); } else { - QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); } } void FileTreeView::SlotRenameSelectedItem() { bool ok; - auto text = QInputDialog::getText( - this, tr("Rename"), tr("New Filename"), QLineEdit::Normal, - QString::fromStdString(selected_path_.filename().u8string()), &ok); + auto text = QInputDialog::getText(this, tr("Rename"), tr("New Filename"), + QLineEdit::Normal, + QFileInfo(selected_path_).fileName(), &ok); if (ok && !text.isEmpty()) { - try { -#ifdef WINDOWS - auto new_name_path = selected_path_.parent_path() / text.toStdU16String(); -#else - auto new_name_path = selected_path_.parent_path() / text.toStdString(); -#endif - GF_UI_LOG_DEBUG("new name path: {}", new_name_path.u8string()); - std::filesystem::rename(selected_path_, new_name_path); - - // refresh - SlotGoPath(current_path_); - } catch (...) { - GF_UI_LOG_ERROR("file tree view rename error: {}", - selected_path_.u8string()); + auto file_info = QFileInfo(selected_path_); + auto new_name_path = file_info.absolutePath() + "/" + text; + GF_UI_LOG_DEBUG("new filename path: {}", new_name_path); + if (QDir().rename(file_info.absoluteFilePath(), new_name_path)) { QMessageBox::critical(this, tr("Error"), tr("Unable to rename the file or folder.")); + return; } + + // refresh + SlotGoPath(current_path_); } } @@ -306,9 +292,8 @@ void FileTreeView::slot_create_popup_menu() { action_open_file_ = new QAction(this); action_open_file_->setText(tr("Open")); - connect(action_open_file_, &QAction::triggered, this, [this](bool) { - emit SignalOpenFile(QString::fromStdString(GetSelectedPath())); - }); + connect(action_open_file_, &QAction::triggered, this, + [this](bool) { emit SignalOpenFile(GetSelectedPath()); }); action_rename_file_ = new QAction(this); action_rename_file_->setText(tr("Rename")); @@ -368,12 +353,12 @@ void FileTreeView::slot_create_popup_menu() { void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { auto target_path = this->GetPathByClickPoint(point); - if (!target_path.empty()) { + if (!target_path.isEmpty()) { action_open_file_->setEnabled(true); action_rename_file_->setEnabled(true); action_delete_file_->setEnabled(true); - QFileInfo const info(QString::fromStdString(this->GetSelectedPath())); + QFileInfo const info(this->GetSelectedPath()); action_calculate_hash_->setEnabled(info.isFile() && info.isReadable()); } else { @@ -388,8 +373,7 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { void FileTreeView::slot_calculate_hash() { emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( - CalculateHash(this->GetSelectedPath().c_str()), - InfoBoardStatus::INFO_ERROR_OK); + CalculateHash(this->GetSelectedPath()), InfoBoardStatus::INFO_ERROR_OK); } void FileTreeView::slot_compress_files() {} diff --git a/src/ui/widgets/FileTreeView.h b/src/ui/widgets/FileTreeView.h index 5d013ae4..9e9b03dd 100644 --- a/src/ui/widgets/FileTreeView.h +++ b/src/ui/widgets/FileTreeView.h @@ -40,14 +40,14 @@ class FileTreeView : public QTreeView { * * @return std::filesystem::path */ - auto GetCurrentPath() -> std::filesystem::path; + auto GetCurrentPath() -> QString; /** * @brief Get the Selected Path object * * @return std::filesystem::path */ - auto GetSelectedPath() -> std::filesystem::path; + auto GetSelectedPath() -> QString; /** * @brief Get the Path By Click Point object @@ -55,7 +55,7 @@ class FileTreeView : public QTreeView { * @param point * @return std::filesystem::path */ - auto GetPathByClickPoint(const QPoint& point) -> std::filesystem::path; + auto GetPathByClickPoint(const QPoint& point) -> QString; /** * @brief Get the Mouse Point Global object @@ -115,7 +115,7 @@ class FileTreeView : public QTreeView { * @brief * */ - void SlotGoPath(const std::filesystem::path&); + void SlotGoPath(const QString&); /** * @brief @@ -213,9 +213,9 @@ class FileTreeView : public QTreeView { void slot_create_popup_menu(); private: - QFileSystemModel* dir_model_; ///< - std::filesystem::path current_path_; ///< - std::filesystem::path selected_path_; ///< + QFileSystemModel* dir_model_; ///< + QString current_path_; ///< + QString selected_path_; ///< QMenu* popup_menu_; QAction* action_open_file_; diff --git a/src/ui/widgets/HelpPage.cpp b/src/ui/widgets/HelpPage.cpp index e3ebac6f..78169944 100644 --- a/src/ui/widgets/HelpPage.cpp +++ b/src/ui/widgets/HelpPage.cpp @@ -32,11 +32,11 @@ namespace GpgFrontend::UI { HelpPage::HelpPage(const QString& path, QWidget* parent) : QWidget(parent) { browser_ = new QTextBrowser(); - auto* mainLayout = new QVBoxLayout(); - mainLayout->setSpacing(0); - mainLayout->addWidget(browser_); - mainLayout->setContentsMargins(0, 0, 0, 0); - setLayout(mainLayout); + auto* main_layout = new QVBoxLayout(); + main_layout->setSpacing(0); + main_layout->addWidget(browser_); + main_layout->setContentsMargins(0, 0, 0, 0); + setLayout(main_layout); connect(browser_, &QTextBrowser::anchorClicked, this, &HelpPage::slot_open_url); @@ -57,7 +57,7 @@ void HelpPage::slot_open_url(const QUrl& url) { * @param url * @return */ -QUrl HelpPage::localized_help(const QUrl& url) { +auto HelpPage::localized_help(const QUrl& url) -> QUrl { QString path = url.toLocalFile(); QString filename = path.mid(path.lastIndexOf("/") + 1); QString filepath = path.left(path.lastIndexOf("/") + 1); @@ -74,9 +74,8 @@ QUrl HelpPage::localized_help(const QUrl& url) { if (QFile(QUrl(langfile).toLocalFile()).exists()) { return langfile; - } else { - return path; } + return path; } QTextBrowser* HelpPage::GetBrowser() { return browser_; } |