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