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

source: content/vI_storage.js @ b08078

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

rearranged storageExtras -> identityDataExtras to cleanup and enable usage in MessengerDialog?

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