rustdesk/lib/models/server_model.dart

371 lines
11 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-03-19 23:28:29 +08:00
import 'dart:convert';
2022-03-25 16:34:27 +08:00
import 'package:dash_chat/dash_chat.dart';
2022-03-19 23:28:29 +08:00
import 'package:flutter/material.dart';
import '../common.dart';
import '../pages/server_page.dart';
import 'model.dart';
final _emptyIdShow = translate("connecting_status");
2022-03-19 23:28:29 +08:00
class ServerModel with ChangeNotifier {
Timer? _interval;
bool _isStart = false;
2022-03-19 23:28:29 +08:00
bool _mediaOk = false;
bool _inputOk = false;
bool _audioOk = false;
bool _fileOk = false;
final _serverId = TextEditingController(text: _emptyIdShow);
final _serverPasswd = TextEditingController(text: "");
2022-03-25 16:34:27 +08:00
Map<int,Client> _clients = {};
2022-03-19 23:28:29 +08:00
bool get isStart => _isStart;
2022-03-19 23:28:29 +08:00
bool get mediaOk => _mediaOk;
bool get inputOk => _inputOk;
2022-03-22 21:47:42 +08:00
bool get audioOk => _audioOk;
bool get fileOk => _fileOk;
TextEditingController get serverId => _serverId;
TextEditingController get serverPasswd => _serverPasswd;
2022-03-25 16:34:27 +08:00
Map<int,Client> get clients => _clients;
2022-03-19 23:28:29 +08:00
ServerModel() {
()async{
await Future.delayed(Duration(seconds: 2));
2022-03-22 21:47:42 +08:00
final audioOption = FFI.getByName('option', 'enable-audio');
_audioOk = audioOption.isEmpty; // audio true by default
final fileOption = FFI.getByName('option', 'enable-file-transfer');
_fileOk = fileOption.isEmpty;
Map<String, String> res = Map()
..["name"] = "enable-keyboard"
..["value"] = 'N';
FFI.setByName('option', jsonEncode(res)); // input false by default
notifyListeners();
}();
2022-03-22 21:47:42 +08:00
}
2022-03-22 21:47:42 +08:00
toggleAudio(){
_audioOk = !_audioOk;
Map<String, String> res = Map()
..["name"] = "enable-audio"
..["value"] = _audioOk ? '' : 'N';
FFI.setByName('option', jsonEncode(res));
notifyListeners();
}
toggleFile() {
_fileOk = !_fileOk;
Map<String, String> res = Map()
..["name"] = "enable-file-transfer"
2022-03-22 21:47:42 +08:00
..["value"] = _fileOk ? '' : 'N';
FFI.setByName('option', jsonEncode(res));
notifyListeners();
}
2022-03-22 21:47:42 +08:00
toggleInput(){
if(_inputOk){
2022-03-24 17:58:33 +08:00
FFI.invokeMethod("stop_input");
2022-03-22 21:47:42 +08:00
}else{
showInputWarnAlert();
}
}
toggleService() async {
if(_isStart){
final res = await DialogManager.show<bool>((setState, close) => CustomAlertDialog(
2022-03-23 15:28:21 +08:00
title: Text(translate("Warning")),
content: Text(translate("android_stop_service_tip")),
2022-03-22 21:47:42 +08:00
actions: [
2022-03-23 15:28:21 +08:00
TextButton(onPressed: ()=>close(), child: Text(translate("Cancel"))),
ElevatedButton(onPressed: ()=>close(true), child: Text(translate("OK"))),
2022-03-22 21:47:42 +08:00
],
));
if(res == true){
stopService();
}
}else{
final res = await DialogManager.show<bool>((setState, close) => CustomAlertDialog(
2022-03-23 15:28:21 +08:00
title: Text(translate("Warning")),
content: Text(translate("android_service_will_start_tip")),
2022-03-22 21:47:42 +08:00
actions: [
2022-03-23 15:28:21 +08:00
TextButton(onPressed: ()=>close(), child: Text(translate("Cancel"))),
ElevatedButton(onPressed: ()=>close(true), child: Text(translate("OK"))),
2022-03-22 21:47:42 +08:00
],
));
if(res == true){
startService();
}
}
}
Future<Null> startService() async {
_isStart = true;
notifyListeners();
FFI.setByName("ensure_init_event_queue");
_interval = Timer.periodic(Duration(milliseconds: 30), (timer) {
FFI.ffiModel.update("");
});
await FFI.invokeMethod("init_service");
FFI.setByName("start_service");
getIDPasswd();
}
Future<Null> stopService() async {
_isStart = false;
2022-03-22 21:47:42 +08:00
_interval?.cancel();
_interval = null;
FFI.serverModel.closeAll();
await FFI.invokeMethod("stop_service");
FFI.setByName("stop_service");
notifyListeners();
}
Future<Null> initInput() async {
await FFI.invokeMethod("init_input");
}
getIDPasswd() async {
if (_serverId.text != _emptyIdShow && _serverPasswd.text != "") {
return;
}
var count = 0;
const maxCount = 10;
while (count < maxCount) {
await Future.delayed(Duration(seconds: 1));
final id = FFI.getByName("server_id");
final passwd = FFI.getByName("server_password");
if (id.isEmpty) {
continue;
} else {
_serverId.text = id;
}
if (passwd.isEmpty) {
continue;
} else {
_serverPasswd.text = passwd;
}
debugPrint(
"fetch id & passwd again at $count:id:${_serverId.text},passwd:${_serverPasswd.text}");
count++;
if (_serverId.text != _emptyIdShow && _serverPasswd.text.isNotEmpty) {
break;
}
}
notifyListeners();
}
2022-03-19 23:28:29 +08:00
changeStatue(String name, bool value) {
debugPrint("changeStatue value $value");
2022-03-19 23:28:29 +08:00
switch (name) {
case "media":
_mediaOk = value;
if(value && !_isStart){
startService();
}
2022-03-19 23:28:29 +08:00
break;
case "input":
2022-03-22 21:47:42 +08:00
if(_inputOk!= value){
Map<String, String> res = Map()
..["name"] = "enable-keyboard"
..["value"] = value ? '' : 'N';
FFI.setByName('option', jsonEncode(res));
}
2022-03-19 23:28:29 +08:00
_inputOk = value;
break;
default:
return;
}
notifyListeners();
}
updateClientState() {
var res = FFI.getByName("clients_state");
try {
final List clientsJson = jsonDecode(res);
2022-03-25 16:34:27 +08:00
for (var clientJson in clientsJson){
final client = Client.fromJson(jsonDecode(clientJson));
_clients[client.id] = client;
}
2022-03-19 23:28:29 +08:00
notifyListeners();
} catch (e) {}
}
loginRequest(Map<String, dynamic> evt) {
try {
2022-03-19 23:28:29 +08:00
final client = Client.fromJson(jsonDecode(evt["client"]));
final Map<String, dynamic> response = Map();
2022-03-19 23:28:29 +08:00
response["id"] = client.id;
DialogManager.show((setState, close) => CustomAlertDialog(
2022-03-25 16:34:27 +08:00
title: Text(translate(client.isFileTransfer?"File Connection":"Screen Connection")),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(translate("Do you accept?")),
SizedBox(height: 20),
clientInfo(client),
2022-03-23 15:28:21 +08:00
Text(translate("android_new_connection_tip")),
],
),
actions: [
TextButton(
child: Text(translate("Dismiss")),
onPressed: () {
response["res"] = false;
FFI.setByName("login_res", jsonEncode(response));
close();
}),
ElevatedButton(
child: Text(translate("Accept")),
onPressed: () async {
response["res"] = true;
FFI.setByName("login_res", jsonEncode(response));
if (!client.isFileTransfer) {
bool res = await FFI.invokeMethod(
"start_capture"); // to Android service
debugPrint("_toAndroidStartCapture:$res");
}
2022-03-25 16:34:27 +08:00
_clients[client.id] = client;
notifyListeners();
close();
}),
2022-03-22 21:47:42 +08:00
],onWillPop: ()async=>true,),barrierDismissible: true);
} catch (e) {
2022-03-19 23:28:29 +08:00
debugPrint("loginRequest failed,error:$e");
}
}
2022-03-22 21:47:42 +08:00
void onClientAuthorized(Map<String, dynamic> evt) {
try{
2022-03-25 16:34:27 +08:00
final client = Client.fromJson(jsonDecode(evt['client']));
_clients[client.id] = client;
2022-03-22 21:47:42 +08:00
notifyListeners();
}catch(e){
}
}
2022-03-19 23:28:29 +08:00
void onClientRemove(Map<String, dynamic> evt) {
try {
2022-03-19 23:28:29 +08:00
final id = int.parse(evt['id'] as String);
2022-03-29 23:10:43 +08:00
if(_clients.containsKey(id)){
_clients.remove(id);
}else{
// reset the login dialog, to-do,it will close any showing dialog
DialogManager.reset();
}
2022-03-19 23:28:29 +08:00
notifyListeners();
} catch (e) {
2022-03-19 23:28:29 +08:00
debugPrint("onClientRemove failed,error:$e");
}
}
closeAll() {
2022-03-25 16:34:27 +08:00
_clients.forEach((id,client) {
FFI.setByName("close_conn", id.toString());
2022-03-19 23:28:29 +08:00
});
2022-03-25 16:34:27 +08:00
_clients.clear();
2022-03-19 23:28:29 +08:00
}
}
class Client {
2022-03-22 21:47:42 +08:00
int id = 0; // client connections inner count id
2022-03-19 23:28:29 +08:00
bool authorized = false;
bool isFileTransfer = false;
String name = "";
2022-03-22 21:47:42 +08:00
String peerId = ""; // peer user's id,show at app
bool keyboard = false;
bool clipboard = false;
bool audio = false;
2022-03-25 16:34:27 +08:00
late ChatUser chatUser;
2022-03-19 23:28:29 +08:00
2022-03-22 21:47:42 +08:00
Client(this.authorized, this.isFileTransfer, this.name, this.peerId,this.keyboard,this.clipboard,this.audio);
2022-03-19 23:28:29 +08:00
Client.fromJson(Map<String, dynamic> json) {
id = json['id'];
authorized = json['authorized'];
isFileTransfer = json['is_file_transfer'];
name = json['name'];
peerId = json['peer_id'];
2022-03-22 21:47:42 +08:00
keyboard= json['keyboard'];
clipboard= json['clipboard'];
audio= json['audio'];
2022-03-25 16:34:27 +08:00
chatUser = ChatUser(
uid:peerId,
name: name,
);
2022-03-19 23:28:29 +08:00
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['is_start'] = this.authorized;
data['is_file_transfer'] = this.isFileTransfer;
data['name'] = this.name;
data['peer_id'] = this.peerId;
2022-03-22 21:47:42 +08:00
data['keyboard'] = this.keyboard;
data['clipboard'] = this.clipboard;
data['audio'] = this.audio;
2022-03-19 23:28:29 +08:00
return data;
}
}
2022-03-22 21:47:42 +08:00
showInputWarnAlert() async {
if (globalKey.currentContext == null) return;
DialogManager.reset();
await showDialog<bool>(
context: globalKey.currentContext!,
builder: (alertContext) {
DialogManager.register(alertContext);
return AlertDialog(
2022-03-23 16:28:37 +08:00
title: Text(translate("How to get Android input permission?")),
2022-03-23 15:28:21 +08:00
// content: Text.rich(TextSpan(style: TextStyle(), children: [
// // [已安装的服务] : [Installed Services]
// // 请在接下来的系统设置页面里,找到并进入[Installed Services]页面,将[RustDesk Input]服务开启。
// TextSpan(text: "请在接下来的系统设置页\n进入"),
// TextSpan(text: " [服务] ", style: TextStyle(color: MyTheme.accent)),
// TextSpan(text: "配置页面\n将"),
// TextSpan(
// text: " [RustDesk Input] ",
// style: TextStyle(color: MyTheme.accent)),
// TextSpan(text: "服务开启")
// ])),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(translate(translate("android_input_permission_tip1"))),
SizedBox(height: 10),
Text(translate(translate("android_input_permission_tip2"))),
],
),
2022-03-22 21:47:42 +08:00
actions: [
TextButton(
2022-03-23 15:28:21 +08:00
child: Text(translate("Cancel")),
2022-03-22 21:47:42 +08:00
onPressed: () {
DialogManager.reset();
}),
ElevatedButton(
2022-03-23 15:28:21 +08:00
child: Text(translate("Open System Setting")),
2022-03-22 21:47:42 +08:00
onPressed: () {
FFI.serverModel.initInput();
DialogManager.reset();
}),
],
);
});
DialogManager.drop();
}