Extracted the stay-awake functionality to separate D-Bus classes

This commit is contained in:
Sebastian Wolf 2020-12-10 23:47:32 +01:00
parent af2a4b0be7
commit ce8047028d
9 changed files with 200 additions and 23 deletions

View file

@ -34,6 +34,8 @@ SOURCES += src/harbour-fernschreiber.cpp \
src/namedaction.cpp \ src/namedaction.cpp \
src/notificationmanager.cpp \ src/notificationmanager.cpp \
src/processlauncher.cpp \ src/processlauncher.cpp \
src/stayawakeadaptor.cpp \
src/stayawakeinterface.cpp \
src/stickermanager.cpp \ src/stickermanager.cpp \
src/tdlibfile.cpp \ src/tdlibfile.cpp \
src/tdlibreceiver.cpp \ src/tdlibreceiver.cpp \
@ -163,6 +165,8 @@ HEADERS += \
src/namedaction.h \ src/namedaction.h \
src/notificationmanager.h \ src/notificationmanager.h \
src/processlauncher.h \ src/processlauncher.h \
src/stayawakeadaptor.h \
src/stayawakeinterface.h \
src/stickermanager.h \ src/stickermanager.h \
src/tdlibfile.h \ src/tdlibfile.h \
src/tdlibreceiver.h \ src/tdlibreceiver.h \

View file

@ -22,6 +22,7 @@
#include <QDBusInterface> #include <QDBusInterface>
#include <QDBusReply> #include <QDBusReply>
#include <QGuiApplication>
namespace { namespace {
const QString KEY_SEND_BY_ENTER("sendByEnter"); const QString KEY_SEND_BY_ENTER("sendByEnter");
@ -76,6 +77,7 @@ void AppSettings::setStayInBackground(bool stayInBackground)
if (getStayInBackground() != stayInBackground) { if (getStayInBackground() != stayInBackground) {
LOG(KEY_STAY_IN_BACKGROUND << stayInBackground); LOG(KEY_STAY_IN_BACKGROUND << stayInBackground);
settings.setValue(KEY_STAY_IN_BACKGROUND, stayInBackground); settings.setValue(KEY_STAY_IN_BACKGROUND, stayInBackground);
QGuiApplication::setQuitOnLastWindowClosed(!stayInBackground);
emit stayInBackgroundChanged(); emit stayInBackgroundChanged();
} }
} }
@ -153,7 +155,7 @@ void AppSettings::setStorageOptimizer(bool enable)
bool AppSettings::isAppRunning() bool AppSettings::isAppRunning()
{ {
LOG("Checking via D-Bus if app is already running..."); LOG("Checking via D-Bus if app is already running...");
QDBusInterface dBusInterface("de.ygriega.fernschreiber", "/de/ygriega/fernschreiber", "", QDBusConnection::sessionBus()); QDBusInterface dBusInterface("de.ygriega.stayawake", "/de/ygriega/stayawake", "", QDBusConnection::sessionBus());
if (dBusInterface.isValid()) { if (dBusInterface.isValid()) {
QDBusReply<bool> reply = dBusInterface.call("showUI"); QDBusReply<bool> reply = dBusInterface.call("showUI");
if (reply.isValid()) { if (reply.isValid()) {

View file

@ -26,11 +26,6 @@ DBusAdaptor::DBusAdaptor(QObject *parent): QDBusAbstractAdaptor(parent)
{ {
} }
void DBusAdaptor::setAppView(QQuickView *appView)
{
this->appView = appView;
}
void DBusAdaptor::openMessage(const QString &chatId, const QString &messageId) void DBusAdaptor::openMessage(const QString &chatId, const QString &messageId)
{ {
LOG("Open Message" << chatId << messageId); LOG("Open Message" << chatId << messageId);
@ -44,10 +39,3 @@ void DBusAdaptor::openUrl(const QStringList &arguments)
emit pleaseOpenUrl(arguments.first()); emit pleaseOpenUrl(arguments.first());
} }
} }
bool DBusAdaptor::showUI()
{
LOG("UI shall wake up!");
this->appView->showFullScreen();
return true;
}

View file

@ -32,8 +32,6 @@ class DBusAdaptor : public QDBusAbstractAdaptor
public: public:
DBusAdaptor(QObject *parent); DBusAdaptor(QObject *parent);
void setAppView(QQuickView* appView);
signals: signals:
void pleaseOpenMessage(const QString &chatId, const QString &messageId); void pleaseOpenMessage(const QString &chatId, const QString &messageId);
void pleaseOpenUrl(const QString &url); void pleaseOpenUrl(const QString &url);
@ -41,10 +39,6 @@ signals:
public slots: public slots:
void openMessage(const QString &chatId, const QString &messageId); void openMessage(const QString &chatId, const QString &messageId);
void openUrl(const QStringList &arguments); void openUrl(const QStringList &arguments);
bool showUI();
private:
QQuickView *appView;
}; };

View file

@ -41,6 +41,8 @@
#include "mceinterface.h" #include "mceinterface.h"
#include "dbusinterface.h" #include "dbusinterface.h"
#include "dbusadaptor.h" #include "dbusadaptor.h"
#include "stayawakeinterface.h"
#include "stayawakeadaptor.h"
#include "processlauncher.h" #include "processlauncher.h"
#include "stickermanager.h" #include "stickermanager.h"
#include "tgsplugin.h" #include "tgsplugin.h"
@ -76,9 +78,9 @@ int main(int argc, char *argv[])
context->setContextProperty("appSettings", appSettings); context->setContextProperty("appSettings", appSettings);
qmlRegisterUncreatableType<AppSettings>(uri, 1, 0, "AppSettings", QString()); qmlRegisterUncreatableType<AppSettings>(uri, 1, 0, "AppSettings", QString());
// if (appSettings->isAppRunning()) { if (appSettings->isAppRunning()) {
// return 0; return 0;
// } }
if (appSettings->getStayInBackground()) { if (appSettings->getStayInBackground()) {
app.data()->setQuitOnLastWindowClosed(false); app.data()->setQuitOnLastWindowClosed(false);
@ -86,9 +88,13 @@ int main(int argc, char *argv[])
DBusInterface *dBusInterface = new DBusInterface(view.data()); DBusInterface *dBusInterface = new DBusInterface(view.data());
DBusAdaptor *dBusAdaptor = dBusInterface->getDBusAdaptor(); DBusAdaptor *dBusAdaptor = dBusInterface->getDBusAdaptor();
dBusAdaptor->setAppView(view.data());
context->setContextProperty("dBusAdaptor", dBusAdaptor); context->setContextProperty("dBusAdaptor", dBusAdaptor);
StayAwakeInterface *stayAwakeInterface = new StayAwakeInterface(view.data());
StayAwakeAdaptor *stayAwakeAdaptor = stayAwakeInterface->getStayAwakeAdaptor();
stayAwakeAdaptor->setAppView(view.data());
context->setContextProperty("stayAwakeAdaptor", stayAwakeAdaptor);
MceInterface *mceInterface = new MceInterface(view.data()); MceInterface *mceInterface = new MceInterface(view.data());
TDLibWrapper *tdLibWrapper = new TDLibWrapper(appSettings, mceInterface, view.data()); TDLibWrapper *tdLibWrapper = new TDLibWrapper(appSettings, mceInterface, view.data());
context->setContextProperty("tdLibWrapper", tdLibWrapper); context->setContextProperty("tdLibWrapper", tdLibWrapper);

41
src/stayawakeadaptor.cpp Normal file
View file

@ -0,0 +1,41 @@
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stayawakeadaptor.h"
#define DEBUG_MODULE StayAwakeAdaptor
#include "debuglog.h"
StayAwakeAdaptor::StayAwakeAdaptor(QObject *parent): QDBusAbstractAdaptor(parent)
{
}
void StayAwakeAdaptor::setAppView(QQuickView *appView)
{
this->appView = appView;
}
bool StayAwakeAdaptor::showUI()
{
LOG("UI shall wake up!");
this->appView->showFullScreen();
return true;
}

47
src/stayawakeadaptor.h Normal file
View file

@ -0,0 +1,47 @@
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef STAYAWAKEADAPTOR_H
#define STAYAWAKEADAPTOR_H
#include <QDBusAbstractAdaptor>
#include <QQuickView>
class StayAwakeAdaptor : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "de.ygriega.stayawake")
public:
StayAwakeAdaptor(QObject *parent);
void setAppView(QQuickView* appView);
public slots:
bool showUI();
private:
QQuickView *appView;
};
#endif // STAYAWAKEADAPTOR_H

View file

@ -0,0 +1,50 @@
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stayawakeinterface.h"
#define DEBUG_MODULE StayAwakeInterface
#include "debuglog.h"
StayAwakeInterface::StayAwakeInterface(QObject *parent) : QObject(parent)
{
LOG("Initializing D-BUS connectivity");
this->stayAwakeAdaptor = new StayAwakeAdaptor(this);
QDBusConnection sessionBusConnection = QDBusConnection::sessionBus();
if (!sessionBusConnection.isConnected()) {
WARN("Error connecting to D-BUS");
return;
}
if (!sessionBusConnection.registerObject("/de/ygriega/stayawake", this)) {
WARN("Error registering root object to D-BUS" << sessionBusConnection.lastError().message());
return;
}
if (!sessionBusConnection.registerService("de.ygriega.stayawake")) {
WARN("Error registering interface to D-BUS" << sessionBusConnection.lastError().message());
return;
}
}
StayAwakeAdaptor *StayAwakeInterface::getStayAwakeAdaptor()
{
return this->stayAwakeAdaptor;
}

45
src/stayawakeinterface.h Normal file
View file

@ -0,0 +1,45 @@
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef STAYAWAKEINTERFACE_H
#define STAYAWAKEINTERFACE_H
#include <QObject>
#include <QtDBus>
#include "stayawakeadaptor.h"
class StayAwakeInterface : public QObject
{
Q_OBJECT
public:
explicit StayAwakeInterface(QObject *parent = nullptr);
StayAwakeAdaptor *getStayAwakeAdaptor();
signals:
public slots:
private:
StayAwakeAdaptor *stayAwakeAdaptor;
};
#endif // STAYAWAKEINTERFACE_H