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