diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index 3318d6e..8fe5463 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -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 \ diff --git a/qml/harbour-fernschreiber.qml b/qml/harbour-fernschreiber.qml index 1cf695d..6c8d6e4 100644 --- a/qml/harbour-fernschreiber.qml +++ b/qml/harbour-fernschreiber.qml @@ -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(); + } + } } diff --git a/src/dbusadaptor.cpp b/src/dbusadaptor.cpp new file mode 100644 index 0000000..4978061 --- /dev/null +++ b/src/dbusadaptor.cpp @@ -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 . +*/ + +#include "dbusadaptor.h" + +#include + +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); +} diff --git a/src/dbusadaptor.h b/src/dbusadaptor.h new file mode 100644 index 0000000..c3bbbc1 --- /dev/null +++ b/src/dbusadaptor.h @@ -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 . +*/ + +#ifndef DBUSADAPTOR_H +#define DBUSADAPTOR_H + +#include + +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 diff --git a/src/dbusinterface.cpp b/src/dbusinterface.cpp new file mode 100644 index 0000000..ed31609 --- /dev/null +++ b/src/dbusinterface.cpp @@ -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 . +*/ + +#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; +} diff --git a/src/dbusinterface.h b/src/dbusinterface.h new file mode 100644 index 0000000..16281c5 --- /dev/null +++ b/src/dbusinterface.h @@ -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 . +*/ + +#ifndef DBUSINTERFACE_H +#define DBUSINTERFACE_H + +#include +#include + +#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 diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index 5f078af..e91aa74 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -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("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI"); + DBusAdaptor *dBusAdaptor = tdLibWrapper->getDBusAdaptor(); + context->setContextProperty("dBusAdaptor", dBusAdaptor); + ChatListModel chatListModel(tdLibWrapper); context->setContextProperty("chatListModel", &chatListModel); diff --git a/src/notificationmanager.cpp b/src/notificationmanager.cpp index 8fb75a7..33c7bfe 100644 --- a/src/notificationmanager.cpp +++ b/src/notificationmanager.cpp @@ -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) { diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index e4dcd1a..6ea44bf 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -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(); + } + } +} + diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 0fc1cfd..19ae049 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -26,6 +26,8 @@ #include #include #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(); };