Skip to content

Commit

Permalink
Initial work on a process launcher in the session wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
iBelieve committed Jun 28, 2015
1 parent 7a86c84 commit 430bba7
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 109 deletions.
9 changes: 2 additions & 7 deletions modules/Papyros/Desktop/desktop/desktopfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ void DesktopFile::load() {
emit dataChanged();
}

void DesktopFile::launch(const QStringList& urls) const
bool DesktopFile::launch(const QStringList& urls) const
{
if (isValid()) {
DesktopFiles::sharedInstance()->launchApplication(m_desktopFile, urls);
}

// TODO: Set DESKTOP_FILE env variable
// TODO: Set Qt and Gtk env variables to force the use of Wayland
return DesktopFiles::sharedInstance()->launchApplication(m_appId, urls);
}

QString DesktopFile::name() const {
Expand Down
2 changes: 1 addition & 1 deletion modules/Papyros/Desktop/desktop/desktopfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DesktopFile : public QObject

static QString getEnvVar(int pid);

Q_INVOKABLE void launch(const QStringList& urls) const;
Q_INVOKABLE bool launch(const QStringList& urls) const;

QString m_appId;
QString m_path;
Expand Down
95 changes: 15 additions & 80 deletions modules/Papyros/Desktop/desktop/desktopfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
Q_GLOBAL_STATIC(DesktopFiles, s_desktopFiles)

DesktopFiles::DesktopFiles(QObject *parent)
: QObject(parent) {
: QObject(parent),
m_interface("io.papyros.session", "/PapyrosSession", "io.papyros.launcher", QDBusConnection::sessionBus())
{
QStringList paths; paths << "~/.local/share/applications"
<< "/usr/local/share/applications"
<< "/usr/share/applications";
Expand Down Expand Up @@ -136,95 +138,28 @@ int DesktopFiles::indexOfName(QString name)
return -1;
}

void DesktopFiles::closeApplications()
bool DesktopFiles::launchApplication(const QString &appId, const QStringList &urls)
{
// Terminate all process launched by us
ApplicationMapIterator i(m_apps);
while (i.hasNext()) {
i.next();

QString fileName = i.key();
QProcess *process = i.value();

i.remove();

qDebug() << "Terminating application from" << fileName << "with pid" << process->pid();

process->terminate();
if (!process->waitForFinished())
process->kill();
process->deleteLater();
}
return m_interface.call(QStringLiteral("launchApplication"), appId, urls)
.arguments().at(0).toBool();
}

bool DesktopFiles::launchApplication(XdgDesktopFile *entry, const QStringList& urls)
bool DesktopFiles::launchDesktopFile(const QString &fileName, const QStringList &urls)
{
QStringList args = entry->expandExecString(urls);

if (args.isEmpty())
return false;

if (entry->value("Terminal").toBool())
{
QString term = getenv("TERM");
if (term.isEmpty())
term = "xterm";

args.prepend("-e");
args.prepend(term);
}

QString command = args.takeAt(0);

qDebug() << "Launching" << args.join(" ") << "from" << entry->fileName();

QProcess *process = new QProcess(this);
process->setProgram(command);
process->setArguments(args);
process->setProcessChannelMode(QProcess::ForwardedChannels);
m_apps[entry->fileName()] = process;
connect(process, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
process->start();
if (!process->waitForStarted()) {
qWarning("Failed to launch \"%s\" (%s)",
qPrintable(entry->fileName()),
qPrintable(entry->name()));
return false;
}

qDebug("Launched \"%s\" (%s) with pid %lld",
qPrintable(entry->fileName()),
qPrintable(entry->name()),
process->pid());

return true;
return m_interface.call(QStringLiteral("launchDesktopFile"), fileName, urls)
.arguments().at(0).toBool();
}

bool DesktopFiles::closeApplication(const QString &fileName)
bool DesktopFiles::closeApplication(const QString &appId)
{
if (!m_apps.contains(fileName))
return false;

QProcess *process = m_apps[fileName];
process->terminate();
if (!process->waitForFinished())
process->kill();
return true;
return m_interface.call(QStringLiteral("closeApplication"), appId)
.arguments().at(0).toBool();
}

void DesktopFiles::processFinished(int exitCode)
bool DesktopFiles::closeDesktopFile(const QString &fileName)
{
QProcess *process = qobject_cast<QProcess *>(sender());
if (!process)
return;

QString fileName = m_apps.key(process);
XdgDesktopFile *entry = XdgDesktopFileCache::getFile(fileName);
if (entry) {
qDebug() << "Application for" << fileName << "finished with exit code" << exitCode;
m_apps.remove(fileName);
process->deleteLater();
}
return m_interface.call(QStringLiteral("closeDesktopFile"), fileName)
.arguments().at(0).toBool();
}

DesktopFiles *DesktopFiles::sharedInstance()
Expand Down
15 changes: 7 additions & 8 deletions modules/Papyros/Desktop/desktop/desktopfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@

#include <QObject>
#include <QFileSystemWatcher>
#include <QDBusInterface>

#include "../qquicklist/qquicklist.h"
#include "desktopfile.h"

typedef QMap<QString, QProcess *> ApplicationMap;
typedef QMutableMapIterator<QString, QProcess *> ApplicationMapIterator;

class DesktopFiles : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -60,9 +58,11 @@ public slots:
iconThemeChanged(name);
}

bool launchApplication(XdgDesktopFile *entry, const QStringList& urls);
bool closeApplication(const QString &fileName);
void closeApplications();
bool launchApplication(const QString &appId, const QStringList &urls);
bool launchDesktopFile(const QString &fileName, const QStringList &urls);

bool closeApplication(const QString &appId);
bool closeDesktopFile(const QString &fileName);

signals:
void desktopFilesChanged(QObjectListModel *);
Expand All @@ -71,10 +71,9 @@ public slots:
private slots:
void onFileChanged(const QString &path);
void onDirectoryChanged(const QString &directory);
void processFinished(int exitCode);

private:
ApplicationMap m_apps;
QDBusInterface m_interface;
QFileSystemWatcher *fileWatcher;
QFileSystemWatcher *dirWatcher;
QQuickList<DesktopFile> desktopList;
Expand Down
2 changes: 1 addition & 1 deletion modules/Papyros/Desktop/session/sessionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

SessionManager::SessionManager(QObject *parent)
: QObject(parent),
m_interface("io.papyros.Session", "/PapyrosSession", "io.papyros.Session", QDBusConnection::sessionBus()),
m_interface("io.papyros.session", "/PapyrosSession", "io.papyros.session", QDBusConnection::sessionBus()),
m_response(nullptr)
{
// Nothing needed here
Expand Down
2 changes: 2 additions & 0 deletions session/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ set(SOURCES
compositorlauncher.cpp
loginmanager.h
loginmanager.cpp
processlauncher.h
processlauncher.cpp
sessionadaptor.h
sessionadaptor.cpp
sessionmanager.h
Expand Down
2 changes: 1 addition & 1 deletion session/compositorlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,6 @@ void CompositorLauncher::setupEnvironment()
qputenv("QT_QPA_PLATFORM", QByteArray("wayland"));
}
qputenv("GDK_BACKEND", QByteArray("wayland"));

qunsetenv("WAYLAND_DISPLAY");
}
Loading

0 comments on commit 430bba7

Please sign in to comment.