1 # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
16 # The Original Code is Mozilla Communicator client code, released
19 # The Initial Developer of the Original Code is
20 # Netscape Communications Corporation.
21 # Portions created by the Initial Developer are Copyright (C) 1998
22 # the Initial Developer. All Rights Reserved.
25 # Ben "Count XULula" Goodger
26 # Brian Ryner <bryner@brianryner.com>
27 # Ehsan Akhgari <ehsan.akhgari@gmail.com>
29 # Alternatively, the contents of this file may be used under the terms of
30 # either the GNU General Public License Version 2 or later (the "GPL"), or
31 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 # in which case the provisions of the GPL or the LGPL are applicable instead
33 # of those above. If you wish to allow use of your version of this file only
34 # under the terms of either the GPL or the LGPL, and not to allow others to
35 # use your version of this file under the terms of the MPL, indicate your
36 # decision by deleting the provisions above and replace them with the notice
37 # and other provisions required by the GPL or the LGPL. If you do not delete
38 # the provisions above, a recipient may use your version of this file under
39 # the terms of any one of the MPL, the GPL or the LGPL.
41 # ***** END LICENSE BLOCK *****
43 /*** =================== INITIALISATION CODE =================== ***/
46 var gSelectUserInUse = false;
48 // interface variables
49 var passwordmanager = null;
51 // password-manager lists
54 var deletedSignons = [];
55 var deletedRejects = [];
60 var showingPasswords = false;
63 // xpconnect to password manager interfaces
64 passwordmanager = Components.classes["@mozilla.org/login-manager;1"]
65 .getService(Components.interfaces.nsILoginManager);
67 // be prepared to reload the display if anything changes
68 kObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
69 kObserverService.addObserver(signonReloadDisplay, "signonChanged", false);
71 // be prepared to disable the buttons when selectuser dialog is in use
72 kObserverService.addObserver(signonReloadDisplay, "signonSelectUser", false);
74 signonsTree = document.getElementById("signonsTree");
75 rejectsTree = document.getElementById("rejectsTree");
79 kObserverService.removeObserver(signonReloadDisplay, "signonChanged");
80 kObserverService.removeObserver(signonReloadDisplay, "signonSelectUser");
83 var signonReloadDisplay = {
84 observe: function(subject, topic, state) {
85 if (topic == "signonChanged") {
86 if (state == "signons") {
88 if (lastSignonSortColumn == "hostname") {
89 lastSignonSortAscending = !lastSignonSortAscending; // prevents sort from being reversed
92 // apply the filter if needed
93 if (document.getElementById("filter") && document.getElementById("filter").value != "") {
96 } else if (state == "rejects") {
98 if (lastRejectSortColumn == "hostname") {
99 lastRejectSortAscending = !lastRejectSortAscending; // prevents sort from being reversed
103 } else if (topic == "signonSelectUser") {
104 if (state == "suspend") {
105 gSelectUserInUse = true;
106 document.getElementById("removeSignon").disabled = true;
107 document.getElementById("removeAllSignons").disabled = true;
108 document.getElementById("togglePasswords").disabled = true;
109 } else if (state == "resume") {
110 gSelectUserInUse = false;
111 var selections = GetTreeSelections(signonsTree);
112 if (selections.length > 0) {
113 document.getElementById("removeSignon").disabled = false;
115 if (signons.length > 0) {
116 document.getElementById("removeAllSignons").disabled = false;
117 document.getElementById("togglePasswords").disabled = false;
119 } else if (state == "inUse") {
120 gSelectUserInUse = true;
126 /*** =================== GENERAL CODE =================== ***/
128 function DeleteAllFromTree(tree, view, table, deletedTable, removeButton, removeAllButton) {
130 // remove all items from table and place in deleted table
131 for (var i=0; i<table.length; i++) {
132 deletedTable[deletedTable.length] = table[i];
136 // clear out selections
137 view.selection.select(-1);
139 // update the tree view and notify the tree
142 var box = tree.treeBoxObject;
143 box.rowCountChanged(0, -deletedTable.length);
148 document.getElementById(removeButton).setAttribute("disabled", "true")
149 document.getElementById(removeAllButton).setAttribute("disabled","true");
152 function DeleteSelectedItemFromTree
153 (tree, view, table, deletedTable, removeButton, removeAllButton) {
155 // Turn off tree selection notifications during the deletion
156 tree.view.selection.selectEventsSuppressed = true;
158 // remove selected items from list (by setting them to null) and place in deleted list
159 var selections = GetTreeSelections(tree);
160 for (var s=selections.length-1; s>= 0; s--) {
161 var i = selections[s];
162 deletedTable[deletedTable.length] = table[i];
166 // collapse list by removing all the null entries
167 for (var j=0; j<table.length; j++) {
168 if (table[j] == null) {
170 while ((k < table.length) && (table[k] == null)) {
173 table.splice(j, k-j);
174 view.rowCount -= k - j;
175 tree.treeBoxObject.rowCountChanged(j, j - k);
179 // update selection and/or buttons
182 var nextSelection = (selections[0] < table.length) ? selections[0] : table.length-1;
183 tree.view.selection.select(nextSelection);
184 tree.treeBoxObject.ensureRowIsVisible(nextSelection);
187 document.getElementById(removeButton).setAttribute("disabled", "true")
188 document.getElementById(removeAllButton).setAttribute("disabled","true");
190 tree.view.selection.selectEventsSuppressed = false;
193 function GetTreeSelections(tree) {
195 var select = tree.view.selection;
197 var count = select.getRangeCount();
198 var min = new Object();
199 var max = new Object();
200 for (var i=0; i<count; i++) {
201 select.getRangeAt(i, min, max);
202 for (var k=min.value; k<=max.value; k++) {
204 selections[selections.length] = k;
212 function SortTree(tree, view, table, column, lastSortColumn, lastSortAscending, updateSelection) {
214 // remember which item was selected so we can restore it after the sort
215 var selections = GetTreeSelections(tree);
216 var selectedNumber = selections.length ? table[selections[0]].number : -1;
218 // determine if sort is to be ascending or descending
219 var ascending = (column == lastSortColumn) ? !lastSortAscending : true;
224 compareFunc = function compare(first, second) {
225 return CompareLowerCase(first[column], second[column]);
228 compareFunc = function compare(first, second) {
229 return CompareLowerCase(second[column], first[column]);
232 table.sort(compareFunc);
234 // restore the selection
235 var selectedRow = -1;
236 if (selectedNumber>=0 && updateSelection) {
237 for (var s=0; s<table.length; s++) {
238 if (table[s].number == selectedNumber) {
240 // note: we need to deselect before reselecting in order to trigger ...Selected()
241 tree.view.selection.select(-1);
242 tree.view.selection.select(s);
249 // display the results
250 tree.treeBoxObject.invalidate();
251 if (selectedRow >= 0) {
252 tree.treeBoxObject.ensureRowIsVisible(selectedRow)
259 * Case insensitive string comparator.
261 function CompareLowerCase(first, second) {
262 var firstLower, secondLower;
264 // Are we sorting nsILoginInfo entries or just strings?
265 if (first.hostname) {
266 firstLower = first.hostname.toLowerCase();
267 secondLower = second.hostname.toLowerCase();
269 firstLower = first.toLowerCase();
270 secondLower = second.toLowerCase();
273 if (firstLower < secondLower) {
277 if (firstLower > secondLower) {