-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions.js
More file actions
2029 lines (1936 loc) · 77.9 KB
/
options.js
File metadata and controls
2029 lines (1936 loc) · 77.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2025 Bastian Kleinschmidt
* Licensed under the GNU Affero General Public License v3.0.
* See LICENSE.txt for details.
*/
'use strict';
const i18n = NCI18n.translate;
const DEFAULT_SHARING_EXPIRE_DAYS = 7;
const DEFAULT_SHARING_ATTACHMENT_THRESHOLD_MB = NCSharingStorage.DEFAULT_ATTACHMENT_THRESHOLD_MB;
const DEFAULT_SHARING_SHARE_NAME = i18n("sharing_share_default") || "Share name";
const DEFAULT_TALK_TITLE = i18n("ui_default_title") || "Meeting";
const FALLBACK_POPUP_WIDTH = 520;
const FALLBACK_POPUP_HEIGHT = 320;
const SHARING_KEYS = NCSharingStorage.SHARING_KEYS;
const normalizeAttachmentThresholdMb = NCSharingStorage.normalizeAttachmentThresholdMb;
const OPTIONS_LOG_PREFIX = "[NCUI][Options]";
const SYSTEM_ADDRESSBOOK_ADMIN_URL = "https://github.com/nc-connector/NC_Connector_for_Thunderbird/blob/main/docs/ADMIN.md#system-address-book-required-for-user-search-and-moderator-selection";
const POLICY_ADMIN_URL = "https://github.com/nc-connector/NC_Connector_for_Thunderbird/blob/main/docs/ADMIN.md";
const ATTACHMENT_AUTOMATION_ADMIN_URL = "https://github.com/nc-connector/NC_Connector_for_Thunderbird/blob/main/docs/ADMIN.md#47-attachment-automation-prerequisite-disable-competing-thunderbird-compose-features";
const NC_CONNECTOR_HOMEPAGE_URL = "https://nc-connector.de";
const EMAIL_SIGNATURE_KEYS = {
onCompose: "emailSignatureOnCompose",
onReply: "emailSignatureOnReply",
onForward: "emailSignatureOnForward"
};
/**
* Log internal options-page errors.
* @param {string} scope
* @param {any} error
*/
function logOptionsError(scope, error){
try{
console.error(OPTIONS_LOG_PREFIX, scope, error);
}catch(logError){
console.error(OPTIONS_LOG_PREFIX, scope, error?.message || String(error), logError?.message || String(logError));
}
}
NCTalkDomI18n.translatePage(i18n, { titleKey: "options_title" });
initTabs();
initAbout();
const statusEl = document.getElementById("status");
const baseUrlInput = document.getElementById("baseUrl");
const userInput = document.getElementById("user");
const appPassInput = document.getElementById("appPass");
const policyWarningRow = document.getElementById("policyWarningRow");
const policyWarningText = document.getElementById("policyWarningText");
const policyWarningAdminLink = document.getElementById("policyWarningAdminLink");
const sharingBaseRow = document.getElementById("sharingBaseRow");
const sharingDefaultShareNameRow = document.getElementById("sharingDefaultShareNameRow");
const sharingDefaultPermissionsRow = document.getElementById("sharingDefaultPermissionsRow");
const sharingDefaultPasswordRow = document.getElementById("sharingDefaultPasswordRow");
const sharingBaseInput = document.getElementById("sharingBase");
const sharingDefaultShareNameInput = document.getElementById("sharingDefaultShareName");
const sharingDefaultPermCreateInput = document.getElementById("sharingDefaultPermCreate");
const sharingDefaultPermWriteInput = document.getElementById("sharingDefaultPermWrite");
const sharingDefaultPermDeleteInput = document.getElementById("sharingDefaultPermDelete");
const sharingDefaultPasswordInput = document.getElementById("sharingDefaultPassword");
const sharingDefaultPasswordSeparateRow = document.getElementById("sharingDefaultPasswordSeparateRow");
const sharingDefaultPasswordSeparateInput = document.getElementById("sharingDefaultPasswordSeparate");
const sharingDefaultExpireDaysRow = document.getElementById("sharingDefaultExpireDaysRow");
const sharingDefaultExpireDaysInput = document.getElementById("sharingDefaultExpireDays");
const sharingAttachmentsAlwaysNcInput = document.getElementById("sharingAttachmentsAlwaysNc");
const sharingAttachmentsAlwaysRow = document.getElementById("sharingAttachmentsAlwaysRow");
const sharingAttachmentsOfferRow = document.getElementById("sharingAttachmentsOfferRow");
const sharingAttachmentsOfferAboveEnabledInput = document.getElementById("sharingAttachmentsOfferAboveEnabled");
const sharingAttachmentsOfferAboveMbInput = document.getElementById("sharingAttachmentsOfferAboveMb");
const sharingAttachmentsLockBox = document.getElementById("sharingAttachmentsLock");
const sharingAttachmentsLockText = document.getElementById("sharingAttachmentsLockText");
const sharingAttachmentsAdminLink = document.getElementById("sharingAttachmentsAdminLink");
const talkDefaultTitleRow = document.getElementById("talkDefaultTitleRow");
const talkDefaultTitleInput = document.getElementById("talkDefaultTitle");
const talkDefaultLobbyRow = document.getElementById("talkDefaultLobbyRow");
const talkDefaultLobbyInput = document.getElementById("talkDefaultLobby");
const talkDefaultListableRow = document.getElementById("talkDefaultListableRow");
const talkDefaultListableInput = document.getElementById("talkDefaultListable");
const talkDefaultAddUsersInput = document.getElementById("talkDefaultAddUsers");
const talkDefaultAddUsersRow = document.getElementById("talkDefaultAddUsersRow");
const optionsAddUsersTooltipList = document.getElementById("optionsAddUsersTooltipList");
const optionsAddUsersAddressbookLockHint = document.getElementById("optionsAddUsersAddressbookLockHint");
const talkDefaultAddGuestsInput = document.getElementById("talkDefaultAddGuests");
const talkDefaultAddGuestsRow = document.getElementById("talkDefaultAddGuestsRow");
const optionsAddGuestsTooltipList = document.getElementById("optionsAddGuestsTooltipList");
const optionsAddGuestsAddressbookLockHint = document.getElementById("optionsAddGuestsAddressbookLockHint");
const talkAddressbookWarningRow = document.getElementById("talkAddressbookWarningRow");
const optionsTalkAddressbookAdminLink = document.getElementById("optionsTalkAddressbookAdminLink");
const talkDefaultPasswordRow = document.getElementById("talkDefaultPasswordRow");
const talkDefaultPasswordInput = document.getElementById("talkDefaultPassword");
const talkDeleteRoomOnEventDeleteRow = document.getElementById("talkDeleteRoomOnEventDeleteRow");
const talkDeleteRoomOnEventDeleteInput = document.getElementById("talkDeleteRoomOnEventDelete");
const talkDefaultRoomTypeRow = document.getElementById("talkDefaultRoomTypeRow");
const talkDefaultRoomTypePicker = document.getElementById("talkDefaultRoomTypePicker");
const talkDefaultRoomTypeButton = document.getElementById("talkDefaultRoomTypeButton");
const talkDefaultRoomTypeButtonLabel = document.getElementById("talkDefaultRoomTypeButtonLabel");
const talkDefaultRoomTypeDropdown = document.getElementById("talkDefaultRoomTypeDropdown");
const talkDefaultRoomTypeValueInput = document.getElementById("talkDefaultRoomType");
const talkDefaultRoomTypeOptions = Array.from(document.querySelectorAll(".options-roomtype-option"));
const shareBlockLangRow = document.getElementById("shareBlockLangRow");
const shareBlockLangSelect = document.getElementById("shareBlockLang");
const eventDescriptionLangRow = document.getElementById("eventDescriptionLangRow");
const eventDescriptionLangSelect = document.getElementById("eventDescriptionLang");
const emailSignaturePolicyHint = document.getElementById("emailSignaturePolicyHint");
const emailSignatureOnComposeRow = document.getElementById("emailSignatureOnComposeRow");
const emailSignatureOnComposeInput = document.getElementById("emailSignatureOnCompose");
const emailSignatureOnReplyRow = document.getElementById("emailSignatureOnReplyRow");
const emailSignatureOnReplyInput = document.getElementById("emailSignatureOnReply");
const emailSignatureOnForwardRow = document.getElementById("emailSignatureOnForwardRow");
const emailSignatureOnForwardInput = document.getElementById("emailSignatureOnForward");
const DEFAULT_SHARING_BASE = (typeof NCSharing !== "undefined" ? NCSharing.DEFAULT_BASE_PATH : "NC Connector");
let statusTimer = null;
let composeAttachmentSettingsLocked = false;
let runtimePolicyStatus = null;
let policyLockTalkAddUsers = false;
let policyLockTalkAddGuests = false;
let policyLockSharingAttachmentsAlways = false;
let policyLockSharingAttachmentsThreshold = false;
let talkAddressbookLockActive = false;
let talkAddressbookLockDetail = "";
let emailSignatureStoredState = {
hasOnCompose: false,
hasOnReply: false,
hasOnForward: false
};
const SUPPORTED_OVERRIDE_LOCALES = getSupportedOverrideLocales();
const LANG_OPTIONS = new Set(["default", "custom", ...SUPPORTED_OVERRIDE_LOCALES]);
initLanguageOverrideSelects();
initTalkDefaultRoomTypePicker();
if (optionsTalkAddressbookAdminLink){
optionsTalkAddressbookAdminLink.href = SYSTEM_ADDRESSBOOK_ADMIN_URL;
}
if (policyWarningAdminLink){
policyWarningAdminLink.href = POLICY_ADMIN_URL;
}
if (sharingAttachmentsAdminLink){
sharingAttachmentsAdminLink.href = ATTACHMENT_AUTOMATION_ADMIN_URL;
}
/**
* Toggle one tooltip list between normal and lock-hint entries via shared UI helper.
* @param {HTMLElement|null} tooltipList
* @param {boolean} lockActive
*/
function applySharedAddressbookTooltipState(tooltipList, lockActive){
const applyTooltipState = window.NCAddressbookUi?.applySystemAddressbookTooltipState;
if (typeof applyTooltipState !== "function"){
return;
}
applyTooltipState(tooltipList, lockActive);
}
/**
* Read the list of supported locale folders for language override settings.
* @returns {string[]}
*/
function getSupportedOverrideLocales(){
try{
if (typeof NCI18nOverride !== "undefined" && Array.isArray(NCI18nOverride?.supportedLocales) && NCI18nOverride.supportedLocales.length){
return Array.from(new Set(NCI18nOverride.supportedLocales));
}
}catch(error){
logOptionsError("supported locales detection failed", error);
}
return ["en", "de", "fr"];
}
/**
* Initialize the language override selects in the advanced settings tab.
*/
function initLanguageOverrideSelects(){
refreshLanguageOverrideSelects();
}
/**
* Get the UI language (BCP47) used for display names.
* @returns {string}
*/
function getUiLanguage(){
try{
if (typeof browser !== "undefined" && browser?.i18n?.getUILanguage){
return browser.i18n.getUILanguage() || "en";
}
}catch(error){
logOptionsError("ui language detection failed", error);
}
return "en";
}
/**
* Convert a locale folder name to a BCP47 language tag.
* @param {string} locale
* @returns {string}
*/
function toBcp47Tag(locale){
return String(locale || "").replace(/_/g, "-");
}
/**
* Create an Intl.DisplayNames instance for language labels.
* @param {string} uiLang
* @returns {Intl.DisplayNames|null}
*/
function makeDisplayNames(uiLang){
if (typeof Intl === "undefined" || typeof Intl.DisplayNames !== "function"){
return null;
}
try{
return new Intl.DisplayNames([uiLang], { type: "language" });
}catch(error){
logOptionsError("Intl.DisplayNames init failed", error);
return null;
}
}
/**
* Create an Intl.Collator instance for locale-aware sorting.
* @param {string} uiLang
* @returns {Intl.Collator|null}
*/
function makeCollator(uiLang){
if (typeof Intl === "undefined" || typeof Intl.Collator !== "function"){
return null;
}
try{
return new Intl.Collator([uiLang], { sensitivity: "base", numeric: true });
}catch(error){
logOptionsError("Intl.Collator init failed", error);
return null;
}
}
/**
* Get a localized display label for a locale.
* @param {string} locale
* @param {Intl.DisplayNames|null} displayNames
* @returns {string}
*/
function getLocaleLabel(locale, displayNames){
const tag = toBcp47Tag(locale);
if (displayNames){
try{
const label = displayNames.of(tag);
if (label){
return label;
}
}catch(error){
logOptionsError("locale label lookup failed", error);
}
}
return tag || String(locale || "");
}
/**
* Order locales with common languages first, then UI-sorted.
* @param {string[]} locales
* @param {Intl.DisplayNames|null} displayNames
* @param {Intl.Collator|null} collator
* @returns {string[]}
*/
function orderOverrideLocales(locales, displayNames, collator){
const list = Array.isArray(locales) ? locales.slice() : [];
const primary = ["en", "de", "fr"];
const prioritized = primary.filter((locale) => list.includes(locale));
const remaining = list.filter((locale) => !prioritized.includes(locale));
const labelCache = new Map();
const labelOf = (locale) => {
if (!labelCache.has(locale)){
labelCache.set(locale, getLocaleLabel(locale, displayNames));
}
return labelCache.get(locale);
};
remaining.sort((a, b) => {
const la = labelOf(a);
const lb = labelOf(b);
const cmp = collator ? collator.compare(la, lb) : String(la).localeCompare(String(lb));
return cmp !== 0 ? cmp : String(a).localeCompare(String(b));
});
return [...prioritized, ...remaining];
}
/**
* Populate a select element with default + supported override locales.
* @param {HTMLSelectElement|null} selectEl
* @param {string[]} locales
* @param {Intl.DisplayNames|null} displayNames
*/
function populateLanguageSelect(selectEl, locales, displayNames, options = {}){
if (!selectEl){
return;
}
selectEl.textContent = "";
const showCustom = !!options.showCustom;
const enableCustom = !!options.enableCustom;
const defaultOption = document.createElement("option");
defaultOption.value = "default";
defaultOption.textContent = i18n("options_lang_default") || "default";
selectEl.appendChild(defaultOption);
if (showCustom){
const customOption = document.createElement("option");
customOption.value = "custom";
customOption.textContent = i18n("options_lang_custom") || "Custom (backend template)";
customOption.disabled = !enableCustom;
selectEl.appendChild(customOption);
}
locales.forEach((locale) => {
const option = document.createElement("option");
option.value = locale;
option.textContent = getLocaleLabel(locale, displayNames);
selectEl.appendChild(option);
});
}
/**
* Show a transient status message in the options UI.
* @param {string} message
* @param {boolean} isError
* @param {boolean} sticky
* @param {boolean} isSuccess
*/
function showStatus(message, isError = false, sticky = false, isSuccess = false){
if (statusTimer){
clearTimeout(statusTimer);
statusTimer = null;
}
statusEl.textContent = message || "";
statusEl.style.color = isError ? "#b00020" : (isSuccess ? "#11883a" : "");
if (message && !isError && !sticky){
statusTimer = setTimeout(() => {
statusEl.textContent = "";
statusTimer = null;
}, 2000);
}
}
/**
* Return true when backend policy mode is currently active.
* @returns {boolean}
*/
function isBackendPolicyActive(){
return !!runtimePolicyStatus?.policyActive;
}
/**
* Return true when one backend policy domain exists in the status payload.
* @param {"share"|"talk"|"email_signature"} domain
* @returns {boolean}
*/
function isBackendPolicyDomainAvailable(domain){
if (typeof NCPolicyRuntime !== "undefined" && typeof NCPolicyRuntime.isDomainAvailable === "function"){
return NCPolicyRuntime.isDomainAvailable(runtimePolicyStatus, domain);
}
const domainState = runtimePolicyStatus?.policyDomains?.[domain];
if (domainState && typeof domainState === "object"
&& Object.prototype.hasOwnProperty.call(domainState, "available")){
return domainState.available === true;
}
const policy = runtimePolicyStatus?.policy?.[domain];
const editable = runtimePolicyStatus?.policyEditable?.[domain];
return !!policy && typeof policy === "object" && !!editable && typeof editable === "object";
}
/**
* Return true when one backend policy domain is usable for the current seat.
* @param {"share"|"talk"|"email_signature"} domain
* @returns {boolean}
*/
function isBackendPolicyDomainActive(domain){
if (typeof NCPolicyRuntime !== "undefined" && typeof NCPolicyRuntime.isDomainActive === "function"){
return NCPolicyRuntime.isDomainActive(runtimePolicyStatus, domain);
}
const domainState = runtimePolicyStatus?.policyDomains?.[domain];
if (domainState && typeof domainState === "object"
&& Object.prototype.hasOwnProperty.call(domainState, "active")){
return domainState.active === true;
}
return isBackendPolicyActive() && isBackendPolicyDomainAvailable(domain);
}
/**
* Return true when the NC Connector backend endpoint is available.
* @returns {boolean}
*/
function isBackendEndpointAvailable(){
return !!runtimePolicyStatus?.endpointAvailable;
}
/**
* Return true when the current user has an active backend seat.
* @returns {boolean}
*/
function hasBackendSeatEntitlement(){
const status = runtimePolicyStatus?.status;
const seatState = String(status?.seatState || "").trim().toLowerCase();
return !!(
runtimePolicyStatus?.endpointAvailable
&& status?.seatAssigned
&& status?.isValid
&& seatState === "active"
);
}
/**
* Return the language policy key for one domain.
* @param {"share"|"talk"|"email_signature"} domain
* @returns {string}
*/
function getPolicyLanguageKey(domain){
return domain === "talk" ? "language_talk_description" : "language_share_html_block";
}
/**
* Return the backend template key for one domain.
* @param {"share"|"talk"|"email_signature"} domain
* @returns {string}
*/
function getPolicyTemplateKey(domain){
return domain === "talk" ? "talk_invitation_template" : "share_html_block_template";
}
/**
* Return true when backend-driven custom template mode can be selected for one domain.
* This requires an active backend policy, language=`custom`, and a non-empty template.
* @param {"share"|"talk"|"email_signature"} domain
* @returns {boolean}
*/
function isCustomLanguageModeAvailable(domain){
if (!isBackendEndpointAvailable() || !isBackendPolicyDomainActive(domain)){
return false;
}
const language = normalizeLangChoice(
coercePolicyString(readBackendPolicyValue(domain, getPolicyLanguageKey(domain)), ""),
{ allowCustom: true }
);
const template = coercePolicyString(readBackendPolicyValue(domain, getPolicyTemplateKey(domain)), "");
return language === "custom" && !!template;
}
/**
* Return true when separate password delivery is available.
* @returns {boolean}
*/
function isSeparatePasswordMailFeatureAvailable(){
return hasBackendSeatEntitlement();
}
/**
* Return the lock hint for separate password delivery when unavailable.
* @returns {string}
*/
function getSeparatePasswordUnavailableHint(){
const status = runtimePolicyStatus?.status;
const seatState = String(status?.seatState || "").trim().toLowerCase();
if (!isBackendEndpointAvailable()){
return i18n("sharing_password_separate_backend_required_tooltip")
|| "This feature requires the Nextcloud backend.";
}
if (!status?.seatAssigned){
return i18n("sharing_password_separate_no_seat_tooltip")
|| "Your administrator must assign an NC Connector seat to your account for this feature.";
}
if (!status?.isValid || seatState !== "active"){
return i18n("sharing_password_separate_paused_tooltip")
|| "Your NC Connector seat is currently paused. Please contact your Nextcloud administrator.";
}
return "";
}
/**
* Repopulate language selects and expose `custom` only when the backend exists.
* The option stays visible but disabled until the backend policy actually uses
* a custom template for the respective domain.
*/
function refreshLanguageOverrideSelects(){
const uiLang = getUiLanguage();
const displayNames = makeDisplayNames(uiLang);
const collator = makeCollator(uiLang);
const orderedLocales = orderOverrideLocales(SUPPORTED_OVERRIDE_LOCALES, displayNames, collator);
const showCustom = isBackendEndpointAvailable();
const allowShareCustom = isCustomLanguageModeAvailable("share");
const allowTalkCustom = isCustomLanguageModeAvailable("talk");
const currentShareLang = normalizeLangChoice(shareBlockLangSelect?.value, { allowCustom: allowShareCustom });
const currentTalkLang = normalizeLangChoice(eventDescriptionLangSelect?.value, { allowCustom: allowTalkCustom });
populateLanguageSelect(shareBlockLangSelect, orderedLocales, displayNames, {
showCustom,
enableCustom: allowShareCustom
});
populateLanguageSelect(eventDescriptionLangSelect, orderedLocales, displayNames, {
showCustom,
enableCustom: allowTalkCustom
});
if (shareBlockLangSelect){
shareBlockLangSelect.value = currentShareLang;
}
if (eventDescriptionLangSelect){
eventDescriptionLangSelect.value = currentTalkLang;
}
}
/**
* Read one backend policy value.
* @param {"share"|"talk"|"email_signature"} domain
* @param {string} key
* @returns {any}
*/
function readBackendPolicyValue(domain, key){
const domainPolicy = runtimePolicyStatus?.policy?.[domain];
if (!domainPolicy || typeof domainPolicy !== "object"){
return null;
}
return Object.prototype.hasOwnProperty.call(domainPolicy, key)
? domainPolicy[key]
: null;
}
/**
* Return true when a backend policy key exists, even if its value is `null`.
* @param {"share"|"talk"|"email_signature"} domain
* @param {string} key
* @returns {boolean}
*/
function hasBackendPolicyKey(domain, key){
const domainPolicy = runtimePolicyStatus?.policy?.[domain];
return !!domainPolicy
&& typeof domainPolicy === "object"
&& Object.prototype.hasOwnProperty.call(domainPolicy, key);
}
/**
* Return true when the backend explicitly disables one setting via `null`.
* @param {"share"|"talk"|"email_signature"} domain
* @param {string} key
* @returns {boolean}
*/
function isBackendPolicyExplicitNull(domain, key){
return hasBackendPolicyKey(domain, key) && readBackendPolicyValue(domain, key) == null;
}
/**
* Return true when a policy setting is admin-locked.
* @param {"share"|"talk"|"email_signature"} domain
* @param {string} key
* @returns {boolean}
*/
function isPolicyLocked(domain, key){
if (!isBackendPolicyDomainActive(domain)){
return false;
}
const editableDomain = runtimePolicyStatus?.policyEditable?.[domain];
if (!editableDomain || typeof editableDomain !== "object"){
return false;
}
return editableDomain[key] === false;
}
/**
* Return the localized admin-controlled tooltip text.
* @returns {string}
*/
function getAdminControlledHint(){
return i18n("policy_admin_controlled_tooltip") || "Admin controlled";
}
/**
* Convert a policy value to boolean while keeping a fallback.
* @param {any} value
* @param {boolean} fallback
* @returns {boolean}
*/
function coercePolicyBoolean(value, fallback){
if (value === true){
return true;
}
if (value === false){
return false;
}
return fallback;
}
/**
* Convert a policy value to integer while keeping a fallback.
* @param {any} value
* @param {number} fallback
* @returns {number}
*/
function coercePolicyInt(value, fallback){
const parsed = Number.parseInt(String(value ?? ""), 10);
if (!Number.isFinite(parsed)){
return fallback;
}
return parsed;
}
/**
* Convert a policy value to non-empty string while keeping a fallback.
* @param {any} value
* @param {string} fallback
* @returns {string}
*/
function coercePolicyString(value, fallback){
const text = String(value ?? "").trim();
return text || fallback;
}
/**
* Resolve one persisted value with policy lock override.
* @param {"share"|"talk"|"email_signature"} domain
* @param {string} key
* @param {any} localValue
* @param {(value:any, fallback:any)=>any} coerce
* @returns {any}
*/
function resolvePersistedValue(domain, key, localValue, coerce){
if (!isPolicyLocked(domain, key)){
return localValue;
}
const policyValue = readBackendPolicyValue(domain, key);
return typeof coerce === "function" ? coerce(policyValue, localValue) : localValue;
}
/**
* Apply one lock title to row/input elements.
* @param {HTMLElement|null} row
* @param {HTMLElement|null} input
* @param {boolean} locked
*/
function applyLockTitle(row, input, locked){
const title = locked ? getAdminControlledHint() : "";
if (row){
row.title = title;
row.classList.toggle("is-disabled", !!locked);
}
if (input){
input.title = title;
}
}
function normalizeEmailAddress(value){
const email = String(value || "").trim().toLowerCase();
return email.includes("@") ? email : "";
}
function getEmailSignatureUnavailableHint(){
const status = runtimePolicyStatus?.status;
const seatState = String(status?.seatState || "").trim().toLowerCase();
if (!isBackendEndpointAvailable()){
return i18n("sharing_password_separate_backend_required_tooltip")
|| "This feature requires the Nextcloud backend.";
}
if (!status?.seatAssigned){
return i18n("sharing_password_separate_no_seat_tooltip")
|| "Your administrator must assign an NC Connector seat to your account for this feature.";
}
if (!status?.isValid || seatState !== "active"){
return i18n("sharing_password_separate_paused_tooltip")
|| "Your NC Connector seat is currently paused. Please contact your Nextcloud administrator.";
}
if (!isBackendPolicyDomainAvailable("email_signature")){
return i18n("options_signature_backend_update_required_tooltip")
|| "Please update the NC Connector backend. This backend version does not support central email signatures yet.";
}
if (!isBackendPolicyDomainActive("email_signature")
|| readBackendPolicyValue("email_signature", "email_signature_on_compose") !== true
|| !String(readBackendPolicyValue("email_signature", "email_signature_template") || "").trim()
|| !normalizeEmailAddress(readBackendPolicyValue("email_signature", "user_email"))){
return i18n("options_signature_backend_inactive_tooltip")
|| "Central signature policy is inactive or incomplete.";
}
return "";
}
function isEmailSignatureRuntimeAvailable(){
return !getEmailSignatureUnavailableHint();
}
function applyEmailSignatureRowState(row, input, disabled, title){
if (row){
row.classList.toggle("is-disabled", !!disabled);
row.title = title || "";
}
if (input){
input.disabled = !!disabled;
input.title = title || "";
}
}
function applyEmailSignatureSettingsOverlay(){
const runtimeAvailable = isEmailSignatureRuntimeAvailable();
const backendOnCompose = readBackendPolicyValue("email_signature", "email_signature_on_compose") === true;
const backendOnReply = readBackendPolicyValue("email_signature", "email_signature_on_reply") === true;
const backendOnForward = readBackendPolicyValue("email_signature", "email_signature_on_forward") === true;
const lockOnCompose = isPolicyLocked("email_signature", "email_signature_on_compose");
const lockOnReply = isPolicyLocked("email_signature", "email_signature_on_reply");
const lockOnForward = isPolicyLocked("email_signature", "email_signature_on_forward");
const inactiveHint = getEmailSignatureUnavailableHint();
const adminHint = getAdminControlledHint();
if (emailSignaturePolicyHint){
emailSignaturePolicyHint.textContent = inactiveHint || "";
emailSignaturePolicyHint.hidden = !inactiveHint;
}
if (emailSignatureOnComposeInput){
if (!runtimeAvailable){
emailSignatureOnComposeInput.checked = false;
}else if (lockOnCompose || !emailSignatureStoredState.hasOnCompose){
emailSignatureOnComposeInput.checked = backendOnCompose;
}
applyEmailSignatureRowState(
emailSignatureOnComposeRow,
emailSignatureOnComposeInput,
!runtimeAvailable || lockOnCompose,
!runtimeAvailable ? inactiveHint : (lockOnCompose ? adminHint : "")
);
}
const composeEnabled = runtimeAvailable && emailSignatureOnComposeInput?.checked === true;
if (emailSignatureOnReplyInput){
if (!composeEnabled){
emailSignatureOnReplyInput.checked = false;
}else if (lockOnReply || !emailSignatureStoredState.hasOnReply){
emailSignatureOnReplyInput.checked = backendOnReply;
}
applyEmailSignatureRowState(
emailSignatureOnReplyRow,
emailSignatureOnReplyInput,
!composeEnabled || lockOnReply,
!runtimeAvailable ? inactiveHint : (lockOnReply ? adminHint : "")
);
}
if (emailSignatureOnForwardInput){
if (!composeEnabled){
emailSignatureOnForwardInput.checked = false;
}else if (lockOnForward || !emailSignatureStoredState.hasOnForward){
emailSignatureOnForwardInput.checked = backendOnForward;
}
applyEmailSignatureRowState(
emailSignatureOnForwardRow,
emailSignatureOnForwardInput,
!composeEnabled || lockOnForward,
!runtimeAvailable ? inactiveHint : (lockOnForward ? adminHint : "")
);
}
}
/**
* Show/hide the policy warning in options.
*/
function applyPolicyWarningUi(){
if (!policyWarningRow){
return;
}
const warning = runtimePolicyStatus?.warning || {};
const visible = !!warning.visible;
policyWarningRow.hidden = !visible;
if (!visible){
return;
}
let message = i18n("policy_warning_license_invalid");
if (warning.code === "license_invalid"){
message = i18n("policy_warning_license_invalid");
}
if (policyWarningText){
policyWarningText.textContent = message || "";
}
}
/**
* Refresh backend policy runtime status.
* @returns {Promise<void>}
*/
async function refreshBackendPolicyStatus(){
try{
const response = await browser.runtime.sendMessage({
type: "policy:getStatus"
});
if (response?.ok && response.status){
runtimePolicyStatus = response.status;
}
}catch(error){
logOptionsError("policy status check failed", error);
}
refreshLanguageOverrideSelects();
applyPolicyWarningUi();
applyPolicySettingsOverlay();
}
/**
* Apply policy values and lock states to options controls.
* Locked controls always show the policy value.
*/
function applyPolicySettingsOverlay(){
const lockShareBase = isPolicyLocked("share", "share_base_directory");
const lockShareName = isPolicyLocked("share", "share_name_template");
const lockPermUpload = isPolicyLocked("share", "share_permission_upload");
const lockPermEdit = isPolicyLocked("share", "share_permission_edit");
const lockPermDelete = isPolicyLocked("share", "share_permission_delete");
const lockSharePassword = isPolicyLocked("share", "share_set_password");
const lockSharePasswordSeparate = isPolicyLocked("share", "share_send_password_separately");
const lockShareExpire = isPolicyLocked("share", "share_expire_days");
policyLockSharingAttachmentsAlways = isPolicyLocked("share", "attachments_always_via_ncconnector");
policyLockSharingAttachmentsThreshold = isPolicyLocked("share", "attachments_min_size_mb");
const lockShareLang = isPolicyLocked("share", "language_share_html_block");
const lockTalkTitle = isPolicyLocked("talk", "talk_title");
const lockTalkLobby = isPolicyLocked("talk", "talk_lobby_active");
const lockTalkListable = isPolicyLocked("talk", "talk_show_in_search");
policyLockTalkAddUsers = isPolicyLocked("talk", "talk_add_users");
policyLockTalkAddGuests = isPolicyLocked("talk", "talk_add_guests");
const lockTalkPassword = isPolicyLocked("talk", "talk_set_password");
const lockTalkDeleteRoomOnEventDelete = isPolicyLocked("talk", "talk_delete_room_on_event_delete");
const lockTalkRoomType = isPolicyLocked("talk", "talk_room_type");
const lockTalkLang = isPolicyLocked("talk", "language_talk_description");
if (lockShareBase && sharingBaseInput){
sharingBaseInput.value = coercePolicyString(readBackendPolicyValue("share", "share_base_directory"), sharingBaseInput.value || DEFAULT_SHARING_BASE);
}
if (lockShareName && sharingDefaultShareNameInput){
sharingDefaultShareNameInput.value = coercePolicyString(readBackendPolicyValue("share", "share_name_template"), sharingDefaultShareNameInput.value || DEFAULT_SHARING_SHARE_NAME);
}
if (lockPermUpload && sharingDefaultPermCreateInput){
sharingDefaultPermCreateInput.checked = coercePolicyBoolean(readBackendPolicyValue("share", "share_permission_upload"), sharingDefaultPermCreateInput.checked);
}
if (lockPermEdit && sharingDefaultPermWriteInput){
sharingDefaultPermWriteInput.checked = coercePolicyBoolean(readBackendPolicyValue("share", "share_permission_edit"), sharingDefaultPermWriteInput.checked);
}
if (lockPermDelete && sharingDefaultPermDeleteInput){
sharingDefaultPermDeleteInput.checked = coercePolicyBoolean(readBackendPolicyValue("share", "share_permission_delete"), sharingDefaultPermDeleteInput.checked);
}
if (lockSharePassword && sharingDefaultPasswordInput){
sharingDefaultPasswordInput.checked = coercePolicyBoolean(readBackendPolicyValue("share", "share_set_password"), sharingDefaultPasswordInput.checked);
}
if (lockSharePasswordSeparate && sharingDefaultPasswordSeparateInput){
sharingDefaultPasswordSeparateInput.checked = coercePolicyBoolean(readBackendPolicyValue("share", "share_send_password_separately"), sharingDefaultPasswordSeparateInput.checked);
}
if (!isSeparatePasswordMailFeatureAvailable() && sharingDefaultPasswordSeparateInput){
sharingDefaultPasswordSeparateInput.checked = false;
}
if (lockShareExpire && sharingDefaultExpireDaysInput){
sharingDefaultExpireDaysInput.value = String(
NCTalkTextUtils.normalizeExpireDays(
coercePolicyInt(readBackendPolicyValue("share", "share_expire_days"), Number.parseInt(sharingDefaultExpireDaysInput.value || "", 10)),
DEFAULT_SHARING_EXPIRE_DAYS
)
);
}
if (policyLockSharingAttachmentsAlways && sharingAttachmentsAlwaysNcInput){
sharingAttachmentsAlwaysNcInput.checked = coercePolicyBoolean(
readBackendPolicyValue("share", "attachments_always_via_ncconnector"),
sharingAttachmentsAlwaysNcInput.checked
);
}
if (policyLockSharingAttachmentsThreshold && sharingAttachmentsOfferAboveMbInput){
const thresholdDisabled = isBackendPolicyExplicitNull("share", "attachments_min_size_mb");
if (!thresholdDisabled){
sharingAttachmentsOfferAboveMbInput.value = String(
normalizeAttachmentThresholdMb(
coercePolicyInt(
readBackendPolicyValue("share", "attachments_min_size_mb"),
Number.parseInt(sharingAttachmentsOfferAboveMbInput.value || "", 10)
)
)
);
}
if (sharingAttachmentsOfferAboveEnabledInput){
sharingAttachmentsOfferAboveEnabledInput.checked = !thresholdDisabled;
}
}
if (lockShareLang && shareBlockLangSelect){
shareBlockLangSelect.value = normalizeLangChoice(
coercePolicyString(readBackendPolicyValue("share", "language_share_html_block"), shareBlockLangSelect.value),
{ allowCustom: isCustomLanguageModeAvailable("share") }
);
}
if (lockTalkTitle && talkDefaultTitleInput){
talkDefaultTitleInput.value = coercePolicyString(readBackendPolicyValue("talk", "talk_title"), talkDefaultTitleInput.value || DEFAULT_TALK_TITLE);
}
if (lockTalkLobby && talkDefaultLobbyInput){
talkDefaultLobbyInput.checked = coercePolicyBoolean(readBackendPolicyValue("talk", "talk_lobby_active"), talkDefaultLobbyInput.checked);
}
if (lockTalkListable && talkDefaultListableInput){
talkDefaultListableInput.checked = coercePolicyBoolean(readBackendPolicyValue("talk", "talk_show_in_search"), talkDefaultListableInput.checked);
}
if (policyLockTalkAddUsers && talkDefaultAddUsersInput){
talkDefaultAddUsersInput.checked = coercePolicyBoolean(readBackendPolicyValue("talk", "talk_add_users"), talkDefaultAddUsersInput.checked);
}
if (policyLockTalkAddGuests && talkDefaultAddGuestsInput){
talkDefaultAddGuestsInput.checked = coercePolicyBoolean(readBackendPolicyValue("talk", "talk_add_guests"), talkDefaultAddGuestsInput.checked);
}
if (lockTalkPassword && talkDefaultPasswordInput){
talkDefaultPasswordInput.checked = coercePolicyBoolean(readBackendPolicyValue("talk", "talk_set_password"), talkDefaultPasswordInput.checked);
}
if (lockTalkDeleteRoomOnEventDelete && talkDeleteRoomOnEventDeleteInput){
talkDeleteRoomOnEventDeleteInput.checked = coercePolicyBoolean(
readBackendPolicyValue("talk", "talk_delete_room_on_event_delete"),
talkDeleteRoomOnEventDeleteInput.checked
);
}
if (lockTalkRoomType){
const raw = coercePolicyString(readBackendPolicyValue("talk", "talk_room_type"), getSelectedTalkDefaultRoomType());
setTalkDefaultRoomType(raw === "event" ? "event" : "normal");
}
if (lockTalkLang && eventDescriptionLangSelect){
eventDescriptionLangSelect.value = normalizeLangChoice(
coercePolicyString(readBackendPolicyValue("talk", "language_talk_description"), eventDescriptionLangSelect.value),
{ allowCustom: isCustomLanguageModeAvailable("talk") }
);
}
if (sharingBaseInput){
sharingBaseInput.disabled = lockShareBase;
applyLockTitle(sharingBaseRow, sharingBaseInput, lockShareBase);
}
if (sharingDefaultShareNameInput){
sharingDefaultShareNameInput.disabled = lockShareName;
applyLockTitle(sharingDefaultShareNameRow, sharingDefaultShareNameInput, lockShareName);
}
if (sharingDefaultPermCreateInput){
sharingDefaultPermCreateInput.disabled = lockPermUpload;
}
if (sharingDefaultPermWriteInput){
sharingDefaultPermWriteInput.disabled = lockPermEdit;
}
if (sharingDefaultPermDeleteInput){
sharingDefaultPermDeleteInput.disabled = lockPermDelete;
}
applyLockTitle(
sharingDefaultPermissionsRow,
sharingDefaultPermCreateInput,
lockPermUpload || lockPermEdit || lockPermDelete
);
if (sharingDefaultPasswordInput){
sharingDefaultPasswordInput.disabled = lockSharePassword;
applyLockTitle(sharingDefaultPasswordRow, sharingDefaultPasswordInput, lockSharePassword);
}
if (sharingDefaultExpireDaysInput){
sharingDefaultExpireDaysInput.disabled = lockShareExpire;
applyLockTitle(sharingDefaultExpireDaysRow, sharingDefaultExpireDaysInput, lockShareExpire);
}
if (shareBlockLangSelect){
shareBlockLangSelect.disabled = lockShareLang;
applyLockTitle(shareBlockLangRow, shareBlockLangSelect, lockShareLang);
}
if (talkDefaultTitleInput){
talkDefaultTitleInput.disabled = lockTalkTitle;
applyLockTitle(talkDefaultTitleRow, talkDefaultTitleInput, lockTalkTitle);
}
if (talkDefaultLobbyInput){
talkDefaultLobbyInput.disabled = lockTalkLobby;
applyLockTitle(talkDefaultLobbyRow, talkDefaultLobbyInput, lockTalkLobby);
}
if (talkDefaultListableInput){
talkDefaultListableInput.disabled = lockTalkListable;
applyLockTitle(talkDefaultListableRow, talkDefaultListableInput, lockTalkListable);
}
if (talkDefaultPasswordInput){
talkDefaultPasswordInput.disabled = lockTalkPassword;
applyLockTitle(talkDefaultPasswordRow, talkDefaultPasswordInput, lockTalkPassword);
}
if (talkDeleteRoomOnEventDeleteInput){
talkDeleteRoomOnEventDeleteInput.disabled = lockTalkDeleteRoomOnEventDelete;
applyLockTitle(talkDeleteRoomOnEventDeleteRow, talkDeleteRoomOnEventDeleteInput, lockTalkDeleteRoomOnEventDelete);
}
if (eventDescriptionLangSelect){
eventDescriptionLangSelect.disabled = lockTalkLang;
applyLockTitle(eventDescriptionLangRow, eventDescriptionLangSelect, lockTalkLang);
}
if (talkDefaultRoomTypeButton){
talkDefaultRoomTypeButton.disabled = lockTalkRoomType;
applyLockTitle(talkDefaultRoomTypeRow, talkDefaultRoomTypeButton, lockTalkRoomType);
}
if (lockTalkRoomType){
closeTalkDefaultRoomTypeDropdown();
}
applyEmailSignatureSettingsOverlay();
updateSharingPasswordState();
updateAttachmentThresholdState();
applyTalkSystemAddressbookLockState(talkAddressbookLockActive, talkAddressbookLockDetail);
}
/**
* Load settings from storage and populate the options UI.
* @returns {Promise<void>}
*/
async function load(){
if (NCSharingStorage?.migrateLegacySharingKeys){
await NCSharingStorage.migrateLegacySharingKeys();
}
const stored = await browser.storage.local.get([
"baseUrl",
"user",
"appPass",
"debugEnabled",
"authMode",
SHARING_KEYS.basePath,
SHARING_KEYS.defaultShareName,
SHARING_KEYS.defaultPermCreate,