Skip to content

Commit

Permalink
Fix bug: When saving a file in Linux, the file name extension is not …
Browse files Browse the repository at this point in the history
…included.
  • Loading branch information
mogoweb authored and tishion committed May 28, 2024
1 parent 94305c1 commit 57aac89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QGridLayout>
#include <QInputMethodQueryEvent>
#include <QPainter>
#include <QStandardPaths>
#include <QWindow>
#pragma endregion qt_headers

Expand Down Expand Up @@ -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
Expand All @@ -669,9 +667,16 @@ QCefViewPrivate::onFileDialog(CefBrowserHost::FileDialogMode mode,
// execute the dialog
if (dialog.exec()) {
std::vector<CefString> 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 {
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 57aac89

Please sign in to comment.