Export tJob interfaes on D-Bus

This commit is contained in:
Victor Tran 2021-03-03 03:02:59 +11:00
parent 4349f06424
commit 3689458d26
No known key found for this signature in database
GPG key ID: 1F0729FE016CDC3E
9 changed files with 281 additions and 2 deletions

View file

@ -0,0 +1,23 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.vicr123.thelibs.tjob.Job">
<signal name="ProgressChanged">
<arg name="progress" type="t" direction="out"/>
</signal>
<signal name="TotalProgressChanged">
<arg name="totalProgress" type="t" direction="out"/>
</signal>
<signal name="StateChanged">
<arg name="state" type="s" direction="out"/>
</signal>
<method name="Progress">
<arg type="t" direction="out"/>
</method>
<method name="TotalProgress">
<arg type="t" direction="out"/>
</method>
<method name="State">
<arg type="s" direction="out"/>
</method>
</interface>
</node>

View file

@ -0,0 +1,11 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.vicr123.thelibs.tjob.Manager">
<signal name="JobAdded">
<arg name="job" type="o" direction="out"/>
</signal>
<method name="Jobs">
<arg type="ao" direction="out"/>
</method>
</interface>
</node>

67
lib/jobs/jobdbus.cpp Normal file
View file

@ -0,0 +1,67 @@
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2021 Victor Tran
*
* 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
* (at your option) 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 "jobdbus.h"
#include "job_adaptor.h"
#include "tjob.h"
struct JobDbusPrivate {
tJob* job;
};
JobDbus::JobDbus(QString path, tJob* job, QObject* parent) : QObject(parent) {
d = new JobDbusPrivate();
d->job = job;
connect(job, &tJob::progressChanged, this, &JobDbus::ProgressChanged);
connect(job, &tJob::totalProgressChanged, this, &JobDbus::TotalProgressChanged);
connect(job, &tJob::stateChanged, this, [ = ] {
emit StateChanged(this->State());
});
new JobAdaptor(this);
QDBusConnection::sessionBus().registerObject(path, this);
}
JobDbus::~JobDbus() {
delete d;
}
quint64 JobDbus::Progress() {
return d->job->progress();
}
quint64 JobDbus::TotalProgress() {
return d->job->totalProgress();
}
QString JobDbus::State() {
switch (d->job->state()) {
case tJob::Processing:
return QStringLiteral("Processing");
case tJob::Finished:
return QStringLiteral("Finished");
case tJob::Failed:
return QStringLiteral("Failed");
case tJob::RequiresAttention:
return QStringLiteral("RequiresAttention");
}
}

48
lib/jobs/jobdbus.h Normal file
View file

@ -0,0 +1,48 @@
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2021 Victor Tran
*
* 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
* (at your option) 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/>.
*
* *************************************/
#ifndef JOBDBUS_H
#define JOBDBUS_H
#include <QObject>
class tJob;
struct JobDbusPrivate;
class JobDbus : public QObject {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.vicr123.thelibs.tjob.Job")
public:
explicit JobDbus(QString path, tJob* job, QObject* parent = nullptr);
~JobDbus();
public slots:
Q_SCRIPTABLE quint64 Progress();
Q_SCRIPTABLE quint64 TotalProgress();
Q_SCRIPTABLE QString State();
signals:
Q_SCRIPTABLE void ProgressChanged(quint64 progress);
Q_SCRIPTABLE void TotalProgressChanged(quint64 totalProgress);
Q_SCRIPTABLE void StateChanged(QString state);
private:
JobDbusPrivate* d;
};
#endif // JOBDBUS_H

View file

@ -0,0 +1,60 @@
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2021 Victor Tran
*
* 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
* (at your option) 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 "jobdbusmanager.h"
#include <tapplication.h>
#include <QDBusConnection>
#include "tjob.h"
#include "tjobmanager.h"
#include "jobdbus.h"
#include "manager_adaptor.h"
struct JobDbusManagerPrivate {
QList<QDBusObjectPath> jobs;
};
JobDbusManager::JobDbusManager(QObject* parent) : QObject(parent) {
d = new JobDbusManagerPrivate();
QDBusConnection::sessionBus().registerService(QStringLiteral("com.vicr123.thelibs.tjob.%1").arg(qApp->applicationName()));
new ManagerAdaptor(this);
QDBusConnection::sessionBus().registerObject("/com/vicr123/thelibs/tjob", this);
connect(tJobManager::instance(), &tJobManager::jobAdded, this, &JobDbusManager::addJob);
for (tJob* job : tJobManager::jobs()) {
addJob(job);
}
}
JobDbusManager::~JobDbusManager() {
delete d;
}
QList<QDBusObjectPath> JobDbusManager::Jobs() {
return d->jobs;
}
void JobDbusManager::addJob(tJob* job) {
QDBusObjectPath path(QStringLiteral("/com/vicr123/thelibs/tjob/%1").arg(d->jobs.count() + 1));
new JobDbus(path.path(), job, this);
d->jobs.append(path);
emit JobAdded(path);
}

47
lib/jobs/jobdbusmanager.h Normal file
View file

@ -0,0 +1,47 @@
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2021 Victor Tran
*
* 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
* (at your option) 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/>.
*
* *************************************/
#ifndef JOBDBUSMANAGER_H
#define JOBDBUSMANAGER_H
#include <QObject>
#include <QDBusObjectPath>
class tJob;
struct JobDbusManagerPrivate;
class JobDbusManager : public QObject {
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.vicr123.thelibs.tjob.Manager")
public:
explicit JobDbusManager(QObject* parent = nullptr);
~JobDbusManager();
public slots:
Q_SCRIPTABLE QList<QDBusObjectPath> Jobs();
signals:
Q_SCRIPTABLE void JobAdded(QDBusObjectPath job);
private:
JobDbusManagerPrivate* d;
void addJob(tJob* job);
};
#endif // JOBDBUSMANAGER_H

View file

@ -149,6 +149,8 @@ HEADERS += tvariantanimation.h\
tswitch.h \
tsystemsound.h
DBUS_ADAPTORS += jobs/com.vicr123.thelibs.tjob.Manager.xml jobs/com.vicr123.thelibs.tjob.Job.xml
# Include required build tools
include($$PWD/prifiles/gentranslations.pri)
@ -171,8 +173,13 @@ unix:!macx:!android {
module.files = qt_thelib.pri
prifiles.path = /usr/share/the-libs/pri
HEADERS += tnotification/tnotification-linux.h
SOURCES += tnotification/tnotification-linux.cpp
HEADERS += tnotification/tnotification-linux.h \
jobs/jobdbus.h \
jobs/jobdbusmanager.h
SOURCES += tnotification/tnotification-linux.cpp \
jobs/jobdbus.cpp \
jobs/jobdbusmanager.cpp
}
macx {

View file

@ -25,14 +25,27 @@
#include "jobs/jobspopover.h"
#include "tpopover.h"
#ifdef T_OS_UNIX_NOT_MAC
#include "jobs/jobdbusmanager.h"
#endif
struct tJobManagerPrivate {
QList<tJob*> jobs;
JobDbusManager* dbusManager = nullptr;
};
tJobManager::tJobManager(QObject* parent) : QObject(parent) {
d = new tJobManagerPrivate();
}
void tJobManager::registerDBus() {
#ifdef T_OS_UNIX_NOT_MAC
if (!d->dbusManager) {
d->dbusManager = new JobDbusManager(this);
}
#endif
}
tJobManager::~tJobManager() {
delete d;
}
@ -45,6 +58,7 @@ tJobManager* tJobManager::instance() {
void tJobManager::trackJob(tJob* job) {
instance()->d->jobs.append(job);
emit instance()->jobAdded(job);
instance()->registerDBus();
}
void tJobManager::trackJobDelayed(tJob* job, quint64 delay) {

View file

@ -46,6 +46,8 @@ class THELIBSSHARED_EXPORT tJobManager : public QObject {
private:
explicit tJobManager(QObject* parent = nullptr);
tJobManagerPrivate* d;
void registerDBus();
};
#endif // TJOBMANAGER_H