summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/content/abAddressBookNameDialog.js
blob: 6c921d9285a1c3d56b4b0a6294912737bd15a1a6 (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
/* 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 { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gOkButton;
var gNameInput;
var gDirectory = null;

var kPersonalAddressbookURI = "jsaddrbook://abook.sqlite";
var kCollectedAddressbookURI = "jsaddrbook://history.sqlite";
var kAllDirectoryRoot = "moz-abdirectory://";

window.addEventListener("DOMContentLoaded", abNameOnLoad);

function abNameOnLoad() {
  // Get the document elements.
  gOkButton = document.querySelector("dialog").getButton("accept");
  gNameInput = document.getElementById("name");

  // look in arguments[0] for parameters to see if we have a directory or not
  if (
    "arguments" in window &&
    window.arguments[0] &&
    "selectedDirectory" in window.arguments[0]
  ) {
    gDirectory = window.arguments[0].selectedDirectory;
    gNameInput.value = gDirectory.dirName;
  }

  // Work out the window title (if we have a directory specified, then it's a
  // rename).
  var bundle = document.getElementById("bundle_addressBook");

  if (gDirectory) {
    let oldListName = gDirectory.dirName;
    document.title = bundle.getFormattedString("addressBookTitleEdit", [
      oldListName,
    ]);
  } else {
    document.title = bundle.getString("addressBookTitleNew");
  }

  if (
    gDirectory &&
    (gDirectory.URI == kCollectedAddressbookURI ||
      gDirectory.URI == kPersonalAddressbookURI ||
      gDirectory.URI == kAllDirectoryRoot + "?")
  ) {
    // Address book name is not editable, therefore disable the field and
    // only have an ok button that doesn't do anything.
    gNameInput.readOnly = true;
    document.querySelector("dialog").buttons = "accept";
  } else {
    document.addEventListener("dialogaccept", abNameOKButton);
    gNameInput.focus();
    abNameDoOkEnabling();
  }
}

function abNameOKButton(event) {
  let newDirName = gNameInput.value.trim();

  // Do not allow an already existing name.
  if (
    MailServices.ab.directoryNameExists(newDirName) &&
    (!gDirectory || newDirName != gDirectory.dirName)
  ) {
    const kAlertTitle = document
      .getElementById("bundle_addressBook")
      .getString("duplicateNameTitle");
    const kAlertText = document
      .getElementById("bundle_addressBook")
      .getFormattedString("duplicateNameText", [newDirName]);
    Services.prompt.alert(window, kAlertTitle, kAlertText);
    event.preventDefault();
    return;
  }

  // Either create a new directory or update an existing one depending on what
  // we were given when we started.
  if (gDirectory) {
    gDirectory.dirName = newDirName;
  } else {
    let dirPrefId = MailServices.ab.newAddressBook(
      newDirName,
      "",
      Ci.nsIAbManager.JS_DIRECTORY_TYPE
    );
    let directory = MailServices.ab.getDirectoryFromId(dirPrefId);
    window.arguments[0].newDirectoryUID = directory.UID;
    if ("onNewDirectory" in window.arguments[0]) {
      window.arguments[0].onNewDirectory(directory);
    }
  }
}

function abNameDoOkEnabling() {
  gOkButton.disabled = gNameInput.value.trim() == "";
}