cwtch-ui/lib/notification_manager.dart

77 lines
2.4 KiB
Dart
Raw Normal View History

2022-01-25 00:03:46 +00:00
import 'dart:io';
import 'package:desktoasts/desktoasts.dart';
2021-06-24 23:10:45 +00:00
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:path/path.dart' as path;
2022-01-25 00:03:46 +00:00
import 'config.dart';
2021-06-24 23:10:45 +00:00
// NotificationsManager provides a wrapper around platform specific notifications logic.
abstract class NotificationsManager {
Future<void> notify(String message);
}
// NullNotificationsManager ignores all notification requests
class NullNotificationsManager implements NotificationsManager {
@override
Future<void> notify(String message) async {}
}
// LinuxNotificationsManager uses the desktop_notifications package to implement
// the standard dbus-powered linux desktop notifications.
class LinuxNotificationsManager implements NotificationsManager {
int previous_id = 0;
late NotificationsClient client;
LinuxNotificationsManager(NotificationsClient client) {
this.client = client;
}
2021-06-24 23:10:45 +00:00
Future<void> notify(String message) async {
var iconPath = Uri.file(path.join(path.current, "cwtch.png"));
2021-06-30 20:59:52 +00:00
client.notify(message, appName: "cwtch", appIcon: iconPath.toString(), replacesId: this.previous_id).then((Notification value) => previous_id = value.id);
2021-06-24 23:10:45 +00:00
}
}
2022-01-25 00:03:46 +00:00
// Windows Notification Manager uses https://pub.dev/packages/desktoasts to implement
// windows notifications
class WindowsNotificationManager implements NotificationsManager {
late ToastService service;
WindowsNotificationManager() {
service = new ToastService(
appName: 'Cwtch',
companyName: 'Open Privacy Research Society',
productName: 'Cwtch',
);
}
Future<void> notify(String message) async {
Toast toast = new Toast(
type: ToastType.text01,
title: 'Cwtch',
subtitle: message,
);
service.show(toast);
}
}
NotificationsManager newDesktopNotificationsManager() {
2022-01-25 00:03:46 +00:00
if (Platform.isLinux) {
try {
// Test that we can actually access DBUS. Otherwise return a null
// notifications manager...
NotificationsClient client = NotificationsClient();
client.getCapabilities();
return LinuxNotificationsManager(client);
} catch (e) {
EnvironmentConfig.debugLog("Attempted to access DBUS for notifications but failed. Switching off notifications.");
2022-01-25 00:03:46 +00:00
}
} else if (Platform.isWindows) {
try {
return WindowsNotificationManager();
} catch (e) {
EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager");
}
}
return NullNotificationsManager();
2021-06-30 20:59:52 +00:00
}