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

source: content/v_identity.js @ d0fa65

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

rewrite of the interface

  • Property mode set to 100644
File size: 16.5 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_replyToSelf.js", virtualIdentityExtension);
33    Components.utils.import("resource://v_identity/vI_accountUtils.js", virtualIdentityExtension);
34    Components.utils.import("resource://v_identity/plugins/signatureSwitch.js", virtualIdentityExtension);
35    Components.utils.import("resource://v_identity/vI_identityData.js", virtualIdentityExtension);
36    Components.utils.import("resource://v_identity/vI_smartIdentity.js", virtualIdentityExtension);
37    Components.utils.import("resource://v_identity/vI_log.js", virtualIdentityExtension);
38    Components.utils.import("resource:///modules/mailServices.js");
39
40    var main = {
41      timeStampID: null,
42      _smartIdentity: null,
43     
44      headerParser: Components.classes["@mozilla.org/messenger/headerparser;1"]
45        .getService(Components.interfaces.nsIMsgHeaderParser),
46
47      unicodeConverter: Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
48        .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
49
50      accountManager: Components.classes["@mozilla.org/messenger/account-manager;1"]
51        .getService(Components.interfaces.nsIMsgAccountManager),
52
53
54      // Those variables keep pointers to original functions which might get replaced later
55      original_functions: {
56        LoadIdentity: null
57      },
58
59      // some pointers to the layout-elements of the extension
60      elements: {
61        init_base: function () {
62          main.elements.Area_MsgIdentityHbox = document.getElementById("virtualIdentityExtension_msgIdentityHbox");
63          main.elements.Obj_MsgIdentity = document.getElementById("msgIdentity");
64        },
65        init_rest: function () {
66          main.elements.Obj_MsgIdentityPopup = document.getElementById("msgIdentityPopup");
67          main.elements.Obj_vILogo = document.getElementById("virtualIdentityExtension_Logo");
68        },
69        strings: null
70      },
71
72      ComposeStateListener: {
73        NotifyComposeBodyReady: function () {
74          Log.debug("NotifyComposeBodyReady");
75          main.initSystemStage2();
76        },
77        NotifyComposeFieldsReady: function () {
78          Log.debug("NotifyComposeFieldsReady");
79        },
80        ComposeProcessDone: function (aResult) {
81          Log.debug("StateListener reports ComposeProcessDone");
82          vI.vIaccount_removeUsedVIAccount();
83          vI.storage.clean();
84        },
85        SaveInFolderDone: function (folderURI) {
86          Log.debug("SaveInFolderDone");
87          vI.vIaccount_removeUsedVIAccount();
88          vI.storage.clean();
89        }
90      },
91
92      replacement_functions: {
93        LoadIdentity: function (startup) {
94          var identityElement = document.getElementById("msgIdentity");
95         
96          // identitykey and accountkey might not be set on new selectedItem, if it's a virtual identity
97          // on startup there might be no identitykey if a virtual identity is selected.
98          Log.debug("run adapted LoadIdentity startup=" + startup);
99         
100          let hasBaseId = identityElement.selectedItem.identityData.id.key;
101          if (hasBaseId == null) {
102            identityElement.selectedItem.setAttribute("identitykey", identityElement.getAttribute("identitykey"));
103            identityElement.selectedItem.setAttribute("accountkey", identityElement.getAttribute("accountkey"));
104          }
105          else {
106            identityElement.setAttribute("description", identityElement.selectedItem.getAttribute("description"));
107          }
108         
109          if (startup)
110            identityElement.identityData = identityElement.selectedItem.identityData.getDuplicate();
111          // else only values are copied into current identityData
112          else
113            identityElement.identityData.takeOverAvailableData(identityElement.selectedItem.identityData);
114         
115          gComposeNotificationBar.clearIdentityWarning();
116         
117          main.original_functions.LoadIdentity(startup);
118         
119          // store identitykey locally to enable restoring after selection of next virtual identity without identitykey
120          identityElement.setAttribute("identitykey", identityElement.selectedItem.getAttribute("identitykey"));
121         
122          // we are not using the smtp display, smtp is selected based on base identity
123          //dentityElement.setAttribute("smtp", identityElement.identityData.smtp.value);
124         
125          identityElement.vid = identityElement.selectedItem.vid;
126        },
127      },
128
129      remove: function () {
130        window.removeEventListener('compose-window-reopen', main.reopen, true);
131        window.removeEventListener('compose-window-close', main.close, true);
132        Log.debug("end. remove Account if there.")
133        vI.storage.clean();
134      },
135
136      _getRecipients: function () {
137        var recipients = [];
138        for (var row = 1; row <= top.MAX_RECIPIENTS; row++) {
139          if (typeof awGetPopupElement(row).selectedItem == 'undefined')
140            continue;
141          var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
142          if (recipientType == "addr_reply" || recipientType == "addr_followup" ||
143            main._recipientIsDoBcc(row) || awGetInputElement(row).value.match(/^\s*$/)) continue;
144          recipients.push({
145            recipient: awGetInputElement(row).value,
146            recipientType: recipientType
147          });
148        }
149        return recipients;
150      },
151
152      _recipientIsDoBcc: function (row) {
153        if (typeof awGetPopupElement(row).selectedItem == 'undefined')
154          return false;
155        var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
156        if (recipientType != "addr_bcc" || !getCurrentIdentity().doBcc) return false
157
158        var doBccArray = gMsgCompose.compFields.splitRecipients(getCurrentIdentity().doBccList, false, {});
159
160        for (var index = 0; index < doBccArray.count; index++) {
161          if (doBccArray.StringAt(index) == awGetInputElement(row).value) {
162            Log.debug("_recipientIsDoBcc: ignoring doBcc field '" +
163              doBccArray.StringAt(index));
164            return true;
165          }
166        }
167        return false
168      },
169
170      // initialization //
171      init: function () {
172        if (!this.timeStampID) {
173          this.timeStampID = parseInt((new Date()).getTime() / 100) % 864000; // give object unified id (per day)
174          Log = vI.setupLogging("virtualIdentity.main[" + this.timeStampID + "]");
175        }
176        window.removeEventListener('load', main.init, false);
177        window.removeEventListener('compose-window-init', main.init, true);
178        if (main.elements.Area_MsgIdentityHbox) return; // init done before, (?reopen)
179        Log.debug("init.")
180        main.unicodeConverter.charset = "UTF-8";
181       
182        // make msgIdentity editable
183//         var identityElement = document.getElementById("msgIdentity");
184//         identityElement.removeAttribute("type");
185//         identityElement.editable = true;
186//         identityElement.focus();
187//         identityElement.value = identityElement.selectedItem.value;
188//         identityElement.select();
189//         identityElement.inputField.placeholder = getComposeBundle().getFormattedString("msgIdentityPlaceholder", [identityElement.selectedItem.value]);
190//         
191//         main.adapt_interface();
192       
193//         main.adapt_loadIdentity();
194       
195       
196        let statusbarLabel = document.getElementById("v_identity_logo_statusbar");
197        statusbarLabel.setAttribute("value", statusbarLabel.getAttribute("value") + vI.extensionVersion);
198       
199        gMsgCompose.RegisterStateListener(main.ComposeStateListener);
200        document.getElementById("virtualIdentityExtension_tooltipPopupset")
201          .addTooltip(document.getElementById("msgIdentity"), false);
202        window.addEventListener('compose-window-reopen', main.reopen, true);
203        window.addEventListener('compose-window-close', main.close, true);
204
205        // append observer to virtualIdentityExtension_fccSwitch, because it does'n work with real identities (hidden by css)
206//         document.getElementById("virtualIdentityExtension_fccSwitch").appendChild(document.getElementById("virtualIdentityExtension_msgIdentityClone_observer").cloneNode(false));
207
208        main.AccountManagerObserver.register();
209
210        main.initSystemStage1();
211        Log.debug("init done.")
212      },
213
214      initSystemStage1: function () {
215        Log.debug("initSystemStage1.");
216//         document.getElementById("msgIdentity").init();
217       
218        Log.debug("initSystemStage1 done.")
219      },
220
221      initSystemStage2: function () {
222        Log.debug("initSystemStage2.");
223        Log.debug("document.title=" + document.title + " gMsgCompose=" + gMsgCompose + " msgIdentityClone=" + document.getElementById("msgIdentity"))
224//         vI.initReplyTo(window);
225        vI.storage.init();
226        vI.statusmenu.init();
227        new vI.smartIdentity(window, gMsgCompose, vI.storage);
228        Log.debug("initSystemStage2 done.")
229      },
230
231      close: function () {
232        vI.storage.clean();
233      },
234
235      adapt_interface: function () {
236        if (main.elements.Obj_MsgIdentityPopup) return; // only rearrange the interface once
237
238        // initialize the pointers to extension elements
239        main.elements.init_base()
240
241        // rearrange the positions of some elements
242        var parent_hbox = main.elements.Obj_MsgIdentity.parentNode;
243        var storage_box = document.getElementById("addresses-box");
244        var virtualIdentityExtension_autoReplyToSelfLabel = document.getElementById("virtualIdentityExtension_autoReplyToSelfLabelBox");
245
246        storage_box.removeChild(virtualIdentityExtension_autoReplyToSelfLabel);
247        parent_hbox.appendChild(virtualIdentityExtension_autoReplyToSelfLabel);
248        storage_box.removeChild(main.elements.Area_MsgIdentityHbox);
249        parent_hbox.appendChild(main.elements.Area_MsgIdentityHbox);
250
251        //main.elements.Obj_MsgIdentity.setAttribute("hidden", "true");
252        main.elements.Obj_MsgIdentity.previousSibling.setAttribute("control", "msgIdentity");
253
254        var access_label = parent_hbox.getElementsByAttribute("control", "msgIdentity")[0];
255        if (access_label) access_label.setAttribute("control", "msgIdentity");
256
257        // initialize the pointers to extension elements (initialize those earlier might brake the interface)
258        main.elements.init_rest();
259      },
260
261      adapt_loadIdentity: function () {
262        if (main.original_functions.LoadIdentity) return true; // only initialize this once
263        Log.debug("adapt LoadIdentity");
264        main.original_functions.LoadIdentity = LoadIdentity;
265        LoadIdentity = main.replacement_functions.LoadIdentity;
266        return true;
267      },
268
269      reopen: function () {
270        vI.clearDebugOutput();
271        Log.debug("composeDialog reopened. (msgType " + gMsgCompose.type + ")")
272        Log.debug("document.title=" + document.title + " gMsgCompose=" + gMsgCompose + " msgIdentityClone=" + document.getElementById("msgIdentity"))
273
274        // clean all elements
275        document.getElementById("msgIdentity").clean();
276        vI.storage.clean(); // just to be sure!
277        Log.debug("everything cleaned.")
278
279        // register StateListener
280        gMsgCompose.RegisterStateListener(main.ComposeStateListener);
281
282        // now (re)init the elements
283        main.initSystemStage1();
284
285        vI.vIprefs.dropLocalChanges();
286
287        // NotifyComposeBodyReady is only triggered in reply-cases
288        // so activate stage2 in reply-cases trough StateListener
289        // in other cases directly
290        var msgComposeType = Components.interfaces.nsIMsgCompType;
291        switch (gMsgCompose.type) {
292        case msgComposeType.New:
293        case msgComposeType.NewsPost:
294        case msgComposeType.MailToUrl:
295        case msgComposeType.Draft:
296        case msgComposeType.Template:
297        case msgComposeType.ForwardAsAttachment:
298        case msgComposeType.ForwardInline:
299          main.initSystemStage2();
300          //             case msgComposeType.Reply:
301          //             case msgComposeType.ReplyAll:
302          //             case msgComposeType.ReplyToGroup:
303          //             case msgComposeType.ReplyToSender:
304          //             case msgComposeType.ReplyToSenderAndGroup:
305          //             case msgComposeType.ReplyWithTemplate:
306          //             case msgComposeType.ReplyToList:
307          //                 main.initSystemStage2() triggered trough NotifyComposeBodyReady;
308        }
309        Log.debug("reopen done.")
310      },
311
312      //  code adapted from http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
313      AccountManagerObserver: {
314        _uninstall: false,
315        observe: function (subject, topic, data) {
316          if (topic == "am-smtpChanges") {
317            Log.debug("smtp changes observed");
318            var msgIdentity = document.getElementById("msgIdentity");
319            document.getAnonymousElementByAttribute(msgIdentity, "class", "smtpServerListHbox").refresh();
320          }
321          if (topic == "am-acceptChanges") {
322            Log.debug("account changes observed");
323            Log.debug("cleaning original msgIdentityPopup");
324            var MenuItems = main.elements.Obj_MsgIdentityPopup.childNodes;
325            while (MenuItems.length > 0) {
326              try {
327                MenuItems[0].clean();
328              } catch (e) {};
329              main.elements.Obj_MsgIdentityPopup.removeChild(MenuItems[0])
330            }
331            let msgIdentity = document.getElementById("msgIdentity")
332            let tmp_identity = msgIdentity.identityData;
333            msgIdentity.clean();
334            msgIdentity.init();
335            Log.debug("cleaning original msgIdentityPopup done.");
336            tmp_identity.existingID = tmp_identity.isExistingIdentity(false)
337            if (tmp_identity.existingID) {
338              tmp_identity.id.key = tmp_identity.existingID
339            } else {
340              tmp_identity.id.key = MailServices.accounts.defaultAccount.defaultIdentity.key
341            }
342            Log.debug("adding previous identity to msgIdentityClone");
343            msgIdentity.selectedMenuItem = msgIdentity.addIdentityToCloneMenu(tmp_identity);
344            Log.debug("adding previous identity to msgIdentityClone done.");
345          }
346        },
347        register: function () {
348          var obsService = Components.classes["@mozilla.org/observer-service;1"].
349          getService(Components.interfaces.nsIObserverService)
350          obsService.addObserver(this, "am-smtpChanges", false);
351          obsService.addObserver(this, "am-acceptChanges", false);
352        },
353        unregister: function () {
354          var obsService = Components.classes["@mozilla.org/observer-service;1"].
355          getService(Components.interfaces.nsIObserverService)
356          obsService.removeObserver(this, "am-smtpChanges");
357          obsService.removeObserver(this, "am-acceptChanges");
358        }
359      }
360    }
361
362    main.elements.init_base();
363    main.elements.init_rest();
364    main.adapt_loadIdentity();
365   
366    window.addEventListener('compose-window-init', main.init, true);
367
368    window.addEventListener("unload", function (e) {
369      main.AccountManagerObserver.unregister();
370      try {
371        vI.statusmenu.removeObserver();
372      } catch (ex) {}
373    }, false);
374    vI.main = main;
375  }
376});
Note: See TracBrowser for help on using the repository browser.