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