Make notification show method async

This commit is contained in:
Ilya Fedin 2021-01-20 02:31:27 +04:00 committed by John Preston
parent 64b12bde55
commit d042963a47

View file

@ -250,7 +250,7 @@ public:
~NotificationData(); ~NotificationData();
bool show(); void show();
void close(); void close();
void setImage(const QString &imagePath); void setImage(const QString &imagePath);
@ -275,6 +275,11 @@ private:
void actionInvoked(uint id, const QString &actionName); void actionInvoked(uint id, const QString &actionName);
void notificationReplied(uint id, const QString &text); void notificationReplied(uint id, const QString &text);
static void notificationShown(
GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static void signalEmitted( static void signalEmitted(
GDBusConnection *connection, GDBusConnection *connection,
const gchar *sender_name, const gchar *sender_name,
@ -443,7 +448,7 @@ NotificationData::~NotificationData() {
} }
} }
bool NotificationData::show() { void NotificationData::show() {
GVariantBuilder actionsBuilder, hintsBuilder; GVariantBuilder actionsBuilder, hintsBuilder;
GError *error = nullptr; GError *error = nullptr;
@ -470,7 +475,7 @@ bool NotificationData::show() {
? GetIconName() ? GetIconName()
: QString(); : QString();
auto reply = g_dbus_connection_call_sync( g_dbus_connection_call(
_dbusConnection, _dbusConnection,
kService.utf8(), kService.utf8(),
kObjectPath.utf8(), kObjectPath.utf8(),
@ -490,19 +495,40 @@ bool NotificationData::show() {
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
kDBusTimeout, kDBusTimeout,
nullptr, nullptr,
notificationShown,
this);
}
void NotificationData::notificationShown(
GObject *source_object,
GAsyncResult *res,
gpointer user_data) {
const auto notificationData = reinterpret_cast<NotificationData*>(
user_data);
if (!notificationData) {
return;
}
GError *error = nullptr;
auto reply = g_dbus_connection_call_finish(
notificationData->_dbusConnection,
res,
&error); &error);
const auto replyValid = !error; if (!error) {
g_variant_get(reply, "(u)", &notificationData->_notificationId);
if (replyValid) {
g_variant_get(reply, "(u)", &_notificationId);
g_variant_unref(reply); g_variant_unref(reply);
} else { } else {
const auto manager = notificationData->_manager;
const auto my = notificationData->_id;
crl::on_main(manager, [=] {
manager->clearNotification(my);
});
LOG(("Native notification error: %1").arg(error->message)); LOG(("Native notification error: %1").arg(error->message));
g_error_free(error); g_error_free(error);
} }
return replyValid;
} }
void NotificationData::close() { void NotificationData::close() {
@ -782,15 +808,7 @@ void Manager::Private::showNotification(
base::flat_map<MsgId, Notification>()).first; base::flat_map<MsgId, Notification>()).first;
} }
i->second.emplace(msgId, notification); i->second.emplace(msgId, notification);
if (!notification->show()) { notification->show();
i = _notifications.find(key);
if (i != _notifications.cend()) {
i->second.remove(msgId);
if (i->second.empty()) {
_notifications.erase(i);
}
}
}
} }
void Manager::Private::clearAll() { void Manager::Private::clearAll() {