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