This is just some static backup of the original site, don't expect every link to work!

source: content/vI_storage.js @ 162a67

ng_0.9
Last change on this file since 162a67 was 162a67, checked in by rene <rene@…>, 8 years ago

if awSetInputAndPopupValue is not available this will result in infinite loop

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
16    The Original Code is the Virtual Identity Extension.
17
18    The Initial Developer of the Original Code is Rene Ejury.
19    Portions created by the Initial Developer are Copyright (C) 2007
20    the Initial Developer. All Rights Reserved.
21
22    Contributor(s): Mike Krieger, Sebastian Apel
23 * ***** END LICENSE BLOCK ***** */
24 
25/**
26* some code copied and adapted from 'addressContext' and from 'Birthday Reminder'
27* thanks to Mike Krieger and Sebastian Apel
28*/
29
30Components.utils.import("resource://v_identity/vI_nameSpaceWrapper.js");
31virtualIdentityExtension.ns(function() { with (virtualIdentityExtension.LIB) {
32
33Components.utils.import("resource://gre/modules/AddonManager.jsm");
34let Log = vI.setupLogging("virtualIdentity.storage");
35Components.utils.import("resource://v_identity/vI_rdfDatasource.js", virtualIdentityExtension);
36Components.utils.import("resource://v_identity/vI_prefs.js", virtualIdentityExtension);
37
38var storage = {
39    focusedElement : null,
40   
41    lastCheckedEmail : {},  // array of last checked emails per row,
42                // to prevent ugly double dialogs and time-consuming double-checks
43   
44    _rdfDatasourceAccess : null,    // local storage
45
46    stringBundle : Components.classes["@mozilla.org/intl/stringbundle;1"]
47        .getService(Components.interfaces.nsIStringBundleService)
48        .createBundle("chrome://v_identity/locale/v_identity.properties"),
49
50    clean: function() {
51        Log.debug("clean.");
52        storage.multipleRecipients = null;
53        storage.lastCheckedEmail = {};
54        storage.firstUsedInputElement = null;
55        awSetInputAndPopupValue = storage.original_functions.awSetInputAndPopupValue;
56        if (storage._rdfDatasourceAccess) storage._rdfDatasourceAccess.clean();
57    },
58   
59    original_functions : {
60        awSetInputAndPopupValue : null
61    },
62
63    replacement_functions : {
64        awSetInputAndPopupValue : function (inputElem, inputValue, popupElem, popupValue, rowNumber) {
65            Log.debug("awSetInputAndPopupValue '" + inputElem.id + "'");
66            storage.original_functions.awSetInputAndPopupValue(inputElem, inputValue, popupElem, popupValue, rowNumber);
67            storage.__updateVIdentityFromStorage(inputElem, storage.currentWindow);
68        }
69    },
70       
71    awOnBlur : function (element, currentWindow) {
72        // only react on events triggered by addressCol2 - textinput Elements
73        if (!element || ! element.id.match(/^addressCol2*/)) return;
74        Log.debug("awOnBlur '" + element.id + "' '" + element.value  + "'");
75        if (typeof element.value == 'undefined') {
76            element.value = element.getAttribute("value");
77            Log.debug("awOnBlur second try'" + element.id + "' '" + element.value  + "'");
78        }
79        if (element.value == "" || typeof element.value == 'undefined') return;
80        storage.__updateVIdentityFromStorage(element, currentWindow);
81        storage.focusedElement = null;
82    },
83
84    awOnFocus : function (element, currentWindow) {
85        if (!element || ! element.id.match(/^addressCol2*/)) return;
86        storage.focusedElement = element;
87    },
88
89    awPopupOnCommand : function (element, currentWindow) {
90        Log.debug("awPopupOnCommand '" + element.id + "' '" + element.value  + "'");
91        storage.__updateVIdentityFromStorage(currentWindow.document.getElementById(element.id.replace(/^addressCol1/,"addressCol2")), currentWindow);
92        if (element.selectedItem.getAttribute("value") == "addr_reply") // if reply-to is manually entered disable AutoReplyToSelf
93            currentWindow.document.getElementById("virtualIdentityExtension_autoReplyToSelfLabel").setAttribute("hidden", "true");
94
95    },
96   
97  initialized : null,
98  currentWindow: null,
99    init: function() {
100        if (!storage.initialized) {
101            storage._rdfDatasourceAccess = new vI.rdfDatasourceAccess();
102
103            // better approach would be to use te onchange event, but this one is not fired in any change case
104            // see https://bugzilla.mozilla.org/show_bug.cgi?id=355367
105            // same seems to happen with the ondragdrop event
106            if (!top.MAX_RECIPIENTS || top.MAX_RECIPIENTS == 0) top.MAX_RECIPIENTS = 1;
107            for (var row = 1; row <= top.MAX_RECIPIENTS ; row ++) {
108                var input = window.awGetInputElement(row);
109                if (input) {
110                    var oldBlur = input.getAttribute("onblur")
111                    input.setAttribute("onblur", (oldBlur?oldBlur+"; ":"") +
112                        "window.setTimeout(virtualIdentityExtension.storage.awOnBlur, 250, this, window);")
113                    var oldFocus = input.getAttribute("onfocus")
114                    input.setAttribute("onfocus", (oldFocus?oldFocus+"; ":"") +
115                        "window.setTimeout(virtualIdentityExtension.storage.awOnFocus, 250, this, window);")
116                }
117                var popup = window.awGetPopupElement(row);
118                if (popup) {
119                    var oldCommand = popup.getAttribute("oncommand")
120                    popup.setAttribute("oncommand", (oldCommand?oldCommand+"; ":"") +
121                        "window.setTimeout(virtualIdentityExtension.storage.awPopupOnCommand, 250, this, window);")
122                }
123            }
124            storage.currentWindow = window;
125            storage.initialized = true;
126        }
127
128        if (typeof awSetInputAndPopupValue == 'function' && storage.original_functions.awSetInputAndPopupValue == null) {
129            storage.original_functions.awSetInputAndPopupValue = awSetInputAndPopupValue;
130            awSetInputAndPopupValue = function (inputElem, inputValue, popupElem, popupValue, rowNumber) {
131                storage.replacement_functions.awSetInputAndPopupValue (inputElem, inputValue, popupElem, popupValue, rowNumber) }
132        }
133        // reset unavailable storageExtras preferences
134        AddonManager.getAddonByID("{847b3a00-7ab1-11d4-8f02-006008948af5}", function(addon) {
135          if (addon && !addon.userDisabled && !addon.appDisable) {
136            vI.vIprefs.commit("storageExtras_openPGP_messageEncryption", false)
137            vI.vIprefs.commit("storageExtras_openPGP_messageSignature", false)
138            vI.vIprefs.commit("storageExtras_openPGP_PGPMIME", false)
139          }
140        }); 
141    },
142   
143    firstUsedInputElement : null,   // this stores the first Element for which a Lookup in the Storage was successfull
144    __updateVIdentityFromStorage: function(inputElement, currentWindow) {
145        if (!vI.vIprefs.get("storage"))
146            { Log.debug("Storage deactivated"); return; }
147        var currentDocument = currentWindow.document;
148    var recipientType = currentDocument.getElementById(inputElement.id.replace(/^addressCol2/,"addressCol1"))
149            .selectedItem.getAttribute("value");
150
151    var row = inputElement.id.replace(/^addressCol2#/,"")
152        if (recipientType == "addr_reply" || recipientType == "addr_followup" || storage.isDoBcc(row, currentWindow)) {
153            // reset firstUsedInputElement if recipientType was changed (and don't care about doBcc fields)
154            if (storage.firstUsedInputElement == inputElement)
155                storage.firstUsedInputElement = null;
156            Log.debug("field is a 'reply-to' or 'followup-to' or preconfigured 'doBcc'. not searched.")
157            return;
158        }
159       
160        if (inputElement.value == "") {
161            Log.debug("no recipient found, not checked."); return;
162        }
163       
164        var row = inputElement.id.replace(/^addressCol2#/,"")
165        if (storage.lastCheckedEmail[row] && storage.lastCheckedEmail[row] == inputElement.value) {
166            Log.debug("same email than before, not checked again."); return;
167        }
168        storage.lastCheckedEmail[row] = inputElement.value;
169       
170        // firstUsedInputElement was set before and we are not editing the same
171        var isNotFirstInputElement = (storage.firstUsedInputElement && storage.firstUsedInputElement != inputElement)
172        var currentIdentity = currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").identityData
173        var storageResult = storage._rdfDatasourceAccess.updateVIdentityFromStorage(inputElement.value, recipientType,
174            currentIdentity, currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").vid, isNotFirstInputElement, window);
175       
176        if (storageResult.identityCollection.number == 0) return; // return if there was no match
177        Log.debug("__updateVIdentityFromStorage result: " + storageResult.result);
178        // found storageData, so store InputElement
179        if (!storage.firstUsedInputElement) storage.firstUsedInputElement = inputElement;
180       
181        var newMenuItem = null;
182        if (storageResult.result != "equal") {
183            for (var j = 0; j < storageResult.identityCollection.number; j++) {
184                Log.debug("__updateVIdentityFromStorage adding: " + storageResult.identityCollection.identityDataCollection[j].combinedName);
185                let menuItem = currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone")
186                  .addIdentityToCloneMenu(storageResult.identityCollection.identityDataCollection[j])
187                if (!newMenuItem) newMenuItem = menuItem;
188            }
189        }
190        if (storageResult.result == "accept") {
191            Log.debug("__updateVIdentityFromStorage selecting: " + storageResult.identityCollection.identityDataCollection[0].combinedName);
192            currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").selectedMenuItem = newMenuItem;
193            if (currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").vid)
194                vI.StorageNotification.info(storage.stringBundle.GetStringFromName("vident.smartIdentity.vIStorageUsage") + ".");
195        }
196    },
197   
198    isDoBcc : function(row, currentWindow) {
199        var recipientType = currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value");
200        if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false
201
202        var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {});
203        if (doBccArray && doBccArray.count) {
204            for (var index = 0; index < doBccArray.count; index++ ) {
205                if (doBccArray.StringAt(index) == currentWindow.awGetInputElement(row).value) {
206                    Log.debug("ignoring doBcc field '" +
207                        doBccArray.StringAt(index));
208                    return true;
209                }
210            }
211        }
212        return false
213    }
214}
215vI.storage = storage;
216}});
Note: See TracBrowser for help on using the repository browser.