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

source: content/vI_storage.js @ 509348

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

code formatting (no code changes)

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