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-01-25 00:03:46 +00:00
|
|
|
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;
|
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"));
|
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
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
late ToastService service;
|
2022-01-26 16:31:07 +00:00
|
|
|
bool active = false;
|
2022-01-25 00:03:46 +00:00
|
|
|
|
|
|
|
WindowsNotificationManager() {
|
|
|
|
service = new ToastService(
|
2022-01-26 16:31:07 +00:00
|
|
|
appName: 'cwtch',
|
2022-01-25 00:03:46 +00:00
|
|
|
companyName: 'Open Privacy Research Society',
|
|
|
|
productName: 'Cwtch',
|
|
|
|
);
|
2022-01-26 16:31:07 +00:00
|
|
|
|
|
|
|
service.stream.listen((event) {
|
2022-01-26 16:48:35 +00:00
|
|
|
// the user closed the notification of the OS timed it out
|
2022-01-26 16:31:07 +00:00
|
|
|
if (event is ToastDismissed) {
|
|
|
|
active = false;
|
|
|
|
}
|
2022-01-26 16:48:35 +00:00
|
|
|
// clicked
|
2022-01-26 16:31:07 +00:00
|
|
|
if (event is ToastActivated) {
|
2022-01-27 20:58:16 +00:00
|
|
|
active = false;
|
2022-01-26 16:31:07 +00:00
|
|
|
}
|
2022-01-26 16:48:35 +00:00
|
|
|
// if a supplied action was clicked
|
2022-01-26 16:31:07 +00:00
|
|
|
if (event is ToastInteracted) {
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
});
|
2022-01-25 00:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> notify(String message) async {
|
2022-01-26 16:31:07 +00:00
|
|
|
if (!globalAppState.focus) {
|
|
|
|
if (!active) {
|
2022-01-26 16:48:35 +00:00
|
|
|
// One string of bold text on the first line (title),
|
|
|
|
// one string (subtitle) of regular text wrapped across the second and third lines.
|
2022-01-26 16:31:07 +00:00
|
|
|
Toast toast = new Toast(
|
|
|
|
type: ToastType.text02,
|
|
|
|
title: 'Cwtch',
|
|
|
|
subtitle: message,
|
|
|
|
);
|
|
|
|
service.show(toast);
|
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
}
|
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-01-26 20:25:03 +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) {
|
|
|
|
EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager");
|
|
|
|
}
|
2021-06-28 21:32:26 +00:00
|
|
|
}
|
|
|
|
return NullNotificationsManager();
|
2021-06-30 20:59:52 +00:00
|
|
|
}
|