2023-02-24 16:22:36 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-02-24 16:22:36 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:local_notifier/local_notifier.dart';
|
2024-04-12 11:23:13 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2023-02-24 16:22:36 +03:00
|
|
|
import 'package:tray_manager/tray_manager.dart';
|
|
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
|
2024-04-12 11:23:13 +03:00
|
|
|
import '../app/logging.dart';
|
2023-02-24 16:22:36 +03:00
|
|
|
import '../app/models.dart';
|
|
|
|
import '../app/shortcuts.dart';
|
|
|
|
import '../app/state.dart';
|
|
|
|
import '../exception/cancellation_exception.dart';
|
|
|
|
import '../oath/models.dart';
|
|
|
|
import '../oath/state.dart';
|
|
|
|
import '../oath/views/utils.dart';
|
|
|
|
import 'oath/state.dart';
|
|
|
|
import 'state.dart';
|
|
|
|
|
2024-04-12 11:23:13 +03:00
|
|
|
final _log = Logger('systray');
|
|
|
|
|
2023-02-24 16:22:36 +03:00
|
|
|
final _favoriteAccounts =
|
2023-05-22 12:20:30 +03:00
|
|
|
Provider.autoDispose<(DevicePath?, List<OathCredential>)>(
|
2023-02-24 16:22:36 +03:00
|
|
|
(ref) {
|
2023-02-24 18:24:25 +03:00
|
|
|
final deviceData = ref.watch(currentDeviceDataProvider).valueOrNull;
|
|
|
|
if (deviceData != null) {
|
2023-02-24 16:22:36 +03:00
|
|
|
final credentials =
|
2023-05-29 10:03:55 +03:00
|
|
|
ref.watch(credentialListProvider(deviceData.node.path));
|
2023-02-24 16:22:36 +03:00
|
|
|
final favorites = ref.watch(favoritesProvider);
|
|
|
|
final listed = credentials
|
|
|
|
?.map((e) => e.credential)
|
|
|
|
.where((c) => favorites.contains(c.id))
|
|
|
|
.toList() ??
|
|
|
|
[];
|
2023-05-22 12:20:30 +03:00
|
|
|
return (deviceData.node.path, listed);
|
2023-02-24 16:22:36 +03:00
|
|
|
}
|
2023-05-22 12:20:30 +03:00
|
|
|
return (null, []);
|
2023-02-24 16:22:36 +03:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
final systrayProvider = Provider.autoDispose((ref) {
|
2023-03-03 11:20:07 +03:00
|
|
|
final systray = _Systray(ref);
|
2023-02-24 16:22:36 +03:00
|
|
|
|
|
|
|
// Keep track of which accounts to show
|
|
|
|
ref.listen(
|
|
|
|
_favoriteAccounts,
|
|
|
|
(_, next) {
|
2023-05-22 12:20:30 +03:00
|
|
|
systray._updateCredentials(next.$1, next.$2);
|
2023-02-24 16:22:36 +03:00
|
|
|
},
|
2023-03-03 11:20:07 +03:00
|
|
|
fireImmediately: true,
|
2023-02-24 16:22:36 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Keep track of the shown/hidden state of the app
|
|
|
|
ref.listen(windowStateProvider.select((value) => value.hidden), (_, hidden) {
|
|
|
|
systray._setHidden(hidden);
|
|
|
|
}, fireImmediately: true);
|
|
|
|
|
2023-03-03 11:20:07 +03:00
|
|
|
// Keep track of the locale of the app
|
|
|
|
ref.listen(l10nProvider, (_, l10n) {
|
|
|
|
systray._updateLocale(l10n);
|
|
|
|
});
|
|
|
|
|
2023-02-24 16:22:36 +03:00
|
|
|
ref.onDispose(systray.dispose);
|
|
|
|
|
|
|
|
return systray;
|
|
|
|
});
|
|
|
|
|
|
|
|
Future<OathCode?> _calculateCode(
|
|
|
|
DevicePath devicePath, OathCredential credential, Ref ref) async {
|
|
|
|
try {
|
|
|
|
return await (ref
|
|
|
|
.read(desktopOathCredentialListProvider(devicePath).notifier))
|
|
|
|
.calculate(credential, headless: true);
|
|
|
|
} on CancellationException catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getIcon() {
|
|
|
|
if (Platform.isMacOS) {
|
2023-08-22 18:25:06 +03:00
|
|
|
return 'resources/icons/systray-template.png';
|
2023-02-24 16:22:36 +03:00
|
|
|
}
|
|
|
|
if (Platform.isWindows) {
|
|
|
|
return 'resources/icons/com.yubico.yubioath.ico';
|
|
|
|
}
|
|
|
|
return 'resources/icons/com.yubico.yubioath-32x32.png';
|
|
|
|
}
|
|
|
|
|
|
|
|
class _Systray extends TrayListener {
|
|
|
|
final Ref _ref;
|
2024-04-12 11:23:13 +03:00
|
|
|
late final String? _cliboardBinary;
|
2023-02-24 16:22:36 +03:00
|
|
|
int _lastClick = 0;
|
2023-03-03 11:20:07 +03:00
|
|
|
AppLocalizations _l10n;
|
2023-02-24 16:22:36 +03:00
|
|
|
DevicePath _devicePath = DevicePath([]);
|
|
|
|
List<OathCredential> _credentials = [];
|
|
|
|
bool _isHidden = false;
|
2024-04-12 11:23:13 +03:00
|
|
|
|
2023-03-03 11:20:07 +03:00
|
|
|
_Systray(this._ref) : _l10n = _ref.read(l10nProvider) {
|
2024-04-12 11:23:13 +03:00
|
|
|
_cliboardBinary =
|
|
|
|
Platform.isLinux ? Platform.environment['_YA_TRAY_CLIPBOARD'] : null;
|
2023-02-24 16:22:36 +03:00
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _init() async {
|
|
|
|
await trayManager.setIcon(_getIcon(), isTemplate: true);
|
|
|
|
await _updateContextMenu();
|
|
|
|
|
|
|
|
// Doesn't seem to work on Linux
|
|
|
|
trayManager.addListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
2023-03-03 11:20:07 +03:00
|
|
|
trayManager.removeListener(this);
|
2023-02-24 16:22:36 +03:00
|
|
|
trayManager.destroy();
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:20:07 +03:00
|
|
|
void _updateLocale(AppLocalizations l10n) async {
|
|
|
|
_l10n = l10n;
|
|
|
|
if (!Platform.isLinux) {
|
|
|
|
await trayManager.setToolTip(l10n.app_name);
|
|
|
|
}
|
|
|
|
await _updateContextMenu();
|
|
|
|
}
|
|
|
|
|
2023-05-22 12:20:30 +03:00
|
|
|
void _updateCredentials(
|
|
|
|
DevicePath? devicePath, List<OathCredential> credentials) {
|
|
|
|
if (!listEquals(_credentials, credentials)) {
|
|
|
|
_devicePath = devicePath ?? _devicePath;
|
|
|
|
_credentials = credentials;
|
2023-02-24 16:22:36 +03:00
|
|
|
_updateContextMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _setHidden(bool hidden) async {
|
|
|
|
_isHidden = hidden;
|
|
|
|
await _updateContextMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-01-23 15:20:04 +03:00
|
|
|
void onTrayIconMouseDown() async {
|
2023-02-24 16:22:36 +03:00
|
|
|
if (Platform.isMacOS) {
|
2024-01-23 15:20:04 +03:00
|
|
|
await _updateContextMenu();
|
|
|
|
await trayManager.popUpContextMenu();
|
2023-02-24 16:22:36 +03:00
|
|
|
} else {
|
|
|
|
final now = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
if (now - _lastClick < 500) {
|
|
|
|
_lastClick = 0;
|
|
|
|
if (_isHidden) {
|
|
|
|
_ref.read(desktopWindowStateProvider.notifier).setWindowHidden(false);
|
|
|
|
} else {
|
2024-01-23 15:20:04 +03:00
|
|
|
await windowManager.focus();
|
2023-02-24 16:22:36 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_lastClick = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-01-23 16:22:05 +03:00
|
|
|
void onTrayIconRightMouseDown() async {
|
|
|
|
await _updateContextMenu();
|
|
|
|
await trayManager.popUpContextMenu();
|
2023-02-24 16:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _updateContextMenu() async {
|
2024-01-23 15:20:04 +03:00
|
|
|
final isVisible = await windowManager.isVisible();
|
2023-02-24 16:22:36 +03:00
|
|
|
await trayManager.setContextMenu(
|
|
|
|
Menu(
|
|
|
|
items: [
|
|
|
|
..._credentials.map(
|
|
|
|
(e) {
|
|
|
|
final label = getTextName(e);
|
|
|
|
return MenuItem(
|
|
|
|
label: label,
|
|
|
|
onClick: (_) async {
|
|
|
|
final code = await _calculateCode(_devicePath, e, _ref);
|
|
|
|
if (code != null) {
|
2024-04-12 11:23:13 +03:00
|
|
|
if (_cliboardBinary != null) {
|
|
|
|
// Copy to clipboard via another executable, which can be needed for Wayland
|
|
|
|
_log.debug(
|
|
|
|
'Using custom binary to copy to clipboard: $_cliboardBinary');
|
|
|
|
final process = await Process.start(_cliboardBinary!, []);
|
|
|
|
process.stdin.writeln(code.value);
|
|
|
|
await process.stdin.close();
|
|
|
|
} else {
|
|
|
|
await _ref
|
|
|
|
.read(clipboardProvider)
|
|
|
|
.setText(code.value, isSensitive: true);
|
|
|
|
}
|
2023-02-24 16:22:36 +03:00
|
|
|
final notification = LocalNotification(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: _l10n.s_code_copied,
|
2023-02-28 17:02:12 +03:00
|
|
|
body: _l10n.p_target_copied_clipboard(label),
|
2023-02-24 16:22:36 +03:00
|
|
|
silent: true,
|
|
|
|
);
|
|
|
|
await notification.show();
|
|
|
|
await Future.delayed(const Duration(seconds: 4));
|
|
|
|
await notification.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (_credentials.isEmpty)
|
|
|
|
MenuItem(
|
2023-03-02 14:45:55 +03:00
|
|
|
label: _l10n.s_no_pinned_accounts,
|
2023-02-24 16:22:36 +03:00
|
|
|
disabled: true,
|
|
|
|
),
|
|
|
|
MenuItem.separator(),
|
|
|
|
MenuItem(
|
2024-01-23 15:20:04 +03:00
|
|
|
label: !isVisible ? _l10n.s_show_window : _l10n.s_hide_window,
|
2023-02-24 16:22:36 +03:00
|
|
|
onClick: (_) {
|
|
|
|
_ref
|
|
|
|
.read(desktopWindowStateProvider.notifier)
|
2024-01-23 15:20:04 +03:00
|
|
|
.setWindowHidden(isVisible);
|
2023-02-24 16:22:36 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
MenuItem.separator(),
|
|
|
|
MenuItem(
|
2023-03-02 14:45:55 +03:00
|
|
|
label: _l10n.s_quit,
|
2023-02-24 16:22:36 +03:00
|
|
|
onClick: (_) {
|
|
|
|
_ref.read(withContextProvider)(
|
|
|
|
(context) async {
|
|
|
|
Actions.invoke(context, const CloseIntent());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|