summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/dialogs/calendar-creation.js
blob: b4d2a7c2e4910294ea98db062b9609b24187298c (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");
var { ExtensionParent } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionParent.sys.mjs"
);
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

ChromeUtils.defineModuleGetter(this, "MsgAuthPrompt", "resource:///modules/MsgAsyncPrompter.jsm");

/* exported checkRequired, fillLocationPlaceholder, selectProvider, updateNoCredentials, */

/* import-globals-from calendar-identity-utils.js */

/**
 * For managing dialog button handler state. Stores the current handlers so we
 * can remove them with removeEventListener. Provides a way to look up the
 * button handler functions to be used with a given panel.
 */
var gButtonHandlers = {
  accept: null,
  extra2: null,

  // Maps a panel DOM node ID to the button handlers to use for that panel.
  forNodeId: {
    "panel-select-calendar-type": {
      accept: selectCalendarType,
    },
    "panel-local-calendar-settings": {
      accept: registerLocalCalendar,
      extra2: () => selectPanel("panel-select-calendar-type"),
    },
    "panel-network-calendar-settings": {
      accept: event => {
        event.preventDefault();
        event.stopPropagation();
        findCalendars();
      },
      extra2: () => selectPanel("panel-select-calendar-type"),
    },
    "panel-select-calendars": {
      accept: createNetworkCalendars,
      extra2: () => selectPanel("panel-network-calendar-settings"),
    },
    "panel-addon-calendar-settings": {
      extra2: () => selectPanel("panel-select-calendar-type"),
      // This 'accept' is set dynamically when the calendar type is selected.
      accept: null,
    },
  },
};

/** @type {calICalendar | null} */
var gLocalCalendar = null;

/**
 * A type of calendar that can be created with this dialog.
 *
 * @typedef {CalendarType}
 * @property {string} id              A unique ID for this type, e.g. "local" or
 *                                      "network" for built-in types and
 *                                      "3" or "4" for add-on types.
 * @property {boolean} builtIn        Whether this is a built in type.
 * @property {Function} onSelected    The "accept" button handler to call when
 *                                      the type is selected.
 * @property {string} [label]         Text to use in calendar type selection UI.
 * @property {string} [panelSrc]      The "src" property for the <browser> for
 *                                      this type's settings panel, typically a
 *                                      path to an html document. Only needed
 *                                      for types registered by add-ons.
 * @property {Function} [onCreated]   The "accept" button handler for this
 *                                      type's settings panel. Only needed for
 *                                      types registered by add-ons.
 */

/**
 * Registry of calendar types. The key should match the type's `id` property.
 * Add-ons may register additional types.
 *
 * @type {Map<string, CalendarType>}
 */
var gCalendarTypes = new Map([
  [
    "local",
    {
      id: "local",
      builtIn: true,
      onSelected: () => {
        // Create a local calendar to use, so we can share code with the calendar
        // preferences dialog.
        if (!gLocalCalendar) {
          gLocalCalendar = cal.manager.createCalendar(
            "storage",
            Services.io.newURI("moz-storage-calendar://")
          );

          initMailIdentitiesRow(gLocalCalendar);
          notifyOnIdentitySelection(gLocalCalendar);
        }
        selectPanel("panel-local-calendar-settings");
      },
    },
  ],
  [
    "network",
    {
      id: "network",
      builtIn: true,
      onSelected: () => selectPanel("panel-network-calendar-settings"),
    },
  ],
]);

/** @type {CalendarType | null} */
var gSelectedCalendarType = null;

/**
 * Register a calendar type to offer in the dialog. For add-ons to use. Add-on
 * code should store the returned ID and use it for unregistering the type.
 *
 * @param {CalendarType} type - The type object to register.
 * @returns {string} The generated ID for the type.
 */
function registerCalendarType(type) {
  type.id = String(gCalendarTypes.size + 1);
  type.builtIn = false;

  if (!type.onSelected) {
    type.onSelected = () => selectPanel("panel-addon-calendar-settings");
  }
  gCalendarTypes.set(type.id, type);

  // Add an option for this type to the "select calendar type" panel.
  let radiogroup = document.getElementById("calendar-type");
  let radio = document.createXULElement("radio");
  radio.setAttribute("value", type.id);
  radio.setAttribute("label", type.label);
  radiogroup.appendChild(radio);

  return type.id;
}

/**
 * Unregister a calendar type. For add-ons to use.
 *
 * @param {string} id - The ID of the type to unregister.
 */
function unregisterCalendarType(id) {
  // Don't allow unregistration of built-in types.
  if (gCalendarTypes.get(id)?.builtIn) {
    cal.WARN(
      `calendar creation dialog: unregistering calendar type "${id}"` +
        " failed because it is a built in type"
    );
    return;
  }
  // We are using the size of gCalendarTypes to generate unique IDs for
  // registered types, so don't fully remove the type.
  gCalendarTypes.set(id, undefined);

  // Remove the option for this type from the "select calendar type" panel.
  let radiogroup = document.getElementById("calendar-type");
  let radio = radiogroup.querySelector(`[value="${id}"]`);
  if (radio) {
    radiogroup.removeChild(radio);
  }
}

/**
 * Tools for managing how providers are used for calendar detection. May be used
 * by add-ons to modify which providers are used and which results are preferred.
 */
var gProviderUsage = {
  /**
   * A function that returns a list of provider types to filter out and not use
   * to detect calendars, for a given location and username. The providers are
   * filtered out before calendar detection. For example, the "Provider for
   * Google Calendar" add-on might filter out the "caldav" provider:
   *
   *  (providers, location, username) => {
   *    domain = username.split("@")[1];
   *    if (providers.includes("gdata") && (domain == "googlemail.com" || domain == "gmail.com")) {
   *      return ["caldav"];
   *    }
   *    return [];
   *  }
   *
   * @callback ProviderFilter
   * @param {string[]} providers - Array of provider types to be used (if not filtered out).
   * @param {string} location - Location to use for calendar detection.
   * @param {string} username - Username to use for calendar detection.
   * @returns {string[]} Array of provider types to be filtered out.
   */

  /** @type {ProviderFilter[]} */
  _preDetectFilters: [],

  /**
   * A mapping from a less preferred provider type to a set of more preferred
   * provider types. Used after calendar detection to default to a more
   * preferred provider when there are results from more than one provider.
   *
   * @typedef {Map<string, Set<string>>} ProviderPreferences
   */

  /**
   * @type {ProviderPreferences}
   */
  _postDetectPreferences: new Map(),

  get preDetectFilters() {
    return this._preDetectFilters;
  },

  get postDetectPreferences() {
    return this._postDetectPreferences;
  },

  /**
   * Add a new provider filter function.
   *
   * @param {ProviderFilter} providerFilter
   */
  addPreDetectFilter(providerFilter) {
    this._preDetectFilters.push(providerFilter);
  },

  /**
   * Add a preference for one provider type over another provider type.
   *
   * @param {string} preferredType - The preferred provider type.
   * @param {string} nonPreferredType - The non-preferred provider type.
   */
  addPostDetectPreference(preferredType, nonPreferredType) {
    let prefs = this._postDetectPreferences;

    if (this.detectPreferenceCycle(prefs, preferredType, nonPreferredType)) {
      cal.WARN(
        `Adding a preference for provider type "${preferredType}" over ` +
          `type "${nonPreferredType}" would cause a preference cycle, ` +
          `not adding this preference to prevent a cycle`
      );
    } else {
      let current = prefs.get(nonPreferredType);
      if (current) {
        current.add(preferredType);
      } else {
        prefs.set(nonPreferredType, new Set([preferredType]));
      }
    }
  },

  /**
   * Check whether adding a preference for one provider type over another would
   * cause a cycle in the order of preferences. We assume that the preferences
   * do not contain any cycles already.
   *
   * @param {ProviderPreferences} prefs - The current preferences.
   * @param {string} preferred - Potential preferred provider.
   * @param {string} nonPreferred - Potential non-preferred provider.
   * @returns {boolean} True if it would cause a cycle.
   */
  detectPreferenceCycle(prefs, preferred, nonPreferred) {
    let cycle = false;

    let innerDetect = preferredSet => {
      if (cycle) {
        // Bail out, a cycle has already been detected.
        return;
      } else if (preferredSet.has(nonPreferred)) {
        // A cycle! We have arrived back at the nonPreferred provider type.
        cycle = true;
        return;
      }
      // Recursively check each preferred type.
      for (let item of preferredSet) {
        let nextPreferredSet = prefs.get(item);
        if (nextPreferredSet) {
          innerDetect(nextPreferredSet);
        }
      }
    };

    innerDetect(new Set([preferred]));
    return cycle;
  },
};

// If both ics and caldav results exist, default to the caldav results.
gProviderUsage.addPostDetectPreference("caldav", "ics");

/**
 * Select a specific panel in the dialog. Used to move from one panel to another.
 *
 * @param {string} id - The id of the panel node to select.
 */
function selectPanel(id) {
  for (let element of document.getElementById("calendar-creation-dialog").children) {
    element.hidden = element.id != id;
  }
  let panel = document.getElementById(id);
  updateButton("accept", panel);
  updateButton("extra2", panel);
  selectNetworkStatus("none");
  checkRequired();

  let firstInput = panel.querySelector("input");
  if (firstInput) {
    firstInput.focus();
  }
}

/**
 * Set a specific network loading status for the network settings panel.
 * See the CSS file for appropriate values to set.
 *
 * @param {string} status - The status to set.
 */
function selectNetworkStatus(status) {
  for (let row of document.querySelectorAll(".network-status-row")) {
    row.setAttribute("status", status);
  }
}

/**
 * Update the label, accesskey, and event listener for a dialog button.
 *
 * @param {string} name - The dialog button name, e.g. 'accept', 'extra2'.
 * @param {Element} sourceNode - The source node to take attribute values from.
 */
function updateButton(name, sourceNode) {
  let dialog = document.getElementById("calendar-creation-dialog");
  let button = dialog.getButton(name);
  let label = sourceNode.getAttribute("buttonlabel" + name);
  let accesskey = sourceNode.getAttribute("buttonaccesskey" + name);

  let handler = gButtonHandlers.forNodeId[sourceNode.id][name];

  if (label) {
    button.setAttribute("label", label);
    button.hidden = false;
  } else {
    button.hidden = true;
  }

  button.setAttribute("accesskey", accesskey || "");

  // 'dialogaccept', 'dialogextra2', etc.
  let eventName = "dialog" + name;

  document.removeEventListener(eventName, gButtonHandlers[name]);
  if (handler) {
    document.addEventListener(eventName, handler);
    // Store a reference to the current handler, to allow removing it later.
    gButtonHandlers[name] = handler;
  }
}

/**
 * Update the disabled state of the accept button by checking the values of
 * required fields, based on the current panel.
 */
function checkRequired() {
  let dialog = document.getElementById("calendar-creation-dialog");
  let selectedPanel = null;
  for (let element of dialog.children) {
    if (!element.hidden) {
      selectedPanel = element;
    }
  }
  if (!selectedPanel) {
    dialog.setAttribute("buttondisabledaccept", "true");
    return;
  }

  let disabled = false;
  switch (selectedPanel.id) {
    case "panel-local-calendar-settings":
      disabled = !selectedPanel.querySelector("form").checkValidity();
      break;
    case "panel-network-calendar-settings": {
      let location = document.getElementById("network-location-input");
      let username = document.getElementById("network-username-input");

      disabled = !location.value && !username.value.split("@")[1];
      break;
    }
  }

  if (disabled) {
    dialog.setAttribute("buttondisabledaccept", "true");
  } else {
    dialog.removeAttribute("buttondisabledaccept");
  }
}

/**
 * Update the placeholder text for the network location field. If the username
 * is a valid email address use the domain part of the username, otherwise use
 * the default placeholder.
 */
function fillLocationPlaceholder() {
  let location = document.getElementById("network-location-input");
  let userval = document.getElementById("network-username-input").value;
  let parts = userval.split("@");
  let domain = parts.length == 2 && parts[1] ? parts[1] : null;

  if (domain) {
    location.setAttribute("placeholder", domain);
  } else {
    location.setAttribute("placeholder", location.getAttribute("default-placeholder"));
  }
}

/**
 * Update the select network calendar panel to show or hide the provider
 * selection dropdown.
 *
 * @param {boolean} isSingle - If true, there is just one matching provider.
 */
function setSingleProvider(isSingle) {
  document.getElementById("network-selectcalendar-description-single").hidden = !isSingle;
  document.getElementById("network-selectcalendar-description-multiple").hidden = isSingle;
  document.getElementById("network-selectcalendar-providertype-box").hidden = isSingle;
}

/**
 * Fill the providers menulist with the given provider types. The types must
 * correspond to the providers that detected calendars.
 *
 * @param {string[]} providerTypes - An array of provider types.
 * @returns {Element} The selected menuitem.
 */
function fillProviders(providerTypes) {
  let menulist = document.getElementById("network-selectcalendar-providertype-menulist");
  let popup = menulist.menupopup;
  while (popup.lastChild) {
    popup.removeChild(popup.lastChild);
  }

  let providers = cal.provider.detection.providers;

  for (let type of providerTypes) {
    let provider = providers.get(type);
    let menuitem = document.createXULElement("menuitem");
    menuitem.value = type;
    menuitem.setAttribute("label", provider.displayName || type);
    popup.appendChild(menuitem);
  }

  // Select a provider menu item based on provider preferences.
  let preferredTypes = new Set(providerTypes);

  for (let [nonPreferred, preferredSet] of gProviderUsage.postDetectPreferences) {
    if (preferredTypes.has(nonPreferred) && setsIntersect(preferredSet, preferredTypes)) {
      preferredTypes.delete(nonPreferred);
    }
  }
  let preferredIndex = providerTypes.findIndex(type => preferredTypes.has(type));
  menulist.selectedIndex = preferredIndex == -1 ? 0 : preferredIndex;

  return menulist.selectedItem;
}

/**
 * Return true if the intersection of two sets contains at least one item.
 *
 * @param {Set} setA - A set.
 * @param {Set} setB - A set.
 * @returns {boolean}
 */
function setsIntersect(setA, setB) {
  for (let item of setA) {
    if (setB.has(item)) {
      return true;
    }
  }
  return false;
}

/**
 * Select the given provider and update the calendar list to fill the
 * corresponding calendars. Will use the results from the last findCalendars
 * response.
 *
 * @param {string} type - The provider type to select.
 */
function selectProvider(type) {
  let providerMap = findCalendars.lastResult;
  let calendarList = document.getElementById("network-calendar-list");

  let calendars = providerMap.get(type) || [];
  renderCalendarList(calendarList, calendars);
}

/**
 * Empty a calendar list and then fill it with calendars.
 *
 * @param {Element} calendarList - A richlistbox element for listing calendars.
 * @param {calICalendar[]} calendars - An array of calendars to display in the list.
 */
function renderCalendarList(calendarList, calendars) {
  while (calendarList.hasChildNodes()) {
    calendarList.lastChild.remove();
  }
  let propertiesButtonLabel = calendarList.getAttribute("propertiesbuttonlabel");
  calendars.forEach((calendar, index) => {
    let item = document.createXULElement("richlistitem");
    item.calendar = calendar;

    let checkbox = document.createXULElement("checkbox");
    let checkboxId = "checkbox" + index;
    checkbox.id = checkboxId;
    checkbox.classList.add("calendar-selected");
    item.appendChild(checkbox);

    let colorMarker = document.createElement("div");
    colorMarker.classList.add("calendar-color");
    colorMarker.style.backgroundColor = calendar.getProperty("color");
    item.appendChild(colorMarker);

    let label = document.createXULElement("label");
    label.classList.add("calendar-name");
    label.value = calendar.name;
    label.control = checkboxId;
    item.appendChild(label);

    let propertiesButton = document.createXULElement("button");
    propertiesButton.classList.add("calendar-edit-button");
    propertiesButton.label = propertiesButtonLabel;
    propertiesButton.addEventListener("command", openCalendarPropertiesFromEvent);
    item.appendChild(propertiesButton);

    if (calendar.getProperty("disabled")) {
      item.disabled = true;
      item.toggleAttribute("calendar-disabled", true);
      checkbox.disabled = true;
      propertiesButton.disabled = true;
    } else {
      checkbox.checked = true;
    }
    calendarList.appendChild(item);
  });
}

/**
 * Update dialog fields based on the value of the "no credentials" checkbox.
 *
 * @param {boolean} noCredentials - True, if "no credentials" is checked.
 */
function updateNoCredentials(noCredentials) {
  if (noCredentials) {
    document.getElementById("network-username-input").setAttribute("disabled", "true");
    document.getElementById("network-username-input").value = "";
  } else {
    document.getElementById("network-username-input").removeAttribute("disabled");
  }
}

/**
 * The accept button event listener for the "select calendar type" panel.
 *
 * @param {Event} event
 */
function selectCalendarType(event) {
  event.preventDefault();
  event.stopPropagation();
  let radiogroup = document.getElementById("calendar-type");
  let calendarType = gCalendarTypes.get(radiogroup.value);

  if (!calendarType.builtIn && calendarType !== gSelectedCalendarType) {
    setUpAddonCalendarSettingsPanel(calendarType);
  }
  gSelectedCalendarType = calendarType;
  calendarType.onSelected();
}

/**
 * Set up the settings panel for calendar types registered by addons.
 *
 * @param {CalendarType} calendarType - The calendar type.
 */
function setUpAddonCalendarSettingsPanel(calendarType) {
  function setUpBrowser(browser, src) {
    // Allow keeping dialog background color without jumping through hoops.
    browser.setAttribute("transparent", "true");
    browser.setAttribute("flex", "1");
    browser.setAttribute("type", "content");
    browser.setAttribute("src", src);
  }
  let panel = document.getElementById("panel-addon-calendar-settings");
  let browser = panel.lastElementChild;

  if (browser) {
    setUpBrowser(browser, calendarType.panelSrc);
  } else {
    browser = document.createXULElement("browser");
    setUpBrowser(browser, calendarType.panelSrc);

    panel.appendChild(browser);
    // The following emit is needed for the browser to work with addon content.
    ExtensionParent.apiManager.emit("extension-browser-inserted", browser);
  }

  // Set up the accept button handler for the panel.
  gButtonHandlers.forNodeId["panel-addon-calendar-settings"].accept = calendarType.onCreated;
}

/**
 * Handle change of the email (identity) menu for local calendar creation.
 * Show a notification when "none" is selected.
 *
 * @param {Event} event - The menu selection event.
 */
function onChangeIdentity(event) {
  notifyOnIdentitySelection(gLocalCalendar);
}

/**
 * Prepare the local storage calendar with the information from the dialog.
 * This can be monkeypatched to add additional values.
 *
 * @param {calICalendar} calendar - The calendar to prepare.
 * @returns {calICalendar} The same calendar, prepared with any
 *                                      extra values.
 */
function prepareLocalCalendar(calendar) {
  calendar.name = document.getElementById("local-calendar-name-input").value;
  calendar.setProperty("color", document.getElementById("local-calendar-color-picker").value);

  if (!document.getElementById("local-fire-alarms-checkbox").checked) {
    calendar.setProperty("suppressAlarms", true);
  }

  saveMailIdentitySelection(calendar);
  return calendar;
}

/**
 * The accept button event listener for the "local calendar settings" panel.
 * Registers the local storage calendar and closes the dialog.
 */
function registerLocalCalendar() {
  cal.manager.registerCalendar(prepareLocalCalendar(gLocalCalendar));
}

/**
 * Start detection and find any calendars using the information from the
 * network settings panel.
 *
 * @param {string} [password] - The password for this attempt, if any.
 * @param {boolean} [savePassword] - Whether to save the password in the
 *                                    password manager.
 */
function findCalendars(password, savePassword = false) {
  selectNetworkStatus("loading");
  let username = document.getElementById("network-username-input");
  let location = document.getElementById("network-location-input");
  let locationValue = location.value || username.value.split("@")[1] || "";

  // webcal(s): doesn't work with content principal.
  locationValue = locationValue.replace(/^webcal(s)?(:.*)/, "http$1$2").trim();
  cal.provider.detection
    .detect(
      username.value,
      password,
      locationValue,
      savePassword,
      gProviderUsage.preDetectFilters,
      {}
    )
    .then(onDetectionSuccess, onDetectionError.bind(null, password, locationValue));
}

/**
 * Called when detection successfully finds calendars. Displays the UI for
 * selecting calendars to subscribe to.
 *
 * @param {Map<string, calICalendar[]>} providerMap   Map from provider type
 *                                                    (e.g. "ics", "caldav")
 *                                                    to an array of calendars.
 */
function onDetectionSuccess(providerMap) {
  // Disable the calendars the user has already subscribed to. In the future
  // we should show a string when all calendars are already subscribed.
  let existing = new Set(cal.manager.getCalendars({}).map(calendar => calendar.uri.spec));

  let calendarsMap = new Map();
  for (let [provider, calendars] of providerMap.entries()) {
    let newCalendars = calendars.map(calendar => {
      let newCalendar = prepareNetworkCalendar(calendar);
      if (existing.has(calendar.uri.spec)) {
        newCalendar.setProperty("disabled", true);
      }
      return newCalendar;
    });

    calendarsMap.set(provider.type, newCalendars);
  }

  if (!calendarsMap.size) {
    selectNetworkStatus("notfound");
    return;
  }

  // Update the panel with the results from the provider map.
  setSingleProvider(calendarsMap.size <= 1);
  findCalendars.lastResult = calendarsMap;

  let selectedItem = fillProviders([...calendarsMap.keys()]);
  selectProvider(selectedItem.value);

  // Select the panel and validate the fields.
  selectPanel("panel-select-calendars");
  checkRequired();
}

/**
 * Called when detection fails to find any calendars. Show an appropriate
 * error message, or if the error is an authentication error and no password
 * was entered for this attempt, prompt the user to enter a password.
 *
 * @param {string} [password] - The password entered, if any.
 * @param {string} [location] - The location input from the dialog.
 * @param {Error} error - An error object.
 */
function onDetectionError(password, location, error) {
  if (error instanceof cal.provider.detection.AuthFailedError) {
    if (password) {
      selectNetworkStatus("authfail");
    } else {
      findCalendarsWithPassword(location);
      return;
    }
  } else if (error instanceof cal.provider.detection.CanceledError) {
    selectNetworkStatus("none");
  } else {
    selectNetworkStatus("notfound");
  }
  cal.ERROR(
    "Error during calendar detection: " +
      `${error.fileName || error.filename}:${error.lineNumber}: ${error}\n${error.stack}`
  );
}

/**
 * Prompt the user for a password and attempt to find calendars with it.
 *
 * @param {string} location - The location input from the dialog.
 */
function findCalendarsWithPassword(location) {
  let password = { value: "" };
  let savePassword = { value: 1 };

  let okWasClicked = new MsgAuthPrompt().promptPassword2(
    null,
    cal.l10n.getAnyString("messenger-mapi", "mapi", "loginText", [location]),
    password,
    cal.l10n.getAnyString("passwordmgr", "passwordmgr", "rememberPassword"),
    savePassword
  );

  if (okWasClicked) {
    findCalendars(password.value, savePassword.value);
  } else {
    selectNetworkStatus("authfail");
  }
}

/**
 * Make preparations on the given calendar (a detected calendar). This
 * function can be monkeypatched to make general preparations, e.g. for values
 * from additional form fields.
 *
 * @param {calICalendar} calendar - The calendar to prepare.
 * @returns {calICalendar} The same calendar, prepared with
 *                                            any extra values.
 */
function prepareNetworkCalendar(calendar) {
  let cached = document.getElementById("network-cache-checkbox").checked;

  if (!calendar.getProperty("cache.always")) {
    let cacheSupported = calendar.getProperty("cache.supported") !== false;
    calendar.setProperty("cache.enabled", cacheSupported ? cached : false);
  }

  return calendar;
}

/**
 * The accept button handler for the 'select network calendars' panel.
 * Subscribes to all of the selected network calendars and allows the dialog to
 * close.
 */
function createNetworkCalendars() {
  for (let listItem of document.getElementById("network-calendar-list").children) {
    if (listItem.querySelector(".calendar-selected").checked) {
      cal.manager.registerCalendar(listItem.calendar);
    }
  }
}

/**
 * Open the calendar properties dialog for a calendar in the calendar list.
 *
 * @param {Event} event - The triggering event.
 */
function openCalendarPropertiesFromEvent(event) {
  let listItem = event.target.closest("richlistitem");
  if (listItem) {
    let calendar = listItem.calendar;
    if (calendar && !calendar.getProperty("disabled")) {
      cal.window.openCalendarProperties(window, { calendar, canDisable: false });

      // Update the calendar list item.
      listItem.querySelector(".calendar-name").value = calendar.name;
      listItem.querySelector(".calendar-color").style.backgroundColor =
        calendar.getProperty("color");
    }
  }
}

window.addEventListener("load", () => {
  fillLocationPlaceholder();
  selectPanel("panel-select-calendar-type");
  if (window.arguments[0]) {
    let spec = window.arguments[0].spec;
    if (/^webcals?:\/\//.test(spec)) {
      selectPanel("panel-network-calendar-settings");
      document.getElementById("network-location-input").value = spec;
      checkRequired();
    }
  }
});