Skip to content

Commit

Permalink
Add Cancel and Open With...
Browse files Browse the repository at this point in the history
  • Loading branch information
probonopd committed Aug 3, 2023
1 parent c63fbb1 commit c5dbb49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
66 changes: 59 additions & 7 deletions src/applicationselectiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#include "extattrs.h"
#include <QDir>
#include <QMessageBox>
#include <QPushButton>
#include "launcher.h"
#include "dbmanager.h"
#include <QFileDialog>


ApplicationSelectionDialog::ApplicationSelectionDialog(QString *fileOrProtocol, QString *mimeType,
bool showAlsoLegacyCandidates,
bool showAlsoLegacyCandidates, bool showAllCandidates,
QWidget *parent)
: QDialog(parent), ui(new Ui::ApplicationSelectionDialog)
{
Expand All @@ -27,6 +32,40 @@ ApplicationSelectionDialog::ApplicationSelectionDialog(QString *fileOrProtocol,
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(ui->listWidget, &QListWidget::doubleClicked, this, &QDialog::accept);

// Add a "Cancel" button
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
cancelButton->setCheckable(true);
cancelButton->setAutoExclusive(true);
cancelButton->setAutoDefault(false);
ui->buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

// Add a button "Other..." that lets the user select an application not in the list
QPushButton *openWithButton = new QPushButton(tr("Other..."));
openWithButton->setCheckable(true);
openWithButton->setAutoExclusive(true);
openWithButton->setAutoDefault(false);
ui->buttonBox->addButton(openWithButton, QDialogButtonBox::ActionRole);
connect(openWithButton, &QPushButton::clicked, [=]() {
QFileDialog fileDialog;
fileDialog.setFileMode(QFileDialog::Directory);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setDirectory(DbManager::localShareLaunchApplicationsPath);
if (fileDialog.exec()) {
// Get the selected file or directory
QStringList selectedFiles = fileDialog.selectedFiles();
if (selectedFiles.size() == 1) {
QString selectedFile = selectedFiles.at(0);
QStringList args;
args << QFileInfo(selectedFile).absoluteFilePath();
args << *fileOrProtocol;
Launcher launcher;
launcher.launch(args);
QApplication::quit();
}
}

});

this->setWindowTitle(tr("Open With"));

ui->checkBoxAlwaysOpenThis->setEnabled(false);
Expand Down Expand Up @@ -60,12 +99,24 @@ ApplicationSelectionDialog::ApplicationSelectionDialog(QString *fileOrProtocol,
dir.mkpath(".");
}

// Populate appCandidates with the syminks at mimePath
appCandidates =
new QStringList(QDir(mimePath).entryList(QDir::NoDotAndDotDot | QDir::AllEntries));
// Prepend each candidate with the path at which it was found
for (auto r = 0; r < appCandidates->length(); r++) {
appCandidates->replace(r, QString("%1/%2").arg(mimePath).arg(appCandidates->at(r)));
if (showAllCandidates == true) {
qDebug() << "Control modifier pressed, showing all applications";
// Get all applications known to the system
db = new DbManager();
appCandidates = new QStringList(db->allApplications());
delete db;
showAlsoLegacyCandidates = true;
} else {
// Normal operation
// Populate appCandidates with the syminks at mimePath
qDebug() << "Normal operation (no modifier key is pressed) showing only applications for" << *mimeType;
appCandidates =
new QStringList(QDir(mimePath).entryList(QDir::NoDotAndDotDot | QDir::AllEntries));
// Prepend each candidate with the path at which it was found
for (auto r = 0; r < appCandidates->length(); r++) {
appCandidates->replace(r, QString("%1/%2").arg(mimePath).arg(appCandidates->at(r)));
}

}

// Order apppCandidates by name and put ones ending in .desktop last
Expand Down Expand Up @@ -177,6 +228,7 @@ ApplicationSelectionDialog::ApplicationSelectionDialog(QString *fileOrProtocol,
qDebug() << "Found" << desktopFilesCount << "desktop files";
// Print whether showAlsoLegacyCandidates is true or false
qDebug() << "showAlsoLegacyCandidates:" << showAlsoLegacyCandidates;
qDebug() << "showAllCandidates:" << showAllCandidates;

if (!showAlsoLegacyCandidates && preferredAppCandidates->length() > 0) {
// Use preferredAppCandidates instead of appCandidates
Expand Down
1 change: 1 addition & 0 deletions src/applicationselectiondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ApplicationSelectionDialog : public QDialog
public:
explicit ApplicationSelectionDialog(QString *fileOrProtocol, QString *mimeType,
bool showAlsoLegacyCandidates = false,
bool showAllCandidates = false,
QWidget *parent = nullptr);
~ApplicationSelectionDialog();
QString getSelectedApplication();
Expand Down
3 changes: 1 addition & 2 deletions src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ void Launcher::handleError(QDetachableProcess *p, QString errorString)
}
}

qDebug() << "Alive?" << lines.length();
QString cleartextString = "";
for (int i = 0; i < 10; i++) {
if (i < lines.length()) {
Expand Down Expand Up @@ -754,7 +753,7 @@ int Launcher::open(QStringList args)

if (showChooserRequested || appCandidates.length() < 1) {
ApplicationSelectionDialog *dlg =
new ApplicationSelectionDialog(&fileOrProtocol, &mimeType, false, nullptr);
new ApplicationSelectionDialog(&fileOrProtocol, &mimeType, true, false, nullptr);
auto result = dlg->exec();
if (result == QDialog::Accepted)
appToBeLaunched = dlg->getSelectedApplication();
Expand Down

0 comments on commit c5dbb49

Please sign in to comment.