Fix notification daemon detection on KDE

Minimize usage of QDBusInterface

Log the whole notification daemon information
This commit is contained in:
Ilya Fedin 2020-03-02 19:55:29 +04:00 committed by John Preston
parent 7aadaca62e
commit fdf16d0aea
2 changed files with 51 additions and 53 deletions

View File

@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "facades.h"
#include <QtCore/QVersionNumber>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusError>
#include <QtDBus/QDBusMetaType>
@ -113,10 +113,7 @@ QVersionNumber ParseSpecificationVersion(
return QVersionNumber();
}
QString GetImageKey() {
const auto specificationVersion = ParseSpecificationVersion(
GetServerInformation());
QString GetImageKey(const QVersionNumber &specificationVersion) {
if (!specificationVersion.isNull()) {
const auto majorVersion = specificationVersion.majorVersion();
const auto minorVersion = specificationVersion.minorVersion();
@ -141,7 +138,6 @@ QString GetImageKey() {
}
NotificationData::NotificationData(
not_null<QDBusInterface*> notificationInterface,
const base::weak_ptr<Manager> &manager,
const QString &title,
const QString &subtitle,
@ -149,10 +145,11 @@ NotificationData::NotificationData(
PeerId peerId,
MsgId msgId,
bool hideReplyButton)
: _notificationInterface(notificationInterface)
: _dbusConnection(QDBusConnection::sessionBus())
, _manager(manager)
, _title(title)
, _imageKey(GetImageKey())
, _imageKey(GetImageKey(ParseSpecificationVersion(
GetServerInformation())))
, _peerId(peerId)
, _msgId(msgId) {
const auto capabilities = GetCapabilities();
@ -172,7 +169,7 @@ NotificationData::NotificationData(
if (capabilities.contains(qsl("actions"))) {
_actions << qsl("default") << QString();
_notificationInterface->connection().connect(
_dbusConnection.connect(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16(),
@ -181,10 +178,11 @@ NotificationData::NotificationData(
SLOT(notificationClicked(uint,QString)));
if (capabilities.contains(qsl("inline-reply")) && !hideReplyButton) {
_actions << qsl("inline-reply")
_actions
<< qsl("inline-reply")
<< tr::lng_notification_reply(tr::now);
_notificationInterface->connection().connect(
_dbusConnection.connect(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16(),
@ -193,7 +191,8 @@ NotificationData::NotificationData(
SLOT(notificationReplied(uint,QString)));
} else {
// icon name according to https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
_actions << qsl("mail-reply-sender")
_actions
<< qsl("mail-reply-sender")
<< tr::lng_notification_reply(tr::now);
}
}
@ -219,7 +218,7 @@ NotificationData::NotificationData(
_hints["category"] = qsl("im.received");
_hints["desktop-entry"] = GetLauncherBasename();
_notificationInterface->connection().connect(
_dbusConnection.connect(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16(),
@ -233,8 +232,13 @@ bool NotificationData::show() {
? GetIconName()
: QString();
const QDBusReply<uint> reply = _notificationInterface->call(
qsl("Notify"),
auto message = QDBusMessage::createMethodCall(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16(),
qsl("Notify"));
message.setArguments({
AppName.utf16(),
uint(0),
iconName,
@ -242,7 +246,11 @@ bool NotificationData::show() {
_body,
_actions,
_hints,
-1);
-1
});
const QDBusReply<uint> reply = _dbusConnection.call(
message);
if (reply.isValid()) {
_notificationId = reply.value();
@ -253,16 +261,18 @@ bool NotificationData::show() {
return reply.isValid();
}
bool NotificationData::close() {
const QDBusReply<void> reply = _notificationInterface->call(
qsl("CloseNotification"),
_notificationId);
void NotificationData::close() {
auto message = QDBusMessage::createMethodCall(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16(),
qsl("CloseNotification"));
if (!reply.isValid()) {
LOG(("Native notification error: %1").arg(reply.error().message()));
}
message.setArguments({
_notificationId
});
return reply.isValid();
_dbusConnection.send(message);
}
void NotificationData::setImage(const QString &imagePath) {
@ -331,7 +341,8 @@ QDBusArgument &operator<<(
QDBusArgument &argument,
const NotificationData::ImageData &imageData) {
argument.beginStructure();
argument << imageData.width
argument
<< imageData.width
<< imageData.height
<< imageData.rowStride
<< imageData.hasAlpha
@ -346,7 +357,8 @@ const QDBusArgument &operator>>(
const QDBusArgument &argument,
NotificationData::ImageData &imageData) {
argument.beginStructure();
argument >> imageData.width
argument
>> imageData.width
>> imageData.height
>> imageData.rowStride
>> imageData.hasAlpha
@ -380,12 +392,7 @@ bool SkipToast() {
bool Supported() {
#ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
static const auto Available = QDBusInterface(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16()
).isValid();
static const auto Available = !GetServerInformation().empty();
return Available;
#else // !TDESKTOP_DISABLE_DBUS_INTEGRATION
return false;
@ -405,27 +412,21 @@ std::unique_ptr<Window::Notifications::Manager> Create(
#ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
Manager::Private::Private(not_null<Manager*> manager, Type type)
: _cachedUserpics(type)
, _manager(manager)
, _notificationInterface(
std::make_shared<QDBusInterface>(
kService.utf16(),
kObjectPath.utf16(),
kInterface.utf16())) {
, _manager(manager) {
qDBusRegisterMetaType<NotificationData::ImageData>();
const auto specificationVersion = ParseSpecificationVersion(
GetServerInformation());
const auto serverInformation = GetServerInformation();
const auto capabilities = GetCapabilities();
if (!specificationVersion.isNull()) {
LOG(("Notification daemon specification version: %1")
.arg(specificationVersion.toString()));
if (!serverInformation.empty()) {
LOG(("Notification daemon product name: %1").arg(serverInformation[0]));
LOG(("Notification daemon vendor name: %1").arg(serverInformation[1]));
LOG(("Notification daemon version: %1").arg(serverInformation[2]));
LOG(("Notification daemon specification version: %1").arg(serverInformation[3]));
}
if (!capabilities.empty()) {
const auto capabilitiesString = capabilities.join(", ");
LOG(("Notification daemon capabilities: %1").arg(capabilitiesString));
if (!capabilities.isEmpty()) {
LOG(("Notification daemon capabilities: %1").arg(capabilities.join(", ")));
}
}
@ -438,7 +439,6 @@ void Manager::Private::showNotification(
bool hideNameAndPhoto,
bool hideReplyButton) {
auto notification = std::make_shared<NotificationData>(
_notificationInterface.get(),
_manager,
title,
subtitle,

View File

@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/weak_ptr.h"
#ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusArgument>
#endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION
@ -28,7 +28,6 @@ class NotificationData : public QObject {
public:
NotificationData(
not_null<QDBusInterface*> notificationInterface,
const base::weak_ptr<Manager> &manager,
const QString &title,
const QString &subtitle,
@ -43,7 +42,7 @@ public:
NotificationData &operator=(NotificationData &&other) = delete;
bool show();
bool close();
void close();
void setImage(const QString &imagePath);
struct ImageData {
@ -54,7 +53,7 @@ public:
};
private:
const not_null<QDBusInterface*> _notificationInterface;
QDBusConnection _dbusConnection;
base::weak_ptr<Manager> _manager;
QString _title;
@ -134,7 +133,6 @@ private:
Window::Notifications::CachedUserpics _cachedUserpics;
base::weak_ptr<Manager> _manager;
std::shared_ptr<QDBusInterface> _notificationInterface;
};
#endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION