Changeset 9cedc0 for chrome/content/v_identity/vI_storage.js
- Timestamp:
- Sep 25, 2011, 9:23:06 PM (11 years ago)
- Branches:
- ng_0.9
- Children:
- 284956
- Parents:
- 7666cb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
chrome/content/v_identity/vI_storage.js
r7666cb r9cedc0 30 30 virtualIdentityExtension.ns(function() { with (virtualIdentityExtension.LIB) { 31 31 var storage = { 32 multipleRecipients : null,33 32 focusedElement : null, 33 _pref : Components.classes["@mozilla.org/preferences-service;1"] 34 .getService(Components.interfaces.nsIPrefService) 35 .getBranch("extensions.virtualIdentity."), 34 36 35 37 lastCheckedEmail : {}, // array of last checked emails per row, 36 38 // to prevent ugly double dialogs and time-consuming double-checks 37 39 38 rdfService : Components.classes["@mozilla.org/rdf/rdf-service;1"] 39 .getService(Components.interfaces.nsIRDFService), 40 41 prefroot : Components.classes["@mozilla.org/preferences-service;1"] 42 .getService(Components.interfaces.nsIPrefService) 43 .getBranch(null).QueryInterface(Components.interfaces.nsIPrefBranch2), 44 45 rdfDatasource : null, // local storage 40 _rdfDatasourceAccess : null, // local storage 46 41 47 42 clean: function() { … … 51 46 storage.firstUsedInputElement = null; 52 47 awSetInputAndPopupValue = storage.original_functions.awSetInputAndPopupValue; 53 if (storage. rdfDatasource) storage.rdfDatasource.clean();48 if (storage._rdfDatasourceAccess) storage._rdfDatasourceAccess.clean(); 54 49 }, 55 50 … … 90 85 init: function() { 91 86 if (!storage.initialized) { 92 storage. rdfDatasource = new vI.rdfDatasource("virtualIdentity.rdf");87 storage._rdfDatasourceAccess = new vI.rdfDatasourceAccess(); 93 88 94 89 // better approach would be to use te onchange event, but this one is not fired in any change case … … 130 125 131 126 firstUsedInputElement : null, // this stores the first Element for which a Lookup in the Storage was successfull 132 updateVIdentityFromStorage: function(inputElement) { 133 if (! vI.main.preferences.getBoolPref("storage"))127 updateVIdentityFromStorage: function(inputElement) { 128 if (!storage._pref.getBoolPref("storage")) 134 129 { vI.notificationBar.dump("## storage: Storage deactivated\n"); return; } 135 130 vI.notificationBar.dump("## storage: updateVIdentityFromStorage()\n"); … … 155 150 } 156 151 storage.lastCheckedEmail[row] = inputElement.value; 157 var recipient = storage.__getDescriptionAndType(inputElement.value, recipientType); 158 159 var matchResults = { storageData : {}, menuItem : {} }; 160 matchResults.storageData[0] = storage.rdfDatasource.readVIdentityFromRDF(recipient.recDesc, recipient.recType); 161 matchResults.storageData[1] = storage.rdfDatasource.findMatchingFilter(recipient.recDesc); 162 163 vI.notificationBar.dump("## storage: updateVIdentityFromStorage add found Identities to CloneMenu.\n"); 164 var matchIndex = null; 165 for (var i = 0; i <= 1; i++) { 166 if (matchResults.storageData[i]) { // check if there is a result in direct match or filter 167 if (matchIndex == null) matchIndex = i; // prefer direct match instead of filter 168 matchResults.menuItem[i] = document.getElementById("msgIdentity_clone") 169 .addIdentityToCloneMenu(matchResults.storageData[i]); 170 } 171 } 172 if (matchIndex == null) { 173 vI.notificationBar.dump("## storage: updateVIdentityFromStorage no usable Storage-Data found.\n"); 174 return; 175 } 176 else { 177 vI.notificationBar.dump("## storage: using data from " + ((matchIndex == 0)?"direct":"filter") + " match\n"); 178 } 152 153 // firstUsedInputElement was set before and we are not editing the same 154 var isNotFirstInputElement = (storage.firstUsedInputElement && storage.firstUsedInputElement != inputElement) 155 var currentIdentity = document.getElementById("msgIdentity_clone").identityData 156 var storageResult = storage._rdfDatasourceAccess.updateVIdentityFromStorage(inputElement.value, recipientType, 157 currentIdentity, document.getElementById("msgIdentity_clone").vid, isNotFirstInputElement); 158 159 if (storageResult.identityCollection.number == 0) return; // return if there was no match 160 vI.notificationBar.dump("## storage: updateVIdentityFromStorage result: " + storageResult.result + "\n"); 179 161 // found storageData, so store InputElement 180 162 if (!storage.firstUsedInputElement) storage.firstUsedInputElement = inputElement; 181 163 182 vI.notificationBar.dump("## storage: compare with current Identity\n"); 183 if (vI.main.preferences.getBoolPref("storage_getOneOnly") && // if requested to retrieve only storageID for first recipient entered 184 storage.firstUsedInputElement && // and the request for the first recipient was already done 185 storage.firstUsedInputElement != inputElement && // and it's not the same element we changed now 186 !matchResults.storageData[matchIndex].equalsCurrentIdentity(false).equal) // and this id is different than the current used one 187 vI.notificationBar.setNote(vI.main.elements.strings 188 .getString("vident.smartIdentity.vIStorageCollidingIdentity"), // than drop the potential changes 189 "storage_notification"); 190 // only update fields if new Identity is different than old one. 191 else { 192 vI.notificationBar.dump("## storage: updateVIdentityFromStorage check if storage-data matches current Identity.\n"); 193 var compResult = matchResults.storageData[matchIndex].equalsCurrentIdentity(true); 194 if (!compResult.equal) { 195 var warning = storage.__getWarning("replaceVIdentity", recipient, compResult.compareMatrix); 196 var msgIdentityCloneElem = document.getElementById("msgIdentity_clone") 197 if ( !msgIdentityCloneElem.vid || 198 !vI.main.preferences.getBoolPref("storage_warn_vI_replace") || 199 (storage.__askWarning(warning) == "accept")) { 200 msgIdentityCloneElem.selectedMenuItem = matchResults.menuItem[matchIndex]; 201 if (msgIdentityCloneElem.vid) 202 vI.notificationBar.setNote(vI.main.elements.strings.getString("vident.smartIdentity.vIStorageUsage") + ".", 203 "storage_notification"); 204 } 205 } 206 else { 207 vI.notificationBar.dump("## storage: updateVIdentityFromStorage doing nothing - equals current Identity.\n"); 164 var selectedMenuItem; 165 if (storageResult.result != "equal") { 166 for (var j = 0; j < storageResult.identityCollection.number; j++) { 167 vI.notificationBar.dump("## storage: updateVIdentityFromStorage adding: " + storageResult.identityCollection.identityDataCollection[j].combinedName + "\n"); 168 selectedMenuItem = document.getElementById("msgIdentity_clone").addIdentityToCloneMenu(storageResult.identityCollection.identityDataCollection[j]) 208 169 } 209 170 } 210 }, 211 212 __getDescriptionAndType : function (recipient, recipientType) { 213 if (recipientType == "addr_newsgroups") return { recDesc : recipient, recType : "newsgroup" } 214 else if (storage.__isMailingList(recipient)) { 215 vI.notificationBar.dump("## __getDescriptionAndType: '" + recipient + "' is MailList\n"); 216 return { recDesc : storage.__getMailListName(recipient), recType : "maillist" } 171 if (storageResult.result == "accept") { 172 vI.notificationBar.dump("## storage: updateVIdentityFromStorage selecting: " + storageResult.identityCollection.identityDataCollection[0].combinedName + "\n"); 173 document.getElementById("msgIdentity_clone").selectedMenuItem = selectedMenuItem; 174 if (document.getElementById("msgIdentity_clone").vid) 175 vI.notificationBar.setNote(vI.main.elements.strings.getString("vident.smartIdentity.vIStorageUsage") + ".", 176 "storage_notification"); 217 177 } 218 else {219 vI.notificationBar.dump("## __getDescriptionAndType: '" + recipient + "' is no MailList\n");220 var localIdentityData = new vI.identityData(recipient, null, null, null, null, null, null);221 return { recDesc : localIdentityData.combinedName, recType : "email" }222 }223 },224 225 storeVIdentityToAllRecipients : function(msgType) {226 if (msgType != nsIMsgCompDeliverMode.Now) return true;227 vI.notificationBar.dump("## storage: ----------------------------------------------------------\n")228 if (!vI.main.preferences.getBoolPref("storage"))229 { vI.notificationBar.dump("## storage: Storage deactivated\n"); return true; }230 231 if (vI.statusmenu.objStorageSaveMenuItem.getAttribute("checked") != "true") {232 vI.notificationBar.dump("## storage: SaveMenuItem not checked.\n")233 return true;234 }235 236 vI.notificationBar.dump("## storage: storeVIdentityToAllRecipients()\n");237 238 // check if there are multiple recipients239 storage.multipleRecipients = false;240 var recipients = 0;241 for (var row = 1; row <= top.MAX_RECIPIENTS; row ++) {242 var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");243 if (recipientType == "addr_reply" || recipientType == "addr_followup" ||244 storage.__isDoBcc(row) || awGetInputElement(row).value.match(/^\s*$/) ) continue;245 if (recipients++ == 1) {246 storage.multipleRecipients = true247 vI.notificationBar.dump("## storage: multiple recipients found.\n")248 break;249 }250 }251 252 for (var row = 1; row <= top.MAX_RECIPIENTS; row ++) {253 var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");254 if (recipientType == "addr_reply" || recipientType == "addr_followup" ||255 storage.__isDoBcc(row) || awGetInputElement(row).value.match(/^\s*$/) ) continue;256 if (!storage.__updateStorageFromVIdentity(awGetInputElement(row).value, recipientType)) {257 vI.notificationBar.dump("## storage: -------------- aborted ---------------------------------\n")258 return false; // abort sending259 }260 }261 vI.notificationBar.dump("## storage: ----------------------------------------------------------\n");262 return true;263 },264 265 __getWarning : function(warningCase, recipient, compareMatrix) {266 var warning = { title: null, recLabel : null, recipient : null, warning : null, css: null, query : null, class : null };267 warning.title = vI.main.elements.strings.getString("vident." + warningCase + ".title")268 warning.recLabel = vI.main.elements.strings.getString("vident." + warningCase + ".recipient") + " (" + recipient.recType + "):"269 warning.recipient = recipient.recDesc;270 warning.warning =271 "<table class='" + warningCase + "'><thead><tr><th class='col1'/>" +272 "<th class='col2'>" + vI.main.elements.strings.getString("vident." + warningCase + ".currentIdentity") + "</th>" +273 "<th class='col3'>" + vI.main.elements.strings.getString("vident." + warningCase + ".storedIdentity") + "</th>" +274 "</tr></thead>" +275 "<tbody>" + compareMatrix + "</tbody>" +276 "</table>"277 warning.css = "vI.DialogBrowser.css";278 warning.query = vI.main.elements.strings.getString("vident." + warningCase + ".query");279 warning.class = warningCase;280 return warning;281 },282 283 __askWarning : function(warning) {284 var retVar = { returnValue: null };285 var answer = window.openDialog("chrome://v_identity/content/vI_Dialog.xul","",286 "chrome, dialog, modal, alwaysRaised, resizable=yes",287 warning, retVar)288 return retVar.returnValue;289 },290 291 __updateStorageFromVIdentity : function(recipient, recipientType) {292 vI.notificationBar.dump("## storage: __updateStorageFromVIdentity.\n")293 var dontUpdateMultipleNoEqual = (vI.main.preferences.getBoolPref("storage_dont_update_multiple") &&294 storage.multipleRecipients)295 vI.notificationBar.dump("## storage: __updateStorageFromVIdentity dontUpdateMultipleNoEqual='" + dontUpdateMultipleNoEqual + "'\n")296 recipient = storage.__getDescriptionAndType(recipient, recipientType);297 298 var storageDataByType = storage.rdfDatasource.readVIdentityFromRDF(recipient.recDesc, recipient.recType);299 var storageDataByFilter = storage.rdfDatasource.findMatchingFilter(recipient.recDesc);300 301 // update (storing) of data by type is required if there is302 // no data stored by type (or different data stored) and no equal filter found303 var storageDataByTypeCompResult = storageDataByType?storageDataByType.equalsCurrentIdentity(true):null;304 var storageDataByTypeEqual = (storageDataByType && storageDataByTypeCompResult.equal);305 var storageDataByFilterEqual = (storageDataByFilter && storageDataByFilter.equalsCurrentIdentity(false).equal);306 307 var doUpdate = "";308 if ( (!storageDataByType && !storageDataByFilterEqual) ||309 (!storageDataByTypeEqual && !storageDataByFilterEqual && !dontUpdateMultipleNoEqual) ) {310 vI.notificationBar.dump("## storage: __updateStorageFromVIdentity updating\n")311 var doUpdate = "accept";312 if (storageDataByType && !storageDataByTypeEqual && vI.main.preferences.getBoolPref("storage_warn_update")) {313 vI.notificationBar.dump("## storage: __updateStorageFromVIdentity overwrite warning\n");314 doUpdate = storage.__askWarning(storage.__getWarning("updateStorage", recipient, storageDataByTypeCompResult.compareMatrix));315 if (doUpdate == "takeover") {316 var msgIdentityCloneElem = document.getElementById("msgIdentity_clone");317 msgIdentityCloneElem.selectedMenuItem = msgIdentityCloneElem.addIdentityToCloneMenu(storageDataByType);318 return false;319 }320 if (doUpdate == "abort") return false;321 }322 }323 if (doUpdate == "accept") storage.rdfDatasource.updateRDFFromVIdentity(recipient.recDesc, recipient.recType);324 return true;325 },326 327 // --------------------------------------------------------------------328 // check if recipient is a mailing list.329 // Similiar to Thunderbird, if there are muliple cards with the same displayName the mailinglist is preferred330 // see also https://bugzilla.mozilla.org/show_bug.cgi?id=408575331 __isMailingList: function(recipient) {332 let abManager = Components.classes["@mozilla.org/abmanager;1"]333 .getService(Components.interfaces.nsIAbManager);334 let allAddressBooks = abManager.directories;335 while (allAddressBooks.hasMoreElements()) {336 let ab = allAddressBooks.getNext();337 if (ab instanceof Components.interfaces.nsIAbDirectory && !ab.isRemote) {338 let abdirectory = abManager.getDirectory(ab.URI +339 "?(and(DisplayName,=," + encodeURIComponent(storage.__getMailListName(recipient)) + ")(IsMailList,=,TRUE))");340 if (abdirectory) {341 try { // just try, sometimes there are no childCards at all...342 let cards = abdirectory.childCards;343 if (cards.hasMoreElements()) return true; // only interested if there is at least one element...344 } catch(e) { }345 }346 }347 }348 return false;349 },350 351 // --------------------------------------------------------------------352 353 __getMailListName : function(recipient) {354 if (recipient.match(/<[^>]*>/) || recipient.match(/$/)) {355 var mailListName = RegExp.leftContext + RegExp.rightContext356 mailListName = mailListName.replace(/^\s+|\s+$/g,"")357 }358 return mailListName;359 178 }, 360 179 … … 373 192 } 374 193 return false 375 },376 377 getVIdentityFromAllRecipients : function(allIdentities) {378 if (!vI.main.preferences.getBoolPref("storage"))379 { vI.notificationBar.dump("## storage: Storage deactivated\n"); return; }380 vI.notificationBar.dump("## storage: getVIdentityFromAllRecipients()\n");381 382 for (var row = 1; row <= top.MAX_RECIPIENTS; row ++) {383 var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");384 if (recipientType == "addr_reply" || recipientType == "addr_followup" || storage.__isDoBcc(row)) continue;385 storage.lastCheckedEmail[row] = awGetInputElement(row).value;386 var recipient = storage.__getDescriptionAndType(awGetInputElement(row).value, recipientType);387 var storageData = storage.rdfDatasource.readVIdentityFromRDF(recipient.recDesc, recipient.recType);388 if (storageData) allIdentities.addWithoutDuplicates(storageData);389 storageData = storage.rdfDatasource.findMatchingFilter(recipient.recDesc);390 if (storageData) allIdentities.addWithoutDuplicates(storageData);391 }392 vI.notificationBar.dump("## storage: found " + allIdentities.number + " address(es)\n")393 194 } 394 195 }
Note: See TracChangeset
for help on using the changeset viewer.