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

source: content/v_identity.js @ a22fe7

ng_0.9 0.10.2
Last change on this file since a22fe7 was 61e3f4, checked in by rene <rene@…>, 4 years ago

compose-window is not recycled anymore

  • Property mode set to 100644
File size: 13.9 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):
23 * ***** END LICENSE BLOCK ***** */
24
25Components.utils.import("resource://v_identity/vI_nameSpaceWrapper.js");
26virtualIdentityExtension.ns(function () {
27  with(virtualIdentityExtension.LIB) {
28
29    let Log = vI.setupLogging("virtualIdentity.main");
30    Components.utils.import("resource://v_identity/vI_account.js", virtualIdentityExtension);
31    Components.utils.import("resource://v_identity/vI_prefs.js", virtualIdentityExtension);
32    Components.utils.import("resource://v_identity/vI_accountUtils.js", virtualIdentityExtension);
33    Components.utils.import("resource://v_identity/vI_identityData.js", virtualIdentityExtension);
34    Components.utils.import("resource://v_identity/vI_smartIdentity.js", virtualIdentityExtension);
35    Components.utils.import("resource://v_identity/vI_log.js", virtualIdentityExtension);
36    Components.utils.import("resource://v_identity/vI_rdfDatasource.js", virtualIdentityExtension);
37    Components.utils.import("resource:///modules/mailServices.js");
38
39    var main = {
40      timeStampID: null,
41      _smartIdentity: null,
42     
43      headerParser: Components.classes["@mozilla.org/messenger/headerparser;1"]
44        .getService(Components.interfaces.nsIMsgHeaderParser),
45
46      unicodeConverter: Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
47        .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
48
49      accountManager: Components.classes["@mozilla.org/messenger/account-manager;1"]
50        .getService(Components.interfaces.nsIMsgAccountManager),
51
52      promptService: Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
53              .getService(Ci.nsIPromptService),
54
55      _stringBundle: Services.strings.createBundle("chrome://v_identity/locale/v_identity.properties"),
56
57      // Those variables keep pointers to original functions which might get replaced later
58      original_functions: {
59        GenericSendMessage: null,
60        LoadIdentity: null
61      },
62
63      // some pointers to the layout-elements of the extension
64      elements: {
65        init_base: function () {
66          main.elements.Area_MsgIdentityHbox = document.getElementById("virtualIdentityExtension_msgIdentityHbox");
67          main.elements.Obj_MsgIdentity = document.getElementById("msgIdentity");
68        },
69        init_rest: function () {
70          main.elements.Obj_MsgIdentityPopup = document.getElementById("msgIdentityPopup");
71          main.elements.Obj_vILogo = document.getElementById("virtualIdentityExtension_Logo");
72        },
73        strings: null
74      },
75
76      ComposeStateListener: {
77        NotifyComposeBodyReady: function () {
78          Log.debug("NotifyComposeBodyReady");
79          main.initSystemStage2();
80        },
81        NotifyComposeFieldsReady: function () {
82          Log.debug("NotifyComposeFieldsReady");
83        },
84        ComposeProcessDone: function (aResult) {
85          Log.debug("StateListener reports ComposeProcessDone");
86          vI.storage.clean();
87        },
88        SaveInFolderDone: function (folderURI) {
89          Log.debug("SaveInFolderDone");
90          vI.storage.clean();
91        }
92      },
93
94      replacement_functions: {
95        LoadIdentity: function (startup) {
96          var identityElement = document.getElementById("msgIdentity");
97         
98          // identitykey and accountkey might not be set on new selectedItem, if it's a virtual identity
99          // on startup there might be no identitykey if a virtual identity is selected.
100          Log.debug("run adapted LoadIdentity startup=" + startup);
101         
102          let hasBaseId = identityElement.selectedItem.identityData.id.key;
103          if (hasBaseId == null) {
104            identityElement.selectedItem.setAttribute("identitykey", identityElement.getAttribute("identitykey"));
105            identityElement.selectedItem.setAttribute("accountkey", identityElement.getAttribute("accountkey"));
106          }
107          else {
108            identityElement.setAttribute("description", identityElement.selectedItem.getAttribute("description"));
109            identityElement.setAttribute("idDescription", identityElement.selectedItem.identityData.id.value);
110          }
111         
112          if (startup)
113            identityElement.identityData = identityElement.selectedItem.identityData.getDuplicate();
114          // else only values are copied into current identityData
115          else
116            identityElement.identityData.takeOverAvailableData(identityElement.selectedItem.identityData);
117         
118          gComposeNotificationBar.clearIdentityWarning();
119         
120          main.original_functions.LoadIdentity(startup);
121         
122          // store identitykey locally to enable restoring after selection of next virtual identity without identitykey
123          identityElement.setAttribute("identitykey", identityElement.selectedItem.getAttribute("identitykey"));
124         
125          identityElement.vid = identityElement.selectedItem.vid;
126         
127          identityElement.selectedItem.identityData.extras.setValuesToEnvironment()
128        },
129       
130        GenericSendMessage: function (msgType) {
131          try { // nice, but not required for sending messages
132            // if addressCol2 is focused while sending check storage for the entered address before continuing
133            vI.storage.awOnBlur(vI.storage.focusedElement, window);
134          } catch (e) {}
135
136          Log.debug("VIdentity_GenericSendMessage");
137
138          // check via virtual / non-virtual constraints and storage results if mail should be sent
139          if (msgType == Ci.nsIMsgCompDeliverMode.Now) {
140            if ((main.elements.Obj_MsgIdentity.vid && vI.vIprefs.get("warn_virtual") &&
141                !(main.promptService.confirm(window, "Warning",
142                  main._stringBundle.GetStringFromName("vident.sendVirtual.warning")))) ||
143              (!main.elements.Obj_MsgIdentity.vid && vI.vIprefs.get("warn_nonvirtual") &&
144                !(main.promptService.confirm(window, "Warning",
145                  main._stringBundle.GetStringFromName("vident.sendNonvirtual.warning"))))) {
146
147              Log.debug("sending: --------------  aborted  ---------------------------------")
148              return;
149            }
150            if (vI.vIprefs.get("storage") && vI.vIprefs.get("storage_store")) {
151              var localeDatasourceAccess = new vI.rdfDatasourceAccess(window);
152              var returnValue = localeDatasourceAccess.storeVIdentityToAllRecipients(
153                main.elements.Obj_MsgIdentity.identityData, main._getRecipients())
154              if (returnValue.update == "takeover")
155                main.elements.Obj_MsgIdentity.selectedMenuItem =
156                  main.elements.Obj_MsgIdentity.addIdentityToMsgIdentityMenu(returnValue.storedIdentity);
157              if (returnValue.update == "takeover" || returnValue.update == "abort") {
158                Log.debug("sending: --------------  aborted  ---------------------------------")
159                return;
160              }
161            } else Log.debug("prepareSendMsg: storage deactivated");
162          }
163          main.original_functions.GenericSendMessage(msgType);
164        },
165      },
166
167      _getRecipients: function () {
168        var recipients = [];
169        for (var row = 1; row <= top.MAX_RECIPIENTS; row++) {
170          if (typeof awGetPopupElement(row).selectedItem == 'undefined')
171            continue;
172          var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
173          if (recipientType == "addr_reply" || recipientType == "addr_followup" ||
174            main._recipientIsDoBcc(row) || awGetInputElement(row).value.match(/^\s*$/)) continue;
175          recipients.push({
176            recipient: awGetInputElement(row).value,
177            recipientType: recipientType
178          });
179        }
180        return recipients;
181      },
182
183      _recipientIsDoBcc: function (row) {
184        if (typeof awGetPopupElement(row).selectedItem == 'undefined')
185          return false;
186        var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
187        if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false
188
189        var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {});
190
191        for (var index = 0; index < doBccArray.count; index++) {
192          if (doBccArray.StringAt(index) == awGetInputElement(row).value) {
193            Log.debug("_recipientIsDoBcc: ignoring doBcc field '" +
194              doBccArray.StringAt(index));
195            return true;
196          }
197        }
198        return false
199      },
200
201      // initialization //
202      init: function () {
203        if (!this.timeStampID) {
204          this.timeStampID = parseInt((new Date()).getTime() / 100) % 864000; // give object unified id (per day)
205          Log = vI.setupLogging("virtualIdentity.main[" + this.timeStampID + "]");
206        }
207        window.removeEventListener('load', main.init, false);
208        window.removeEventListener('compose-window-init', main.init, true);
209
210        Log.debug("init.")
211        main.unicodeConverter.charset = "UTF-8";
212       
213        if (!main.adapt_genericSendMessage()) {
214          Log.error("init failed.");
215          return;
216        }
217       
218        let statusbarLabel = document.getElementById("v_identity_logo_statusbar");
219        statusbarLabel.setAttribute("value", statusbarLabel.getAttribute("value") + vI.extensionVersion);
220       
221        gMsgCompose.RegisterStateListener(main.ComposeStateListener);
222        document.getElementById("virtualIdentityExtension_tooltipPopupset")
223          .addTooltip(document.getElementById("msgIdentity"), false);
224
225        main.AccountManagerObserver.register();
226
227        Log.debug("init done.")
228      },
229
230      initSystemStage2: function () {
231        Log.debug("initSystemStage2.");
232//         Log.debug("document.title=" + document.title + " gMsgCompose=" + gMsgCompose + " msgIdentityMenu=" + document.getElementById("msgIdentity"))
233        vI.storage.init();
234        vI.statusmenu.init();
235        new vI.smartIdentity(window, gMsgCompose, vI.storage);
236        Log.debug("initSystemStage2 done.")
237      },
238
239      adapt_loadIdentity: function () {
240        if (main.original_functions.LoadIdentity) return true; // only initialize this once
241        Log.debug("adapt LoadIdentity");
242        main.original_functions.LoadIdentity = LoadIdentity;
243        LoadIdentity = main.replacement_functions.LoadIdentity;
244        return true;
245      },
246
247      adapt_genericSendMessage: function () {
248        if (main.original_functions.GenericSendMessage) return true; // only initialize this once
249        Log.debug("adapt GenericSendMessage");
250        main.original_functions.GenericSendMessage = GenericSendMessage;
251        GenericSendMessage = main.replacement_functions.GenericSendMessage;
252        return true;
253      },
254
255      //  code adapted from http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
256      AccountManagerObserver: {
257        _uninstall: false,
258        observe: function (subject, topic, data) {
259          if (topic == "am-acceptChanges") {
260            Log.debug("account changes observed");
261            Log.debug("cleaning original msgIdentityPopup");
262            var MenuItems = main.elements.Obj_MsgIdentityPopup.childNodes;
263            while (MenuItems.length > 0) {
264              try {
265                MenuItems[0].clean();
266              } catch (e) {};
267              main.elements.Obj_MsgIdentityPopup.removeChild(MenuItems[0])
268            }
269            let msgIdentity = document.getElementById("msgIdentity")
270            let tmp_identity = msgIdentity.identityData;
271            msgIdentity.clean();
272            msgIdentity.init();
273            Log.debug("cleaning original msgIdentityPopup done.");
274            tmp_identity.existingID = tmp_identity.isExistingIdentity(false)
275            if (tmp_identity.existingID) {
276              tmp_identity.id.key = tmp_identity.existingID
277            } else {
278              tmp_identity.id.key = MailServices.accounts.defaultAccount.defaultIdentity.key
279            }
280            Log.debug("adding previous identity to msgIdentityMenu");
281            msgIdentity.selectedMenuItem = msgIdentity.addIdentityToMsgIdentityMenu(tmp_identity);
282            Log.debug("adding previous identity to msgIdentityMenu done.");
283          }
284        },
285        register: function () {
286          var obsService = Components.classes["@mozilla.org/observer-service;1"].
287          getService(Components.interfaces.nsIObserverService)
288          obsService.addObserver(this, "am-acceptChanges", false);
289        },
290        unregister: function () {
291          var obsService = Components.classes["@mozilla.org/observer-service;1"].
292          getService(Components.interfaces.nsIObserverService)
293          obsService.removeObserver(this, "am-acceptChanges");
294        }
295      }
296    }
297
298    main.elements.init_base();
299    main.elements.init_rest();
300    main.adapt_loadIdentity();
301   
302    window.addEventListener('compose-window-init', main.init, true);
303
304    window.addEventListener("unload", function (e) {
305      main.AccountManagerObserver.unregister();
306      try {
307        vI.statusmenu.removeObserver();
308      } catch (ex) {}
309    }, false);
310    vI.main = main;
311  }
312});
Note: See TracBrowser for help on using the repository browser.