summaryrefslogtreecommitdiffstats
path: root/mailnews/addrbook/prefs/content/pref-editdirectories.js
blob: dfb3e05590b669fac14989d4d7786fe76b5cdc16 (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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource:///modules/mailServices.js");

// 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 = {
  onItemAdded: function(parentDir, item) {
    if (item instanceof Components.interfaces.nsIAbDirectory) {
      fillDirectoryList();
    }
  },
  onItemRemoved: function(parentDir, item) {
    if (item instanceof Components.interfaces.nsIAbDirectory) {
      fillDirectoryList();
    }
  },
  onItemPropertyChanged: function(item, property, oldValue, newValue) {
    if (item instanceof Components.interfaces.nsIAbDirectory) {
      fillDirectoryList();
    }
  }
};

function onInitEditDirectories()
{
  // For AbDeleteDirectory in abCommon.js
  gAddressBookBundle = document.getElementById("bundle_addressBook");

  // 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();

  const nsIAbListener = Components.interfaces.nsIAbListener;
  // Add a listener so we can update correctly if the list should change
  MailServices.ab.addAddressBookListener(gAddressBookAbListener,
                                         nsIAbListener.itemAdded |
                                         nsIAbListener.directoryRemoved |
                                         nsIAbListener.itemChanged);
}

function onUninitEditDirectories()
{
  MailServices.ab.removeAddressBookListener(gAddressBookAbListener);
}

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

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

  // Init the address book list
  let directories = MailServices.ab.directories;
  let holdingArray = [];
  while (directories && directories.hasMoreElements()) {
    let ab = directories.getNext();
    if (ab instanceof Components.interfaces.nsIAbDirectory && ab.isRemote)
      holdingArray.push(ab);
  }

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

  holdingArray.forEach(function (ab) {
    var item = document.createElement('listitem');
    item.setAttribute("label", ab.dirName);
    item.setAttribute("value", ab.URI);

    abList.appendChild(item);
  });
}

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 disable = false;
    let ab = MailServices.ab.getDirectory(abList.value);
    try {
      disable = Services.prefs.getBoolPref(ab.dirPrefId + ".disable_delete");
    }
    catch(ex){
      // If this preference is not set, it's ok.
    }
    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 editDirectory()
{
  var abList = document.getElementById("directoriesList");

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

    window.openDialog(ab.propertiesChromeURI, "editDirectory",
                      "chrome,modal=yes,resizable=no",
                      { selectedDirectory: ab });
  }
}

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

  if (abList && abList.selectedItem)
    AbDeleteDirectory(abList.value);
}