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