Open Fernschreiber from notification

This commit is contained in:
Sebastian J. Wolf 2020-09-14 23:43:21 +02:00
parent 1641813b06
commit 266a72036b
10 changed files with 229 additions and 0 deletions

View file

@ -21,6 +21,8 @@ QT += core dbus
SOURCES += src/harbour-fernschreiber.cpp \
src/chatlistmodel.cpp \
src/chatmodel.cpp \
src/dbusadaptor.cpp \
src/dbusinterface.cpp \
src/notificationmanager.cpp \
src/tdlibreceiver.cpp \
src/tdlibwrapper.cpp
@ -89,6 +91,8 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \
HEADERS += \
src/chatlistmodel.h \
src/chatmodel.h \
src/dbusadaptor.h \
src/dbusinterface.h \
src/notificationmanager.h \
src/tdlibreceiver.h \
src/tdlibsecrets.h \

View file

@ -27,4 +27,11 @@ ApplicationWindow
initialPage: Qt.resolvedUrl("pages/OverviewPage.qml")
cover: Qt.resolvedUrl("pages/CoverPage.qml")
allowedOrientations: defaultAllowedOrientations
Connections {
target: dBusAdaptor
onPleaseOpenMessage: {
appWindow.activate();
}
}
}

33
src/dbusadaptor.cpp Normal file
View file

@ -0,0 +1,33 @@
/*
Copyright (C) 2020 Sebastian J. Wolf
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 "dbusadaptor.h"
#include <QDebug>
DBusAdaptor::DBusAdaptor(QObject *parent): QDBusAbstractAdaptor(parent)
{
}
void DBusAdaptor::openMessage(const QString &chatId, const QString &messageId)
{
qDebug() << "[DBusAdaptor] Open Message " << chatId << messageId;
emit pleaseOpenMessage(chatId, messageId);
}

42
src/dbusadaptor.h Normal file
View file

@ -0,0 +1,42 @@
/*
Copyright (C) 2020 Sebastian J. Wolf
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 DBUSADAPTOR_H
#define DBUSADAPTOR_H
#include <QDBusAbstractAdaptor>
class DBusAdaptor : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "de.ygriega.fernschreiber")
public:
DBusAdaptor(QObject *parent);
signals:
void pleaseOpenMessage(const QString &chatId, const QString &messageId);
public slots:
void openMessage(const QString &chatId, const QString &messageId);
};
#endif // DBUSADAPTOR_H

48
src/dbusinterface.cpp Normal file
View file

@ -0,0 +1,48 @@
/*
Copyright (C) 2020 Sebastian J. Wolf
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 "dbusinterface.h"
DBusInterface::DBusInterface(QObject *parent) : QObject(parent)
{
qDebug() << "[DBusInterface] Initializing D-BUS connectivity";
this->dbusAdaptor = new DBusAdaptor(this);
QDBusConnection sessionBusConnection = QDBusConnection::sessionBus();
if (!sessionBusConnection.isConnected()) {
qDebug() << "[DBusInterface] Error connecting to D-BUS";
return;
}
if (!sessionBusConnection.registerObject(PATH_NAME, this)) {
qDebug() << "[DBusInterface] Error registering root object to D-BUS" << sessionBusConnection.lastError().message();
return;
}
if (!sessionBusConnection.registerService(INTERFACE_NAME)) {
qDebug() << "[DBusInterface] Error registering interface to D-BUS" << sessionBusConnection.lastError().message();
return;
}
}
DBusAdaptor *DBusInterface::getDBusAdaptor()
{
return this->dbusAdaptor;
}

47
src/dbusinterface.h Normal file
View file

@ -0,0 +1,47 @@
/*
Copyright (C) 2020 Sebastian J. Wolf
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 DBUSINTERFACE_H
#define DBUSINTERFACE_H
#include <QObject>
#include <QtDBus>
#include "dbusadaptor.h"
const QString INTERFACE_NAME = "de.ygriega.fernschreiber";
const QString PATH_NAME = "/de/ygriega/fernschreiber";
class DBusInterface : public QObject
{
Q_OBJECT
public:
explicit DBusInterface(QObject *parent = nullptr);
DBusAdaptor *getDBusAdaptor();
signals:
public slots:
private:
DBusAdaptor *dbusAdaptor;
};
#endif // DBUSINTERFACE_H

View file

@ -33,6 +33,7 @@
#include "chatlistmodel.h"
#include "chatmodel.h"
#include "notificationmanager.h"
#include "dbusadaptor.h"
int main(int argc, char *argv[])
{
@ -45,6 +46,9 @@ int main(int argc, char *argv[])
context->setContextProperty("tdLibWrapper", tdLibWrapper);
qmlRegisterType<TDLibWrapper>("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI");
DBusAdaptor *dBusAdaptor = tdLibWrapper->getDBusAdaptor();
context->setContextProperty("dBusAdaptor", dBusAdaptor);
ChatListModel chatListModel(tdLibWrapper);
context->setContextProperty("chatListModel", &chatListModel);

View file

@ -180,6 +180,10 @@ QVariantMap NotificationManager::sendNotification(const QString &chatId, const Q
nemoNotification.setSummary(chatInformation.value("title").toString());
nemoNotification.setCategory("x-nemo.messaging.im");
nemoNotification.setTimestamp(QDateTime::fromMSecsSinceEpoch(messageMap.value("date").toLongLong() * 1000));
QVariantList remoteActionArguments;
remoteActionArguments.append(chatId);
remoteActionArguments.append(messageMap.value("id").toString());
nemoNotification.setRemoteAction(Notification::remoteAction("default", "openMessage", "de.ygriega.fernschreiber", "/de/ygriega/fernschreiber", "de.ygriega.fernschreiber", "openMessage", remoteActionArguments));
if (activeNotifications.isEmpty()) {
QString notificationBody;
if (addAuthor) {

View file

@ -42,6 +42,9 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent)
tdLibDatabaseDirectory.mkpath(tdLibDatabaseDirectoryPath);
}
this->dbusInterface = new DBusInterface(this);
this->initializeOpenWith();
connect(this->tdLibReceiver, SIGNAL(versionDetected(QString)), this, SLOT(handleVersionDetected(QString)));
connect(this->tdLibReceiver, SIGNAL(authorizationStateChanged(QString)), this, SLOT(handleAuthorizationStateChanged(QString)));
connect(this->tdLibReceiver, SIGNAL(optionUpdated(QString, QVariant)), this, SLOT(handleOptionUpdated(QString, QVariant)));
@ -312,6 +315,11 @@ void TDLibWrapper::controlScreenSaver(const bool &enabled)
}
}
DBusAdaptor *TDLibWrapper::getDBusAdaptor()
{
return this->dbusInterface->getDBusAdaptor();
}
void TDLibWrapper::handleVersionDetected(const QString &version)
{
this->version = version;
@ -566,3 +574,29 @@ void TDLibWrapper::setLogVerbosityLevel()
this->sendRequest(requestObject);
}
void TDLibWrapper::initializeOpenWith()
{
qDebug() << "[TDLibWrapper] Initialize open-with";
QString dbusPathName = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/dbus-1/services";
QDir dbusPath(dbusPathName);
if (!dbusPath.exists()) {
qDebug() << "[TDLibWrapper] Creating D-Bus directory " << dbusPathName;
dbusPath.mkpath(dbusPathName);
}
QString dbusServiceFileName = dbusPathName + "/de.ygriega.fernschreiber.service";
QFile dbusServiceFile(dbusServiceFileName);
if (!dbusServiceFile.exists()) {
qDebug() << "[TDLibWrapper] Creating D-Bus service file at " << dbusServiceFile.fileName();
if (dbusServiceFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream fileOut(&dbusServiceFile);
fileOut.setCodec("UTF-8");
fileOut << QString("[D-BUS Service]").toUtf8() << "\n";
fileOut << QString("Name=de.ygriega.fernschreiber").toUtf8() << "\n";
fileOut << QString("Exec=/usr/bin/invoker -s --type=silica-qt5 /usr/bin/harbour-fernschreiber").toUtf8() << "\n";
fileOut.flush();
dbusServiceFile.close();
}
}
}

View file

@ -26,6 +26,8 @@
#include <QStandardPaths>
#include <td/telegram/td_json_client.h>
#include "tdlibreceiver.h"
#include "dbusadaptor.h"
#include "dbusinterface.h"
class TDLibWrapper : public QObject
{
@ -71,6 +73,8 @@ public:
Q_INVOKABLE void openFileOnDevice(const QString &filePath);
Q_INVOKABLE void controlScreenSaver(const bool &enabled);
DBusAdaptor *getDBusAdaptor();
// Direct TDLib functions
Q_INVOKABLE void sendRequest(const QVariantMap &requestObject);
Q_INVOKABLE void setAuthenticationPhoneNumber(const QString &phoneNumber);
@ -142,6 +146,7 @@ public slots:
private:
void *tdLibClient;
TDLibReceiver *tdLibReceiver;
DBusInterface *dbusInterface;
QString version;
TDLibWrapper::AuthorizationState authorizationState;
TDLibWrapper::ConnectionState connectionState;
@ -157,6 +162,7 @@ private:
void setInitialParameters();
void setEncryptionKey();
void setLogVerbosityLevel();
void initializeOpenWith();
};