filemanager/helper/filelauncher.cpp

109 lines
2.8 KiB
C++
Raw Normal View History

2021-07-28 12:30:17 -07:00
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reionwong@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filelauncher.h"
2021-11-20 10:25:31 -08:00
#include <QDBusInterface>
#include <QDBusPendingCall>
2021-07-28 12:30:17 -07:00
#include <QSettings>
#include <QProcess>
#include <QFile>
#include <QDir>
#include <QDebug>
FileLauncher *SELF = nullptr;
FileLauncher *FileLauncher::self()
{
if (SELF == nullptr)
SELF = new FileLauncher;
return SELF;
}
FileLauncher::FileLauncher(QObject *parent)
: QObject(parent)
{
}
bool FileLauncher::launchApp(const QString &desktopFile, const QString &fileName)
{
QSettings settings(desktopFile, QSettings::IniFormat);
settings.beginGroup("Desktop Entry");
QStringList list = settings.value("Exec").toString().split(' ');
QStringList args;
if (list.isEmpty() || list.size() < 0)
return false;
QString exec = list.first();
list.removeOne(exec);
for (const QString &arg : list) {
QString newArg = arg;
if (newArg.startsWith("%F", Qt::CaseInsensitive))
newArg.replace("%F", fileName, Qt::CaseInsensitive);
if (newArg.startsWith("%U", Qt::CaseInsensitive))
newArg.replace("%U", fileName, Qt::CaseInsensitive);
args.append(newArg);
}
qDebug() << "launchApp()" << exec << args;
2021-11-20 10:25:31 -08:00
return startDetached(exec, args);
2021-07-28 12:30:17 -07:00
}
bool FileLauncher::launchExecutable(const QString &fileName)
{
2021-11-20 10:25:31 -08:00
return startDetached(fileName);
}
bool FileLauncher::startDetached(const QString &exec, QStringList args)
{
QDBusInterface iface("com.cutefish.Session",
"/Session",
"com.cutefish.Session", QDBusConnection::sessionBus());
if (iface.isValid()) {
iface.asyncCall("launch", exec, args).waitForFinished();
} else {
QProcess::startDetached(exec, args);
}
return true;
}
bool FileLauncher::startDetached(const QString &exec, const QString &workingDir, QStringList args)
{
QDBusInterface iface("com.cutefish.Session",
"/Session",
"com.cutefish.Session", QDBusConnection::sessionBus());
if (iface.isValid()) {
iface.asyncCall("launch", exec, workingDir, args).waitForFinished();
}
return true;
2021-07-28 12:30:17 -07:00
}