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 @ 92e82b

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

do cleanup after major error

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