fluffychat/lib/pages/chat/encryption_button.dart

115 lines
3.7 KiB
Dart
Raw Normal View History

2020-02-22 09:03:44 +01:00
import 'dart:async';
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-10-26 18:50:34 +02:00
2021-11-09 21:32:16 +01:00
import '../../widgets/matrix.dart';
2020-02-22 09:03:44 +01:00
class EncryptionButton extends StatefulWidget {
final Room room;
2022-01-29 12:35:03 +01:00
const EncryptionButton(this.room, {Key? key}) : super(key: key);
2020-02-22 09:03:44 +01:00
@override
_EncryptionButtonState createState() => _EncryptionButtonState();
}
class _EncryptionButtonState extends State<EncryptionButton> {
2022-01-29 12:35:03 +01:00
StreamSubscription? _onSyncSub;
2020-02-22 09:03:44 +01:00
2020-02-23 09:31:44 +01:00
void _enableEncryptionAction() async {
if (widget.room.encrypted) {
2021-08-15 13:26:16 +02:00
VRouter.of(context).toSegments(['rooms', widget.room.id, 'encryption']);
2020-02-23 09:31:44 +01:00
return;
}
2021-02-27 10:41:58 +01:00
if (widget.room.joinRules == JoinRules.public) {
await showOkAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-02-27 10:41:58 +01:00
context: context,
2022-01-29 12:35:03 +01:00
okLabel: L10n.of(context)!.ok,
message: L10n.of(context)!.noEncryptionForPublicRooms,
2021-02-27 10:41:58 +01:00
);
2020-02-23 09:31:44 +01:00
return;
}
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2020-11-14 10:08:13 +01:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.enableEncryption,
2020-11-14 10:08:13 +01:00
message: widget.room.client.encryptionEnabled
2022-01-29 12:35:03 +01:00
? L10n.of(context)!.enableEncryptionWarning
: L10n.of(context)!.needPantalaimonWarning,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
2020-02-23 09:31:44 +01:00
) ==
2020-11-14 10:08:13 +01:00
OkCancelResult.ok) {
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => widget.room.enableEncryption(),
2020-02-23 09:31:44 +01:00
);
// we want to enable the lock icon
2022-01-29 12:35:03 +01:00
setState(() {});
2020-02-23 09:31:44 +01:00
}
}
2020-02-22 09:03:44 +01:00
@override
void dispose() {
_onSyncSub?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget.room.encrypted) {
_onSyncSub ??= Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.deviceLists != null)
2022-01-29 12:35:03 +01:00
.listen((s) => setState(() {}));
}
2020-10-31 10:39:38 +01:00
return FutureBuilder<List<User>>(
future:
widget.room.encrypted ? widget.room.requestParticipants() : null,
2020-02-22 09:03:44 +01:00
builder: (BuildContext context, snapshot) {
2022-01-29 12:35:03 +01:00
Color? color;
2020-02-22 09:03:44 +01:00
if (widget.room.encrypted && snapshot.hasData) {
2022-01-29 12:35:03 +01:00
final users = snapshot.data!;
2020-10-31 10:39:38 +01:00
users.removeWhere((u) =>
!{Membership.invite, Membership.join}.contains(u.membership) ||
!widget.room.client.userDeviceKeys.containsKey(u.id));
var allUsersValid = true;
var oneUserInvalid = false;
for (final u in users) {
2022-01-29 12:35:03 +01:00
final status = widget.room.client.userDeviceKeys[u.id]!.verified;
2020-10-31 10:39:38 +01:00
if (status != UserVerifiedStatus.verified) {
allUsersValid = false;
}
if (status == UserVerifiedStatus.unknownDevice) {
oneUserInvalid = true;
}
2020-02-22 09:03:44 +01:00
}
if (oneUserInvalid) color = Colors.red;
if (!allUsersValid) color = Colors.orange;
2020-02-22 09:03:44 +01:00
} else if (!widget.room.encrypted &&
widget.room.joinRules != JoinRules.public) {
2021-10-10 11:40:08 +02:00
color = Colors.red;
2020-02-22 09:03:44 +01:00
}
return IconButton(
2021-02-13 13:27:44 +01:00
tooltip: widget.room.encrypted
2022-01-29 12:35:03 +01:00
? L10n.of(context)!.encrypted
: L10n.of(context)!.encryptionNotEnabled,
2020-12-06 10:31:35 +01:00
icon: Icon(
widget.room.encrypted
? Icons.lock_outlined
: Icons.lock_open_outlined,
size: 20,
color: color),
2020-02-23 09:31:44 +01:00
onPressed: _enableEncryptionAction,
2020-02-22 09:03:44 +01:00
);
});
}
}