fluffychat/lib/pages/chat_encryption_settings/chat_encryption_settings.dart

92 lines
2.7 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
2022-12-26 17:47:08 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/chat_encryption_settings/chat_encryption_settings_view.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../key_verification/key_verification_dialog.dart';
2021-04-14 14:02:10 +02:00
class ChatEncryptionSettings extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const ChatEncryptionSettings({Key? key}) : super(key: key);
2021-04-14 14:02:10 +02:00
@override
ChatEncryptionSettingsController createState() =>
ChatEncryptionSettingsController();
}
class ChatEncryptionSettingsController extends State<ChatEncryptionSettings> {
2022-01-29 12:35:03 +01:00
String? get roomId => VRouter.of(context).pathParameters['roomid'];
2021-10-14 18:09:30 +02:00
2022-12-26 17:47:08 +01:00
Room get room => Matrix.of(context).client.getRoomById(roomId!)!;
2021-10-14 18:09:30 +02:00
Future<void> unblock(DeviceKeys key) async {
if (key.blocked) {
await key.setBlocked(false);
}
}
2022-12-26 17:47:08 +01:00
void enableEncryption(_) async {
if (room.encrypted) {
showOkAlertDialog(
context: context,
title: L10n.of(context)!.sorryThatsNotPossible,
message: L10n.of(context)!.disableEncryptionWarning,
);
return;
}
if (room.joinRules == JoinRules.public) {
showOkAlertDialog(
context: context,
title: L10n.of(context)!.sorryThatsNotPossible,
message: L10n.of(context)!.noEncryptionForPublicRooms,
);
return;
}
if (!room.canChangeStateEvent(EventTypes.Encryption)) {
showOkAlertDialog(
context: context,
title: L10n.of(context)!.sorryThatsNotPossible,
message: L10n.of(context)!.noPermission,
);
return;
2021-04-14 14:02:10 +02:00
}
2022-12-26 17:47:08 +01:00
final consent = await showOkCancelAlertDialog(
context: context,
title: L10n.of(context)!.areYouSure,
message: L10n.of(context)!.enableEncryptionWarning,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
);
if (consent != OkCancelResult.ok) return;
await showFutureLoadingDialog(
context: context,
future: () => room.enableEncryption(),
);
}
void startVerification() async {
final req = await room.client.userDeviceKeys[room.directChatMatrixID]!
.startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() {});
}
};
await KeyVerificationDialog(request: req).show(context);
}
void toggleDeviceKey(DeviceKeys key) {
setState(() {
key.setBlocked(!key.blocked);
});
2021-04-14 14:02:10 +02:00
}
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => ChatEncryptionSettingsView(this);
2021-04-14 14:02:10 +02:00
}