summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/addrbook/prefs/content/pref-editdirectories.js
blob: 2ba3421c5ab19c8c56324b09e3bfc0f8e853bf93 (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
/* 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/. */

/* import-globals-from ../../../../mail/components/addrbook/content/abCommon.js */

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

window.addEventListener("DOMContentLoaded", onInitEditDirectories);

// Listener to refresh the list items if something changes. In all these
// cases we just rebuild the list as it is easier than searching/adding in the
// correct places an would be an infrequent operation.
var gAddressBookAbListener = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIObserver",
    "nsISupportsWeakReference",
  ]),

  init() {
    for (let topic of [
      "addrbook-directory-created",
      "addrbook-directory-updated",
      "addrbook-directory-deleted",
    ]) {
      Services.obs.addObserver(this, topic, true);
    }
  },

  observe(subject, topic, data) {
    subject.QueryInterface(Ci.nsIAbDirectory);
    fillDirectoryList(subject);
  },
};

function onInitEditDirectories() {
  // If the pref is locked disable the "Add" button
  if (Services.prefs.prefIsLocked("ldap_2.disable_button_add")) {
    document.getElementById("addButton").setAttribute("disabled", true);
  }

  // Fill out the directory list
  fillDirectoryList();

  // Add a listener so we can update correctly if the list should change
  gAddressBookAbListener.init();
}

function fillDirectoryList(aItem = null) {
  var abList = document.getElementById("directoriesList");

  // Empty out anything in the list
  while (abList.hasChildNodes()) {
    abList.lastChild.remove();
  }

  // Init the address book list
  let holdingArray = [];
  for (let ab of MailServices.ab.directories) {
    if (ab.isRemote) {
      holdingArray.push(ab);
    }
  }

  holdingArray.sort(function (a, b) {
    return a.dirName.localeCompare(b.dirName);
  });

  holdingArray.forEach(function (ab) {
    let item = document.createXULElement("richlistitem");
    let label = document.createXULElement("label");
    label.setAttribute("value", ab.dirName);
    item.appendChild(label);
    item.setAttribute("value", ab.URI);

    abList.appendChild(item);
  });

  // Forces the focus back on the list and on the first item.
  // We also select an edited or recently added item.
  abList.focus();
  if (aItem) {
    abList.selectedIndex = holdingArray.findIndex(d => {
      return d && d.URI == aItem.URI;
    });
  }
}

function selectDirectory() {
  var abList = document.getElementById("directoriesList");
  var editButton = document.getElementById("editButton");
  var removeButton = document.getElementById("removeButton");

  if (abList && abList.selectedItem) {
    editButton.removeAttribute("disabled");

    // If the disable delete button pref for the selected directory is set,
    // disable the delete button for that directory.
    let ab = MailServices.ab.getDirectory(abList.value);
    let disable = Services.prefs.getBoolPref(
      ab.dirPrefId + ".disable_delete",
      false
    );
    if (disable) {
      removeButton.setAttribute("disabled", true);
    } else {
      removeButton.removeAttribute("disabled");
    }
  } else {
    editButton.setAttribute("disabled", true);
    removeButton.setAttribute("disabled", true);
  }
}

function dblClickDirectory(event) {
  // We only care about left click events.
  if (event.button != 0) {
    return;
  }

  editDirectory();
}

function addDirectory() {
  parent.gSubDialog.open(
    "chrome://messenger/content/addressbook/pref-directory-add.xhtml",
    { features: "resizable=no" }
  );
}

function editDirectory() {
  var abList = document.getElementById("directoriesList");

  if (abList && abList.selectedItem) {
    let abURI = abList.value;
    let ab = MailServices.ab.getDirectory(abURI);

    parent.gSubDialog.open(
      "chrome://messenger/content/addressbook/pref-directory-add.xhtml",
      { features: "resizable=no" },
      { selectedDirectory: ab }
    );
  }
}

async function removeDirectory() {
  let abList = document.getElementById("directoriesList");

  if (!abList.selectedItem) {
    return;
  }

  let directory = GetDirectoryFromURI(abList.value);
  if (
    !directory ||
    ["ldap_2.servers.history", "ldap_2.servers.pab"].includes(
      directory.dirPrefId
    )
  ) {
    return;
  }

  let action = "delete-book";
  if (directory.isMailList) {
    action = "delete-lists";
  } else if (
    [
      Ci.nsIAbManager.CARDDAV_DIRECTORY_TYPE,
      Ci.nsIAbManager.LDAP_DIRECTORY_TYPE,
    ].includes(directory.dirType)
  ) {
    action = "remove-remote-book";
  }

  let [title, message] = await document.l10n.formatValues([
    { id: `about-addressbook-confirm-${action}-title`, args: { count: 1 } },
    {
      id: `about-addressbook-confirm-${action}`,
      args: { name: directory.dirName, count: 1 },
    },
  ]);

  if (Services.prompt.confirm(window, title, message)) {
    MailServices.ab.deleteAddressBook(directory.URI);
  }
}