diff --git a/lib/main.dart b/lib/main.dart index fddc0ec0..9bf7c380 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -64,7 +64,7 @@ class FlwtchState extends State { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState); cwtch = CwtchGomobile(cwtchNotifier); } else if (Platform.isLinux) { - var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, LinuxNotificationsManager(), globalAppState); + var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(), globalAppState); cwtch = CwtchFfi(cwtchNotifier); } else { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState); diff --git a/lib/notification_manager.dart b/lib/notification_manager.dart index 2f6701ad..ea4e9a78 100644 --- a/lib/notification_manager.dart +++ b/lib/notification_manager.dart @@ -16,10 +16,28 @@ class NullNotificationsManager implements NotificationsManager { // the standard dbus-powered linux desktop notifications. class LinuxNotificationsManager implements NotificationsManager { int previous_id = 0; - final NotificationsClient client = NotificationsClient(); - LinuxNotificationsManager() {} + late NotificationsClient client; + LinuxNotificationsManager(NotificationsClient client) { + this.client = client; + } Future notify(String message) async { - var icon_path = Uri.file(path.join(path.current, "cwtch.png")); - client.notify(message, appName: "cwtch", appIcon: icon_path.toString(), replacesId: this.previous_id).then((Notification value) => previous_id = value.id); + var iconPath = Uri.file(path.join(path.current, "cwtch.png")); + client.notify(message, appName: "cwtch", + appIcon: iconPath.toString(), + replacesId: this.previous_id).then((Notification value) => + previous_id = value.id); } } + +NotificationsManager newDesktopNotificationsManager() { + try { + // Test that we can actually access DBUS. Otherwise return a null + // notifications manager... + NotificationsClient client = NotificationsClient(); + client.getCapabilities(); + return LinuxNotificationsManager(client); + } catch (e) { + print("Attempted to access DBUS for notifications but failed. Switching off notifications."); + } + return NullNotificationsManager(); +} \ No newline at end of file