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 @ a2f8236

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

ensure that new identity is changed before GenericSendMessage? is called

  • Property mode set to 100644
File size: 15.4 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 = {
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.elements.Area_MsgIdentityHbox = document.getElementById("msgIdentityHbox");
49            vI.elements.Obj_MsgIdentity = document.getElementById("msgIdentity");
50        },
51        init_rest : function() {
52            vI.elements.Obj_MsgIdentityPopup = document.getElementById("msgIdentityPopup");
53            vI.elements.Obj_vILogo = document.getElementById("v_identity_logo");
54            vI.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.initSystemStage2();
63        },
64        NotifyComposeFieldsReady: function() { 
65            vI_notificationBar.dump("## v_identity: NotifyComposeFieldsReady\n");
66            if (vI_helper.olderVersion("TB", "2.0a")) vI.initSystemStage2();
67        },
68        ComposeProcessDone: function(aResult) {
69            vI_notificationBar.dump("## v_identity: StateListener reports ComposeProcessDone\n");
70            vI.Cleanup(); // not really required, parallel handled by vI.close
71            vI_storage.clean();
72        },
73        SaveInFolderDone: function(folderURI) { 
74            vI_notificationBar.dump("## v_identity: SaveInFolderDone\n");
75            vI.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.replacement_functions.GenericSendMessageInProgress) return;
134            vI.replacement_functions.GenericSendMessageInProgress = true;
135           
136            var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
137                .getService(Components.interfaces.nsIPromptService);
138            vI_notificationBar.dump("\n## v_identity: VIdentity_GenericSendMessage\n");
139           
140            var vid = document.getElementById("msgIdentity_clone").vid
141
142            if (msgType != nsIMsgCompDeliverMode.Now) {
143                // dont allow user to fake identity if Message is not sended NOW and thunderbird-version is below 2.0 !!!!
144                if (vid && (vI_helper.olderVersion("TB", "2.0b") || vI_helper.olderVersion("SM", "1.5a"))) {
145                    var server = gAccountManager.defaultAccount.incomingServer.prettyName
146                    var name = gAccountManager.defaultAccount.defaultIdentity.fullName
147                    var email = gAccountManager.defaultAccount.defaultIdentity.email
148                    var query = vI.elements.strings.getString("vident.sendLater.warning") +
149                        vI.elements.strings.getString("vident.sendLater.prefix") +
150                        name + " " + email + " [" + server + "]" + 
151                        vI.elements.strings.getString("vident.sendLater.postfix")
152                   
153                    if (!promptService.confirm(window,"Error",query)) {
154                        vI.replacement_functions.GenericSendMessageInProgress = false;
155                        return;
156                    }
157                    else { document.getElementById("msgIdentity_clone").selectedMenuItem = "default"; vid = false; }
158                }
159            }
160            else {
161                if ( (vid && vI.preferences.getBoolPref("warn_virtual") &&
162                    !(promptService.confirm(window,"Warning",
163                        vI.elements.strings.getString("vident.sendVirtual.warning")))) ||
164                  (!vid && vI.preferences.getBoolPref("warn_nonvirtual") &&
165                    !(promptService.confirm(window,"Warning",
166                        vI.elements.strings.getString("vident.sendNonvirtual.warning")))) ) {
167                    vI.replacement_functions.GenericSendMessageInProgress = false;
168                    return;
169                }
170                if (!vI_storage.storeVIdentityToAllRecipients(msgType)) {
171//                  vI_notificationBar.dump("## v_identity: sending aborted\n");
172                    vI.replacement_functions.GenericSendMessageInProgress = false;
173                    return;
174                }
175                vI_msgIdentityCloneTools.addReplyToSelf();
176            }
177            if (vid) vI.prepareAccount();
178            vI.replacement_functions.GenericSendMessageInProgress = false;
179//          vI_notificationBar.dump("## v_identity: original_functions.GenericSendMessage\n");
180
181            // final check if eyerything is nice before we handover to the real sending...
182            var virtualIdentityData = document.getElementById("msgIdentity_clone").identityData;
183            var currentIdentity = getCurrentIdentity();
184            if (    currentIdentity.fullName == virtualIdentityData.fullName    &&
185                currentIdentity.email == virtualIdentityData.email      &&
186                currentIdentity.smtpServerKey == virtualIdentityData.smtp.keyNice   ) {
187                    vI.original_functions.GenericSendMessage(msgType);
188            }
189            else {
190                alert(vI.elements.strings.getString("vident.genericSendMessage.error"));
191            }
192//          vI_notificationBar.dump("## v_identity: original_functions.GenericSendMessage done\n");
193        },
194       
195        replace_FillIdentityList : function() {
196            if (typeof(FillIdentityList)=="function") {
197                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityList (TB 3.x)\n");
198                vI.original_functions.FillIdentityList = FillIdentityList;
199                FillIdentityList = vI.replacement_functions.FillIdentityList;
200            }
201            else {
202                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityListPopup (TB 2.x)\n");
203                vI.original_functions.FillIdentityListPopup = FillIdentityListPopup;
204                FillIdentityListPopup = vI.replacement_functions.FillIdentityListPopup;
205            }
206        }
207    },
208
209    remove: function() {
210        window.removeEventListener('compose-window-reopen', vI.reopen, true);
211        window.removeEventListener('compose-window-close', vI.close, true);
212        vI_notificationBar.dump("## v_identity: end. remove Account if there.\n")
213        vI.Cleanup();
214        vI_storage.clean();
215    },
216
217    // initialization //
218    init: function() {
219        window.removeEventListener('load', vI.init, false);
220        window.removeEventListener('compose-window-init', vI.init, true);
221        if (vI.elements.Area_MsgIdentityHbox) return; // init done before, (?reopen)
222        vI_notificationBar.dump("\n## v_identity: init.\n")
223        vI.unicodeConverter.charset="UTF-8";
224        if (!vI.adapt_genericSendMessage()) { vI_notificationBar.dump("\n## v_identity: init failed.\n"); return; }
225        vI.adapt_interface();
226        gMsgCompose.RegisterStateListener(vI.ComposeStateListener);
227        document.getElementById("vI_tooltipPopupset")
228            .addTooltip(document.getElementById("msgIdentity_clone"), false);
229        window.addEventListener('compose-window-reopen', vI.reopen, true);
230        window.addEventListener('compose-window-close', vI.close, true);
231       
232        // append observer to fcc_switch, because it does'n work with real identities (hidden by css)
233        document.getElementById("fcc_switch").appendChild(document.getElementById("msgIdentity_clone_observer").cloneNode(false));
234
235        vI.initSystemStage1();
236        vI_notificationBar.dump("## v_identity: init done.\n\n")
237    },
238   
239    initSystemStage1 : function() {
240        vI_notificationBar.dump("## v_identity: initSystemStage1.\n")
241        vI.gMsgCompose = gMsgCompose;
242        document.getElementById("msgIdentity_clone").init();
243        vI_statusmenu.init();
244    },
245   
246    initSystemStage2 : function() {
247        vI_notificationBar.dump("## v_identity: initSystemStage2.\n")
248        vI_msgIdentityCloneTools.initReplyTo();
249        vI_storage.init();
250        vI_smartIdentity.init();
251    },
252   
253    close : function() {
254        vI.Cleanup();
255        vI_storage.clean();
256    },
257   
258    adapt_interface : function() {
259        if (vI.elements.strings) return; // only rearrange the interface once
260       
261        // initialize the pointers to extension elements
262        vI.elements.init_base()
263       
264        // rearrange the positions of some elements
265        var parent_hbox = vI.elements.Obj_MsgIdentity.parentNode;
266        var storage_box = document.getElementById("addresses-box");
267        var autoReplyToSelfLabel = document.getElementById("autoReplyToSelfLabel");
268       
269        storage_box.removeChild(autoReplyToSelfLabel);
270        parent_hbox.appendChild(autoReplyToSelfLabel);
271        storage_box.removeChild(vI.elements.Area_MsgIdentityHbox);
272        parent_hbox.appendChild(vI.elements.Area_MsgIdentityHbox);
273
274        vI.elements.Obj_MsgIdentity.setAttribute("hidden", "true");
275        vI.elements.Obj_MsgIdentity.previousSibling.setAttribute("control", "msgIdentity_clone");
276       
277        // initialize the pointers to extension elements (initialize those earlier might brake the interface)
278        vI.elements.init_rest();   
279    },
280   
281    adapt_genericSendMessage : function() {
282        if (vI.original_functions.GenericSendMessage) return true; // only initialize this once
283        vI_notificationBar.dump("## v_identity: adapt GenericSendMessage\n");
284        vI.original_functions.GenericSendMessage = GenericSendMessage;
285        GenericSendMessage = vI.replacement_functions.GenericSendMessage;
286        return true;
287    },
288   
289    reopen: function() {
290        vI_notificationBar.clear();
291        vI_notificationBar.clear_dump();
292        vI_notificationBar.dump("## v_identity: composeDialog reopened. (msgType " + gMsgCompose.type + ")\n")
293       
294        // clean all elements
295        document.getElementById("msgIdentity_clone").clean();
296        vI_smartIdentity.clean();
297        vI_notificationBar.dump("## v_identity: everything cleaned.\n")
298       
299        // now (re)init the elements
300        vI.initSystemStage1();
301       
302        // stateListener only works in reply-cases
303        // so activate stage2 in reply-cases trough StateListener
304        // in other cases directly
305        var msgComposeType = Components.interfaces.nsIMsgCompType;
306        switch (gMsgCompose.type) {
307            case msgComposeType.New:
308            case msgComposeType.NewsPost:
309            case msgComposeType.MailToUrl:
310            case msgComposeType.Draft:
311            case msgComposeType.Template:
312            case msgComposeType.ForwardAsAttachment:
313            case msgComposeType.ForwardInline:
314                vI.initSystemStage2(); break;
315            case msgComposeType.Reply:
316            case msgComposeType.ReplyAll:
317            case msgComposeType.ReplyToGroup:
318            case msgComposeType.ReplyToSender:
319            case msgComposeType.ReplyToSenderAndGroup:
320            case msgComposeType.ReplyWithTemplate:
321                gMsgCompose.RegisterStateListener(vI.ComposeStateListener);
322        }
323        vI_notificationBar.dump("## v_identity: reopen done.\n")
324    },
325   
326    tempStorage: { BaseIdentity : null, NewIdentity : null },
327
328    __setSelectedIdentity : function(menuItem) {
329        vI.elements.Obj_MsgIdentity.selectedItem = menuItem;
330        vI.elements.Obj_MsgIdentity.setAttribute("label", menuItem.getAttribute("label"));
331        vI.elements.Obj_MsgIdentity.setAttribute("accountname", menuItem.getAttribute("accountname"));
332        vI.elements.Obj_MsgIdentity.setAttribute("value", menuItem.getAttribute("value"));
333    },
334
335    // sets the values of the dropdown-menu to the ones of the newly created account
336    addVirtualIdentityToMsgIdentityMenu : function()
337    {
338        vI.tempStorage.BaseIdentity = vI.elements.Obj_MsgIdentity.selectedItem;
339        vI.tempStorage.NewIdentity = document.createElement("menuitem");
340        vI.tempStorage.NewIdentity.className = "identity-popup-item";
341       
342        // set the account name in the choosen menu item
343        vI.tempStorage.NewIdentity.setAttribute("label", vI_account.account.defaultIdentity.identityName);
344        vI.tempStorage.NewIdentity.setAttribute("accountname", " - " +  vI_account.account.incomingServer.prettyName);
345        vI.tempStorage.NewIdentity.setAttribute("accountkey", vI_account.account.key);
346        vI.tempStorage.NewIdentity.setAttribute("value", vI_account.account.defaultIdentity.key);
347       
348        vI.elements.Obj_MsgIdentityPopup.appendChild(vI.tempStorage.NewIdentity);
349        vI.__setSelectedIdentity(vI.tempStorage.NewIdentity);
350    },
351   
352    removeVirtualIdentityFromMsgIdentityMenu : function()
353    {
354        if (!vI.tempStorage.BaseIdentity) return; // don't try to remove Item twice
355        try {   // might not exist anymore (window closed), so just try to remove it
356            document.getElementById("msgIdentity").firstChild.removeChild(vI.tempStorage.NewIdentity);
357            vI.__setSelectedIdentity(vI.tempStorage.BaseIdentity);
358        } catch (e) { };
359        vI.tempStorage.NewIdentity = null;
360        vI.tempStorage.BaseIdentity = null;
361    },
362
363    prepareAccount : function() {
364        vI.Cleanup(); // just to be sure that nothing is left (maybe last time sending was irregularily stopped)
365        vI_account.createAccount();
366        vI.addVirtualIdentityToMsgIdentityMenu();
367    },
368
369    Cleanup : function() {
370        vI.removeVirtualIdentityFromMsgIdentityMenu();
371        vI_account.removeUsedVIAccount();
372    }
373}
374
375
376vI.replacement_functions.replace_FillIdentityList();
377window.addEventListener('load', vI.init, false);        // TB 1.5x, SM
378window.addEventListener('compose-window-init', vI.init, true);  // TB 2.x 3.x
379
380window.addEventListener("unload", function(e) { try {vI_statusmenu.removeObserver();} catch (ex) { } }, false);
381
Note: See TracBrowser for help on using the repository browser.