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