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

source: content/vI_storage.js @ f29419

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

tons of changes, mostly namespace-related

  • Property mode set to 100644
File size: 9.4 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.\n");
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 +"'\n");
66            storage.original_functions.awSetInputAndPopupValue(inputElem, inputValue, popupElem, popupValue, rowNumber);
67            storage.updateVIdentityFromStorage(inputElem);
68        }
69    },
70       
71    awOnBlur : function (element) {
72        // only react on events triggered by addressCol2 - textinput Elements
73        if (!element || ! element.id.match(/^addressCol2*/)) return;
74        Log.debug("\nawOnBlur '" + element.id +"'\n");
75        storage.updateVIdentityFromStorage(element);
76        storage.focusedElement = null;
77    },
78
79    awOnFocus : function (element) {
80        if (!element || ! element.id.match(/^addressCol2*/)) return;
81        storage.focusedElement = element;
82    },
83
84    awPopupOnCommand : function (element) {
85        Log.debug("\nawPopupOnCommand'" + element.id +"'\n");
86        storage.updateVIdentityFromStorage(document.getElementById(element.id.replace(/^addressCol1/,"addressCol2")));
87        if (element.selectedItem.getAttribute("value") == "addr_reply") // if reply-to is manually entered disable AutoReplyToSelf
88            document.getElementById("virtualIdentityExtension_autoReplyToSelfLabel").setAttribute("hidden", "true");
89
90    },
91   
92    initialized : null,
93    init: function() {
94        if (!storage.initialized) {
95            storage._rdfDatasourceAccess = new vI.rdfDatasourceAccess();
96
97            // better approach would be to use te onchange event, but this one is not fired in any change case
98            // see https://bugzilla.mozilla.org/show_bug.cgi?id=355367
99            // same seems to happen with the ondragdrop event
100            if (top.MAX_RECIPIENTS == 0) top.MAX_RECIPIENTS = 1;
101            for (var row = 1; row <= top.MAX_RECIPIENTS ; row ++) {
102                var input = awGetInputElement(row);
103                if (input) {
104                    var oldBlur = input.getAttribute("onblur")
105                    input.setAttribute("onblur", (oldBlur?oldBlur+"; ":"") +
106                        "window.setTimeout(virtualIdentityExtension.storage.awOnBlur, 250, this.parentNode.parentNode.parentNode);")
107                    var oldFocus = input.getAttribute("onfocus")
108                    input.setAttribute("onfocus", (oldFocus?oldFocus+"; ":"") +
109                        "window.setTimeout(virtualIdentityExtension.storage.awOnFocus, 250, this.parentNode.parentNode.parentNode);")
110                }
111                var popup = awGetPopupElement(row);
112                if (popup) {
113                    var oldCommand = popup.getAttribute("oncommand")
114                    popup.setAttribute("oncommand", (oldCommand?oldCommand+"; ":"") +
115                        "window.setTimeout(virtualIdentityExtension.storage.awPopupOnCommand, 250, this);")
116                }
117            }
118            storage.initialized = true;
119        }
120        storage.original_functions.awSetInputAndPopupValue = awSetInputAndPopupValue;
121        awSetInputAndPopupValue = function (inputElem, inputValue, popupElem, popupValue, rowNumber) {
122            storage.replacement_functions.awSetInputAndPopupValue (inputElem, inputValue, popupElem, popupValue, rowNumber) }
123
124        // reset unavailable storageExtras preferences
125        AddonManager.getAddonByID("{847b3a00-7ab1-11d4-8f02-006008948af5}", function(addon) {
126          if (addon && !addon.userDisabled && !addon.appDisable) {
127            vI.vI_prefs.commit("storageExtras_openPGP_messageEncryption", false)
128            vI.vI_prefs.commit("storageExtras_openPGP_messageSignature", false)
129            vI.vI_prefs.commit("storageExtras_openPGP_PGPMIME", false)
130          }
131        }); 
132    },
133   
134    firstUsedInputElement : null,   // this stores the first Element for which a Lookup in the Storage was successfull
135    updateVIdentityFromStorage: function(inputElement) {
136        if (!vI.vIprefs.get("storage"))
137            { Log.debug("Storage deactivated\n"); return; }
138        Log.debug("updateVIdentityFromStorage()\n");
139
140        var recipientType = document.getElementById(inputElement.id.replace(/^addressCol2/,"addressCol1"))
141            .selectedItem.getAttribute("value");
142        var row = inputElement.id.replace(/^addressCol2#/,"")
143        if (recipientType == "addr_reply" || recipientType == "addr_followup" || storage.__isDoBcc(row)) {
144            // reset firstUsedInputElement if recipientType was changed (and don't care about doBcc fields)
145            if (storage.firstUsedInputElement == inputElement)
146                storage.firstUsedInputElement = null;
147            Log.debug("field is a 'reply-to' or 'followup-to' or preconfigured 'doBcc'. not searched.\n")
148            return;
149        }
150       
151        if (inputElement.value == "") {
152            Log.debug("no recipient found, not checked.\n"); return;
153        }
154       
155        var row = inputElement.id.replace(/^addressCol2#/,"")
156        if (storage.lastCheckedEmail[row] && storage.lastCheckedEmail[row] == inputElement.value) {
157            Log.debug("same email than before, not checked again.\n"); return;
158        }
159        storage.lastCheckedEmail[row] = inputElement.value;
160       
161        // firstUsedInputElement was set before and we are not editing the same
162        var isNotFirstInputElement = (storage.firstUsedInputElement && storage.firstUsedInputElement != inputElement)
163        var currentIdentity = document.getElementById("virtualIdentityExtension_msgIdentityClone").identityData
164        var storageResult = storage._rdfDatasourceAccess.updateVIdentityFromStorage(inputElement.value, recipientType,
165            currentIdentity, document.getElementById("virtualIdentityExtension_msgIdentityClone").vid, isNotFirstInputElement);
166       
167        if (storageResult.identityCollection.number == 0) return; // return if there was no match
168        Log.debug("updateVIdentityFromStorage result: " + storageResult.result + "\n");
169        // found storageData, so store InputElement
170        if (!storage.firstUsedInputElement) storage.firstUsedInputElement = inputElement;
171       
172        var newMenuItem = null;
173        if (storageResult.result != "equal") {
174            for (var j = 0; j < storageResult.identityCollection.number; j++) {
175                Log.debug("updateVIdentityFromStorage adding: " + storageResult.identityCollection.identityDataCollection[j].combinedName + "\n");
176                let menuItem = document.getElementById("virtualIdentityExtension_msgIdentityClone")
177                  .addIdentityToCloneMenu(storageResult.identityCollection.identityDataCollection[j])
178                if (!newMenuItem) newMenuItem = menuItem;
179            }
180        }
181        if (storageResult.result == "accept") {
182            Log.debug("updateVIdentityFromStorage selecting: " + storageResult.identityCollection.identityDataCollection[0].combinedName + "\n");
183            document.getElementById("virtualIdentityExtension_msgIdentityClone").selectedMenuItem = newMenuItem;
184            if (document.getElementById("virtualIdentityExtension_msgIdentityClone").vid)
185                vI.StorageNotification.info(storage.stringBundle.GetStringFromName("vident.smartIdentity.vIStorageUsage") + ".");
186        }
187    },
188   
189    __isDoBcc : function(row) {
190        var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
191        if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false
192
193        var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {});
194
195        for (var index = 0; index < doBccArray.count; index++ ) {
196            if (doBccArray.StringAt(index) == awGetInputElement(row).value) {
197                Log.debug("ignoring doBcc field '" +
198                    doBccArray.StringAt(index) + "'.\n");
199                return true;
200            }
201        }       
202        return false
203    }
204}
205vI.storage = storage;
206}});
Note: See TracBrowser for help on using the repository browser.