fix: conditionally open file with openUri or execute depending on its type

This commit is contained in:
maelstrom 2025-05-15 21:51:27 +02:00
parent 59f2aef102
commit cd4a51212c

View file

@ -7,6 +7,8 @@
#include <QMessageBox>
#include <QSettings>
#include <QFileDialog>
#include <QDesktopServices>
#include <QMimeDatabase>
RunApplet::RunApplet(QWidget *parent)
: QDialog(parent)
@ -32,7 +34,12 @@ RunApplet::RunApplet(QWidget *parent)
});
connect(ui->browseBtn, &QPushButton::clicked, this, [&]() {
QString file = QFileDialog::getOpenFileName(this, tr("Browse"), "", tr("All Files (*)"));
QFileDialog dialog;
dialog.setWindowTitle(tr("Browse"));
dialog.setMimeTypeFilters({"application/x-executable"});
dialog.exec();
QStringList files = dialog.selectedFiles();
QString file = files.size() == 1 ? files[0] : "";
if (file == "") return;
ui->comboBox->lineEdit()->setText(file);
});
@ -58,6 +65,16 @@ void RunApplet::runApp(QString command) {
args.push_back(QString::fromWCharArray(p_args[i]));
}
// If the file exists and is not an executable, use QDesktopServices instead
QMimeDatabase mimeDb;
QMimeType mime = mimeDb.mimeTypeForFile(args[0]);
if (QFile::exists(command) && mime.name() != "application/x-executable") {
QDesktopServices::openUrl(QUrl::fromLocalFile(command));
return;
}
QProcess *process = new QProcess;
process->setProgram(args[0]);
args.remove(0);