mirror of
https://invent.kde.org/marcoa/shift-shell.git
synced 2026-04-26 14:23:09 +00:00
waydroid: Refactor with QCoro
This commit is contained in:
parent
1e2ba92a5a
commit
5c93d1e77a
8 changed files with 63 additions and 38 deletions
|
|
@ -87,7 +87,7 @@ find_package(KPipeWire ${PROJECT_DEP_VERSION} REQUIRED)
|
|||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
find_package(QCoro6 REQUIRED COMPONENTS DBus)
|
||||
find_package(QCoro6 REQUIRED COMPONENTS DBus Qml)
|
||||
qcoro_enable_coroutines()
|
||||
kde_enable_exceptions()
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ target_link_libraries(waydroidintegrationplugin PRIVATE
|
|||
Qt::Quick
|
||||
KF6::AuthCore
|
||||
KF6::I18n
|
||||
QCoro::Core
|
||||
QCoro::Qml
|
||||
)
|
||||
|
||||
ecm_finalize_qml_module(waydroidintegrationplugin)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "waydroidshared.h"
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QCoroProcess>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QGuiApplication>
|
||||
|
|
@ -175,10 +176,15 @@ void WaydroidState::resetError()
|
|||
}
|
||||
}
|
||||
|
||||
void WaydroidState::initialize(const SystemType systemType, const RomType romType, const bool forced)
|
||||
QCoro::QmlTask WaydroidState::initializeQml(const SystemType systemType, const RomType romType, const bool forced)
|
||||
{
|
||||
return initialize(systemType, romType, forced);
|
||||
}
|
||||
|
||||
QCoro::Task<void> WaydroidState::initialize(const SystemType systemType, const RomType romType, const bool forced)
|
||||
{
|
||||
if (m_status == Initializing) {
|
||||
return;
|
||||
co_return;
|
||||
}
|
||||
|
||||
m_status = Initializing;
|
||||
|
|
@ -230,27 +236,32 @@ void WaydroidState::initialize(const SystemType systemType, const RomType romTyp
|
|||
Q_EMIT downloadStatusChanged(downloaded, total, speed);
|
||||
});
|
||||
|
||||
connect(job, &KAuth::ExecuteJob::finished, this, [this](KJob *job, auto) {
|
||||
if (job->error() == 0) {
|
||||
m_status = Initialized;
|
||||
} else {
|
||||
m_errorTitle = i18n("Failed to initialize Waydroid.");
|
||||
Q_EMIT errorTitleChanged();
|
||||
m_errorMessage = job->errorString();
|
||||
Q_EMIT errorMessageChanged();
|
||||
co_await qCoro(job, &KAuth::ExecuteJob::finished);
|
||||
|
||||
m_status = NotInitialized;
|
||||
qCWarning(WAYDROIDINTEGRATIONPLUGIN) << "KAuth returned an error code:" << job->error() << " message: " << job->errorString();
|
||||
}
|
||||
if (job->error() == 0) {
|
||||
m_status = Initialized;
|
||||
} else {
|
||||
m_errorTitle = i18n("Failed to initialize Waydroid.");
|
||||
Q_EMIT errorTitleChanged();
|
||||
m_errorMessage = job->errorString();
|
||||
Q_EMIT errorMessageChanged();
|
||||
|
||||
Q_EMIT statusChanged();
|
||||
});
|
||||
m_status = NotInitialized;
|
||||
qCWarning(WAYDROIDINTEGRATIONPLUGIN) << "KAuth returned an error code:" << job->error() << " message: " << job->errorString();
|
||||
}
|
||||
|
||||
Q_EMIT statusChanged();
|
||||
}
|
||||
|
||||
void WaydroidState::startSession()
|
||||
QCoro::QmlTask WaydroidState::startSessionQml()
|
||||
{
|
||||
return startSession();
|
||||
}
|
||||
|
||||
QCoro::Task<void> WaydroidState::startSession()
|
||||
{
|
||||
if (m_sessionStatus == SessionStarting || m_sessionStatus == SessionRunning) {
|
||||
return;
|
||||
co_return;
|
||||
}
|
||||
|
||||
m_sessionStatus = SessionStarting;
|
||||
|
|
@ -258,11 +269,11 @@ void WaydroidState::startSession()
|
|||
|
||||
const QStringList arguments{u"session"_s, u"start"_s};
|
||||
|
||||
// Don't wait for result because the command is blocking
|
||||
QProcess *process = new QProcess(this);
|
||||
process->start(WAYDROID_COMMAND, arguments);
|
||||
QProcess *basicProcess = new QProcess(this);
|
||||
auto process = qCoro(basicProcess);
|
||||
co_await process.start(WAYDROID_COMMAND, arguments);
|
||||
|
||||
connect(process, &QProcess::finished, this, [this, process](int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
connect(basicProcess, &QProcess::finished, this, [this, basicProcess](int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
Q_UNUSED(exitStatus);
|
||||
|
||||
if (exitCode == 0) {
|
||||
|
|
@ -272,7 +283,7 @@ void WaydroidState::startSession()
|
|||
m_sessionStatus = SessionStopped;
|
||||
Q_EMIT sessionStatusChanged();
|
||||
|
||||
QByteArray errorData = process->readAllStandardError();
|
||||
QByteArray errorData = basicProcess->readAllStandardError();
|
||||
QString errorString = QString::fromUtf8(errorData);
|
||||
|
||||
m_errorTitle = i18n("Failed to start the Waydroid session.");
|
||||
|
|
@ -286,23 +297,29 @@ void WaydroidState::startSession()
|
|||
checkSessionStarting(10);
|
||||
}
|
||||
|
||||
void WaydroidState::stopSession()
|
||||
QCoro::QmlTask WaydroidState::stopSessionQml()
|
||||
{
|
||||
return stopSession();
|
||||
}
|
||||
|
||||
QCoro::Task<void> WaydroidState::stopSession()
|
||||
{
|
||||
if (m_sessionStatus == SessionStopped) {
|
||||
return;
|
||||
co_return;
|
||||
}
|
||||
|
||||
const QStringList arguments{u"session"_s, u"stop"_s};
|
||||
|
||||
QProcess *process = new QProcess(this);
|
||||
process->start(WAYDROID_COMMAND, arguments);
|
||||
process->waitForFinished();
|
||||
QProcess basicProcess = QProcess(this);
|
||||
auto process = qCoro(basicProcess);
|
||||
co_await process.start(WAYDROID_COMMAND, arguments);
|
||||
co_await process.waitForFinished();
|
||||
|
||||
if (process->exitCode() == 0) {
|
||||
if (basicProcess.exitCode() == 0) {
|
||||
m_sessionStatus = SessionStopped;
|
||||
Q_EMIT sessionStatusChanged();
|
||||
} else {
|
||||
qCWarning(WAYDROIDINTEGRATIONPLUGIN) << "Failed to stop the Waydroid session: " << process->readAllStandardError();
|
||||
qCWarning(WAYDROIDINTEGRATIONPLUGIN) << "Failed to stop the Waydroid session: " << basicProcess.readAllStandardError();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#include "waydroidapplicationlistmodel.h"
|
||||
|
||||
#include <QCoroCore>
|
||||
#include <QCoroQmlTask>
|
||||
#include <QObject>
|
||||
|
||||
#include <qqmlregistration.h>
|
||||
|
|
@ -95,9 +97,13 @@ public:
|
|||
Q_INVOKABLE void refreshAndroidId();
|
||||
Q_INVOKABLE void refreshPropsInfo();
|
||||
Q_INVOKABLE void resetError();
|
||||
Q_INVOKABLE void initialize(const SystemType systemType, const RomType romType, const bool forced = false);
|
||||
Q_INVOKABLE void startSession();
|
||||
Q_INVOKABLE void stopSession();
|
||||
Q_INVOKABLE QCoro::QmlTask initializeQml(const SystemType systemType, const RomType romType, const bool forced = false);
|
||||
QCoro::Task<void> initialize(const SystemType systemType, const RomType romType, const bool forced = false);
|
||||
Q_INVOKABLE QCoro::QmlTask startSessionQml();
|
||||
QCoro::Task<void> startSession();
|
||||
Q_INVOKABLE QCoro::QmlTask stopSessionQml();
|
||||
QCoro::Task<void> stopSession();
|
||||
|
||||
Q_INVOKABLE void copyToClipboard(const QString text);
|
||||
|
||||
Status status() const;
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ ColumnLayout {
|
|||
|
||||
trailing: PC3.Button {
|
||||
text: i18n("Stop session")
|
||||
onClicked: AIP.WaydroidState.stopSession()
|
||||
onClicked: AIP.WaydroidState.stopSessionQml()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,6 @@ ColumnLayout {
|
|||
Layout.alignment: Qt.AlignHCenter
|
||||
enabled: systemType.currentValue !== undefined
|
||||
|
||||
onClicked: AIP.WaydroidState.initialize(systemType.currentValue, AIP.WaydroidState.Lineage)
|
||||
onClicked: AIP.WaydroidState.initializeQml(systemType.currentValue, AIP.WaydroidState.Lineage)
|
||||
}
|
||||
}
|
||||
|
|
@ -76,7 +76,7 @@ KCM.SimpleKCM {
|
|||
PC3.Button {
|
||||
text: i18n("Start the session")
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
onClicked: AIP.WaydroidState.startSession()
|
||||
onClicked: AIP.WaydroidState.startSessionQml()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ QS.QuickSetting {
|
|||
|
||||
function toggle(): void {
|
||||
if (AIP.WaydroidState.sessionStatus === AIP.WaydroidState.SessionRunning) {
|
||||
AIP.WaydroidState.stopSession()
|
||||
AIP.WaydroidState.stopSessionQml()
|
||||
} else {
|
||||
AIP.WaydroidState.startSession()
|
||||
AIP.WaydroidState.startSessionQml()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue