2022-02-02 20:18:08 +00:00
|
|
|
import 'dart:async';
|
2022-01-25 00:03:46 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-01-26 16:31:07 +00:00
|
|
|
import 'package:cwtch/main.dart';
|
2022-02-02 20:18:08 +00:00
|
|
|
import 'package:win_toast/win_toast.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;
|
2021-06-28 21:32:26 +00:00
|
|
|
late NotificationsClient client;
|
2022-01-26 16:31:07 +00:00
|
|
|
|
2021-06-28 21:32:26 +00:00
|
|
|
LinuxNotificationsManager(NotificationsClient client) {
|
|
|
|
this.client = client;
|
|
|
|
}
|
2022-01-26 16:31:07 +00:00
|
|
|
|
2021-06-24 23:10:45 +00:00
|
|
|
Future<void> notify(String message) async {
|
2021-06-28 21:32:26 +00:00
|
|
|
var iconPath = Uri.file(path.join(path.current, "cwtch.png"));
|
2022-02-03 21:58:51 +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
|
|
|
}
|
|
|
|
}
|
2021-06-28 21:32:26 +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 {
|
2022-01-26 16:31:07 +00:00
|
|
|
bool active = false;
|
2022-02-02 20:18:08 +00:00
|
|
|
bool initialized = false;
|
2022-01-25 00:03:46 +00:00
|
|
|
|
|
|
|
WindowsNotificationManager() {
|
2022-02-02 20:18:08 +00:00
|
|
|
scheduleMicrotask(() async {
|
2022-02-03 21:58:51 +00:00
|
|
|
initialized = await WinToast.instance().initialize(appName: 'cwtch', productName: 'Cwtch', companyName: 'Open Privacy Research Society');
|
2022-01-26 16:31:07 +00:00
|
|
|
});
|
2022-01-25 00:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> notify(String message) async {
|
2022-02-02 20:18:08 +00:00
|
|
|
if (initialized && !globalAppState.focus) {
|
2022-01-26 16:31:07 +00:00
|
|
|
if (!active) {
|
|
|
|
active = true;
|
2022-02-03 21:58:51 +00:00
|
|
|
WinToast.instance().clear();
|
|
|
|
final toast = await WinToast.instance().showToast(type: ToastType.text01, title: message);
|
2022-02-02 20:18:08 +00:00
|
|
|
toast?.eventStream.listen((event) {
|
|
|
|
if (event is ActivatedEvent) {
|
|
|
|
WinToast.instance().bringWindowToFront();
|
|
|
|
}
|
|
|
|
active = false;
|
|
|
|
});
|
2022-01-26 16:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-25 00:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:32:26 +00:00
|
|
|
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) {
|
2022-02-03 21:58:51 +00:00
|
|
|
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) {
|
2022-02-03 21:58:51 +00:00
|
|
|
EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager");
|
2022-01-25 00:03:46 +00:00
|
|
|
}
|
2021-06-28 21:32:26 +00:00
|
|
|
}
|
|
|
|
return NullNotificationsManager();
|
2021-06-30 20:59:52 +00:00
|
|
|
}
|