notifications: Use _as_uint32 everywhere

This commit is contained in:
Florian Bruhin 2021-03-26 17:29:38 +01:00
parent 1dcd0d5483
commit a0f7c36a54
2 changed files with 18 additions and 13 deletions

View File

@ -370,6 +370,13 @@ class _ServerQuirks:
icon_key: Optional[str] = None
def _as_uint32(x: int) -> QVariant:
"""Convert the given int to an uint32 for DBus."""
variant = QVariant(x)
assert variant.convert(QVariant.UInt)
return variant
class DBusNotificationAdapter(AbstractNotificationAdapter):
"""Sends notifications over DBus."""
@ -516,11 +523,8 @@ class DBusNotificationAdapter(AbstractNotificationAdapter):
replaces_id: Optional[int],
) -> int:
"""Shows a notification over DBus."""
# We can't just pass the int because it won't get sent as the right type.
if replaces_id is None:
replaces_id = 0 # 0 is never a valid ID according to the spec
replaces_id_arg = QVariant(replaces_id)
replaces_id_arg.convert(QVariant.UInt)
actions = []
if 'actions' in self._capabilities:
@ -546,7 +550,7 @@ class DBusNotificationAdapter(AbstractNotificationAdapter):
QDBus.BlockWithGui,
"Notify",
"qutebrowser", # application name
replaces_id_arg, # replaces notification id
_as_uint32(replaces_id), # replaces notification id
"", # icon name/file URL, we use image-data and friends instead.
# Titles don't support markup, so no need to escape them.
qt_notification.title(),
@ -641,9 +645,11 @@ class DBusNotificationAdapter(AbstractNotificationAdapter):
@pyqtSlot(int)
def on_web_closed(self, notification_id: int) -> None:
id_arg = QVariant(notification_id)
id_arg.convert(QVariant.UInt)
self.interface.call(QDBus.NoBlock, "CloseNotification", id_arg)
self.interface.call(
QDBus.NoBlock,
"CloseNotification",
_as_uint32(notification_id),
)
def _fetch_capabilities(self) -> None:
"""Fetch capabilities from the notification server."""

View File

@ -42,10 +42,6 @@ class NotificationProperties:
closed_via_web: bool = False
def _as_uint32(x: int) -> QVariant:
variant = QVariant(x)
assert variant.convert(QVariant.UInt)
return variant
class TestNotificationServer(QObject):
@ -156,7 +152,10 @@ class TestNotificationServer(QObject):
# The 2 here is the notification removal reason ("dismissed by the user")
# it's effectively arbitrary as we don't use that information
message.setArguments([_as_uint32(notification_id), _as_uint32(2)])
message.setArguments([
notification._as_uint32(notification_id),
notification._as_uint32(2),
])
if not self._bus.send(message):
raise OSError("Could not send close notification")
@ -171,7 +170,7 @@ class TestNotificationServer(QObject):
notification.DBusNotificationAdapter.INTERFACE,
"ActionInvoked")
message.setArguments([_as_uint32(notification_id), name])
message.setArguments([notification._as_uint32(notification_id), name])
if not self._bus.send(message):
raise OSError("Could not send action notification")