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 | |
---|
30 | Components.utils.import("resource://v_identity/vI_nameSpaceWrapper.js"); |
---|
31 | virtualIdentityExtension.ns(function () { |
---|
32 | with(virtualIdentityExtension.LIB) { |
---|
33 | |
---|
34 | Components.utils.import("resource://gre/modules/AddonManager.jsm"); |
---|
35 | let Log = vI.setupLogging("virtualIdentity.storage"); |
---|
36 | Components.utils.import("resource://v_identity/vI_rdfDatasource.js", virtualIdentityExtension); |
---|
37 | Components.utils.import("resource://v_identity/vI_prefs.js", virtualIdentityExtension); |
---|
38 | |
---|
39 | var storage = { |
---|
40 | timeStampID: null, // used to trace different objects of same type |
---|
41 | focusedElement: null, |
---|
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 | stringBundle: Components.classes["@mozilla.org/intl/stringbundle;1"] |
---|
49 | .getService(Components.interfaces.nsIStringBundleService) |
---|
50 | .createBundle("chrome://v_identity/locale/v_identity.properties"), |
---|
51 | |
---|
52 | clean: function () { |
---|
53 | Log.debug("clean"); |
---|
54 | storage.multipleRecipients = null; |
---|
55 | storage.lastCheckedEmail = {}; |
---|
56 | storage.firstUsedInputElement = null; |
---|
57 | if (storage.original_functions.awSetInputAndPopupValue) { |
---|
58 | awSetInputAndPopupValue = storage.original_functions.awSetInputAndPopupValue; |
---|
59 | storage.original_functions.awSetInputAndPopupValue = null; |
---|
60 | } |
---|
61 | if (storage._rdfDatasourceAccess) storage._rdfDatasourceAccess.clean(); |
---|
62 | }, |
---|
63 | |
---|
64 | original_functions: { |
---|
65 | awSetInputAndPopupValue: null |
---|
66 | }, |
---|
67 | |
---|
68 | replacement_functions: { |
---|
69 | awSetInputAndPopupValue: function (inputElem, inputValue, popupElem, popupValue, rowNumber) { |
---|
70 | Log.debug("[" + storage.timeStampID + "] " + "awSetInputAndPopupValue '" + inputElem.id + "'"); |
---|
71 | storage.original_functions.awSetInputAndPopupValue(inputElem, inputValue, popupElem, popupValue, rowNumber); |
---|
72 | storage.__updateVIdentityFromStorage(inputElem, storage.currentWindow); |
---|
73 | } |
---|
74 | }, |
---|
75 | |
---|
76 | awOnBlur: function (element, currentWindow) { |
---|
77 | // only react on events triggered by addressCol2 - textinput Elements |
---|
78 | if (!element || !element.id.match(/^addressCol2*/)) return; |
---|
79 | Log.debug("awOnBlur '" + element.id); |
---|
80 | if (!element.value || element.value == "" || typeof element.value == 'undefined') return; |
---|
81 | storage.__updateVIdentityFromStorage(element, currentWindow); |
---|
82 | storage.focusedElement = null; |
---|
83 | }, |
---|
84 | |
---|
85 | awOnFocus: function (element, currentWindow) { |
---|
86 | if (!element || !element.id.match(/^addressCol2*/)) return; |
---|
87 | Log.debug("awOnFocus '" + element.id); |
---|
88 | storage.focusedElement = element; |
---|
89 | }, |
---|
90 | |
---|
91 | awPopupOnCommand: function (element, currentWindow) { |
---|
92 | Log.debug("awPopupOnCommand '" + element.id + "' '" + element.value + "'"); |
---|
93 | storage.__updateVIdentityFromStorage(element.parentNode.nextSibling.firstChild, currentWindow); |
---|
94 | }, |
---|
95 | |
---|
96 | initialized: null, |
---|
97 | currentWindow: null, |
---|
98 | init: function () { |
---|
99 | if (!this.timeStampID) { |
---|
100 | this.timeStampID = parseInt((new Date()).getTime() / 100) % 864000; // give object unified id (per day) |
---|
101 | Log = vI.setupLogging("virtualIdentity.storage[" + this.timeStampID + "]"); |
---|
102 | } |
---|
103 | if (!storage.initialized) { |
---|
104 | Log.debug("initializing storage request environment"); |
---|
105 | storage._rdfDatasourceAccess = new vI.rdfDatasourceAccess(window); |
---|
106 | |
---|
107 | // better approach would be to use te onchange event, but this one is not fired in any change case |
---|
108 | // see https://bugzilla.mozilla.org/show_bug.cgi?id=355367 |
---|
109 | // same seems to happen with the ondragdrop event |
---|
110 | if (!top.MAX_RECIPIENTS || top.MAX_RECIPIENTS == 0) top.MAX_RECIPIENTS = 1; |
---|
111 | for (var row = 1; row <= top.MAX_RECIPIENTS; row++) { |
---|
112 | var input = window.awGetInputElement(row); |
---|
113 | if (input) { |
---|
114 | var oldBlur = input.getAttribute("onblur") |
---|
115 | input.setAttribute("onblur", (oldBlur ? oldBlur + "; " : "") + |
---|
116 | "window.setTimeout(virtualIdentityExtension.storage.awOnBlur, 250, this, window);") |
---|
117 | var oldFocus = input.getAttribute("onfocus") |
---|
118 | input.setAttribute("onfocus", (oldFocus ? oldFocus + "; " : "") + |
---|
119 | "window.setTimeout(virtualIdentityExtension.storage.awOnFocus, 250, this, window);") |
---|
120 | } |
---|
121 | var popup = window.awGetPopupElement(row); |
---|
122 | if (popup) { |
---|
123 | var oldCommand = popup.getAttribute("oncommand") |
---|
124 | popup.setAttribute("oncommand", |
---|
125 | "window.setTimeout(virtualIdentityExtension.storage.awPopupOnCommand, 250, this, window);" + |
---|
126 | (oldCommand ? "; " + oldCommand : "")) |
---|
127 | } |
---|
128 | } |
---|
129 | storage.currentWindow = window; |
---|
130 | Log.debug("initializing storage request environment - done."); |
---|
131 | storage.initialized = true; |
---|
132 | } else { |
---|
133 | Log.debug("storage request environment already initialized"); |
---|
134 | } |
---|
135 | |
---|
136 | if (typeof awSetInputAndPopupValue == 'function' && storage.original_functions.awSetInputAndPopupValue == null) { |
---|
137 | Log.debug("replacing awSetInputAndPopupValue"); |
---|
138 | storage.original_functions.awSetInputAndPopupValue = awSetInputAndPopupValue; |
---|
139 | awSetInputAndPopupValue = function (inputElem, inputValue, popupElem, popupValue, rowNumber) { |
---|
140 | storage.replacement_functions.awSetInputAndPopupValue(inputElem, inputValue, popupElem, popupValue, rowNumber) |
---|
141 | } |
---|
142 | } |
---|
143 | }, |
---|
144 | |
---|
145 | firstUsedInputElement: null, // this stores the first Element for which a Lookup in the Storage was successfull |
---|
146 | __updateVIdentityFromStorage: function (inputElement, currentWindow) { |
---|
147 | Log.debug("__updateVIdentityFromStorage"); |
---|
148 | if (!vI.vIprefs.get("storage")) { |
---|
149 | Log.debug("Storage deactivated"); |
---|
150 | return; |
---|
151 | } |
---|
152 | var currentDocument = currentWindow.document; |
---|
153 | try { |
---|
154 | var recipientType = inputElement.parentNode.previousSibling.firstChild.selectedItem.getAttribute("value"); |
---|
155 | } catch (e) { |
---|
156 | Log.debug("inputElement.parentNode.previousSibling.firstChild.selectedItem.getAttribute('value') raised an error") |
---|
157 | return; |
---|
158 | } |
---|
159 | |
---|
160 | var row = inputElement.id.replace(/^addressCol2#/, "") |
---|
161 | if (recipientType == "addr_reply" || recipientType == "addr_followup" || storage.isDoBcc(row, currentWindow)) { |
---|
162 | // reset firstUsedInputElement if recipientType was changed (and don't care about doBcc fields) |
---|
163 | if (storage.firstUsedInputElement == inputElement) |
---|
164 | storage.firstUsedInputElement = null; |
---|
165 | Log.debug("field is a 'reply-to' or 'followup-to' or preconfigured 'doBcc'. not searched.") |
---|
166 | return; |
---|
167 | } |
---|
168 | |
---|
169 | if (inputElement.value == "") { |
---|
170 | Log.debug("no recipient found, not checked."); |
---|
171 | return; |
---|
172 | } |
---|
173 | |
---|
174 | var row = inputElement.id.replace(/^addressCol2#/, "") |
---|
175 | if (storage.lastCheckedEmail[row] && storage.lastCheckedEmail[row] == inputElement.value) { |
---|
176 | Log.debug("same email than before, not checked again."); |
---|
177 | return; |
---|
178 | } |
---|
179 | storage.lastCheckedEmail[row] = inputElement.value; |
---|
180 | |
---|
181 | // firstUsedInputElement was set before and we are not editing the same |
---|
182 | var isNotFirstInputElement = (storage.firstUsedInputElement && storage.firstUsedInputElement != inputElement) |
---|
183 | var currentIdentity = currentDocument.getElementById("msgIdentity").identityData |
---|
184 | var storageResult = storage._rdfDatasourceAccess.updateVIdentityFromStorage(inputElement.value, recipientType, |
---|
185 | currentIdentity, currentDocument.getElementById("msgIdentity").vid, isNotFirstInputElement); |
---|
186 | |
---|
187 | if (storageResult.identityCollection.number == 0) return; // return if there was no match |
---|
188 | Log.debug("__updateVIdentityFromStorage result: " + storageResult.result); |
---|
189 | // found storageData, so store InputElement |
---|
190 | if (!storage.firstUsedInputElement) storage.firstUsedInputElement = inputElement; |
---|
191 | |
---|
192 | var newMenuItem = null; |
---|
193 | if (storageResult.result != "equal") { |
---|
194 | for (var j = 0; j < storageResult.identityCollection.number; j++) { |
---|
195 | Log.debug("__updateVIdentityFromStorage adding: " + storageResult.identityCollection.identityDataCollection[j].combinedName); |
---|
196 | let menuItem = currentDocument.getElementById("msgIdentity") |
---|
197 | .addIdentityToMsgIdentityMenu(storageResult.identityCollection.identityDataCollection[j]) |
---|
198 | if (!newMenuItem) newMenuItem = menuItem; |
---|
199 | } |
---|
200 | } |
---|
201 | if (storageResult.result == "accept") { |
---|
202 | Log.debug("__updateVIdentityFromStorage selecting: " + storageResult.identityCollection.identityDataCollection[0].combinedName); |
---|
203 | currentDocument.getElementById("msgIdentity").selectedMenuItem = newMenuItem; |
---|
204 | if (currentDocument.getElementById("msgIdentity").vid) |
---|
205 | vI.StorageNotification.info(storage.stringBundle.GetStringFromName("vident.smartIdentity.vIStorageUsage") + "."); |
---|
206 | } |
---|
207 | }, |
---|
208 | |
---|
209 | isDoBcc: function (row, currentWindow) { |
---|
210 | if (typeof currentWindow.awGetPopupElement(row).selectedItem == 'undefined') |
---|
211 | return false; |
---|
212 | var recipientType = currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value"); |
---|
213 | if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false |
---|
214 | |
---|
215 | var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {}); |
---|
216 | if (doBccArray && doBccArray.count) { |
---|
217 | for (var index = 0; index < doBccArray.count; index++) { |
---|
218 | if (doBccArray.StringAt(index) == currentWindow.awGetInputElement(row).value) { |
---|
219 | Log.debug("ignoring doBcc field '" + |
---|
220 | doBccArray.StringAt(index)); |
---|
221 | return true; |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | return false |
---|
226 | } |
---|
227 | } |
---|
228 | vI.storage = storage; |
---|
229 | } |
---|
230 | }); |
---|