Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /*** =================== INITIALISATION CODE =================== ***/ |
michael@0 | 8 | |
michael@0 | 9 | var kObserverService; |
michael@0 | 10 | |
michael@0 | 11 | // interface variables |
michael@0 | 12 | var passwordmanager = null; |
michael@0 | 13 | |
michael@0 | 14 | // password-manager lists |
michael@0 | 15 | var signons = []; |
michael@0 | 16 | var rejects = []; |
michael@0 | 17 | var deletedSignons = []; |
michael@0 | 18 | var deletedRejects = []; |
michael@0 | 19 | |
michael@0 | 20 | var signonsTree; |
michael@0 | 21 | var rejectsTree; |
michael@0 | 22 | |
michael@0 | 23 | var showingPasswords = false; |
michael@0 | 24 | |
michael@0 | 25 | function Startup() { |
michael@0 | 26 | // xpconnect to password manager interfaces |
michael@0 | 27 | passwordmanager = Components.classes["@mozilla.org/login-manager;1"] |
michael@0 | 28 | .getService(Components.interfaces.nsILoginManager); |
michael@0 | 29 | |
michael@0 | 30 | // be prepared to reload the display if anything changes |
michael@0 | 31 | kObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); |
michael@0 | 32 | kObserverService.addObserver(signonReloadDisplay, "passwordmgr-storage-changed", false); |
michael@0 | 33 | |
michael@0 | 34 | signonsTree = document.getElementById("signonsTree"); |
michael@0 | 35 | rejectsTree = document.getElementById("rejectsTree"); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function Shutdown() { |
michael@0 | 39 | kObserverService.removeObserver(signonReloadDisplay, "passwordmgr-storage-changed"); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | var signonReloadDisplay = { |
michael@0 | 43 | observe: function(subject, topic, data) { |
michael@0 | 44 | if (topic == "passwordmgr-storage-changed") { |
michael@0 | 45 | switch (data) { |
michael@0 | 46 | case "addLogin": |
michael@0 | 47 | case "modifyLogin": |
michael@0 | 48 | case "removeLogin": |
michael@0 | 49 | case "removeAllLogins": |
michael@0 | 50 | if (!signonsTree) { |
michael@0 | 51 | return; |
michael@0 | 52 | } |
michael@0 | 53 | signons.length = 0; |
michael@0 | 54 | LoadSignons(); |
michael@0 | 55 | // apply the filter if needed |
michael@0 | 56 | if (document.getElementById("filter") && document.getElementById("filter").value != "") { |
michael@0 | 57 | _filterPasswords(); |
michael@0 | 58 | } |
michael@0 | 59 | break; |
michael@0 | 60 | case "hostSavingEnabled": |
michael@0 | 61 | case "hostSavingDisabled": |
michael@0 | 62 | if (!rejectsTree) { |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | rejects.length = 0; |
michael@0 | 66 | if (lastRejectSortColumn == "hostname") { |
michael@0 | 67 | lastRejectSortAscending = !lastRejectSortAscending; // prevents sort from being reversed |
michael@0 | 68 | } |
michael@0 | 69 | LoadRejects(); |
michael@0 | 70 | break; |
michael@0 | 71 | } |
michael@0 | 72 | kObserverService.notifyObservers(null, "passwordmgr-dialog-updated", null); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /*** =================== GENERAL CODE =================== ***/ |
michael@0 | 78 | |
michael@0 | 79 | function DeleteAllFromTree(tree, view, table, deletedTable, removeButton, removeAllButton) { |
michael@0 | 80 | |
michael@0 | 81 | // remove all items from table and place in deleted table |
michael@0 | 82 | for (var i=0; i<table.length; i++) { |
michael@0 | 83 | deletedTable[deletedTable.length] = table[i]; |
michael@0 | 84 | } |
michael@0 | 85 | table.length = 0; |
michael@0 | 86 | |
michael@0 | 87 | // clear out selections |
michael@0 | 88 | view.selection.select(-1); |
michael@0 | 89 | |
michael@0 | 90 | // update the tree view and notify the tree |
michael@0 | 91 | view.rowCount = 0; |
michael@0 | 92 | |
michael@0 | 93 | var box = tree.treeBoxObject; |
michael@0 | 94 | box.rowCountChanged(0, -deletedTable.length); |
michael@0 | 95 | box.invalidate(); |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | // disable buttons |
michael@0 | 99 | document.getElementById(removeButton).setAttribute("disabled", "true") |
michael@0 | 100 | document.getElementById(removeAllButton).setAttribute("disabled","true"); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function DeleteSelectedItemFromTree |
michael@0 | 104 | (tree, view, table, deletedTable, removeButton, removeAllButton) { |
michael@0 | 105 | |
michael@0 | 106 | // Turn off tree selection notifications during the deletion |
michael@0 | 107 | tree.view.selection.selectEventsSuppressed = true; |
michael@0 | 108 | |
michael@0 | 109 | // remove selected items from list (by setting them to null) and place in deleted list |
michael@0 | 110 | var selections = GetTreeSelections(tree); |
michael@0 | 111 | for (var s=selections.length-1; s>= 0; s--) { |
michael@0 | 112 | var i = selections[s]; |
michael@0 | 113 | deletedTable[deletedTable.length] = table[i]; |
michael@0 | 114 | table[i] = null; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // collapse list by removing all the null entries |
michael@0 | 118 | for (var j=0; j<table.length; j++) { |
michael@0 | 119 | if (table[j] == null) { |
michael@0 | 120 | var k = j; |
michael@0 | 121 | while ((k < table.length) && (table[k] == null)) { |
michael@0 | 122 | k++; |
michael@0 | 123 | } |
michael@0 | 124 | table.splice(j, k-j); |
michael@0 | 125 | view.rowCount -= k - j; |
michael@0 | 126 | tree.treeBoxObject.rowCountChanged(j, j - k); |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // update selection and/or buttons |
michael@0 | 131 | if (table.length) { |
michael@0 | 132 | // update selection |
michael@0 | 133 | var nextSelection = (selections[0] < table.length) ? selections[0] : table.length-1; |
michael@0 | 134 | tree.view.selection.select(nextSelection); |
michael@0 | 135 | tree.treeBoxObject.ensureRowIsVisible(nextSelection); |
michael@0 | 136 | } else { |
michael@0 | 137 | // disable buttons |
michael@0 | 138 | document.getElementById(removeButton).setAttribute("disabled", "true") |
michael@0 | 139 | document.getElementById(removeAllButton).setAttribute("disabled","true"); |
michael@0 | 140 | } |
michael@0 | 141 | tree.view.selection.selectEventsSuppressed = false; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | function GetTreeSelections(tree) { |
michael@0 | 145 | var selections = []; |
michael@0 | 146 | var select = tree.view.selection; |
michael@0 | 147 | if (select) { |
michael@0 | 148 | var count = select.getRangeCount(); |
michael@0 | 149 | var min = new Object(); |
michael@0 | 150 | var max = new Object(); |
michael@0 | 151 | for (var i=0; i<count; i++) { |
michael@0 | 152 | select.getRangeAt(i, min, max); |
michael@0 | 153 | for (var k=min.value; k<=max.value; k++) { |
michael@0 | 154 | if (k != -1) { |
michael@0 | 155 | selections[selections.length] = k; |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | return selections; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | function SortTree(tree, view, table, column, lastSortColumn, lastSortAscending, updateSelection) { |
michael@0 | 164 | |
michael@0 | 165 | // remember which item was selected so we can restore it after the sort |
michael@0 | 166 | var selections = GetTreeSelections(tree); |
michael@0 | 167 | var selectedNumber = selections.length ? table[selections[0]].number : -1; |
michael@0 | 168 | |
michael@0 | 169 | // determine if sort is to be ascending or descending |
michael@0 | 170 | var ascending = (column == lastSortColumn) ? !lastSortAscending : true; |
michael@0 | 171 | |
michael@0 | 172 | // do the sort |
michael@0 | 173 | var compareFunc; |
michael@0 | 174 | if (ascending) { |
michael@0 | 175 | compareFunc = function compare(first, second) { |
michael@0 | 176 | return CompareLowerCase(first[column], second[column]); |
michael@0 | 177 | } |
michael@0 | 178 | } else { |
michael@0 | 179 | compareFunc = function compare(first, second) { |
michael@0 | 180 | return CompareLowerCase(second[column], first[column]); |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | table.sort(compareFunc); |
michael@0 | 184 | |
michael@0 | 185 | // restore the selection |
michael@0 | 186 | var selectedRow = -1; |
michael@0 | 187 | if (selectedNumber>=0 && updateSelection) { |
michael@0 | 188 | for (var s=0; s<table.length; s++) { |
michael@0 | 189 | if (table[s].number == selectedNumber) { |
michael@0 | 190 | // update selection |
michael@0 | 191 | // note: we need to deselect before reselecting in order to trigger ...Selected() |
michael@0 | 192 | tree.view.selection.select(-1); |
michael@0 | 193 | tree.view.selection.select(s); |
michael@0 | 194 | selectedRow = s; |
michael@0 | 195 | break; |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | // display the results |
michael@0 | 201 | tree.treeBoxObject.invalidate(); |
michael@0 | 202 | if (selectedRow >= 0) { |
michael@0 | 203 | tree.treeBoxObject.ensureRowIsVisible(selectedRow) |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | return ascending; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | /** |
michael@0 | 210 | * Case insensitive string comparator. |
michael@0 | 211 | */ |
michael@0 | 212 | function CompareLowerCase(first, second) { |
michael@0 | 213 | var firstLower, secondLower; |
michael@0 | 214 | |
michael@0 | 215 | // Are we sorting nsILoginInfo entries or just strings? |
michael@0 | 216 | if (first.hostname) { |
michael@0 | 217 | firstLower = first.hostname.toLowerCase(); |
michael@0 | 218 | secondLower = second.hostname.toLowerCase(); |
michael@0 | 219 | } else { |
michael@0 | 220 | firstLower = first.toLowerCase(); |
michael@0 | 221 | secondLower = second.toLowerCase(); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | if (firstLower < secondLower) { |
michael@0 | 225 | return -1; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | if (firstLower > secondLower) { |
michael@0 | 229 | return 1; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | return 0; |
michael@0 | 233 | } |