App launcher: start in terminal when defined in desktop files

This commit is contained in:
lupoDharkael
2017-12-14 19:31:41 +01:00
parent 24a5aa3a30
commit fb8112a2a4
7 changed files with 98 additions and 20 deletions

View File

@@ -92,7 +92,8 @@ SOURCES += src/main.cpp\
src/utils/desktopfileparse.cpp \
src/capture/workers/launcher/launcheritemdelegate.cpp \
src/capture/tools/blurtool.cpp \
src/capture/workers/launcher/moreappswidget.cpp
src/capture/workers/launcher/moreappswidget.cpp \
src/capture/workers/launcher/terminallauncher.cpp
HEADERS += \
src/capture/widget/buttonhandler.h \
@@ -149,7 +150,8 @@ HEADERS += \
src/utils/desktopfileparse.h \
src/capture/workers/launcher/launcheritemdelegate.h \
src/capture/tools/blurtool.h \
src/capture/workers/launcher/moreappswidget.h
src/capture/workers/launcher/moreappswidget.h \
src/capture/workers/launcher/terminallauncher.h
RESOURCES += \
graphics.qrc

View File

@@ -34,22 +34,22 @@ public:
// Don't forget to add the new types to CaptureButton::iterableButtonTypes
// in the .cpp and the order value in the private array buttonTypeOrder
enum ButtonType {
TYPE_PENCIL,
TYPE_LINE,
TYPE_ARROW,
TYPE_SELECTION,
TYPE_RECTANGLE,
TYPE_CIRCLE,
TYPE_MARKER,
TYPE_SELECTIONINDICATOR,
TYPE_MOVESELECTION,
TYPE_UNDO,
TYPE_COPY,
TYPE_SAVE,
TYPE_EXIT,
TYPE_IMAGEUPLOADER,
TYPE_OPEN_APP,
TYPE_BLUR,
TYPE_PENCIL = 0,
TYPE_LINE = 1,
TYPE_ARROW = 2,
TYPE_SELECTION = 3,
TYPE_RECTANGLE = 4,
TYPE_CIRCLE = 5,
TYPE_MARKER = 6,
TYPE_SELECTIONINDICATOR = 7,
TYPE_MOVESELECTION = 8,
TYPE_UNDO = 9,
TYPE_COPY = 10,
TYPE_SAVE = 11,
TYPE_EXIT = 12,
TYPE_IMAGEUPLOADER = 13,
TYPE_OPEN_APP = 14,
TYPE_BLUR = 15,
};
CaptureButton() = delete;

View File

@@ -19,6 +19,7 @@
#include "src/utils/filenamehandler.h"
#include "src/capture/workers/launcher/launcheritemdelegate.h"
#include "src/utils/confighandler.h"
#include "terminallauncher.h"
#include "moreappswidget.h"
#include <QDir>
#include <QList>
@@ -66,6 +67,7 @@ AppLauncherWidget::AppLauncherWidget(const QPixmap &p, QWidget *parent):
buttonItem->setData(Qt::DecorationRole, app.icon);
buttonItem->setData(Qt::DisplayRole, app.name);
buttonItem->setData(Qt::UserRole, app.exec);
buttonItem->setData(Qt::UserRole+1, app.showInTerminal);
QColor foregroundColor = this->palette().color(QWidget::foregroundRole());
buttonItem->setForeground(foregroundColor);
@@ -99,7 +101,15 @@ void AppLauncherWidget::launch(const QModelIndex &index) {
// TO DO
return;
}
QProcess::startDetached(command);
bool inTerminal = index.data(Qt::UserRole+1).toBool();
if (inTerminal) {
ok = TerminalLauncher::launchDetached(command);
if (!ok) {
// TO DO
}
} else {
QProcess::startDetached(command);
}
}
if (!m_keepOpen) {
close();

View File

@@ -94,6 +94,7 @@ MoreAppsWidget::MoreAppsWidget(
buttonItem->setData(Qt::DecorationRole, app.icon);
buttonItem->setData(Qt::DisplayRole, app.name);
buttonItem->setData(Qt::UserRole, app.exec);
buttonItem->setData(Qt::UserRole+1, app.showInTerminal);
QColor foregroundColor =
this->palette().color(QWidget::foregroundRole());
buttonItem->setForeground(foregroundColor);

View File

@@ -0,0 +1,43 @@
#include "terminallauncher.h"
#include <QProcess>
#include <QDir>
#include <QStandardPaths>
#include <QProcessEnvironment>
namespace {
static const TerminalApp terminalApps[] = {
{ "xterm", "-e" },
{ "x-terminal-emulator", "-e" },
{ "aterm", "-e" },
{ "Eterm", "-e" },
{ "rxvt", "-e" },
{ "urxvt", "-e" },
{ "xfce4-terminal", "-x" },
{ "konsole", "-e" },
{ "gnome-terminal", "--" },
{ "terminator", "-e" },
{ "terminology", "-e" },
{ "tilix", "-e" },
};
}
TerminalLauncher::TerminalLauncher(QObject *parent) : QObject(parent) {
}
TerminalApp TerminalLauncher::getPreferedTerminal() {
TerminalApp res;
for (const TerminalApp &app : terminalApps) {
QString path = QStandardPaths::findExecutable(app.name);
if (!path.isEmpty()) {
res = app;
break;
}
}
return res;
}
bool TerminalLauncher::launchDetached(const QString &command) {
TerminalApp app = getPreferedTerminal();
QString s = app.name + " " + app.arg + " " + command;
return QProcess::startDetached(s);
}

View File

@@ -0,0 +1,22 @@
#ifndef TERMINALLAUNCHER_H
#define TERMINALLAUNCHER_H
#include <QObject>
struct TerminalApp {
QString name;
QString arg;
};
class TerminalLauncher : public QObject
{
Q_OBJECT
public:
explicit TerminalLauncher(QObject *parent = nullptr);
static bool launchDetached(const QString &command);
private:
static TerminalApp getPreferedTerminal();
};
#endif // TERMINALLAUNCHER_H

View File

@@ -27,7 +27,7 @@ class QString;
class QTextStream;
struct DesktopAppData {
DesktopAppData() = default;
DesktopAppData() : showInTerminal() {}
DesktopAppData(
QString name,