Fix a crash in NotificationData::show

This commit is contained in:
Ilya Fedin 2021-05-02 10:30:47 +04:00 committed by John Preston
parent 34534a9653
commit d87ea056c6

View file

@ -46,9 +46,7 @@ bool InhibitionSupported = false;
std::optional<ServerInformation> CurrentServerInformation; std::optional<ServerInformation> CurrentServerInformation;
QStringList CurrentCapabilities; QStringList CurrentCapabilities;
void StartServiceAsync( void StartServiceAsync(Fn<void()> callback) {
Fn<void()> callback,
const Glib::RefPtr<Gio::Cancellable> &cancellable = Glib::RefPtr<Gio::Cancellable>()) {
try { try {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -77,8 +75,7 @@ void StartServiceAsync(
} }
crl::on_main([=] { callback(); }); crl::on_main([=] { callback(); });
}, });
cancellable);
return; return;
} catch (...) { } catch (...) {
@ -357,7 +354,7 @@ Glib::ustring GetImageKey(const QVersionNumber &specificationVersion) {
return "icon_data"; return "icon_data";
} }
class NotificationData : public sigc::trackable { class NotificationData : public base::has_weak_ptr {
public: public:
using NotificationId = Window::Notifications::Manager::NotificationId; using NotificationId = Window::Notifications::Manager::NotificationId;
@ -382,7 +379,6 @@ public:
private: private:
Glib::RefPtr<Gio::DBus::Connection> _dbusConnection; Glib::RefPtr<Gio::DBus::Connection> _dbusConnection;
Glib::RefPtr<Gio::Cancellable> _cancellable;
base::weak_ptr<Manager> _manager; base::weak_ptr<Manager> _manager;
Glib::ustring _title; Glib::ustring _title;
@ -397,21 +393,10 @@ private:
uint _notificationClosedSignalId = 0; uint _notificationClosedSignalId = 0;
NotificationId _id; NotificationId _id;
void notificationShown(
const Glib::RefPtr<Gio::AsyncResult> &result);
void notificationClosed(uint id, uint reason); void notificationClosed(uint id, uint reason);
void actionInvoked(uint id, const Glib::ustring &actionName); void actionInvoked(uint id, const Glib::ustring &actionName);
void notificationReplied(uint id, const Glib::ustring &text); void notificationReplied(uint id, const Glib::ustring &text);
void signalEmitted(
const Glib::RefPtr<Gio::DBus::Connection> &connection,
const Glib::ustring &sender_name,
const Glib::ustring &object_path,
const Glib::ustring &interface_name,
const Glib::ustring &signal_name,
const Glib::VariantContainerBase &parameters);
}; };
using Notification = std::shared_ptr<NotificationData>; using Notification = std::shared_ptr<NotificationData>;
@ -423,8 +408,7 @@ NotificationData::NotificationData(
const QString &msg, const QString &msg,
NotificationId id, NotificationId id,
bool hideReplyButton) bool hideReplyButton)
: _cancellable(Gio::Cancellable::create()) : _manager(manager)
, _manager(manager)
, _title(title.toStdString()) , _title(title.toStdString())
, _imageKey(GetImageKey(CurrentServerInformationValue().specVersion)) , _imageKey(GetImageKey(CurrentServerInformationValue().specVersion))
, _id(id) { , _id(id) {
@ -440,192 +424,7 @@ NotificationData::NotificationData(
const auto capabilities = CurrentCapabilities; const auto capabilities = CurrentCapabilities;
if (capabilities.contains(qsl("body-markup"))) { const auto signalEmitted = crl::guard(this, [=](
_body = subtitle.isEmpty()
? msg.toHtmlEscaped().toStdString()
: qsl("<b>%1</b>\n%2").arg(
subtitle.toHtmlEscaped(),
msg.toHtmlEscaped()).toStdString();
} else {
_body = subtitle.isEmpty()
? msg.toStdString()
: qsl("%1\n%2").arg(subtitle, msg).toStdString();
}
if (capabilities.contains("actions")) {
_actions.push_back("default");
_actions.push_back({});
if (!hideReplyButton) {
_actions.push_back("mail-mark-read");
_actions.push_back(
tr::lng_context_mark_read(tr::now).toStdString());
}
if (capabilities.contains("inline-reply") && !hideReplyButton) {
_actions.push_back("inline-reply");
_actions.push_back(
tr::lng_notification_reply(tr::now).toStdString());
_notificationRepliedSignalId = _dbusConnection->signal_subscribe(
sigc::mem_fun(this, &NotificationData::signalEmitted),
std::string(kService),
std::string(kInterface),
"NotificationReplied",
std::string(kObjectPath));
} else {
// icon name according to https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
_actions.push_back("mail-reply-sender");
_actions.push_back(
tr::lng_notification_reply(tr::now).toStdString());
}
_actionInvokedSignalId = _dbusConnection->signal_subscribe(
sigc::mem_fun(this, &NotificationData::signalEmitted),
std::string(kService),
std::string(kInterface),
"ActionInvoked",
std::string(kObjectPath));
}
if (capabilities.contains("action-icons")) {
_hints["action-icons"] = Glib::Variant<bool>::create(true);
}
// suppress system sound if telegram sound activated,
// otherwise use system sound
if (capabilities.contains("sound")) {
if (Core::App().settings().soundNotify()) {
_hints["suppress-sound"] = Glib::Variant<bool>::create(true);
} else {
// sound name according to http://0pointer.de/public/sound-naming-spec.html
_hints["sound-name"] = Glib::Variant<Glib::ustring>::create(
"message-new-instant");
}
}
if (capabilities.contains("x-canonical-append")) {
_hints["x-canonical-append"] = Glib::Variant<Glib::ustring>::create(
"true");
}
_hints["category"] = Glib::Variant<Glib::ustring>::create("im.received");
_hints["desktop-entry"] = Glib::Variant<Glib::ustring>::create(
QGuiApplication::desktopFileName().chopped(8).toStdString());
_notificationClosedSignalId = _dbusConnection->signal_subscribe(
sigc::mem_fun(this, &NotificationData::signalEmitted),
std::string(kService),
std::string(kInterface),
"NotificationClosed",
std::string(kObjectPath));
}
NotificationData::~NotificationData() {
if (_cancellable) {
_cancellable->cancel();
}
if (_dbusConnection) {
if (_actionInvokedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_actionInvokedSignalId);
}
if (_notificationRepliedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_notificationRepliedSignalId);
}
if (_notificationClosedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_notificationClosedSignalId);
}
}
}
void NotificationData::show() {
// a hack for snap's activation restriction
StartServiceAsync([=] {
const auto iconName = _imageKey.empty()
|| _hints.find(_imageKey) == end(_hints)
? Glib::ustring(GetIconName().toStdString())
: Glib::ustring();
_dbusConnection->call(
std::string(kObjectPath),
std::string(kInterface),
"Notify",
base::Platform::MakeGlibVariant(std::tuple{
Glib::ustring(std::string(AppName)),
uint(0),
iconName,
_title,
_body,
_actions,
_hints,
-1,
}),
sigc::mem_fun(this, &NotificationData::notificationShown),
std::string(kService));
}, _cancellable);
}
void NotificationData::notificationShown(
const Glib::RefPtr<Gio::AsyncResult> &result) {
try {
auto reply = _dbusConnection->call_finish(result);
_notificationId = base::Platform::GlibVariantCast<uint>(
reply.get_child(0));
return;
} catch (const Glib::Error &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
const auto manager = _manager;
const auto my = _id;
crl::on_main(manager, [=] {
manager->clearNotification(my);
});
}
void NotificationData::close() {
_dbusConnection->call(
std::string(kObjectPath),
std::string(kInterface),
"CloseNotification",
base::Platform::MakeGlibVariant(std::tuple{
_notificationId,
}),
{},
std::string(kService));
}
void NotificationData::setImage(const QString &imagePath) {
if (_imageKey.empty()) {
return;
}
const auto image = QImage(imagePath)
.convertToFormat(QImage::Format_RGBA8888);
_hints[_imageKey] = base::Platform::MakeGlibVariant(std::tuple{
image.width(),
image.height(),
image.bytesPerLine(),
true,
8,
4,
std::vector<uchar>(
image.constBits(),
image.constBits() + image.sizeInBytes()),
});
}
void NotificationData::signalEmitted(
const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::RefPtr<Gio::DBus::Connection> &connection,
const Glib::ustring &sender_name, const Glib::ustring &sender_name,
const Glib::ustring &object_path, const Glib::ustring &object_path,
@ -664,6 +463,185 @@ void NotificationData::signalEmitted(
LOG(("Native Notification Error: %1").arg( LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what()))); QString::fromStdString(e.what())));
} }
});
if (capabilities.contains(qsl("body-markup"))) {
_body = subtitle.isEmpty()
? msg.toHtmlEscaped().toStdString()
: qsl("<b>%1</b>\n%2").arg(
subtitle.toHtmlEscaped(),
msg.toHtmlEscaped()).toStdString();
} else {
_body = subtitle.isEmpty()
? msg.toStdString()
: qsl("%1\n%2").arg(subtitle, msg).toStdString();
}
if (capabilities.contains("actions")) {
_actions.push_back("default");
_actions.push_back({});
if (!hideReplyButton) {
_actions.push_back("mail-mark-read");
_actions.push_back(
tr::lng_context_mark_read(tr::now).toStdString());
}
if (capabilities.contains("inline-reply") && !hideReplyButton) {
_actions.push_back("inline-reply");
_actions.push_back(
tr::lng_notification_reply(tr::now).toStdString());
_notificationRepliedSignalId = _dbusConnection->signal_subscribe(
signalEmitted,
std::string(kService),
std::string(kInterface),
"NotificationReplied",
std::string(kObjectPath));
} else {
// icon name according to https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
_actions.push_back("mail-reply-sender");
_actions.push_back(
tr::lng_notification_reply(tr::now).toStdString());
}
_actionInvokedSignalId = _dbusConnection->signal_subscribe(
signalEmitted,
std::string(kService),
std::string(kInterface),
"ActionInvoked",
std::string(kObjectPath));
}
if (capabilities.contains("action-icons")) {
_hints["action-icons"] = Glib::Variant<bool>::create(true);
}
// suppress system sound if telegram sound activated,
// otherwise use system sound
if (capabilities.contains("sound")) {
if (Core::App().settings().soundNotify()) {
_hints["suppress-sound"] = Glib::Variant<bool>::create(true);
} else {
// sound name according to http://0pointer.de/public/sound-naming-spec.html
_hints["sound-name"] = Glib::Variant<Glib::ustring>::create(
"message-new-instant");
}
}
if (capabilities.contains("x-canonical-append")) {
_hints["x-canonical-append"] = Glib::Variant<Glib::ustring>::create(
"true");
}
_hints["category"] = Glib::Variant<Glib::ustring>::create("im.received");
_hints["desktop-entry"] = Glib::Variant<Glib::ustring>::create(
QGuiApplication::desktopFileName().chopped(8).toStdString());
_notificationClosedSignalId = _dbusConnection->signal_subscribe(
signalEmitted,
std::string(kService),
std::string(kInterface),
"NotificationClosed",
std::string(kObjectPath));
}
NotificationData::~NotificationData() {
if (_dbusConnection) {
if (_actionInvokedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_actionInvokedSignalId);
}
if (_notificationRepliedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_notificationRepliedSignalId);
}
if (_notificationClosedSignalId != 0) {
_dbusConnection->signal_unsubscribe(_notificationClosedSignalId);
}
}
}
void NotificationData::show() {
// a hack for snap's activation restriction
StartServiceAsync(crl::guard(this, [=] {
const auto iconName = _imageKey.empty()
|| _hints.find(_imageKey) == end(_hints)
? Glib::ustring(GetIconName().toStdString())
: Glib::ustring();
_dbusConnection->call(
std::string(kObjectPath),
std::string(kInterface),
"Notify",
base::Platform::MakeGlibVariant(std::tuple{
Glib::ustring(std::string(AppName)),
uint(0),
iconName,
_title,
_body,
_actions,
_hints,
-1,
}),
crl::guard(this, [=](
const Glib::RefPtr<Gio::AsyncResult> &result) {
try {
auto reply = _dbusConnection->call_finish(result);
_notificationId = base::Platform::GlibVariantCast<uint>(
reply.get_child(0));
return;
} catch (const Glib::Error &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
const auto manager = _manager;
const auto my = _id;
crl::on_main(manager, [=] {
manager->clearNotification(my);
});
}),
std::string(kService));
}));
}
void NotificationData::close() {
_dbusConnection->call(
std::string(kObjectPath),
std::string(kInterface),
"CloseNotification",
base::Platform::MakeGlibVariant(std::tuple{
_notificationId,
}),
{},
std::string(kService));
}
void NotificationData::setImage(const QString &imagePath) {
if (_imageKey.empty()) {
return;
}
const auto image = QImage(imagePath)
.convertToFormat(QImage::Format_RGBA8888);
_hints[_imageKey] = base::Platform::MakeGlibVariant(std::tuple{
image.width(),
image.height(),
image.bytesPerLine(),
true,
8,
4,
std::vector<uchar>(
image.constBits(),
image.constBits() + image.sizeInBytes()),
});
} }
void NotificationData::notificationClosed(uint id, uint reason) { void NotificationData::notificationClosed(uint id, uint reason) {