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