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 @ 3f9026

ng_0.8ng_0.9
Last change on this file since 3f9026 was 3f9026, checked in by rene <rene@…>, 11 years ago

changed preferences dialog

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