dialog.dart 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:bot_toast/bot_toast.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter/widgets.dart';
  7. import 'package:flutter_hbb/common/shared_state.dart';
  8. import 'package:flutter_hbb/common/widgets/setting_widgets.dart';
  9. import 'package:flutter_hbb/consts.dart';
  10. import 'package:flutter_hbb/models/peer_model.dart';
  11. import 'package:flutter_hbb/models/peer_tab_model.dart';
  12. import 'package:flutter_hbb/models/state_model.dart';
  13. import 'package:get/get.dart';
  14. import 'package:qr_flutter/qr_flutter.dart';
  15. import '../../common.dart';
  16. import '../../models/model.dart';
  17. import '../../models/platform_model.dart';
  18. import 'address_book.dart';
  19. void clientClose(SessionID sessionId, OverlayDialogManager dialogManager) {
  20. msgBox(sessionId, 'info', 'Close', 'Are you sure to close the connection?',
  21. '', dialogManager);
  22. }
  23. abstract class ValidationRule {
  24. String get name;
  25. bool validate(String value);
  26. }
  27. class LengthRangeValidationRule extends ValidationRule {
  28. final int _min;
  29. final int _max;
  30. LengthRangeValidationRule(this._min, this._max);
  31. @override
  32. String get name => translate('length %min% to %max%')
  33. .replaceAll('%min%', _min.toString())
  34. .replaceAll('%max%', _max.toString());
  35. @override
  36. bool validate(String value) {
  37. return value.length >= _min && value.length <= _max;
  38. }
  39. }
  40. class RegexValidationRule extends ValidationRule {
  41. final String _name;
  42. final RegExp _regex;
  43. RegexValidationRule(this._name, this._regex);
  44. @override
  45. String get name => translate(_name);
  46. @override
  47. bool validate(String value) {
  48. return value.isNotEmpty ? value.contains(_regex) : false;
  49. }
  50. }
  51. void changeIdDialog() {
  52. var newId = "";
  53. var msg = "";
  54. var isInProgress = false;
  55. TextEditingController controller = TextEditingController();
  56. final RxString rxId = controller.text.trim().obs;
  57. final rules = [
  58. RegexValidationRule('starts with a letter', RegExp(r'^[a-zA-Z]')),
  59. LengthRangeValidationRule(6, 16),
  60. RegexValidationRule('allowed characters', RegExp(r'^\w*$'))
  61. ];
  62. gFFI.dialogManager.show((setState, close, context) {
  63. submit() async {
  64. debugPrint("onSubmit");
  65. newId = controller.text.trim();
  66. final Iterable violations = rules.where((r) => !r.validate(newId));
  67. if (violations.isNotEmpty) {
  68. setState(() {
  69. msg = (isDesktop || isWebDesktop)
  70. ? '${translate('Prompt')}: ${violations.map((r) => r.name).join(', ')}'
  71. : violations.map((r) => r.name).join(', ');
  72. });
  73. return;
  74. }
  75. setState(() {
  76. msg = "";
  77. isInProgress = true;
  78. bind.mainChangeId(newId: newId);
  79. });
  80. var status = await bind.mainGetAsyncStatus();
  81. while (status == " ") {
  82. await Future.delayed(const Duration(milliseconds: 100));
  83. status = await bind.mainGetAsyncStatus();
  84. }
  85. if (status.isEmpty) {
  86. // ok
  87. close();
  88. return;
  89. }
  90. setState(() {
  91. isInProgress = false;
  92. msg = (isDesktop || isWebDesktop)
  93. ? '${translate('Prompt')}: ${translate(status)}'
  94. : translate(status);
  95. });
  96. }
  97. return CustomAlertDialog(
  98. title: Text(translate("Change ID")),
  99. content: Column(
  100. crossAxisAlignment: CrossAxisAlignment.start,
  101. children: [
  102. Text(translate("id_change_tip")),
  103. const SizedBox(
  104. height: 12.0,
  105. ),
  106. TextField(
  107. decoration: InputDecoration(
  108. labelText: translate('Your new ID'),
  109. errorText: msg.isEmpty ? null : translate(msg),
  110. suffixText: '${rxId.value.length}/16',
  111. suffixStyle: const TextStyle(fontSize: 12, color: Colors.grey)),
  112. inputFormatters: [
  113. LengthLimitingTextInputFormatter(16),
  114. // FilteringTextInputFormatter(RegExp(r"[a-zA-z][a-zA-z0-9\_]*"), allow: true)
  115. ],
  116. controller: controller,
  117. autofocus: true,
  118. onChanged: (value) {
  119. setState(() {
  120. rxId.value = value.trim();
  121. msg = '';
  122. });
  123. },
  124. ),
  125. const SizedBox(
  126. height: 8.0,
  127. ),
  128. (isDesktop || isWebDesktop)
  129. ? Obx(() => Wrap(
  130. runSpacing: 8,
  131. spacing: 4,
  132. children: rules.map((e) {
  133. var checked = e.validate(rxId.value);
  134. return Chip(
  135. label: Text(
  136. e.name,
  137. style: TextStyle(
  138. color: checked
  139. ? const Color(0xFF0A9471)
  140. : Color.fromARGB(255, 198, 86, 157)),
  141. ),
  142. backgroundColor: checked
  143. ? const Color(0xFFD0F7ED)
  144. : Color.fromARGB(255, 247, 205, 232));
  145. }).toList(),
  146. )).marginOnly(bottom: 8)
  147. : SizedBox.shrink(),
  148. // NOT use Offstage to wrap LinearProgressIndicator
  149. if (isInProgress) const LinearProgressIndicator(),
  150. ],
  151. ),
  152. actions: [
  153. dialogButton("Cancel", onPressed: close, isOutline: true),
  154. dialogButton("OK", onPressed: submit),
  155. ],
  156. onSubmit: submit,
  157. onCancel: close,
  158. );
  159. });
  160. }
  161. void changeWhiteList({Function()? callback}) async {
  162. final curWhiteList = await bind.mainGetOption(key: kOptionWhitelist);
  163. var newWhiteListField = curWhiteList == defaultOptionWhitelist
  164. ? ''
  165. : curWhiteList.split(',').join('\n');
  166. var controller = TextEditingController(text: newWhiteListField);
  167. var msg = "";
  168. var isInProgress = false;
  169. final isOptFixed = isOptionFixed(kOptionWhitelist);
  170. gFFI.dialogManager.show((setState, close, context) {
  171. return CustomAlertDialog(
  172. title: Text(translate("IP Whitelisting")),
  173. content: Column(
  174. crossAxisAlignment: CrossAxisAlignment.start,
  175. children: [
  176. Text(translate("whitelist_sep")),
  177. const SizedBox(
  178. height: 8.0,
  179. ),
  180. Row(
  181. children: [
  182. Expanded(
  183. child: TextField(
  184. maxLines: null,
  185. decoration: InputDecoration(
  186. errorText: msg.isEmpty ? null : translate(msg),
  187. ),
  188. controller: controller,
  189. enabled: !isOptFixed,
  190. autofocus: true),
  191. ),
  192. ],
  193. ),
  194. const SizedBox(
  195. height: 4.0,
  196. ),
  197. // NOT use Offstage to wrap LinearProgressIndicator
  198. if (isInProgress) const LinearProgressIndicator(),
  199. ],
  200. ),
  201. actions: [
  202. dialogButton("Cancel", onPressed: close, isOutline: true),
  203. if (!isOptFixed)
  204. dialogButton("Clear", onPressed: () async {
  205. await bind.mainSetOption(
  206. key: kOptionWhitelist, value: defaultOptionWhitelist);
  207. callback?.call();
  208. close();
  209. }, isOutline: true),
  210. if (!isOptFixed)
  211. dialogButton(
  212. "OK",
  213. onPressed: () async {
  214. setState(() {
  215. msg = "";
  216. isInProgress = true;
  217. });
  218. newWhiteListField = controller.text.trim();
  219. var newWhiteList = "";
  220. if (newWhiteListField.isEmpty) {
  221. // pass
  222. } else {
  223. final ips =
  224. newWhiteListField.trim().split(RegExp(r"[\s,;\n]+"));
  225. // test ip
  226. final ipMatch = RegExp(
  227. r"^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)(\/([1-9]|[1-2][0-9]|3[0-2])){0,1}$");
  228. final ipv6Match = RegExp(
  229. r"^(((?:[0-9A-Fa-f]{1,4}))*((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))*((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7})(\/([1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){0,1}$");
  230. for (final ip in ips) {
  231. if (!ipMatch.hasMatch(ip) && !ipv6Match.hasMatch(ip)) {
  232. msg = "${translate("Invalid IP")} $ip";
  233. setState(() {
  234. isInProgress = false;
  235. });
  236. return;
  237. }
  238. }
  239. newWhiteList = ips.join(',');
  240. }
  241. if (newWhiteList.trim().isEmpty) {
  242. newWhiteList = defaultOptionWhitelist;
  243. }
  244. await bind.mainSetOption(
  245. key: kOptionWhitelist, value: newWhiteList);
  246. callback?.call();
  247. close();
  248. },
  249. ),
  250. ],
  251. onCancel: close,
  252. );
  253. });
  254. }
  255. Future<String> changeDirectAccessPort(
  256. String currentIP, String currentPort) async {
  257. final controller = TextEditingController(text: currentPort);
  258. await gFFI.dialogManager.show((setState, close, context) {
  259. return CustomAlertDialog(
  260. title: Text(translate("Change Local Port")),
  261. content: Column(
  262. crossAxisAlignment: CrossAxisAlignment.start,
  263. children: [
  264. const SizedBox(height: 8.0),
  265. Row(
  266. children: [
  267. Expanded(
  268. child: TextField(
  269. maxLines: null,
  270. keyboardType: TextInputType.number,
  271. decoration: InputDecoration(
  272. hintText: '21118',
  273. isCollapsed: true,
  274. prefix: Text('$currentIP : '),
  275. suffix: IconButton(
  276. padding: EdgeInsets.zero,
  277. icon: const Icon(Icons.clear, size: 16),
  278. onPressed: () => controller.clear())),
  279. inputFormatters: [
  280. FilteringTextInputFormatter.allow(RegExp(
  281. r'^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$')),
  282. ],
  283. controller: controller,
  284. autofocus: true),
  285. ),
  286. ],
  287. ),
  288. ],
  289. ),
  290. actions: [
  291. dialogButton("Cancel", onPressed: close, isOutline: true),
  292. dialogButton("OK", onPressed: () async {
  293. await bind.mainSetOption(
  294. key: kOptionDirectAccessPort, value: controller.text);
  295. close();
  296. }),
  297. ],
  298. onCancel: close,
  299. );
  300. });
  301. return controller.text;
  302. }
  303. Future<String> changeAutoDisconnectTimeout(String old) async {
  304. final controller = TextEditingController(text: old);
  305. await gFFI.dialogManager.show((setState, close, context) {
  306. return CustomAlertDialog(
  307. title: Text(translate("Timeout in minutes")),
  308. content: Column(
  309. crossAxisAlignment: CrossAxisAlignment.start,
  310. children: [
  311. const SizedBox(height: 8.0),
  312. Row(
  313. children: [
  314. Expanded(
  315. child: TextField(
  316. maxLines: null,
  317. keyboardType: TextInputType.number,
  318. decoration: InputDecoration(
  319. hintText: '10',
  320. isCollapsed: true,
  321. suffix: IconButton(
  322. padding: EdgeInsets.zero,
  323. icon: const Icon(Icons.clear, size: 16),
  324. onPressed: () => controller.clear())),
  325. inputFormatters: [
  326. FilteringTextInputFormatter.allow(RegExp(
  327. r'^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$')),
  328. ],
  329. controller: controller,
  330. autofocus: true),
  331. ),
  332. ],
  333. ),
  334. ],
  335. ),
  336. actions: [
  337. dialogButton("Cancel", onPressed: close, isOutline: true),
  338. dialogButton("OK", onPressed: () async {
  339. await bind.mainSetOption(
  340. key: kOptionAutoDisconnectTimeout, value: controller.text);
  341. close();
  342. }),
  343. ],
  344. onCancel: close,
  345. );
  346. });
  347. return controller.text;
  348. }
  349. class DialogTextField extends StatelessWidget {
  350. final String title;
  351. final String? hintText;
  352. final bool obscureText;
  353. final String? errorText;
  354. final String? helperText;
  355. final Widget? prefixIcon;
  356. final Widget? suffixIcon;
  357. final TextEditingController controller;
  358. final FocusNode? focusNode;
  359. final TextInputType? keyboardType;
  360. final List<TextInputFormatter>? inputFormatters;
  361. final int? maxLength;
  362. static const kUsernameTitle = 'Username';
  363. static const kUsernameIcon = Icon(Icons.account_circle_outlined);
  364. static const kPasswordTitle = 'Password';
  365. static const kPasswordIcon = Icon(Icons.lock_outline);
  366. DialogTextField(
  367. {Key? key,
  368. this.focusNode,
  369. this.obscureText = false,
  370. this.errorText,
  371. this.helperText,
  372. this.prefixIcon,
  373. this.suffixIcon,
  374. this.hintText,
  375. this.keyboardType,
  376. this.inputFormatters,
  377. this.maxLength,
  378. required this.title,
  379. required this.controller})
  380. : super(key: key);
  381. @override
  382. Widget build(BuildContext context) {
  383. return Row(
  384. children: [
  385. Expanded(
  386. child: TextField(
  387. decoration: InputDecoration(
  388. labelText: title,
  389. hintText: hintText,
  390. prefixIcon: prefixIcon,
  391. suffixIcon: suffixIcon,
  392. helperText: helperText,
  393. helperMaxLines: 8,
  394. errorText: errorText,
  395. errorMaxLines: 8,
  396. ),
  397. controller: controller,
  398. focusNode: focusNode,
  399. autofocus: true,
  400. obscureText: obscureText,
  401. keyboardType: keyboardType,
  402. inputFormatters: inputFormatters,
  403. maxLength: maxLength,
  404. ),
  405. ),
  406. ],
  407. ).paddingSymmetric(vertical: 4.0);
  408. }
  409. }
  410. abstract class ValidationField extends StatelessWidget {
  411. ValidationField({Key? key}) : super(key: key);
  412. String? validate();
  413. bool get isReady;
  414. }
  415. class Dialog2FaField extends ValidationField {
  416. Dialog2FaField({
  417. Key? key,
  418. required this.controller,
  419. this.autoFocus = true,
  420. this.reRequestFocus = false,
  421. this.title,
  422. this.hintText,
  423. this.errorText,
  424. this.readyCallback,
  425. this.onChanged,
  426. }) : super(key: key);
  427. final TextEditingController controller;
  428. final bool autoFocus;
  429. final bool reRequestFocus;
  430. final String? title;
  431. final String? hintText;
  432. final String? errorText;
  433. final VoidCallback? readyCallback;
  434. final VoidCallback? onChanged;
  435. final errMsg = translate('2FA code must be 6 digits.');
  436. @override
  437. Widget build(BuildContext context) {
  438. return DialogVerificationCodeField(
  439. title: title ?? translate('2FA code'),
  440. controller: controller,
  441. errorText: errorText,
  442. autoFocus: autoFocus,
  443. reRequestFocus: reRequestFocus,
  444. hintText: hintText,
  445. readyCallback: readyCallback,
  446. onChanged: _onChanged,
  447. keyboardType: TextInputType.number,
  448. inputFormatters: [
  449. FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
  450. ],
  451. );
  452. }
  453. String get text => controller.text;
  454. bool get isAllDigits => text.codeUnits.every((e) => e >= 48 && e <= 57);
  455. @override
  456. bool get isReady => text.length == 6 && isAllDigits;
  457. @override
  458. String? validate() => isReady ? null : errMsg;
  459. _onChanged(StateSetter setState, SimpleWrapper<String?> errText) {
  460. onChanged?.call();
  461. if (text.length > 6) {
  462. setState(() => errText.value = errMsg);
  463. return;
  464. }
  465. if (!isAllDigits) {
  466. setState(() => errText.value = errMsg);
  467. return;
  468. }
  469. if (isReady) {
  470. readyCallback?.call();
  471. return;
  472. }
  473. if (errText.value != null) {
  474. setState(() => errText.value = null);
  475. }
  476. }
  477. }
  478. class DialogEmailCodeField extends ValidationField {
  479. DialogEmailCodeField({
  480. Key? key,
  481. required this.controller,
  482. this.autoFocus = true,
  483. this.reRequestFocus = false,
  484. this.hintText,
  485. this.errorText,
  486. this.readyCallback,
  487. this.onChanged,
  488. }) : super(key: key);
  489. final TextEditingController controller;
  490. final bool autoFocus;
  491. final bool reRequestFocus;
  492. final String? hintText;
  493. final String? errorText;
  494. final VoidCallback? readyCallback;
  495. final VoidCallback? onChanged;
  496. final errMsg = translate('Email verification code must be 6 characters.');
  497. @override
  498. Widget build(BuildContext context) {
  499. return DialogVerificationCodeField(
  500. title: translate('Verification code'),
  501. controller: controller,
  502. errorText: errorText,
  503. autoFocus: autoFocus,
  504. reRequestFocus: reRequestFocus,
  505. hintText: hintText,
  506. readyCallback: readyCallback,
  507. helperText: translate('verification_tip'),
  508. onChanged: _onChanged,
  509. keyboardType: TextInputType.visiblePassword,
  510. );
  511. }
  512. String get text => controller.text;
  513. @override
  514. bool get isReady => text.length == 6;
  515. @override
  516. String? validate() => isReady ? null : errMsg;
  517. _onChanged(StateSetter setState, SimpleWrapper<String?> errText) {
  518. onChanged?.call();
  519. if (text.length > 6) {
  520. setState(() => errText.value = errMsg);
  521. return;
  522. }
  523. if (isReady) {
  524. readyCallback?.call();
  525. return;
  526. }
  527. if (errText.value != null) {
  528. setState(() => errText.value = null);
  529. }
  530. }
  531. }
  532. class DialogVerificationCodeField extends StatefulWidget {
  533. DialogVerificationCodeField({
  534. Key? key,
  535. required this.controller,
  536. required this.title,
  537. this.autoFocus = true,
  538. this.reRequestFocus = false,
  539. this.helperText,
  540. this.hintText,
  541. this.errorText,
  542. this.textLength,
  543. this.readyCallback,
  544. this.onChanged,
  545. this.keyboardType,
  546. this.inputFormatters,
  547. }) : super(key: key);
  548. final TextEditingController controller;
  549. final bool autoFocus;
  550. final bool reRequestFocus;
  551. final String title;
  552. final String? helperText;
  553. final String? hintText;
  554. final String? errorText;
  555. final int? textLength;
  556. final VoidCallback? readyCallback;
  557. final Function(StateSetter setState, SimpleWrapper<String?> errText)?
  558. onChanged;
  559. final TextInputType? keyboardType;
  560. final List<TextInputFormatter>? inputFormatters;
  561. @override
  562. State<DialogVerificationCodeField> createState() =>
  563. _DialogVerificationCodeField();
  564. }
  565. class _DialogVerificationCodeField extends State<DialogVerificationCodeField> {
  566. final _focusNode = FocusNode();
  567. Timer? _timer;
  568. Timer? _timerReRequestFocus;
  569. SimpleWrapper<String?> errorText = SimpleWrapper(null);
  570. String _preText = '';
  571. @override
  572. void initState() {
  573. super.initState();
  574. if (widget.autoFocus) {
  575. _timer =
  576. Timer(Duration(milliseconds: 50), () => _focusNode.requestFocus());
  577. if (widget.onChanged != null) {
  578. widget.controller.addListener(() {
  579. final text = widget.controller.text.trim();
  580. if (text == _preText) return;
  581. widget.onChanged!(setState, errorText);
  582. _preText = text;
  583. });
  584. }
  585. }
  586. // software secure keyboard will take the focus since flutter 3.13
  587. // request focus again when android account password obtain focus
  588. if (isAndroid && widget.reRequestFocus) {
  589. _focusNode.addListener(() {
  590. if (_focusNode.hasFocus) {
  591. _timerReRequestFocus?.cancel();
  592. _timerReRequestFocus = Timer(
  593. Duration(milliseconds: 100), () => _focusNode.requestFocus());
  594. }
  595. });
  596. }
  597. }
  598. @override
  599. void dispose() {
  600. _timer?.cancel();
  601. _timerReRequestFocus?.cancel();
  602. _focusNode.unfocus();
  603. _focusNode.dispose();
  604. super.dispose();
  605. }
  606. @override
  607. Widget build(BuildContext context) {
  608. return DialogTextField(
  609. title: widget.title,
  610. controller: widget.controller,
  611. errorText: widget.errorText ?? errorText.value,
  612. focusNode: _focusNode,
  613. helperText: widget.helperText,
  614. keyboardType: widget.keyboardType,
  615. inputFormatters: widget.inputFormatters,
  616. );
  617. }
  618. }
  619. class PasswordWidget extends StatefulWidget {
  620. PasswordWidget({
  621. Key? key,
  622. required this.controller,
  623. this.autoFocus = true,
  624. this.reRequestFocus = false,
  625. this.hintText,
  626. this.errorText,
  627. this.title,
  628. this.maxLength,
  629. }) : super(key: key);
  630. final TextEditingController controller;
  631. final bool autoFocus;
  632. final bool reRequestFocus;
  633. final String? hintText;
  634. final String? errorText;
  635. final String? title;
  636. final int? maxLength;
  637. @override
  638. State<PasswordWidget> createState() => _PasswordWidgetState();
  639. }
  640. class _PasswordWidgetState extends State<PasswordWidget> {
  641. bool _passwordVisible = false;
  642. final _focusNode = FocusNode();
  643. Timer? _timer;
  644. Timer? _timerReRequestFocus;
  645. @override
  646. void initState() {
  647. super.initState();
  648. if (widget.autoFocus) {
  649. _timer =
  650. Timer(Duration(milliseconds: 50), () => _focusNode.requestFocus());
  651. }
  652. // software secure keyboard will take the focus since flutter 3.13
  653. // request focus again when android account password obtain focus
  654. if (isAndroid && widget.reRequestFocus) {
  655. _focusNode.addListener(() {
  656. if (_focusNode.hasFocus) {
  657. _timerReRequestFocus?.cancel();
  658. _timerReRequestFocus = Timer(
  659. Duration(milliseconds: 100), () => _focusNode.requestFocus());
  660. }
  661. });
  662. }
  663. }
  664. @override
  665. void dispose() {
  666. _timer?.cancel();
  667. _timerReRequestFocus?.cancel();
  668. _focusNode.unfocus();
  669. _focusNode.dispose();
  670. super.dispose();
  671. }
  672. @override
  673. Widget build(BuildContext context) {
  674. return DialogTextField(
  675. title: translate(widget.title ?? DialogTextField.kPasswordTitle),
  676. hintText: translate(widget.hintText ?? 'Enter your password'),
  677. controller: widget.controller,
  678. prefixIcon: DialogTextField.kPasswordIcon,
  679. suffixIcon: IconButton(
  680. icon: Icon(
  681. // Based on passwordVisible state choose the icon
  682. _passwordVisible ? Icons.visibility : Icons.visibility_off,
  683. color: MyTheme.lightTheme.primaryColor),
  684. onPressed: () {
  685. // Update the state i.e. toggle the state of passwordVisible variable
  686. setState(() {
  687. _passwordVisible = !_passwordVisible;
  688. });
  689. },
  690. ),
  691. obscureText: !_passwordVisible,
  692. errorText: widget.errorText,
  693. focusNode: _focusNode,
  694. maxLength: widget.maxLength,
  695. );
  696. }
  697. }
  698. void wrongPasswordDialog(SessionID sessionId,
  699. OverlayDialogManager dialogManager, type, title, text) {
  700. dialogManager.dismissAll();
  701. dialogManager.show((setState, close, context) {
  702. cancel() {
  703. close();
  704. closeConnection();
  705. }
  706. submit() {
  707. enterPasswordDialog(sessionId, dialogManager);
  708. }
  709. return CustomAlertDialog(
  710. title: null,
  711. content: msgboxContent(type, title, text),
  712. onSubmit: submit,
  713. onCancel: cancel,
  714. actions: [
  715. dialogButton(
  716. 'Cancel',
  717. onPressed: cancel,
  718. isOutline: true,
  719. ),
  720. dialogButton(
  721. 'Retry',
  722. onPressed: submit,
  723. ),
  724. ]);
  725. });
  726. }
  727. void enterPasswordDialog(
  728. SessionID sessionId, OverlayDialogManager dialogManager) async {
  729. await _connectDialog(
  730. sessionId,
  731. dialogManager,
  732. passwordController: TextEditingController(),
  733. );
  734. }
  735. void enterUserLoginDialog(
  736. SessionID sessionId, OverlayDialogManager dialogManager) async {
  737. await _connectDialog(
  738. sessionId,
  739. dialogManager,
  740. osUsernameController: TextEditingController(),
  741. osPasswordController: TextEditingController(),
  742. );
  743. }
  744. void enterUserLoginAndPasswordDialog(
  745. SessionID sessionId, OverlayDialogManager dialogManager) async {
  746. await _connectDialog(
  747. sessionId,
  748. dialogManager,
  749. osUsernameController: TextEditingController(),
  750. osPasswordController: TextEditingController(),
  751. passwordController: TextEditingController(),
  752. );
  753. }
  754. _connectDialog(
  755. SessionID sessionId,
  756. OverlayDialogManager dialogManager, {
  757. TextEditingController? osUsernameController,
  758. TextEditingController? osPasswordController,
  759. TextEditingController? passwordController,
  760. }) async {
  761. var rememberPassword = false;
  762. if (passwordController != null) {
  763. rememberPassword =
  764. await bind.sessionGetRemember(sessionId: sessionId) ?? false;
  765. }
  766. var rememberAccount = false;
  767. if (osUsernameController != null) {
  768. rememberAccount =
  769. await bind.sessionGetRemember(sessionId: sessionId) ?? false;
  770. }
  771. dialogManager.dismissAll();
  772. dialogManager.show((setState, close, context) {
  773. cancel() {
  774. close();
  775. closeConnection();
  776. }
  777. submit() {
  778. final osUsername = osUsernameController?.text.trim() ?? '';
  779. final osPassword = osPasswordController?.text.trim() ?? '';
  780. final password = passwordController?.text.trim() ?? '';
  781. if (passwordController != null && password.isEmpty) return;
  782. if (rememberAccount) {
  783. bind.sessionPeerOption(
  784. sessionId: sessionId, name: 'os-username', value: osUsername);
  785. bind.sessionPeerOption(
  786. sessionId: sessionId, name: 'os-password', value: osPassword);
  787. }
  788. gFFI.login(
  789. osUsername,
  790. osPassword,
  791. sessionId,
  792. password,
  793. rememberPassword,
  794. );
  795. close();
  796. dialogManager.showLoading(translate('Logging in...'),
  797. onCancel: closeConnection);
  798. }
  799. descWidget(String text) {
  800. return Column(
  801. children: [
  802. Align(
  803. alignment: Alignment.centerLeft,
  804. child: Text(
  805. text,
  806. maxLines: 3,
  807. softWrap: true,
  808. overflow: TextOverflow.ellipsis,
  809. style: TextStyle(fontSize: 16),
  810. ),
  811. ),
  812. Container(
  813. height: 8,
  814. ),
  815. ],
  816. );
  817. }
  818. rememberWidget(
  819. String desc,
  820. bool remember,
  821. ValueChanged<bool?>? onChanged,
  822. ) {
  823. return CheckboxListTile(
  824. contentPadding: const EdgeInsets.all(0),
  825. dense: true,
  826. controlAffinity: ListTileControlAffinity.leading,
  827. title: Text(desc),
  828. value: remember,
  829. onChanged: onChanged,
  830. );
  831. }
  832. osAccountWidget() {
  833. if (osUsernameController == null || osPasswordController == null) {
  834. return Offstage();
  835. }
  836. return Column(
  837. children: [
  838. descWidget(translate('login_linux_tip')),
  839. DialogTextField(
  840. title: translate(DialogTextField.kUsernameTitle),
  841. controller: osUsernameController,
  842. prefixIcon: DialogTextField.kUsernameIcon,
  843. errorText: null,
  844. ),
  845. PasswordWidget(
  846. controller: osPasswordController,
  847. autoFocus: false,
  848. ),
  849. rememberWidget(
  850. translate('remember_account_tip'),
  851. rememberAccount,
  852. (v) {
  853. if (v != null) {
  854. setState(() => rememberAccount = v);
  855. }
  856. },
  857. ),
  858. ],
  859. );
  860. }
  861. passwdWidget() {
  862. if (passwordController == null) {
  863. return Offstage();
  864. }
  865. return Column(
  866. children: [
  867. descWidget(translate('verify_rustdesk_password_tip')),
  868. PasswordWidget(
  869. controller: passwordController,
  870. autoFocus: osUsernameController == null,
  871. ),
  872. rememberWidget(
  873. translate('Remember password'),
  874. rememberPassword,
  875. (v) {
  876. if (v != null) {
  877. setState(() => rememberPassword = v);
  878. }
  879. },
  880. ),
  881. ],
  882. );
  883. }
  884. return CustomAlertDialog(
  885. title: Row(
  886. mainAxisAlignment: MainAxisAlignment.center,
  887. children: [
  888. Icon(Icons.password_rounded, color: MyTheme.accent),
  889. Text(translate('Password Required')).paddingOnly(left: 10),
  890. ],
  891. ),
  892. content: Column(mainAxisSize: MainAxisSize.min, children: [
  893. osAccountWidget(),
  894. osUsernameController == null || passwordController == null
  895. ? Offstage()
  896. : Container(height: 12),
  897. passwdWidget(),
  898. ]),
  899. actions: [
  900. dialogButton(
  901. 'Cancel',
  902. icon: Icon(Icons.close_rounded),
  903. onPressed: cancel,
  904. isOutline: true,
  905. ),
  906. dialogButton(
  907. 'OK',
  908. icon: Icon(Icons.done_rounded),
  909. onPressed: submit,
  910. ),
  911. ],
  912. onSubmit: submit,
  913. onCancel: cancel,
  914. );
  915. });
  916. }
  917. void showWaitUacDialog(
  918. SessionID sessionId, OverlayDialogManager dialogManager, String type) {
  919. dialogManager.dismissAll();
  920. dialogManager.show(
  921. tag: '$sessionId-wait-uac',
  922. (setState, close, context) => CustomAlertDialog(
  923. title: null,
  924. content: msgboxContent(type, 'Wait', 'wait_accept_uac_tip'),
  925. actions: [
  926. dialogButton(
  927. 'OK',
  928. icon: Icon(Icons.done_rounded),
  929. onPressed: close,
  930. ),
  931. ],
  932. ));
  933. }
  934. // Another username && password dialog?
  935. void showRequestElevationDialog(
  936. SessionID sessionId, OverlayDialogManager dialogManager) {
  937. RxString groupValue = ''.obs;
  938. RxString errUser = ''.obs;
  939. RxString errPwd = ''.obs;
  940. TextEditingController userController = TextEditingController();
  941. TextEditingController pwdController = TextEditingController();
  942. void onRadioChanged(String? value) {
  943. if (value != null) {
  944. groupValue.value = value;
  945. }
  946. }
  947. // TODO get from theme
  948. final double fontSizeNote = 13.00;
  949. Widget OptionRequestPermissions = Obx(
  950. () => Row(
  951. crossAxisAlignment: CrossAxisAlignment.start,
  952. children: [
  953. Radio(
  954. visualDensity: VisualDensity(horizontal: -4, vertical: -4),
  955. value: '',
  956. groupValue: groupValue.value,
  957. onChanged: onRadioChanged,
  958. ).marginOnly(right: 10),
  959. Expanded(
  960. child: Column(
  961. crossAxisAlignment: CrossAxisAlignment.start,
  962. children: [
  963. InkWell(
  964. hoverColor: Colors.transparent,
  965. onTap: () => groupValue.value = '',
  966. child: Text(
  967. translate('Ask the remote user for authentication'),
  968. ),
  969. ).marginOnly(bottom: 10),
  970. Text(
  971. translate('Choose this if the remote account is administrator'),
  972. style: TextStyle(fontSize: fontSizeNote),
  973. ),
  974. ],
  975. ).marginOnly(top: 3),
  976. ),
  977. ],
  978. ),
  979. );
  980. Widget OptionCredentials = Obx(
  981. () => Row(
  982. crossAxisAlignment: CrossAxisAlignment.start,
  983. children: [
  984. Radio(
  985. visualDensity: VisualDensity(horizontal: -4, vertical: -4),
  986. value: 'logon',
  987. groupValue: groupValue.value,
  988. onChanged: onRadioChanged,
  989. ).marginOnly(right: 10),
  990. Expanded(
  991. child: InkWell(
  992. hoverColor: Colors.transparent,
  993. onTap: () => onRadioChanged('logon'),
  994. child: Text(
  995. translate('Transmit the username and password of administrator'),
  996. ),
  997. ).marginOnly(top: 4),
  998. ),
  999. ],
  1000. ),
  1001. );
  1002. Widget UacNote = Container(
  1003. padding: EdgeInsets.fromLTRB(10, 8, 8, 8),
  1004. decoration: BoxDecoration(
  1005. color: MyTheme.currentThemeMode() == ThemeMode.dark
  1006. ? Color.fromARGB(135, 87, 87, 90)
  1007. : Colors.grey[100],
  1008. borderRadius: BorderRadius.circular(8),
  1009. border: Border.all(color: Colors.grey),
  1010. ),
  1011. child: Row(
  1012. children: [
  1013. Icon(Icons.info_outline_rounded, size: 20).marginOnly(right: 10),
  1014. Expanded(
  1015. child: Text(
  1016. translate('still_click_uac_tip'),
  1017. style: TextStyle(
  1018. fontSize: fontSizeNote, fontWeight: FontWeight.normal),
  1019. ),
  1020. )
  1021. ],
  1022. ),
  1023. );
  1024. var content = Obx(
  1025. () => Column(
  1026. children: [
  1027. OptionRequestPermissions.marginOnly(bottom: 15),
  1028. OptionCredentials,
  1029. Offstage(
  1030. offstage: 'logon' != groupValue.value,
  1031. child: Column(
  1032. children: [
  1033. UacNote.marginOnly(bottom: 10),
  1034. DialogTextField(
  1035. controller: userController,
  1036. title: translate('Username'),
  1037. hintText: translate('eg: admin'),
  1038. prefixIcon: DialogTextField.kUsernameIcon,
  1039. errorText: errUser.isEmpty ? null : errUser.value,
  1040. ),
  1041. PasswordWidget(
  1042. controller: pwdController,
  1043. autoFocus: false,
  1044. errorText: errPwd.isEmpty ? null : errPwd.value,
  1045. ),
  1046. ],
  1047. ).marginOnly(left: stateGlobal.isPortrait.isFalse ? 35 : 0),
  1048. ).marginOnly(top: 10),
  1049. ],
  1050. ),
  1051. );
  1052. dialogManager.dismissAll();
  1053. dialogManager.show(tag: '$sessionId-request-elevation',
  1054. (setState, close, context) {
  1055. void submit() {
  1056. if (groupValue.value == 'logon') {
  1057. if (userController.text.isEmpty) {
  1058. errUser.value = translate('Empty Username');
  1059. return;
  1060. }
  1061. if (pwdController.text.isEmpty) {
  1062. errPwd.value = translate('Empty Password');
  1063. return;
  1064. }
  1065. bind.sessionElevateWithLogon(
  1066. sessionId: sessionId,
  1067. username: userController.text,
  1068. password: pwdController.text);
  1069. } else {
  1070. bind.sessionElevateDirect(sessionId: sessionId);
  1071. }
  1072. close();
  1073. showWaitUacDialog(sessionId, dialogManager, "wait-uac");
  1074. }
  1075. return CustomAlertDialog(
  1076. title: Text(translate('Request Elevation')),
  1077. content: content,
  1078. actions: [
  1079. dialogButton(
  1080. 'Cancel',
  1081. icon: Icon(Icons.close_rounded),
  1082. onPressed: close,
  1083. isOutline: true,
  1084. ),
  1085. dialogButton(
  1086. 'OK',
  1087. icon: Icon(Icons.done_rounded),
  1088. onPressed: submit,
  1089. )
  1090. ],
  1091. onSubmit: submit,
  1092. onCancel: close,
  1093. );
  1094. });
  1095. }
  1096. void showOnBlockDialog(
  1097. SessionID sessionId,
  1098. String type,
  1099. String title,
  1100. String text,
  1101. OverlayDialogManager dialogManager,
  1102. ) {
  1103. if (dialogManager.existing('$sessionId-wait-uac') ||
  1104. dialogManager.existing('$sessionId-request-elevation')) {
  1105. return;
  1106. }
  1107. dialogManager.show(tag: '$sessionId-$type', (setState, close, context) {
  1108. void submit() {
  1109. close();
  1110. showRequestElevationDialog(sessionId, dialogManager);
  1111. }
  1112. return CustomAlertDialog(
  1113. title: null,
  1114. content: msgboxContent(type, title,
  1115. "${translate(text)}${type.contains('uac') ? '\n' : '\n\n'}${translate('request_elevation_tip')}"),
  1116. actions: [
  1117. dialogButton('Wait', onPressed: close, isOutline: true),
  1118. dialogButton('Request Elevation', onPressed: submit),
  1119. ],
  1120. onSubmit: submit,
  1121. onCancel: close,
  1122. );
  1123. });
  1124. }
  1125. void showElevationError(SessionID sessionId, String type, String title,
  1126. String text, OverlayDialogManager dialogManager) {
  1127. dialogManager.show(tag: '$sessionId-$type', (setState, close, context) {
  1128. void submit() {
  1129. close();
  1130. showRequestElevationDialog(sessionId, dialogManager);
  1131. }
  1132. return CustomAlertDialog(
  1133. title: null,
  1134. content: msgboxContent(type, title, text),
  1135. actions: [
  1136. dialogButton('Cancel', onPressed: () {
  1137. close();
  1138. }, isOutline: true),
  1139. if (text != 'No permission') dialogButton('Retry', onPressed: submit),
  1140. ],
  1141. onSubmit: submit,
  1142. onCancel: close,
  1143. );
  1144. });
  1145. }
  1146. void showWaitAcceptDialog(SessionID sessionId, String type, String title,
  1147. String text, OverlayDialogManager dialogManager) {
  1148. dialogManager.dismissAll();
  1149. dialogManager.show((setState, close, context) {
  1150. onCancel() {
  1151. closeConnection();
  1152. }
  1153. return CustomAlertDialog(
  1154. title: null,
  1155. content: msgboxContent(type, title, text),
  1156. actions: [
  1157. dialogButton('Cancel', onPressed: onCancel, isOutline: true),
  1158. ],
  1159. onCancel: onCancel,
  1160. );
  1161. });
  1162. }
  1163. void showRestartRemoteDevice(PeerInfo pi, String id, SessionID sessionId,
  1164. OverlayDialogManager dialogManager) async {
  1165. final res = await dialogManager
  1166. .show<bool>((setState, close, context) => CustomAlertDialog(
  1167. title: Row(children: [
  1168. Icon(Icons.warning_rounded, color: Colors.redAccent, size: 28),
  1169. Flexible(
  1170. child: Text(translate("Restart remote device"))
  1171. .paddingOnly(left: 10)),
  1172. ]),
  1173. content: Text(
  1174. "${translate('Are you sure you want to restart')} \n${pi.username}@${pi.hostname}($id) ?"),
  1175. actions: [
  1176. dialogButton(
  1177. "Cancel",
  1178. icon: Icon(Icons.close_rounded),
  1179. onPressed: close,
  1180. isOutline: true,
  1181. ),
  1182. dialogButton(
  1183. "OK",
  1184. icon: Icon(Icons.done_rounded),
  1185. onPressed: () => close(true),
  1186. ),
  1187. ],
  1188. onCancel: close,
  1189. onSubmit: () => close(true),
  1190. ));
  1191. if (res == true) bind.sessionRestartRemoteDevice(sessionId: sessionId);
  1192. }
  1193. showSetOSPassword(
  1194. SessionID sessionId,
  1195. bool login,
  1196. OverlayDialogManager dialogManager,
  1197. String? osPassword,
  1198. Function()? closeCallback,
  1199. ) async {
  1200. final controller = TextEditingController();
  1201. osPassword ??=
  1202. await bind.sessionGetOption(sessionId: sessionId, arg: 'os-password') ??
  1203. '';
  1204. var autoLogin =
  1205. await bind.sessionGetOption(sessionId: sessionId, arg: 'auto-login') !=
  1206. '';
  1207. controller.text = osPassword;
  1208. dialogManager.show((setState, close, context) {
  1209. closeWithCallback([dynamic]) {
  1210. close();
  1211. if (closeCallback != null) closeCallback();
  1212. }
  1213. submit() {
  1214. var text = controller.text.trim();
  1215. bind.sessionPeerOption(
  1216. sessionId: sessionId, name: 'os-password', value: text);
  1217. bind.sessionPeerOption(
  1218. sessionId: sessionId,
  1219. name: 'auto-login',
  1220. value: autoLogin ? 'Y' : '');
  1221. if (text != '' && login) {
  1222. bind.sessionInputOsPassword(sessionId: sessionId, value: text);
  1223. }
  1224. closeWithCallback();
  1225. }
  1226. return CustomAlertDialog(
  1227. title: Row(
  1228. mainAxisAlignment: MainAxisAlignment.center,
  1229. children: [
  1230. Icon(Icons.password_rounded, color: MyTheme.accent),
  1231. Text(translate('OS Password')).paddingOnly(left: 10),
  1232. ],
  1233. ),
  1234. content: Column(
  1235. mainAxisSize: MainAxisSize.min,
  1236. children: [
  1237. PasswordWidget(controller: controller),
  1238. CheckboxListTile(
  1239. contentPadding: const EdgeInsets.all(0),
  1240. dense: true,
  1241. controlAffinity: ListTileControlAffinity.leading,
  1242. title: Text(
  1243. translate('Auto Login'),
  1244. ),
  1245. value: autoLogin,
  1246. onChanged: (v) {
  1247. if (v == null) return;
  1248. setState(() => autoLogin = v);
  1249. },
  1250. ),
  1251. ],
  1252. ),
  1253. actions: [
  1254. dialogButton(
  1255. "Cancel",
  1256. icon: Icon(Icons.close_rounded),
  1257. onPressed: closeWithCallback,
  1258. isOutline: true,
  1259. ),
  1260. dialogButton(
  1261. "OK",
  1262. icon: Icon(Icons.done_rounded),
  1263. onPressed: submit,
  1264. ),
  1265. ],
  1266. onSubmit: submit,
  1267. onCancel: closeWithCallback,
  1268. );
  1269. });
  1270. }
  1271. showSetOSAccount(
  1272. SessionID sessionId,
  1273. OverlayDialogManager dialogManager,
  1274. ) async {
  1275. final usernameController = TextEditingController();
  1276. final passwdController = TextEditingController();
  1277. var username =
  1278. await bind.sessionGetOption(sessionId: sessionId, arg: 'os-username') ??
  1279. '';
  1280. var password =
  1281. await bind.sessionGetOption(sessionId: sessionId, arg: 'os-password') ??
  1282. '';
  1283. usernameController.text = username;
  1284. passwdController.text = password;
  1285. dialogManager.show((setState, close, context) {
  1286. submit() {
  1287. final username = usernameController.text.trim();
  1288. final password = usernameController.text.trim();
  1289. bind.sessionPeerOption(
  1290. sessionId: sessionId, name: 'os-username', value: username);
  1291. bind.sessionPeerOption(
  1292. sessionId: sessionId, name: 'os-password', value: password);
  1293. close();
  1294. }
  1295. descWidget(String text) {
  1296. return Column(
  1297. children: [
  1298. Align(
  1299. alignment: Alignment.centerLeft,
  1300. child: Text(
  1301. text,
  1302. maxLines: 3,
  1303. softWrap: true,
  1304. overflow: TextOverflow.ellipsis,
  1305. style: TextStyle(fontSize: 16),
  1306. ),
  1307. ),
  1308. Container(
  1309. height: 8,
  1310. ),
  1311. ],
  1312. );
  1313. }
  1314. return CustomAlertDialog(
  1315. title: Row(
  1316. mainAxisAlignment: MainAxisAlignment.center,
  1317. children: [
  1318. Icon(Icons.password_rounded, color: MyTheme.accent),
  1319. Text(translate('OS Account')).paddingOnly(left: 10),
  1320. ],
  1321. ),
  1322. content: Column(
  1323. mainAxisSize: MainAxisSize.min,
  1324. children: [
  1325. descWidget(translate("os_account_desk_tip")),
  1326. DialogTextField(
  1327. title: translate(DialogTextField.kUsernameTitle),
  1328. controller: usernameController,
  1329. prefixIcon: DialogTextField.kUsernameIcon,
  1330. errorText: null,
  1331. ),
  1332. PasswordWidget(controller: passwdController),
  1333. ],
  1334. ),
  1335. actions: [
  1336. dialogButton(
  1337. "Cancel",
  1338. icon: Icon(Icons.close_rounded),
  1339. onPressed: close,
  1340. isOutline: true,
  1341. ),
  1342. dialogButton(
  1343. "OK",
  1344. icon: Icon(Icons.done_rounded),
  1345. onPressed: submit,
  1346. ),
  1347. ],
  1348. onSubmit: submit,
  1349. onCancel: close,
  1350. );
  1351. });
  1352. }
  1353. showAuditDialog(FFI ffi) async {
  1354. final controller = TextEditingController(text: ffi.auditNote);
  1355. ffi.dialogManager.show((setState, close, context) {
  1356. submit() {
  1357. var text = controller.text;
  1358. bind.sessionSendNote(sessionId: ffi.sessionId, note: text);
  1359. ffi.auditNote = text;
  1360. close();
  1361. }
  1362. late final focusNode = FocusNode(
  1363. onKey: (FocusNode node, RawKeyEvent evt) {
  1364. if (evt.logicalKey.keyLabel == 'Enter') {
  1365. if (evt is RawKeyDownEvent) {
  1366. int pos = controller.selection.base.offset;
  1367. controller.text =
  1368. '${controller.text.substring(0, pos)}\n${controller.text.substring(pos)}';
  1369. controller.selection =
  1370. TextSelection.fromPosition(TextPosition(offset: pos + 1));
  1371. }
  1372. return KeyEventResult.handled;
  1373. }
  1374. if (evt.logicalKey.keyLabel == 'Esc') {
  1375. if (evt is RawKeyDownEvent) {
  1376. close();
  1377. }
  1378. return KeyEventResult.handled;
  1379. } else {
  1380. return KeyEventResult.ignored;
  1381. }
  1382. },
  1383. );
  1384. return CustomAlertDialog(
  1385. title: Text(translate('Note')),
  1386. content: SizedBox(
  1387. width: 250,
  1388. height: 120,
  1389. child: TextField(
  1390. autofocus: true,
  1391. keyboardType: TextInputType.multiline,
  1392. textInputAction: TextInputAction.newline,
  1393. decoration: const InputDecoration.collapsed(
  1394. hintText: 'input note here',
  1395. ),
  1396. maxLines: null,
  1397. maxLength: 256,
  1398. controller: controller,
  1399. focusNode: focusNode,
  1400. )),
  1401. actions: [
  1402. dialogButton('Cancel', onPressed: close, isOutline: true),
  1403. dialogButton('OK', onPressed: submit)
  1404. ],
  1405. onSubmit: submit,
  1406. onCancel: close,
  1407. );
  1408. });
  1409. }
  1410. void showConfirmSwitchSidesDialog(
  1411. SessionID sessionId, String id, OverlayDialogManager dialogManager) async {
  1412. dialogManager.show((setState, close, context) {
  1413. submit() async {
  1414. await bind.sessionSwitchSides(sessionId: sessionId);
  1415. closeConnection(id: id);
  1416. }
  1417. return CustomAlertDialog(
  1418. content: msgboxContent('info', 'Switch Sides',
  1419. 'Please confirm if you want to share your desktop?'),
  1420. actions: [
  1421. dialogButton('Cancel', onPressed: close, isOutline: true),
  1422. dialogButton('OK', onPressed: submit),
  1423. ],
  1424. onSubmit: submit,
  1425. onCancel: close,
  1426. );
  1427. });
  1428. }
  1429. customImageQualityDialog(SessionID sessionId, String id, FFI ffi) async {
  1430. double initQuality = kDefaultQuality;
  1431. double initFps = kDefaultFps;
  1432. bool qualitySet = false;
  1433. bool fpsSet = false;
  1434. bool? direct;
  1435. try {
  1436. direct =
  1437. ConnectionTypeState.find(id).direct.value == ConnectionType.strDirect;
  1438. } catch (_) {}
  1439. bool hideFps = (await bind.mainIsUsingPublicServer() && direct != true) ||
  1440. versionCmp(ffi.ffiModel.pi.version, '1.2.0') < 0;
  1441. bool hideMoreQuality =
  1442. (await bind.mainIsUsingPublicServer() && direct != true) ||
  1443. versionCmp(ffi.ffiModel.pi.version, '1.2.2') < 0;
  1444. setCustomValues({double? quality, double? fps}) async {
  1445. debugPrint("setCustomValues quality:$quality, fps:$fps");
  1446. if (quality != null) {
  1447. qualitySet = true;
  1448. await bind.sessionSetCustomImageQuality(
  1449. sessionId: sessionId, value: quality.toInt());
  1450. }
  1451. if (fps != null) {
  1452. fpsSet = true;
  1453. await bind.sessionSetCustomFps(sessionId: sessionId, fps: fps.toInt());
  1454. }
  1455. if (!qualitySet) {
  1456. qualitySet = true;
  1457. await bind.sessionSetCustomImageQuality(
  1458. sessionId: sessionId, value: initQuality.toInt());
  1459. }
  1460. if (!hideFps && !fpsSet) {
  1461. fpsSet = true;
  1462. await bind.sessionSetCustomFps(
  1463. sessionId: sessionId, fps: initFps.toInt());
  1464. }
  1465. }
  1466. final btnClose = dialogButton('Close', onPressed: () async {
  1467. await setCustomValues();
  1468. ffi.dialogManager.dismissAll();
  1469. });
  1470. // quality
  1471. final quality = await bind.sessionGetCustomImageQuality(sessionId: sessionId);
  1472. initQuality = quality != null && quality.isNotEmpty
  1473. ? quality[0].toDouble()
  1474. : kDefaultQuality;
  1475. if (initQuality < kMinQuality ||
  1476. initQuality > (!hideMoreQuality ? kMaxMoreQuality : kMaxQuality)) {
  1477. initQuality = kDefaultQuality;
  1478. }
  1479. // fps
  1480. final fpsOption =
  1481. await bind.sessionGetOption(sessionId: sessionId, arg: 'custom-fps');
  1482. initFps = fpsOption == null
  1483. ? kDefaultFps
  1484. : double.tryParse(fpsOption) ?? kDefaultFps;
  1485. if (initFps < kMinFps || initFps > kMaxFps) {
  1486. initFps = kDefaultFps;
  1487. }
  1488. final content = customImageQualityWidget(
  1489. initQuality: initQuality,
  1490. initFps: initFps,
  1491. setQuality: (v) => setCustomValues(quality: v),
  1492. setFps: (v) => setCustomValues(fps: v),
  1493. showFps: !hideFps,
  1494. showMoreQuality: !hideMoreQuality);
  1495. msgBoxCommon(ffi.dialogManager, 'Custom Image Quality', content, [btnClose]);
  1496. }
  1497. void deleteConfirmDialog(Function onSubmit, String title) async {
  1498. gFFI.dialogManager.show(
  1499. (setState, close, context) {
  1500. submit() async {
  1501. await onSubmit();
  1502. close();
  1503. }
  1504. return CustomAlertDialog(
  1505. title: Row(
  1506. mainAxisAlignment: MainAxisAlignment.center,
  1507. children: [
  1508. Icon(
  1509. Icons.delete_rounded,
  1510. color: Colors.red,
  1511. ),
  1512. Expanded(
  1513. child: Text(title, overflow: TextOverflow.ellipsis).paddingOnly(
  1514. left: 10,
  1515. ),
  1516. ),
  1517. ],
  1518. ),
  1519. content: SizedBox.shrink(),
  1520. actions: [
  1521. dialogButton(
  1522. "Cancel",
  1523. icon: Icon(Icons.close_rounded),
  1524. onPressed: close,
  1525. isOutline: true,
  1526. ),
  1527. dialogButton(
  1528. "OK",
  1529. icon: Icon(Icons.done_rounded),
  1530. onPressed: submit,
  1531. ),
  1532. ],
  1533. onSubmit: submit,
  1534. onCancel: close,
  1535. );
  1536. },
  1537. );
  1538. }
  1539. void editAbTagDialog(
  1540. List<dynamic> currentTags, Function(List<dynamic>) onSubmit) {
  1541. var isInProgress = false;
  1542. final tags = List.of(gFFI.abModel.currentAbTags);
  1543. var selectedTag = currentTags.obs;
  1544. gFFI.dialogManager.show((setState, close, context) {
  1545. submit() async {
  1546. setState(() {
  1547. isInProgress = true;
  1548. });
  1549. await onSubmit(selectedTag);
  1550. close();
  1551. }
  1552. return CustomAlertDialog(
  1553. title: Text(translate("Edit Tag")),
  1554. content: Column(
  1555. crossAxisAlignment: CrossAxisAlignment.start,
  1556. children: [
  1557. Container(
  1558. padding: const EdgeInsets.symmetric(vertical: 8.0),
  1559. child: Wrap(
  1560. children: tags
  1561. .map((e) => AddressBookTag(
  1562. name: e,
  1563. tags: selectedTag,
  1564. onTap: () {
  1565. if (selectedTag.contains(e)) {
  1566. selectedTag.remove(e);
  1567. } else {
  1568. selectedTag.add(e);
  1569. }
  1570. },
  1571. showActionMenu: false))
  1572. .toList(growable: false),
  1573. ),
  1574. ),
  1575. // NOT use Offstage to wrap LinearProgressIndicator
  1576. if (isInProgress) const LinearProgressIndicator(),
  1577. ],
  1578. ),
  1579. actions: [
  1580. dialogButton("Cancel", onPressed: close, isOutline: true),
  1581. dialogButton("OK", onPressed: submit),
  1582. ],
  1583. onSubmit: submit,
  1584. onCancel: close,
  1585. );
  1586. });
  1587. }
  1588. void renameDialog(
  1589. {required String oldName,
  1590. FormFieldValidator<String>? validator,
  1591. required ValueChanged<String> onSubmit,
  1592. Function? onCancel}) async {
  1593. RxBool isInProgress = false.obs;
  1594. var controller = TextEditingController(text: oldName);
  1595. final formKey = GlobalKey<FormState>();
  1596. gFFI.dialogManager.show((setState, close, context) {
  1597. submit() async {
  1598. String text = controller.text.trim();
  1599. if (validator != null && formKey.currentState?.validate() == false) {
  1600. return;
  1601. }
  1602. isInProgress.value = true;
  1603. onSubmit(text);
  1604. close();
  1605. isInProgress.value = false;
  1606. }
  1607. cancel() {
  1608. onCancel?.call();
  1609. close();
  1610. }
  1611. return CustomAlertDialog(
  1612. title: Row(
  1613. mainAxisAlignment: MainAxisAlignment.center,
  1614. children: [
  1615. Icon(Icons.edit_rounded, color: MyTheme.accent),
  1616. Text(translate('Rename')).paddingOnly(left: 10),
  1617. ],
  1618. ),
  1619. content: Column(
  1620. crossAxisAlignment: CrossAxisAlignment.start,
  1621. children: [
  1622. Container(
  1623. child: Form(
  1624. key: formKey,
  1625. child: TextFormField(
  1626. controller: controller,
  1627. autofocus: true,
  1628. decoration: InputDecoration(labelText: translate('Name')),
  1629. validator: validator,
  1630. ),
  1631. ),
  1632. ),
  1633. // NOT use Offstage to wrap LinearProgressIndicator
  1634. Obx(() =>
  1635. isInProgress.value ? const LinearProgressIndicator() : Offstage())
  1636. ],
  1637. ),
  1638. actions: [
  1639. dialogButton(
  1640. "Cancel",
  1641. icon: Icon(Icons.close_rounded),
  1642. onPressed: cancel,
  1643. isOutline: true,
  1644. ),
  1645. dialogButton(
  1646. "OK",
  1647. icon: Icon(Icons.done_rounded),
  1648. onPressed: submit,
  1649. ),
  1650. ],
  1651. onSubmit: submit,
  1652. onCancel: cancel,
  1653. );
  1654. });
  1655. }
  1656. void changeBot({Function()? callback}) async {
  1657. if (bind.mainHasValidBotSync()) {
  1658. await bind.mainSetOption(key: "bot", value: "");
  1659. callback?.call();
  1660. return;
  1661. }
  1662. String errorText = '';
  1663. bool loading = false;
  1664. final controller = TextEditingController();
  1665. gFFI.dialogManager.show((setState, close, context) {
  1666. onVerify() async {
  1667. final token = controller.text.trim();
  1668. if (token == "") return;
  1669. loading = true;
  1670. errorText = '';
  1671. setState(() {});
  1672. final error = await bind.mainVerifyBot(token: token);
  1673. if (error == "") {
  1674. callback?.call();
  1675. close();
  1676. } else {
  1677. errorText = translate(error);
  1678. loading = false;
  1679. setState(() {});
  1680. }
  1681. }
  1682. final codeField = TextField(
  1683. autofocus: true,
  1684. controller: controller,
  1685. decoration: InputDecoration(
  1686. hintText: translate('Token'),
  1687. ),
  1688. );
  1689. return CustomAlertDialog(
  1690. title: Text(translate("Telegram bot")),
  1691. content: Column(
  1692. crossAxisAlignment: CrossAxisAlignment.start,
  1693. children: [
  1694. SelectableText(translate("enable-bot-desc"),
  1695. style: TextStyle(fontSize: 12))
  1696. .marginOnly(bottom: 12),
  1697. Row(children: [Expanded(child: codeField)]),
  1698. if (errorText != '')
  1699. Text(errorText, style: TextStyle(color: Colors.red))
  1700. .marginOnly(top: 12),
  1701. ],
  1702. ),
  1703. actions: [
  1704. dialogButton("Cancel", onPressed: close, isOutline: true),
  1705. loading
  1706. ? CircularProgressIndicator()
  1707. : dialogButton("OK", onPressed: onVerify),
  1708. ],
  1709. onCancel: close,
  1710. );
  1711. });
  1712. }
  1713. void change2fa({Function()? callback}) async {
  1714. if (bind.mainHasValid2FaSync()) {
  1715. await bind.mainSetOption(key: "2fa", value: "");
  1716. await bind.mainClearTrustedDevices();
  1717. callback?.call();
  1718. return;
  1719. }
  1720. var new2fa = (await bind.mainGenerate2Fa());
  1721. final secretRegex = RegExp(r'secret=([^&]+)');
  1722. final secret = secretRegex.firstMatch(new2fa)?.group(1);
  1723. String? errorText;
  1724. final controller = TextEditingController();
  1725. gFFI.dialogManager.show((setState, close, context) {
  1726. onVerify() async {
  1727. if (await bind.mainVerify2Fa(code: controller.text.trim())) {
  1728. callback?.call();
  1729. close();
  1730. } else {
  1731. errorText = translate('wrong-2fa-code');
  1732. }
  1733. }
  1734. final codeField = Dialog2FaField(
  1735. controller: controller,
  1736. errorText: errorText,
  1737. onChanged: () => setState(() => errorText = null),
  1738. title: translate('Verification code'),
  1739. readyCallback: () {
  1740. onVerify();
  1741. setState(() {});
  1742. },
  1743. );
  1744. getOnSubmit() => codeField.isReady ? onVerify : null;
  1745. return CustomAlertDialog(
  1746. title: Text(translate("enable-2fa-title")),
  1747. content: Column(
  1748. crossAxisAlignment: CrossAxisAlignment.start,
  1749. children: [
  1750. SelectableText(translate("enable-2fa-desc"),
  1751. style: TextStyle(fontSize: 12))
  1752. .marginOnly(bottom: 12),
  1753. SizedBox(
  1754. width: 160,
  1755. height: 160,
  1756. child: QrImageView(
  1757. backgroundColor: Colors.white,
  1758. data: new2fa,
  1759. version: QrVersions.auto,
  1760. size: 160,
  1761. gapless: false,
  1762. )).marginOnly(bottom: 6),
  1763. SelectableText(secret ?? '', style: TextStyle(fontSize: 12))
  1764. .marginOnly(bottom: 12),
  1765. Row(children: [Expanded(child: codeField)]),
  1766. ],
  1767. ),
  1768. actions: [
  1769. dialogButton("Cancel", onPressed: close, isOutline: true),
  1770. dialogButton("OK", onPressed: getOnSubmit()),
  1771. ],
  1772. onCancel: close,
  1773. );
  1774. });
  1775. }
  1776. void enter2FaDialog(
  1777. SessionID sessionId, OverlayDialogManager dialogManager) async {
  1778. final controller = TextEditingController();
  1779. final RxBool submitReady = false.obs;
  1780. final RxBool trustThisDevice = false.obs;
  1781. dialogManager.dismissAll();
  1782. dialogManager.show((setState, close, context) {
  1783. cancel() {
  1784. close();
  1785. closeConnection();
  1786. }
  1787. submit() {
  1788. gFFI.send2FA(sessionId, controller.text.trim(), trustThisDevice.value);
  1789. close();
  1790. dialogManager.showLoading(translate('Logging in...'),
  1791. onCancel: closeConnection);
  1792. }
  1793. late Dialog2FaField codeField;
  1794. codeField = Dialog2FaField(
  1795. controller: controller,
  1796. title: translate('Verification code'),
  1797. onChanged: () => submitReady.value = codeField.isReady,
  1798. );
  1799. final trustField = Obx(() => CheckboxListTile(
  1800. contentPadding: const EdgeInsets.all(0),
  1801. dense: true,
  1802. controlAffinity: ListTileControlAffinity.leading,
  1803. title: Text(translate("Trust this device")),
  1804. value: trustThisDevice.value,
  1805. onChanged: (value) {
  1806. if (value == null) return;
  1807. trustThisDevice.value = value;
  1808. },
  1809. ));
  1810. return CustomAlertDialog(
  1811. title: Text(translate('enter-2fa-title')),
  1812. content: Column(
  1813. children: [
  1814. codeField,
  1815. if (bind.sessionGetEnableTrustedDevices(sessionId: sessionId))
  1816. trustField,
  1817. ],
  1818. ),
  1819. actions: [
  1820. dialogButton('Cancel',
  1821. onPressed: cancel,
  1822. isOutline: true,
  1823. style: TextStyle(
  1824. color: Theme.of(context).textTheme.bodyMedium?.color)),
  1825. Obx(() => dialogButton(
  1826. 'OK',
  1827. onPressed: submitReady.isTrue ? submit : null,
  1828. )),
  1829. ],
  1830. onSubmit: submit,
  1831. onCancel: cancel);
  1832. });
  1833. }
  1834. // This dialog should not be dismissed, otherwise it will be black screen, have not reproduced this.
  1835. void showWindowsSessionsDialog(
  1836. String type,
  1837. String title,
  1838. String text,
  1839. OverlayDialogManager dialogManager,
  1840. SessionID sessionId,
  1841. String peerId,
  1842. String sessions) {
  1843. List<dynamic> sessionsList = [];
  1844. try {
  1845. sessionsList = json.decode(sessions);
  1846. } catch (e) {
  1847. print(e);
  1848. }
  1849. List<String> sids = [];
  1850. List<String> names = [];
  1851. for (var session in sessionsList) {
  1852. sids.add(session['sid']);
  1853. names.add(session['name']);
  1854. }
  1855. String selectedUserValue = sids.first;
  1856. dialogManager.dismissAll();
  1857. dialogManager.show((setState, close, context) {
  1858. submit() {
  1859. bind.sessionSendSelectedSessionId(
  1860. sessionId: sessionId, sid: selectedUserValue);
  1861. close();
  1862. }
  1863. return CustomAlertDialog(
  1864. title: null,
  1865. content: msgboxContent(type, title, text),
  1866. actions: [
  1867. ComboBox(
  1868. keys: sids,
  1869. values: names,
  1870. initialKey: selectedUserValue,
  1871. onChanged: (value) {
  1872. selectedUserValue = value;
  1873. }),
  1874. dialogButton('Connect', onPressed: submit, isOutline: false),
  1875. ],
  1876. );
  1877. });
  1878. }
  1879. void addPeersToAbDialog(
  1880. List<Peer> peers,
  1881. ) async {
  1882. Future<bool> addTo(String abname) async {
  1883. final mapList = peers.map((e) {
  1884. var json = e.toJson();
  1885. // remove password when add to another address book to avoid re-share
  1886. json.remove('password');
  1887. json.remove('hash');
  1888. return json;
  1889. }).toList();
  1890. final errMsg = await gFFI.abModel.addPeersTo(mapList, abname);
  1891. if (errMsg == null) {
  1892. showToast(translate('Successful'));
  1893. return true;
  1894. } else {
  1895. BotToast.showText(text: errMsg, contentColor: Colors.red);
  1896. return false;
  1897. }
  1898. }
  1899. // if only one address book and it is personal, add to it directly
  1900. if (gFFI.abModel.addressbooks.length == 1 &&
  1901. gFFI.abModel.current.isPersonal()) {
  1902. await addTo(gFFI.abModel.currentName.value);
  1903. return;
  1904. }
  1905. RxBool isInProgress = false.obs;
  1906. final names = gFFI.abModel.addressBooksCanWrite();
  1907. RxString currentName = gFFI.abModel.currentName.value.obs;
  1908. TextEditingController controller = TextEditingController();
  1909. if (gFFI.peerTabModel.currentTab == PeerTabIndex.ab.index) {
  1910. names.remove(currentName.value);
  1911. }
  1912. if (names.isEmpty) {
  1913. debugPrint('no address book to add peers to, should not happen');
  1914. return;
  1915. }
  1916. if (!names.contains(currentName.value)) {
  1917. currentName.value = names[0];
  1918. }
  1919. gFFI.dialogManager.show((setState, close, context) {
  1920. submit() async {
  1921. if (controller.text != gFFI.abModel.translatedName(currentName.value)) {
  1922. BotToast.showText(
  1923. text: 'illegal address book name: ${controller.text}',
  1924. contentColor: Colors.red);
  1925. return;
  1926. }
  1927. isInProgress.value = true;
  1928. if (await addTo(currentName.value)) {
  1929. close();
  1930. }
  1931. isInProgress.value = false;
  1932. }
  1933. cancel() {
  1934. close();
  1935. }
  1936. return CustomAlertDialog(
  1937. title: Row(
  1938. mainAxisAlignment: MainAxisAlignment.center,
  1939. children: [
  1940. Icon(IconFont.addressBook, color: MyTheme.accent),
  1941. Text(translate('Add to address book')).paddingOnly(left: 10),
  1942. ],
  1943. ),
  1944. content: Obx(() => Column(
  1945. crossAxisAlignment: CrossAxisAlignment.center,
  1946. children: [
  1947. // https://github.com/flutter/flutter/issues/145081
  1948. DropdownMenu(
  1949. initialSelection: currentName.value,
  1950. onSelected: (value) {
  1951. if (value != null) {
  1952. currentName.value = value;
  1953. }
  1954. },
  1955. dropdownMenuEntries: names
  1956. .map((e) => DropdownMenuEntry(
  1957. value: e, label: gFFI.abModel.translatedName(e)))
  1958. .toList(),
  1959. inputDecorationTheme: InputDecorationTheme(
  1960. isDense: true, border: UnderlineInputBorder()),
  1961. enableFilter: true,
  1962. controller: controller,
  1963. ),
  1964. // NOT use Offstage to wrap LinearProgressIndicator
  1965. isInProgress.value ? const LinearProgressIndicator() : Offstage()
  1966. ],
  1967. )),
  1968. actions: [
  1969. dialogButton(
  1970. "Cancel",
  1971. icon: Icon(Icons.close_rounded),
  1972. onPressed: cancel,
  1973. isOutline: true,
  1974. ),
  1975. dialogButton(
  1976. "OK",
  1977. icon: Icon(Icons.done_rounded),
  1978. onPressed: submit,
  1979. ),
  1980. ],
  1981. onSubmit: submit,
  1982. onCancel: cancel,
  1983. );
  1984. });
  1985. }
  1986. void setSharedAbPasswordDialog(String abName, Peer peer) {
  1987. TextEditingController controller = TextEditingController(text: '');
  1988. RxBool isInProgress = false.obs;
  1989. RxBool isInputEmpty = true.obs;
  1990. bool passwordVisible = false;
  1991. controller.addListener(() {
  1992. isInputEmpty.value = controller.text.isEmpty;
  1993. });
  1994. gFFI.dialogManager.show((setState, close, context) {
  1995. change(String password) async {
  1996. isInProgress.value = true;
  1997. bool res =
  1998. await gFFI.abModel.changeSharedPassword(abName, peer.id, password);
  1999. isInProgress.value = false;
  2000. if (res) {
  2001. showToast(translate('Successful'));
  2002. }
  2003. close();
  2004. }
  2005. cancel() {
  2006. close();
  2007. }
  2008. return CustomAlertDialog(
  2009. title: Row(
  2010. mainAxisAlignment: MainAxisAlignment.center,
  2011. children: [
  2012. Icon(Icons.key, color: MyTheme.accent),
  2013. Text(translate(peer.password.isEmpty
  2014. ? 'Set shared password'
  2015. : 'Change Password'))
  2016. .paddingOnly(left: 10),
  2017. ],
  2018. ),
  2019. content: Obx(() => Column(children: [
  2020. TextField(
  2021. controller: controller,
  2022. autofocus: true,
  2023. obscureText: !passwordVisible,
  2024. decoration: InputDecoration(
  2025. suffixIcon: IconButton(
  2026. icon: Icon(
  2027. passwordVisible ? Icons.visibility : Icons.visibility_off,
  2028. color: MyTheme.lightTheme.primaryColor),
  2029. onPressed: () {
  2030. setState(() {
  2031. passwordVisible = !passwordVisible;
  2032. });
  2033. },
  2034. ),
  2035. ),
  2036. ),
  2037. if (!gFFI.abModel.current.isPersonal())
  2038. Row(children: [
  2039. Icon(Icons.info, color: Colors.amber).marginOnly(right: 4),
  2040. Text(
  2041. translate('share_warning_tip'),
  2042. style: TextStyle(fontSize: 12),
  2043. )
  2044. ]).marginSymmetric(vertical: 10),
  2045. // NOT use Offstage to wrap LinearProgressIndicator
  2046. isInProgress.value ? const LinearProgressIndicator() : Offstage()
  2047. ])),
  2048. actions: [
  2049. dialogButton(
  2050. "Cancel",
  2051. icon: Icon(Icons.close_rounded),
  2052. onPressed: cancel,
  2053. isOutline: true,
  2054. ),
  2055. if (peer.password.isNotEmpty)
  2056. dialogButton(
  2057. "Remove",
  2058. icon: Icon(Icons.delete_outline_rounded),
  2059. onPressed: () => change(''),
  2060. buttonStyle: ButtonStyle(
  2061. backgroundColor: MaterialStatePropertyAll(Colors.red)),
  2062. ),
  2063. Obx(() => dialogButton(
  2064. "OK",
  2065. icon: Icon(Icons.done_rounded),
  2066. onPressed:
  2067. isInputEmpty.value ? null : () => change(controller.text),
  2068. )),
  2069. ],
  2070. onSubmit: isInputEmpty.value ? null : () => change(controller.text),
  2071. onCancel: cancel,
  2072. );
  2073. });
  2074. }
  2075. void CommonConfirmDialog(OverlayDialogManager dialogManager, String content,
  2076. VoidCallback onConfirm) {
  2077. dialogManager.show((setState, close, context) {
  2078. submit() {
  2079. close();
  2080. onConfirm.call();
  2081. }
  2082. return CustomAlertDialog(
  2083. content: Row(
  2084. children: [
  2085. Expanded(
  2086. child: Text(content,
  2087. style: const TextStyle(fontSize: 15),
  2088. textAlign: TextAlign.start),
  2089. ),
  2090. ],
  2091. ).marginOnly(bottom: 12),
  2092. actions: [
  2093. dialogButton(translate("Cancel"), onPressed: close, isOutline: true),
  2094. dialogButton(translate("OK"), onPressed: submit),
  2095. ],
  2096. onSubmit: submit,
  2097. onCancel: close,
  2098. );
  2099. });
  2100. }
  2101. void changeUnlockPinDialog(String oldPin, Function() callback) {
  2102. final pinController = TextEditingController(text: oldPin);
  2103. final confirmController = TextEditingController(text: oldPin);
  2104. String? pinErrorText;
  2105. String? confirmationErrorText;
  2106. final maxLength = bind.mainMaxEncryptLen();
  2107. gFFI.dialogManager.show((setState, close, context) {
  2108. submit() async {
  2109. pinErrorText = null;
  2110. confirmationErrorText = null;
  2111. final pin = pinController.text.trim();
  2112. final confirm = confirmController.text.trim();
  2113. if (pin != confirm) {
  2114. setState(() {
  2115. confirmationErrorText =
  2116. translate('The confirmation is not identical.');
  2117. });
  2118. return;
  2119. }
  2120. final errorMsg = bind.mainSetUnlockPin(pin: pin);
  2121. if (errorMsg != '') {
  2122. setState(() {
  2123. pinErrorText = translate(errorMsg);
  2124. });
  2125. return;
  2126. }
  2127. callback.call();
  2128. close();
  2129. }
  2130. return CustomAlertDialog(
  2131. title: Text(translate("Set PIN")),
  2132. content: Column(
  2133. children: [
  2134. DialogTextField(
  2135. title: 'PIN',
  2136. controller: pinController,
  2137. obscureText: true,
  2138. errorText: pinErrorText,
  2139. maxLength: maxLength,
  2140. ),
  2141. DialogTextField(
  2142. title: translate('Confirmation'),
  2143. controller: confirmController,
  2144. obscureText: true,
  2145. errorText: confirmationErrorText,
  2146. maxLength: maxLength,
  2147. )
  2148. ],
  2149. ).marginOnly(bottom: 12),
  2150. actions: [
  2151. dialogButton(translate("Cancel"), onPressed: close, isOutline: true),
  2152. dialogButton(translate("OK"), onPressed: submit),
  2153. ],
  2154. onSubmit: submit,
  2155. onCancel: close,
  2156. );
  2157. });
  2158. }
  2159. void checkUnlockPinDialog(String correctPin, Function() passCallback) {
  2160. final controller = TextEditingController();
  2161. String? errorText;
  2162. gFFI.dialogManager.show((setState, close, context) {
  2163. submit() async {
  2164. final pin = controller.text.trim();
  2165. if (correctPin != pin) {
  2166. setState(() {
  2167. errorText = translate('Wrong PIN');
  2168. });
  2169. return;
  2170. }
  2171. passCallback.call();
  2172. close();
  2173. }
  2174. return CustomAlertDialog(
  2175. content: Row(
  2176. children: [
  2177. Expanded(
  2178. child: PasswordWidget(
  2179. title: 'PIN',
  2180. controller: controller,
  2181. errorText: errorText,
  2182. hintText: '',
  2183. ))
  2184. ],
  2185. ).marginOnly(bottom: 12),
  2186. actions: [
  2187. dialogButton(translate("Cancel"), onPressed: close, isOutline: true),
  2188. dialogButton(translate("OK"), onPressed: submit),
  2189. ],
  2190. onSubmit: submit,
  2191. onCancel: close,
  2192. );
  2193. });
  2194. }
  2195. void confrimDeleteTrustedDevicesDialog(
  2196. RxList<TrustedDevice> trustedDevices, RxList<Uint8List> selectedDevices) {
  2197. CommonConfirmDialog(gFFI.dialogManager, '${translate('Confirm Delete')}?',
  2198. () async {
  2199. if (selectedDevices.isEmpty) return;
  2200. if (selectedDevices.length == trustedDevices.length) {
  2201. await bind.mainClearTrustedDevices();
  2202. trustedDevices.clear();
  2203. selectedDevices.clear();
  2204. } else {
  2205. final json = jsonEncode(selectedDevices.map((e) => e.toList()).toList());
  2206. await bind.mainRemoveTrustedDevices(json: json);
  2207. trustedDevices.removeWhere((element) {
  2208. return selectedDevices.contains(element.hwid);
  2209. });
  2210. selectedDevices.clear();
  2211. }
  2212. });
  2213. }
  2214. void manageTrustedDeviceDialog() async {
  2215. RxList<TrustedDevice> trustedDevices = (await TrustedDevice.get()).obs;
  2216. RxList<Uint8List> selectedDevices = RxList.empty();
  2217. gFFI.dialogManager.show((setState, close, context) {
  2218. return CustomAlertDialog(
  2219. title: Text(translate("Manage trusted devices")),
  2220. content: trustedDevicesTable(trustedDevices, selectedDevices),
  2221. actions: [
  2222. Obx(() => dialogButton(translate("Delete"),
  2223. onPressed: selectedDevices.isEmpty
  2224. ? null
  2225. : () {
  2226. confrimDeleteTrustedDevicesDialog(
  2227. trustedDevices,
  2228. selectedDevices,
  2229. );
  2230. },
  2231. isOutline: false)
  2232. .marginOnly(top: 12)),
  2233. dialogButton(translate("Close"), onPressed: close, isOutline: true)
  2234. .marginOnly(top: 12),
  2235. ],
  2236. onCancel: close,
  2237. );
  2238. });
  2239. }
  2240. class TrustedDevice {
  2241. late final Uint8List hwid;
  2242. late final int time;
  2243. late final String id;
  2244. late final String name;
  2245. late final String platform;
  2246. TrustedDevice.fromJson(Map<String, dynamic> json) {
  2247. final hwidList = json['hwid'] as List<dynamic>;
  2248. hwid = Uint8List.fromList(hwidList.cast<int>());
  2249. time = json['time'];
  2250. id = json['id'];
  2251. name = json['name'];
  2252. platform = json['platform'];
  2253. }
  2254. String daysRemaining() {
  2255. final expiry = time + 90 * 24 * 60 * 60 * 1000;
  2256. final remaining = expiry - DateTime.now().millisecondsSinceEpoch;
  2257. if (remaining < 0) {
  2258. return '0';
  2259. }
  2260. return (remaining / (24 * 60 * 60 * 1000)).toStringAsFixed(0);
  2261. }
  2262. static Future<List<TrustedDevice>> get() async {
  2263. final List<TrustedDevice> devices = List.empty(growable: true);
  2264. try {
  2265. final devicesJson = await bind.mainGetTrustedDevices();
  2266. if (devicesJson.isNotEmpty) {
  2267. final devicesList = json.decode(devicesJson);
  2268. if (devicesList is List) {
  2269. for (var device in devicesList) {
  2270. devices.add(TrustedDevice.fromJson(device));
  2271. }
  2272. }
  2273. }
  2274. } catch (e) {
  2275. print(e.toString());
  2276. }
  2277. devices.sort((a, b) => b.time.compareTo(a.time));
  2278. return devices;
  2279. }
  2280. }
  2281. Widget trustedDevicesTable(
  2282. RxList<TrustedDevice> devices, RxList<Uint8List> selectedDevices) {
  2283. RxBool selectAll = false.obs;
  2284. setSelectAll() {
  2285. if (selectedDevices.isNotEmpty &&
  2286. selectedDevices.length == devices.length) {
  2287. selectAll.value = true;
  2288. } else {
  2289. selectAll.value = false;
  2290. }
  2291. }
  2292. devices.listen((_) {
  2293. setSelectAll();
  2294. });
  2295. selectedDevices.listen((_) {
  2296. setSelectAll();
  2297. });
  2298. return FittedBox(
  2299. child: Obx(() => DataTable(
  2300. columns: [
  2301. DataColumn(
  2302. label: Checkbox(
  2303. value: selectAll.value,
  2304. onChanged: (value) {
  2305. if (value == true) {
  2306. selectedDevices.clear();
  2307. selectedDevices.addAll(devices.map((e) => e.hwid));
  2308. } else {
  2309. selectedDevices.clear();
  2310. }
  2311. },
  2312. )),
  2313. DataColumn(label: Text(translate('Platform'))),
  2314. DataColumn(label: Text(translate('ID'))),
  2315. DataColumn(label: Text(translate('Username'))),
  2316. DataColumn(label: Text(translate('Days remaining'))),
  2317. ],
  2318. rows: devices.map((device) {
  2319. return DataRow(cells: [
  2320. DataCell(Checkbox(
  2321. value: selectedDevices.contains(device.hwid),
  2322. onChanged: (value) {
  2323. if (value == null) return;
  2324. if (value) {
  2325. selectedDevices.remove(device.hwid);
  2326. selectedDevices.add(device.hwid);
  2327. } else {
  2328. selectedDevices.remove(device.hwid);
  2329. }
  2330. },
  2331. )),
  2332. DataCell(Text(device.platform)),
  2333. DataCell(Text(device.id)),
  2334. DataCell(Text(device.name)),
  2335. DataCell(Text(device.daysRemaining())),
  2336. ]);
  2337. }).toList(),
  2338. )),
  2339. );
  2340. }