From 57aac89ce640c78e6655c41b552975d1da641413 Mon Sep 17 00:00:00 2001 From: alexzchen Date: Sat, 25 May 2024 17:17:48 +0800 Subject: [PATCH] Fix bug: When saving a file in Linux, the file name extension is not included. --- src/details/QCefViewPrivate.cpp | 41 +++++++++++++++++++++++++-------- src/details/QCefViewPrivate.h | 2 ++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/details/QCefViewPrivate.cpp b/src/details/QCefViewPrivate.cpp index 84e204e1..c0a61158 100644 --- a/src/details/QCefViewPrivate.cpp +++ b/src/details/QCefViewPrivate.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #pragma endregion qt_headers @@ -647,14 +648,11 @@ QCefViewPrivate::onFileDialog(CefBrowserHost::FileDialogMode mode, dialog.setWindowTitle(caption); } - // set initial folder + // set initial folder & default file name if (!default_file_path.empty() && mode == FILE_DIALOG_SAVE) { - QDir dir(QString::fromStdString(default_file_path.ToString())); - if (dir.exists()) { - dialog.setDirectory(dir); - } else { - dialog.setDirectory(QDir::homePath()); - } + QString initialPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); + dialog.setDirectory(initialPath); + dialog.selectFile(initialPath + "/" + QString::fromStdString(default_file_path.ToString())); } // set accepted file types @@ -669,9 +667,16 @@ QCefViewPrivate::onFileDialog(CefBrowserHost::FileDialogMode mode, // execute the dialog if (dialog.exec()) { std::vector file_paths; - auto selected_files = dialog.selectedFiles(); - for (const auto& file : selected_files) { - file_paths.push_back(file.toStdString()); + if (mode == FILE_DIALOG_SAVE) { + QString filePath = dialog.selectedFiles().first(); + QString selectedFilter = dialog.selectedNameFilter(); + filePath = addExtensionIfNeeded(filePath, selectedFilter); + file_paths.push_back(filePath.toStdString()); + } else { + auto selected_files = dialog.selectedFiles(); + for (const auto& file : selected_files) { + file_paths.push_back(file.toStdString()); + } } callback->Continue(file_paths); } else { @@ -1286,3 +1291,19 @@ QCefViewPrivate::setPreference(const QString& name, const QVariant& value, const return false; } + +QString +QCefViewPrivate::addExtensionIfNeeded(const QString &filePath, const QString &selectedFilter) +{ + QFileInfo fileInfo(filePath); + + if (fileInfo.suffix().isEmpty()) { + QRegExp rx("\\*\\.([^\\s\\);]+)"); + if (rx.indexIn(selectedFilter) != -1) { + QString extension = rx.cap(1); + return filePath + "." + extension; + } + } + + return filePath; +} diff --git a/src/details/QCefViewPrivate.h b/src/details/QCefViewPrivate.h index 266e699d..6096ff74 100644 --- a/src/details/QCefViewPrivate.h +++ b/src/details/QCefViewPrivate.h @@ -317,4 +317,6 @@ public slots: bool sendEventNotifyMessage(int64_t frameId, const QString& name, const QVariantList& args); bool setPreference(const QString& name, const QVariant& value, const QString& error); +private: + QString addExtensionIfNeeded(const QString &filePath, const QString &selectedFilter); };