Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: When saving a file in Linux, the file name extension is not … #402

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
Loading