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

source: chrome/content/v_identity/v_identity.js @ 188fde

multiEditng_0.6ng_0.8ng_0.9
Last change on this file since 188fde was 188fde, checked in by rene <rene@…>, 13 years ago

ignore case of names/emails in final check

  • Property mode set to 100644
File size: 17.1 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
25var vI_main = {
26    preferences : Components.classes["@mozilla.org/preferences-service;1"]
27            .getService(Components.interfaces.nsIPrefService)
28            .getBranch("extensions.virtualIdentity."),
29   
30    headerParser : Components.classes["@mozilla.org/messenger/headerparser;1"]
31                .getService(Components.interfaces.nsIMsgHeaderParser),
32   
33    unicodeConverter : Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
34                .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
35
36    gMsgCompose : null, // to store the global gMsgCompose after MsgComposeDialog is closed
37
38    // Those variables keep pointers to original functions which might get replaced later
39    original_functions : {
40        GenericSendMessage : null,
41        FillIdentityListPopup : null,   // TB 2.x
42        FillIdentityList : null     // TB 3.x
43    },
44
45    // some pointers to the layout-elements of the extension
46    elements : {
47        init_base : function() {
48            vI_main.elements.Area_MsgIdentityHbox = document.getElementById("msgIdentityHbox");
49            vI_main.elements.Obj_MsgIdentity = document.getElementById("msgIdentity");
50        },
51        init_rest : function() {
52            vI_main.elements.Obj_MsgIdentityPopup = document.getElementById("msgIdentityPopup");
53            vI_main.elements.Obj_vILogo = document.getElementById("v_identity_logo");
54            vI_main.elements.strings = document.getElementById("vIdentBundle");
55        },
56        strings : null
57    },
58
59    ComposeStateListener : {
60        NotifyComposeBodyReady: function() { 
61            vI_notificationBar.dump("## v_identity: NotifyComposeBodyReady\n");
62            if (!vI_helper.olderVersion("TB", "2.0a")) vI_main.initSystemStage2();
63        },
64        NotifyComposeFieldsReady: function() { 
65            vI_notificationBar.dump("## v_identity: NotifyComposeFieldsReady\n");
66            if (vI_helper.olderVersion("TB", "2.0a")) vI_main.initSystemStage2();
67        },
68        ComposeProcessDone: function(aResult) {
69            vI_notificationBar.dump("## v_identity: StateListener reports ComposeProcessDone\n");
70            vI_main.Cleanup(); // not really required, parallel handled by vI_main.close
71            vI_storage.clean();
72        },
73        SaveInFolderDone: function(folderURI) { 
74            vI_notificationBar.dump("## v_identity: SaveInFolderDone\n");
75            vI_main.Cleanup();
76            vI_storage.clean();
77        }
78    },
79       
80    replacement_functions : {
81        // TB 2.x
82        FillIdentityListPopup: function(popup) {
83            vI_notificationBar.dump("## v_identity: mod. FillIdentityListPopup\n");
84            var accounts = queryISupportsArray(gAccountManager.accounts, Components.interfaces.nsIMsgAccount);
85            accounts.sort(compareAccountSortOrder);
86
87            for (var i in accounts) {
88                var server = accounts[i].incomingServer;
89                if (!server) continue;
90                // check for VirtualIdentity Account
91                try {   vI_account.prefroot.getBoolPref("mail.account." + accounts[i].key + ".vIdentity");
92                    continue; } catch (e) { };
93
94                var identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity);
95                for (var j in identities) {
96                    var identity = identities[j];
97                    var item = document.createElement("menuitem");
98                    item.className = "identity-popup-item";
99                    item.setAttribute("label", identity.identityName);
100                    item.setAttribute("value", identity.key);
101                    item.setAttribute("accountkey", accounts[i].key);
102                    item.setAttribute("accountname", " - " + server.prettyName);
103                    popup.appendChild(item);
104                }
105            }
106        },
107       
108        // TB 3.x
109        FillIdentityList: function(menulist) {
110            vI_notificationBar.dump("## v_identity: mod. FillIdentityList\n");
111            var accounts = queryISupportsArray(gAccountManager.accounts, Components.interfaces.nsIMsgAccount);
112            if (typeof(sortAccounts)=="function") // TB 3.x
113                accounts.sort(sortAccounts);
114
115            for (var i in accounts) {
116                var server = accounts[i].incomingServer;
117                if (!server) continue;
118                // check for VirtualIdentity Account
119                try {   vI_account.prefroot.getBoolPref("mail.account." + accounts[i].key + ".vIdentity");
120                    continue; } catch (e) { };
121
122                var identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity);
123                for (var j in identities) {
124                    var identity = identities[j];
125                    var item = menulist.appendItem(identity.identityName, identity.key, server.prettyName);
126                    item.setAttribute("accountkey", accounts[i].key);
127                }
128            }
129        },
130       
131        GenericSendMessageInProgress : false,
132        GenericSendMessage: function (msgType) {
133            if (vI_main.replacement_functions.GenericSendMessageInProgress) return;
134            vI_main.replacement_functions.GenericSendMessageInProgress = true;
135           
136            // if addressCol2 is focused while sending check storage for the entered address before continuing
137            vI_storage.awOnBlur(vI_storage.focusedElement);
138
139            var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
140                .getService(Components.interfaces.nsIPromptService);
141            vI_notificationBar.dump("\n## v_identity: VIdentity_GenericSendMessage\n");
142           
143            var vid = document.getElementById("msgIdentity_clone").vid
144
145            if (msgType != nsIMsgCompDeliverMode.Now) {
146                // dont allow user to fake identity if Message is not sended NOW and thunderbird-version is below 2.0 !!!!
147                if (vid && (vI_helper.olderVersion("TB", "2.0b") || vI_helper.olderVersion("SM", "1.5a"))) {
148                    var server = gAccountManager.defaultAccount.incomingServer.prettyName
149                    var name = gAccountManager.defaultAccount.defaultIdentity.fullName
150                    var email = gAccountManager.defaultAccount.defaultIdentity.email
151                    var query = vI_main.elements.strings.getString("vident.sendLater.warning") +
152                        vI_main.elements.strings.getString("vident.sendLater.prefix") +
153                        name + " " + email + " [" + server + "]" + 
154                        vI_main.elements.strings.getString("vident.sendLater.postfix")
155                   
156                    if (!promptService.confirm(window,"Error",query)) {
157                        vI_main.replacement_functions.GenericSendMessageInProgress = false;
158                        return;
159                    }
160                    else { document.getElementById("msgIdentity_clone").selectedMenuItem = "default"; vid = false; }
161                }
162            }
163            else {
164                if ( (vid && vI_main.preferences.getBoolPref("warn_virtual") &&
165                    !(promptService.confirm(window,"Warning",
166                        vI_main.elements.strings.getString("vident.sendVirtual.warning")))) ||
167                  (!vid && vI_main.preferences.getBoolPref("warn_nonvirtual") &&
168                    !(promptService.confirm(window,"Warning",
169                        vI_main.elements.strings.getString("vident.sendNonvirtual.warning")))) ) {
170                    vI_main.replacement_functions.GenericSendMessageInProgress = false;
171                    return;
172                }
173                if (!vI_storage.storeVIdentityToAllRecipients(msgType)) {
174//                  vI_notificationBar.dump("## v_identity: sending aborted\n");
175                    vI_main.replacement_functions.GenericSendMessageInProgress = false;
176                    return;
177                }
178                vI_msgIdentityCloneTools.addReplyToSelf();
179            }
180            if (vid) vI_main.prepareAccount();
181            vI_main.replacement_functions.GenericSendMessageInProgress = false;
182//          vI_notificationBar.dump("## v_identity: original_functions.GenericSendMessage\n");
183
184            // final check if eyerything is nice before we handover to the real sending...
185            var virtualIdentityData = document.getElementById("msgIdentity_clone").identityData;
186            var currentIdentity = getCurrentIdentity();
187            var currentSMTPobj = new vI_smtpObj(currentIdentity.smtpServerKey);
188           
189            vI_notificationBar.dump("\n## vI_identityData GenericSendMessage Final Check\n");
190            vI_notificationBar.dump("## vI_identityData currentIdentity: fullName='" + currentIdentity.fullName + "' email='" + currentIdentity.email + "' smtp='" + currentSMTPobj.key + "'\n");
191            vI_notificationBar.dump("## vI_identityData virtualIdentityData: fullName='" + virtualIdentityData.fullName + "' email='" + virtualIdentityData.email + "' smtp='" + virtualIdentityData.smtp.key + "'\n");
192
193            if  (currentIdentity.fullName.toLowerCase() == virtualIdentityData.fullName.toLowerCase()   &&
194                currentIdentity.email.toLowerCase() == virtualIdentityData.email.toLowerCase()      &&
195                virtualIdentityData.smtp.equal(currentSMTPobj)  ) {
196                    vI_main.original_functions.GenericSendMessage(msgType);
197            }
198            else {
199                if (!(currentIdentity.fullName.toLowerCase() == virtualIdentityData.fullName.toLowerCase())) vI_notificationBar.dump("\n## vI_identityData failed check for fullName.\n");
200                if (!(currentIdentity.email.toLowerCase() == virtualIdentityData.email.toLowerCase())) vI_notificationBar.dump("\n## vI_identityData failed check for email.\n");
201                if (!(virtualIdentityData.smtp.equal(currentSMTPobj))) vI_notificationBar.dump("\n## vI_identityData failed check for SMTP.\n");
202                alert(vI_main.elements.strings.getString("vident.genericSendMessage.error"));
203                vI_main.Cleanup();
204            }
205//          vI_notificationBar.dump("## v_identity: original_functions.GenericSendMessage done\n");
206        },
207       
208        replace_FillIdentityList : function() {
209            if (typeof(FillIdentityList)=="function") {
210                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityList (TB 3.x)\n");
211                vI_main.original_functions.FillIdentityList = FillIdentityList;
212                FillIdentityList = vI_main.replacement_functions.FillIdentityList;
213            }
214            else {
215                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityListPopup (TB 2.x)\n");
216                vI_main.original_functions.FillIdentityListPopup = FillIdentityListPopup;
217                FillIdentityListPopup = vI_main.replacement_functions.FillIdentityListPopup;
218            }
219        }
220    },
221
222    remove: function() {
223        window.removeEventListener('compose-window-reopen', vI_main.reopen, true);
224        window.removeEventListener('compose-window-close', vI_main.close, true);
225        vI_notificationBar.dump("## v_identity: end. remove Account if there.\n")
226        vI_main.Cleanup();
227        vI_storage.clean();
228    },
229
230    // initialization //
231    init: function() {
232        window.removeEventListener('load', vI_main.init, false);
233        window.removeEventListener('compose-window-init', vI_main.init, true);
234        if (vI_main.elements.Area_MsgIdentityHbox) return; // init done before, (?reopen)
235        vI_notificationBar.dump("\n## v_identity: init.\n")
236        vI_main.unicodeConverter.charset="UTF-8";
237        if (!vI_main.adapt_genericSendMessage()) { vI_notificationBar.dump("\n## v_identity: init failed.\n"); return; }
238        vI_main.adapt_interface();
239        gMsgCompose.RegisterStateListener(vI_main.ComposeStateListener);
240        document.getElementById("vI_tooltipPopupset")
241            .addTooltip(document.getElementById("msgIdentity_clone"), false);
242        window.addEventListener('compose-window-reopen', vI_main.reopen, true);
243        window.addEventListener('compose-window-close', vI_main.close, true);
244       
245        // append observer to fcc_switch, because it does'n work with real identities (hidden by css)
246        document.getElementById("fcc_switch").appendChild(document.getElementById("msgIdentity_clone_observer").cloneNode(false));
247
248        vI_main.initSystemStage1();
249        vI_notificationBar.dump("## v_identity: init done.\n\n")
250    },
251   
252    initSystemStage1 : function() {
253        vI_notificationBar.dump("## v_identity: initSystemStage1.\n")
254        vI_main.gMsgCompose = gMsgCompose;
255        document.getElementById("msgIdentity_clone").init();
256        vI_statusmenu.init();
257    },
258   
259    initSystemStage2 : function() {
260        vI_notificationBar.dump("## v_identity: initSystemStage2.\n")
261        vI_msgIdentityCloneTools.initReplyTo();
262        vI_storage.init();
263        vI_smartIdentity.init();
264    },
265   
266    close : function() {
267        vI_main.Cleanup();
268        vI_storage.clean();
269    },
270   
271    adapt_interface : function() {
272        if (vI_main.elements.strings) return; // only rearrange the interface once
273       
274        // initialize the pointers to extension elements
275        vI_main.elements.init_base()
276       
277        // rearrange the positions of some elements
278        var parent_hbox = vI_main.elements.Obj_MsgIdentity.parentNode;
279        var storage_box = document.getElementById("addresses-box");
280        var autoReplyToSelfLabel = document.getElementById("autoReplyToSelfLabel");
281       
282        storage_box.removeChild(autoReplyToSelfLabel);
283        parent_hbox.appendChild(autoReplyToSelfLabel);
284        storage_box.removeChild(vI_main.elements.Area_MsgIdentityHbox);
285        parent_hbox.appendChild(vI_main.elements.Area_MsgIdentityHbox);
286
287        vI_main.elements.Obj_MsgIdentity.setAttribute("hidden", "true");
288        vI_main.elements.Obj_MsgIdentity.previousSibling.setAttribute("control", "msgIdentity_clone");
289       
290        // initialize the pointers to extension elements (initialize those earlier might brake the interface)
291        vI_main.elements.init_rest();   
292    },
293   
294    adapt_genericSendMessage : function() {
295        if (vI_main.original_functions.GenericSendMessage) return true; // only initialize this once
296        vI_notificationBar.dump("## v_identity: adapt GenericSendMessage\n");
297        vI_main.original_functions.GenericSendMessage = GenericSendMessage;
298        GenericSendMessage = vI_main.replacement_functions.GenericSendMessage;
299        return true;
300    },
301   
302    reopen: function() {
303        vI_notificationBar.clear();
304        vI_notificationBar.clear_dump();
305        vI_notificationBar.dump("## v_identity: composeDialog reopened. (msgType " + gMsgCompose.type + ")\n")
306       
307        // clean all elements
308        document.getElementById("msgIdentity_clone").clean();
309        vI_smartIdentity.clean();
310        vI_notificationBar.dump("## v_identity: everything cleaned.\n")
311       
312        // now (re)init the elements
313        vI_main.initSystemStage1();
314       
315        // stateListener only works in reply-cases
316        // so activate stage2 in reply-cases trough StateListener
317        // in other cases directly
318        var msgComposeType = Components.interfaces.nsIMsgCompType;
319        switch (gMsgCompose.type) {
320            case msgComposeType.New:
321            case msgComposeType.NewsPost:
322            case msgComposeType.MailToUrl:
323            case msgComposeType.Draft:
324            case msgComposeType.Template:
325            case msgComposeType.ForwardAsAttachment:
326            case msgComposeType.ForwardInline:
327                vI_main.initSystemStage2(); break;
328            case msgComposeType.Reply:
329            case msgComposeType.ReplyAll:
330            case msgComposeType.ReplyToGroup:
331            case msgComposeType.ReplyToSender:
332            case msgComposeType.ReplyToSenderAndGroup:
333            case msgComposeType.ReplyWithTemplate:
334            case msgComposeType.ReplyToList:
335                gMsgCompose.RegisterStateListener(vI_main.ComposeStateListener);
336        }
337        vI_notificationBar.dump("## v_identity: reopen done.\n")
338    },
339   
340    tempStorage: { BaseIdentity : null, NewIdentity : null },
341
342    __setSelectedIdentity : function(menuItem) {
343        vI_main.elements.Obj_MsgIdentity.selectedItem = menuItem;
344        vI_main.elements.Obj_MsgIdentity.setAttribute("label", menuItem.getAttribute("label"));
345        vI_main.elements.Obj_MsgIdentity.setAttribute("accountname", menuItem.getAttribute("accountname"));
346        vI_main.elements.Obj_MsgIdentity.setAttribute("value", menuItem.getAttribute("value"));
347    },
348
349    // sets the values of the dropdown-menu to the ones of the newly created account
350    addVirtualIdentityToMsgIdentityMenu : function()
351    {
352        vI_main.tempStorage.BaseIdentity = vI_main.elements.Obj_MsgIdentity.selectedItem;
353        vI_main.tempStorage.NewIdentity = document.createElement("menuitem");
354        vI_main.tempStorage.NewIdentity.className = "identity-popup-item";
355       
356        // set the account name in the choosen menu item
357        vI_main.tempStorage.NewIdentity.setAttribute("label", vI_account.account.defaultIdentity.identityName);
358        vI_main.tempStorage.NewIdentity.setAttribute("accountname", " - " +  vI_account.account.incomingServer.prettyName);
359        vI_main.tempStorage.NewIdentity.setAttribute("accountkey", vI_account.account.key);
360        vI_main.tempStorage.NewIdentity.setAttribute("value", vI_account.account.defaultIdentity.key);
361       
362        vI_main.elements.Obj_MsgIdentityPopup.appendChild(vI_main.tempStorage.NewIdentity);
363        vI_main.__setSelectedIdentity(vI_main.tempStorage.NewIdentity);
364    },
365   
366    removeVirtualIdentityFromMsgIdentityMenu : function()
367    {
368        if (!vI_main.tempStorage.BaseIdentity) return; // don't try to remove Item twice
369        try {   // might not exist anymore (window closed), so just try to remove it
370            document.getElementById("msgIdentity").firstChild.removeChild(vI_main.tempStorage.NewIdentity);
371            vI_main.__setSelectedIdentity(vI_main.tempStorage.BaseIdentity);
372        } catch (e) { };
373        vI_main.tempStorage.NewIdentity = null;
374        vI_main.tempStorage.BaseIdentity = null;
375    },
376
377    prepareAccount : function() {
378        vI_main.Cleanup(); // just to be sure that nothing is left (maybe last time sending was irregularily stopped)
379        vI_account.createAccount();
380        vI_main.addVirtualIdentityToMsgIdentityMenu();
381    },
382
383    Cleanup : function() {
384        vI_main.removeVirtualIdentityFromMsgIdentityMenu();
385        vI_account.removeUsedVIAccount();
386    }
387}
388
389
390vI_main.replacement_functions.replace_FillIdentityList();
391window.addEventListener('load', vI_main.init, false);       // TB 1.5x, SM
392window.addEventListener('compose-window-init', vI_main.init, true); // TB 2.x 3.x
393
394window.addEventListener("unload", function(e) { try {vI_statusmenu.removeObserver();} catch (ex) { } }, false);
395
Note: See TracBrowser for help on using the repository browser.