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

source: content/vI_storage.js @ c424bd

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

still not robust enough?

  • Property mode set to 100644
File size: 10.1 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 (element.value == "" || 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        storage.original_functions.awSetInputAndPopupValue = awSetInputAndPopupValue;
128        awSetInputAndPopupValue = function (inputElem, inputValue, popupElem, popupValue, rowNumber) {
129            storage.replacement_functions.awSetInputAndPopupValue (inputElem, inputValue, popupElem, popupValue, rowNumber) }
130
131        // reset unavailable storageExtras preferences
132        AddonManager.getAddonByID("{847b3a00-7ab1-11d4-8f02-006008948af5}", function(addon) {
133          if (addon && !addon.userDisabled && !addon.appDisable) {
134            vI.vIprefs.commit("storageExtras_openPGP_messageEncryption", false)
135            vI.vIprefs.commit("storageExtras_openPGP_messageSignature", false)
136            vI.vIprefs.commit("storageExtras_openPGP_PGPMIME", false)
137          }
138        }); 
139    },
140   
141    firstUsedInputElement : null,   // this stores the first Element for which a Lookup in the Storage was successfull
142    __updateVIdentityFromStorage: function(inputElement, currentWindow) {
143        if (!vI.vIprefs.get("storage"))
144            { Log.debug("Storage deactivated"); return; }
145        var currentDocument = currentWindow.document;
146    var recipientType = currentDocument.getElementById(inputElement.id.replace(/^addressCol2/,"addressCol1"))
147            .selectedItem.getAttribute("value");
148
149    var row = inputElement.id.replace(/^addressCol2#/,"")
150        if (recipientType == "addr_reply" || recipientType == "addr_followup" || storage.isDoBcc(row, currentWindow)) {
151            // reset firstUsedInputElement if recipientType was changed (and don't care about doBcc fields)
152            if (storage.firstUsedInputElement == inputElement)
153                storage.firstUsedInputElement = null;
154            Log.debug("field is a 'reply-to' or 'followup-to' or preconfigured 'doBcc'. not searched.")
155            return;
156        }
157       
158        if (inputElement.value == "") {
159            Log.debug("no recipient found, not checked."); return;
160        }
161       
162        var row = inputElement.id.replace(/^addressCol2#/,"")
163        if (storage.lastCheckedEmail[row] && storage.lastCheckedEmail[row] == inputElement.value) {
164            Log.debug("same email than before, not checked again."); return;
165        }
166        storage.lastCheckedEmail[row] = inputElement.value;
167       
168        // firstUsedInputElement was set before and we are not editing the same
169        var isNotFirstInputElement = (storage.firstUsedInputElement && storage.firstUsedInputElement != inputElement)
170        var currentIdentity = currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").identityData
171        var storageResult = storage._rdfDatasourceAccess.updateVIdentityFromStorage(inputElement.value, recipientType,
172            currentIdentity, currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").vid, isNotFirstInputElement, window);
173       
174        if (storageResult.identityCollection.number == 0) return; // return if there was no match
175        Log.debug("__updateVIdentityFromStorage result: " + storageResult.result);
176        // found storageData, so store InputElement
177        if (!storage.firstUsedInputElement) storage.firstUsedInputElement = inputElement;
178       
179        var newMenuItem = null;
180        if (storageResult.result != "equal") {
181            for (var j = 0; j < storageResult.identityCollection.number; j++) {
182                Log.debug("__updateVIdentityFromStorage adding: " + storageResult.identityCollection.identityDataCollection[j].combinedName);
183                let menuItem = currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone")
184                  .addIdentityToCloneMenu(storageResult.identityCollection.identityDataCollection[j])
185                if (!newMenuItem) newMenuItem = menuItem;
186            }
187        }
188        if (storageResult.result == "accept") {
189            Log.debug("__updateVIdentityFromStorage selecting: " + storageResult.identityCollection.identityDataCollection[0].combinedName);
190            currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").selectedMenuItem = newMenuItem;
191            if (currentDocument.getElementById("virtualIdentityExtension_msgIdentityClone").vid)
192                vI.StorageNotification.info(storage.stringBundle.GetStringFromName("vident.smartIdentity.vIStorageUsage") + ".");
193        }
194    },
195   
196    isDoBcc : function(row, currentWindow) {
197        var recipientType = currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value");
198        if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false
199
200        var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {});
201        if (doBccArray && doBccArray.count) {
202            for (var index = 0; index < doBccArray.count; index++ ) {
203                if (doBccArray.StringAt(index) == currentWindow.awGetInputElement(row).value) {
204                    Log.debug("ignoring doBcc field '" +
205                        doBccArray.StringAt(index));
206                    return true;
207                }
208            }
209        }
210        return false
211    }
212}
213vI.storage = storage;
214}});
Note: See TracBrowser for help on using the repository browser.