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 | var EXPORTED_SYMBOLS = ["smartIdentity"] |
---|
26 | |
---|
27 | Components.utils.import("resource://v_identity/vI_log.js"); |
---|
28 | Components.utils.import("resource://v_identity/vI_identityData.js"); |
---|
29 | Components.utils.import("resource://v_identity/vI_smartIdentityCollection.js"); |
---|
30 | Components.utils.import("resource://v_identity/vI_prefs.js"); |
---|
31 | |
---|
32 | let Log = setupLogging("virtualIdentity.smartIdentity"); |
---|
33 | |
---|
34 | function smartIdentity(currentWindow, msgCompose, storage) { |
---|
35 | this._currentWindow = currentWindow; |
---|
36 | this._document = currentWindow.document; |
---|
37 | this._msgCompose = msgCompose; |
---|
38 | this._storage = storage; |
---|
39 | this.init(); |
---|
40 | }; |
---|
41 | |
---|
42 | smartIdentity.prototype = { |
---|
43 | _window: null, |
---|
44 | _document: null, |
---|
45 | _msgCompose: null, |
---|
46 | _storage: null, |
---|
47 | _smartIdentityCollection: null, |
---|
48 | |
---|
49 | messenger: Components.classes["@mozilla.org/messenger;1"].createInstance() |
---|
50 | .QueryInterface(Components.interfaces.nsIMessenger), |
---|
51 | |
---|
52 | stringBundle: Components.classes["@mozilla.org/intl/stringbundle;1"] |
---|
53 | .getService(Components.interfaces.nsIStringBundleService) |
---|
54 | .createBundle("chrome://v_identity/locale/v_identity.properties"), |
---|
55 | |
---|
56 | // After Loading the MessageComposeDialog, check if smartIdentity is needed |
---|
57 | init: function () { |
---|
58 | var msgHdr; |
---|
59 | var msgComposeTypeReference = Components.interfaces.nsIMsgCompType; |
---|
60 | // Log.debug("this._document.title=" + this._document.title) |
---|
61 | |
---|
62 | var newsgroup = this._msgCompose.compFields.newsgroups; |
---|
63 | var autocreate = false; |
---|
64 | Log.debug("msgComposeTypeReference = " + this._msgCompose.type); |
---|
65 | switch (this._msgCompose.type) { |
---|
66 | case msgComposeTypeReference.Reply: |
---|
67 | case msgComposeTypeReference.ReplyAll: |
---|
68 | case msgComposeTypeReference.ReplyToGroup: // reply to a newsgroup, would possibly be stopped later |
---|
69 | case msgComposeTypeReference.ReplyToSender: |
---|
70 | case msgComposeTypeReference.ReplyToSenderAndGroup: // reply to a newsgroup, would possibly be stopped later |
---|
71 | case msgComposeTypeReference.ReplyWithTemplate: |
---|
72 | case msgComposeTypeReference.ReplyToList: |
---|
73 | case msgComposeTypeReference.ReplyIgnoreQuote: |
---|
74 | Log.debug("Reply"); |
---|
75 | msgHdr = this.messenger. |
---|
76 | messageServiceFromURI(this._msgCompose.originalMsgURI).messageURIToMsgHdr(this._msgCompose.originalMsgURI); |
---|
77 | this._smartIdentityCollection = new smartIdentityCollection(this._currentWindow, msgHdr, this._currentWindow.getCurrentIdentity(), this._document.getElementById("msgIdentity").vid, |
---|
78 | newsgroup, this._getRecipients()); |
---|
79 | this._smartIdentityCollection.Reply(); |
---|
80 | autocreate = false; |
---|
81 | break; |
---|
82 | case msgComposeTypeReference.Draft: |
---|
83 | case msgComposeTypeReference.Template: |
---|
84 | Log.debug("Draft"); |
---|
85 | msgHdr = this.messenger. |
---|
86 | messageServiceFromURI(this._msgCompose.compFields.draftId).messageURIToMsgHdr(this._msgCompose.compFields.draftId); |
---|
87 | this._smartIdentityCollection = new smartIdentityCollection(this._currentWindow, msgHdr, this._currentWindow.getCurrentIdentity(), this._document.getElementById("msgIdentity").vid, |
---|
88 | newsgroup, this._getRecipients()); |
---|
89 | this._smartIdentityCollection.Draft(); |
---|
90 | autocreate = false; |
---|
91 | break; |
---|
92 | case msgComposeTypeReference.ForwardAsAttachment: |
---|
93 | case msgComposeTypeReference.ForwardInline: |
---|
94 | case msgComposeTypeReference.New: |
---|
95 | case msgComposeTypeReference.NewsPost: |
---|
96 | case msgComposeTypeReference.MailToUrl: |
---|
97 | case msgComposeTypeReference.EditAsNew: |
---|
98 | case msgComposeTypeReference.EditTemplate: |
---|
99 | Log.debug("New Mail"); |
---|
100 | this._smartIdentityCollection = new smartIdentityCollection(this._currentWindow, null, this._currentWindow.getCurrentIdentity(), this._document.getElementById("msgIdentity").vid, |
---|
101 | newsgroup, this._getRecipients()); |
---|
102 | // to enable composing new email with new identity: identity is hidden in subject line |
---|
103 | // used for instance from conversation addon |
---|
104 | var subject = this._msgCompose.compFields.subject.split(/\n/); |
---|
105 | if (subject.length > 1 && subject[1] == "virtualIdentityExtension") { |
---|
106 | Log.debug("NewMail() found stored identity preset: " + subject[2]); |
---|
107 | this._smartIdentityCollection.__parseHeadersWithArray(subject[2], this._smartIdentityCollection._allIdentities); |
---|
108 | this._msgCompose.compFields.subject = subject[0]; |
---|
109 | this._document.getElementById("msgSubject").value = subject[0]; |
---|
110 | } else this._smartIdentityCollection.NewMail(); |
---|
111 | autocreate = true; |
---|
112 | break; |
---|
113 | } |
---|
114 | if (this._smartIdentityCollection && |
---|
115 | this._smartIdentityCollection._allIdentities.number > 0) this.__smartIdentitySelection(autocreate); |
---|
116 | }, |
---|
117 | |
---|
118 | _getRecipients: function () { |
---|
119 | var recipients = []; |
---|
120 | for (var row = 1; row <= this._currentWindow.top.MAX_RECIPIENTS; row++) { |
---|
121 | if (typeof this._currentWindow.awGetPopupElement(row).selectedItem == 'undefined') |
---|
122 | continue; |
---|
123 | var recipientType = this._currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value"); |
---|
124 | if (recipientType == "addr_reply" || recipientType == "addr_followup" || |
---|
125 | this._storage.isDoBcc(row, this._currentWindow) || this._currentWindow.awGetInputElement(row).value.match(/^\s*$/)) continue; |
---|
126 | recipients.push({ |
---|
127 | recipient: this._currentWindow.awGetInputElement(row).value, |
---|
128 | recipientType: recipientType |
---|
129 | }); |
---|
130 | } |
---|
131 | return recipients; |
---|
132 | }, |
---|
133 | |
---|
134 | __smartIdentitySelection: function (autocreate) { |
---|
135 | Log.debug("__smartIdentitySelection autocreate=" + autocreate); |
---|
136 | |
---|
137 | if (vIprefs.get("idSelection_preferExisting")) { |
---|
138 | var existingIDIndex = this._smartIdentityCollection._foundExistingIdentity(); |
---|
139 | if (existingIDIndex) { |
---|
140 | Log.debug("found existing Identity, use without interaction."); |
---|
141 | // add all Indentities to MsgIdentity Menu before selecting and leaving the function |
---|
142 | this._document.getElementById("msgIdentity").addIdentitiesToMsgIdentityMenu(this._smartIdentityCollection._allIdentities); |
---|
143 | this.changeIdentityToSmartIdentity(this, existingIDIndex.key); |
---|
144 | return; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | this._document.getElementById("msgIdentity").addIdentitiesToMsgIdentityMenu(this._smartIdentityCollection._allIdentities); |
---|
149 | |
---|
150 | |
---|
151 | Log.debug("__smartIdentitySelection _allIdentities.number=" + |
---|
152 | this._smartIdentityCollection._allIdentities.number + |
---|
153 | " _ask_always=" + vIprefs.get("idSelection_ask_always") + |
---|
154 | " _ask=" + vIprefs.get("idSelection_ask")); |
---|
155 | if (!autocreate && vIprefs.get("idSelection_ask") && |
---|
156 | ((this._smartIdentityCollection._allIdentities.number == 1 && vIprefs.get("idSelection_ask_always")) || this._smartIdentityCollection._allIdentities.number > 1)) { |
---|
157 | for (var index = 0; index < this._smartIdentityCollection._allIdentities.number; index++) { |
---|
158 | Log.debug("smartIdentityReplyDialog index=" + index + ": '" + this._smartIdentityCollection._allIdentities.identityDataCollection[index].combinedName + "' " + "(" + this._smartIdentityCollection._allIdentities.identityDataCollection[index].id.value + ")"); |
---|
159 | } |
---|
160 | this._currentWindow.openDialog("chrome://v_identity/content/vI_smartReplyDialog.xul", 0, |
---|
161 | "chrome, dialog, modal, alwaysRaised, resizable=yes", |
---|
162 | this._smartIdentityCollection._allIdentities, this, |
---|
163 | /* callback: */ |
---|
164 | this.changeIdentityToSmartIdentity).focus(); |
---|
165 | } else if (autocreate || vIprefs.get("idSelection_autocreate")) { |
---|
166 | this.changeIdentityToSmartIdentity(this, 0); |
---|
167 | } |
---|
168 | }, |
---|
169 | |
---|
170 | // might be called from external window |
---|
171 | changeIdentityToSmartIdentity: function (self, selectedValue) { |
---|
172 | let allIdentities = self._smartIdentityCollection._allIdentities; |
---|
173 | Log.debug("changeIdentityToSmartIdentity selectedValue=" + selectedValue + " from " + allIdentities.number); |
---|
174 | Log.debug("changeIdentityToSmartIdentity selectedValue=" + selectedValue + ": '" + allIdentities.identityDataCollection[selectedValue].combinedName + "' " + "(" + allIdentities.identityDataCollection[selectedValue].id.value + ")"); |
---|
175 | |
---|
176 | // allIdentities.menuItems[selectedValue].setAttribute("accountkey", |
---|
177 | // self._document.getElementById("msgIdentity").selectedMenuItem.accountkey); |
---|
178 | // allIdentities.menuItems[selectedValue].setAttribute("identitykey", |
---|
179 | // self._document.getElementById("msgIdentity").selectedMenuItem.identitykey); |
---|
180 | self._document.getElementById("msgIdentity").selectedMenuItem = allIdentities.menuItems[selectedValue]; |
---|
181 | if (self._document.getElementById("msgIdentity").vid) { |
---|
182 | var label = self.stringBundle.GetStringFromName("vident.smartIdentity.vIUsage"); |
---|
183 | if (allIdentities.number > 1) label += " " + self.stringBundle.GetStringFromName("vident.smartIdentity.moreThanOne"); |
---|
184 | SmartReplyNotification.info(label + "."); |
---|
185 | } |
---|
186 | self.__removeSmartIdentityFromRecipients(allIdentities, selectedValue); |
---|
187 | }, |
---|
188 | |
---|
189 | __removeSmartIdentityFromRecipients: function (allIdentities, index) { |
---|
190 | if (!vIprefs.get("idSelection_removeSmartIdentityFromRecipients")) return; |
---|
191 | |
---|
192 | // check if selected email is defined as doBcc address. If so, it should not be removed. |
---|
193 | var skip_bcc = false; |
---|
194 | if (this._currentWindow.getCurrentIdentity().doBcc) { |
---|
195 | var bcc_addresses = new identityCollection(); |
---|
196 | this.__parseHeadersWithArray(this._currentWindow.getCurrentIdentity().doBccList, bcc_addresses); |
---|
197 | |
---|
198 | for (var i = 0; i < bcc_addresses.number; i++) { |
---|
199 | if (allIdentities.identityDataCollection[index].email == bcc_addresses.identityDataCollection[i].email) { |
---|
200 | skip_bcc = true; |
---|
201 | break; |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | // check if there is more than one recipient for this mail. If not, preserve the only one existing. |
---|
207 | var recipientCount = 0; |
---|
208 | for (var row = 1; row <= this._currentWindow.top.MAX_RECIPIENTS; row++) { |
---|
209 | if (typeof this._currentWindow.awGetPopupElement(row).selectedItem == 'undefined') |
---|
210 | continue; |
---|
211 | var recipientType = this._currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value"); |
---|
212 | if (recipientType == "addr_to" || recipientType == "addr_cc") recipientCount++; |
---|
213 | } |
---|
214 | if (recipientCount < 2) return; |
---|
215 | |
---|
216 | |
---|
217 | for (var row = 1; row <= this._currentWindow.top.MAX_RECIPIENTS; row++) { |
---|
218 | var popup = this._currentWindow.awGetPopupElement(row); |
---|
219 | var input = this._currentWindow.awGetInputElement(row); |
---|
220 | if (typeof popup.selectedItem == 'undefined') |
---|
221 | continue; |
---|
222 | var recipientType = popup.selectedItem.getAttribute("value"); |
---|
223 | // if the entry is not a recipient, just continue |
---|
224 | if (recipientType == "addr_reply" || recipientType == "addr_followup") continue; |
---|
225 | // check if the entry is used as a BCC selected in account settings |
---|
226 | if (recipientType == "addr_bcc" && skip_bcc) continue; |
---|
227 | // check if entry is matching senders address, if so, remove it |
---|
228 | if (input.value == allIdentities.identityDataCollection[index].email || |
---|
229 | input.value == allIdentities.identityDataCollection[index].combinedName) { |
---|
230 | this._currentWindow.awSetInputAndPopupValue(input, "", popup, "addr_to", -1); |
---|
231 | this._currentWindow.awCleanupRows() |
---|
232 | SmartReplyNotification.info(" " + this.stringBundle.GetStringFromName("vident.smartIdentity.remRecipient")); |
---|
233 | break; |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|