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 @ 2bd853

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

don't do final check if it's no virtual identity / really compare SMTP

  • Property mode set to 100644
File size: 15.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
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            var currentSMTPobj = new smtpObj(currentIdentity.smtpServerKey);
185            if (    !vid ||
186                (currentIdentity.fullName == virtualIdentityData.fullName   &&
187                currentIdentity.email == virtualIdentityData.email      &&
188                virtualIdentityData.smtp.equal(currentSMTPobj)  )) {
189                    vI.original_functions.GenericSendMessage(msgType);
190            }
191            else {
192                alert(vI.elements.strings.getString("vident.genericSendMessage.error"));
193                vI.Cleanup();
194            }
195//          vI_notificationBar.dump("## v_identity: original_functions.GenericSendMessage done\n");
196        },
197       
198        replace_FillIdentityList : function() {
199            if (typeof(FillIdentityList)=="function") {
200                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityList (TB 3.x)\n");
201                vI.original_functions.FillIdentityList = FillIdentityList;
202                FillIdentityList = vI.replacement_functions.FillIdentityList;
203            }
204            else {
205                //~ vI_notificationBar.dump("## v_identity: replace FillIdentityListPopup (TB 2.x)\n");
206                vI.original_functions.FillIdentityListPopup = FillIdentityListPopup;
207                FillIdentityListPopup = vI.replacement_functions.FillIdentityListPopup;
208            }
209        }
210    },
211
212    remove: function() {
213        window.removeEventListener('compose-window-reopen', vI.reopen, true);
214        window.removeEventListener('compose-window-close', vI.close, true);
215        vI_notificationBar.dump("## v_identity: end. remove Account if there.\n")
216        vI.Cleanup();
217        vI_storage.clean();
218    },
219
220    // initialization //
221    init: function() {
222        window.removeEventListener('load', vI.init, false);
223        window.removeEventListener('compose-window-init', vI.init, true);
224        if (vI.elements.Area_MsgIdentityHbox) return; // init done before, (?reopen)
225        vI_notificationBar.dump("\n## v_identity: init.\n")
226        vI.unicodeConverter.charset="UTF-8";
227        if (!vI.adapt_genericSendMessage()) { vI_notificationBar.dump("\n## v_identity: init failed.\n"); return; }
228        vI.adapt_interface();
229        gMsgCompose.RegisterStateListener(vI.ComposeStateListener);
230        document.getElementById("vI_tooltipPopupset")
231            .addTooltip(document.getElementById("msgIdentity_clone"), false);
232        window.addEventListener('compose-window-reopen', vI.reopen, true);
233        window.addEventListener('compose-window-close', vI.close, true);
234       
235        // append observer to fcc_switch, because it does'n work with real identities (hidden by css)
236        document.getElementById("fcc_switch").appendChild(document.getElementById("msgIdentity_clone_observer").cloneNode(false));
237
238        vI.initSystemStage1();
239        vI_notificationBar.dump("## v_identity: init done.\n\n")
240    },
241   
242    initSystemStage1 : function() {
243        vI_notificationBar.dump("## v_identity: initSystemStage1.\n")
244        vI.gMsgCompose = gMsgCompose;
245        document.getElementById("msgIdentity_clone").init();
246        vI_statusmenu.init();
247    },
248   
249    initSystemStage2 : function() {
250        vI_notificationBar.dump("## v_identity: initSystemStage2.\n")
251        vI_msgIdentityCloneTools.initReplyTo();
252        vI_storage.init();
253        vI_smartIdentity.init();
254    },
255   
256    close : function() {
257        vI.Cleanup();
258        vI_storage.clean();
259    },
260   
261    adapt_interface : function() {
262        if (vI.elements.strings) return; // only rearrange the interface once
263       
264        // initialize the pointers to extension elements
265        vI.elements.init_base()
266       
267        // rearrange the positions of some elements
268        var parent_hbox = vI.elements.Obj_MsgIdentity.parentNode;
269        var storage_box = document.getElementById("addresses-box");
270        var autoReplyToSelfLabel = document.getElementById("autoReplyToSelfLabel");
271       
272        storage_box.removeChild(autoReplyToSelfLabel);
273        parent_hbox.appendChild(autoReplyToSelfLabel);
274        storage_box.removeChild(vI.elements.Area_MsgIdentityHbox);
275        parent_hbox.appendChild(vI.elements.Area_MsgIdentityHbox);
276
277        vI.elements.Obj_MsgIdentity.setAttribute("hidden", "true");
278        vI.elements.Obj_MsgIdentity.previousSibling.setAttribute("control", "msgIdentity_clone");
279       
280        // initialize the pointers to extension elements (initialize those earlier might brake the interface)
281        vI.elements.init_rest();   
282    },
283   
284    adapt_genericSendMessage : function() {
285        if (vI.original_functions.GenericSendMessage) return true; // only initialize this once
286        vI_notificationBar.dump("## v_identity: adapt GenericSendMessage\n");
287        vI.original_functions.GenericSendMessage = GenericSendMessage;
288        GenericSendMessage = vI.replacement_functions.GenericSendMessage;
289        return true;
290    },
291   
292    reopen: function() {
293        vI_notificationBar.clear();
294        vI_notificationBar.clear_dump();
295        vI_notificationBar.dump("## v_identity: composeDialog reopened. (msgType " + gMsgCompose.type + ")\n")
296       
297        // clean all elements
298        document.getElementById("msgIdentity_clone").clean();
299        vI_smartIdentity.clean();
300        vI_notificationBar.dump("## v_identity: everything cleaned.\n")
301       
302        // now (re)init the elements
303        vI.initSystemStage1();
304       
305        // stateListener only works in reply-cases
306        // so activate stage2 in reply-cases trough StateListener
307        // in other cases directly
308        var msgComposeType = Components.interfaces.nsIMsgCompType;
309        switch (gMsgCompose.type) {
310            case msgComposeType.New:
311            case msgComposeType.NewsPost:
312            case msgComposeType.MailToUrl:
313            case msgComposeType.Draft:
314            case msgComposeType.Template:
315            case msgComposeType.ForwardAsAttachment:
316            case msgComposeType.ForwardInline:
317                vI.initSystemStage2(); break;
318            case msgComposeType.Reply:
319            case msgComposeType.ReplyAll:
320            case msgComposeType.ReplyToGroup:
321            case msgComposeType.ReplyToSender:
322            case msgComposeType.ReplyToSenderAndGroup:
323            case msgComposeType.ReplyWithTemplate:
324                gMsgCompose.RegisterStateListener(vI.ComposeStateListener);
325        }
326        vI_notificationBar.dump("## v_identity: reopen done.\n")
327    },
328   
329    tempStorage: { BaseIdentity : null, NewIdentity : null },
330
331    __setSelectedIdentity : function(menuItem) {
332        vI.elements.Obj_MsgIdentity.selectedItem = menuItem;
333        vI.elements.Obj_MsgIdentity.setAttribute("label", menuItem.getAttribute("label"));
334        vI.elements.Obj_MsgIdentity.setAttribute("accountname", menuItem.getAttribute("accountname"));
335        vI.elements.Obj_MsgIdentity.setAttribute("value", menuItem.getAttribute("value"));
336    },
337
338    // sets the values of the dropdown-menu to the ones of the newly created account
339    addVirtualIdentityToMsgIdentityMenu : function()
340    {
341        vI.tempStorage.BaseIdentity = vI.elements.Obj_MsgIdentity.selectedItem;
342        vI.tempStorage.NewIdentity = document.createElement("menuitem");
343        vI.tempStorage.NewIdentity.className = "identity-popup-item";
344       
345        // set the account name in the choosen menu item
346        vI.tempStorage.NewIdentity.setAttribute("label", vI_account.account.defaultIdentity.identityName);
347        vI.tempStorage.NewIdentity.setAttribute("accountname", " - " +  vI_account.account.incomingServer.prettyName);
348        vI.tempStorage.NewIdentity.setAttribute("accountkey", vI_account.account.key);
349        vI.tempStorage.NewIdentity.setAttribute("value", vI_account.account.defaultIdentity.key);
350       
351        vI.elements.Obj_MsgIdentityPopup.appendChild(vI.tempStorage.NewIdentity);
352        vI.__setSelectedIdentity(vI.tempStorage.NewIdentity);
353    },
354   
355    removeVirtualIdentityFromMsgIdentityMenu : function()
356    {
357        if (!vI.tempStorage.BaseIdentity) return; // don't try to remove Item twice
358        try {   // might not exist anymore (window closed), so just try to remove it
359            document.getElementById("msgIdentity").firstChild.removeChild(vI.tempStorage.NewIdentity);
360            vI.__setSelectedIdentity(vI.tempStorage.BaseIdentity);
361        } catch (e) { };
362        vI.tempStorage.NewIdentity = null;
363        vI.tempStorage.BaseIdentity = null;
364    },
365
366    prepareAccount : function() {
367        vI.Cleanup(); // just to be sure that nothing is left (maybe last time sending was irregularily stopped)
368        vI_account.createAccount();
369        vI.addVirtualIdentityToMsgIdentityMenu();
370    },
371
372    Cleanup : function() {
373        vI.removeVirtualIdentityFromMsgIdentityMenu();
374        vI_account.removeUsedVIAccount();
375    }
376}
377
378
379vI.replacement_functions.replace_FillIdentityList();
380window.addEventListener('load', vI.init, false);        // TB 1.5x, SM
381window.addEventListener('compose-window-init', vI.init, true);  // TB 2.x 3.x
382
383window.addEventListener("unload", function(e) { try {vI_statusmenu.removeObserver();} catch (ex) { } }, false);
384
Note: See TracBrowser for help on using the repository browser.