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 | function vI_identityData(email, fullName, id, smtp, extras, sideDescription, existingID) { |
---|
26 | this.email = email; |
---|
27 | this.fullName = (fullName?fullName:''); |
---|
28 | this.id = new vI_idObj(id); |
---|
29 | this.smtp = new vI_smtpObj(smtp); |
---|
30 | this.extras = extras?extras:new vI_storageExtras(); |
---|
31 | this.comp = { // holds the results of the last comparison for later creation of a compareMatrix |
---|
32 | compareID : null, |
---|
33 | equals : { fullName : {}, email : {}, smtp : {}, id : {}, extras : {} } |
---|
34 | } |
---|
35 | if (sideDescription) this.sideDescription = sideDescription; |
---|
36 | if (existingID) this.existingID = existingID; |
---|
37 | else if (this.id.value) this.sideDescription = " - " + this.id.value; |
---|
38 | this.stringBundle = document.getElementById("vIdentBundle"); |
---|
39 | } |
---|
40 | vI_identityData.prototype = { |
---|
41 | email : null, |
---|
42 | fullName : null, |
---|
43 | id : null, |
---|
44 | smtp : null, |
---|
45 | extras : null, |
---|
46 | sideDescription : null, |
---|
47 | existingID : null, // indicates that this is a pre-defined Identity, which might handled slightly differently |
---|
48 | |
---|
49 | stringBundle : null, |
---|
50 | comp : null, |
---|
51 | |
---|
52 | get combinedName() { |
---|
53 | var email = this.email?this.email.replace(/^\s+|\s+$/g,""):""; |
---|
54 | var fullName = this.fullName?this.fullName.replace(/^\s+|\s+$/g,""):""; |
---|
55 | return fullName?fullName+(email?" <"+email+">":""):email |
---|
56 | }, |
---|
57 | set combinedName(combinedName) { |
---|
58 | var name = ""; var email = ""; |
---|
59 | // prefer an email address separated with < >, only if not found use any other |
---|
60 | if (combinedName.match(/<\s*[^>\s]*@[^>\s]*\s*>/) || combinedName.match(/<?\s*[^>\s]*@[^>\s]*\s*>?/) || combinedName.match(/$/)) { |
---|
61 | name = RegExp.leftContext + RegExp.rightContext |
---|
62 | email = RegExp.lastMatch |
---|
63 | email = email.replace(/\s+|<|>/g,"") |
---|
64 | name = name.replace(/^\s+|\s+$/g,"") |
---|
65 | name = name.replace(/^\"|\"$/g,"") |
---|
66 | name = name.replace(/^\'|\'$/g,"") |
---|
67 | } |
---|
68 | this.fullName = name; |
---|
69 | this.email = email; |
---|
70 | }, |
---|
71 | |
---|
72 | __makeHtml : function (string) { return string?string.replace(/>/g,">").replace(/</g,"<"):"" }, |
---|
73 | get idHtml() { return this.__makeHtml(this.id.value); }, |
---|
74 | get smtpHtml() { return this.__makeHtml(this.smtp.value); }, |
---|
75 | get fullNameHtml() { return this.__makeHtml(this.fullName); }, |
---|
76 | get emailHtml() { return this.__makeHtml(this.email); }, |
---|
77 | get combinedNameHtml() { return this.__makeHtml(this.combinedName); }, |
---|
78 | |
---|
79 | get idLabel() { return this.stringBundle.getString("vident.identityData.baseID") }, |
---|
80 | get smtpLabel() { return this.stringBundle.getString("vident.identityData.SMTP") }, |
---|
81 | get fullNameLabel() { return this.stringBundle.getString("vident.identityData.Name") }, |
---|
82 | get emailLabel() { return this.stringBundle.getString("vident.identityData.Address") }, |
---|
83 | |
---|
84 | // creates an Duplicate of the current IdentityData, cause usually we are working with a pointer |
---|
85 | getDuplicate : function() { |
---|
86 | return new vI_identityData(this.email, this.fullName, this.id.key, this.smtp.key, this.extras.getDuplicate(), this.sideDescription, this.existingID); |
---|
87 | }, |
---|
88 | |
---|
89 | // copys all values of an identity. This way we can create a new object with a different document-context |
---|
90 | copy : function(identityData) { |
---|
91 | this.email = identityData.email; |
---|
92 | this.fullName = identityData.fullName; |
---|
93 | this.id.key = identityData.id.key; |
---|
94 | this.smtp.key = identityData.smtp.key; |
---|
95 | this.sideDescription = identityData.sideDescription; |
---|
96 | this.extras.copy(identityData.extras); |
---|
97 | }, |
---|
98 | |
---|
99 | // dependent on MsgComposeCommands, should/will only be called in ComposeDialog |
---|
100 | isExistingIdentity : function(ignoreFullNameWhileComparing) { |
---|
101 | vI_notificationBar.dump("## vI_identityData: isExistingIdentity: ignoreFullNameWhileComparing='" + ignoreFullNameWhileComparing + "'\n"); |
---|
102 | // vI_notificationBar.dump("## vI_identityData base: fullName.toLowerCase()='" + this.fullName + "' email.toLowerCase()='" + this.email + "' smtp='" + this.smtp.key + "'\n"); |
---|
103 | |
---|
104 | var accounts = queryISupportsArray(gAccountManager.accounts, Components.interfaces.nsIMsgAccount); |
---|
105 | for (var i in accounts) { |
---|
106 | // skip possible active VirtualIdentity Accounts |
---|
107 | try { vI_account.prefroot.getBoolPref("mail.account."+accounts[i].key+".vIdentity"); continue; } catch (e) { }; |
---|
108 | |
---|
109 | var identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity); |
---|
110 | for (var j in identities) { |
---|
111 | // vI_notificationBar.dump("## vI_identityData comp: fullName.toLowerCase()='" + identities[j].fullName.toLowerCase() + "' email.toLowerCase()='" + identities[j].email.toLowerCase() + "' smtp='" + identities[j].smtpServerKey + "'\n"); |
---|
112 | var email = this.email?this.email:""; // might be null if no identity is set |
---|
113 | var idEmail = identities[j].email?identities[j].email:""; // might be null if no identity is set |
---|
114 | if ( (ignoreFullNameWhileComparing || |
---|
115 | this.fullName.toLowerCase() == identities[j].fullName.toLowerCase()) && |
---|
116 | (email.toLowerCase() == idEmail.toLowerCase()) && |
---|
117 | this.smtp.equal(new vI_smtpObj(identities[j].smtpServerKey)) ) { |
---|
118 | vI_notificationBar.dump("## vI_identityData: isExistingIdentity: " + this.combinedName + " found, id='" + identities[j].key + "'\n"); |
---|
119 | return identities[j].key; |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | vI_notificationBar.dump("## vI_identityData: isExistingIdentity: " + this.combinedName + " not found\n"); |
---|
124 | return null; |
---|
125 | }, |
---|
126 | |
---|
127 | equals : function(compareIdentityData) { |
---|
128 | this.comp.compareID = compareIdentityData; |
---|
129 | |
---|
130 | this.comp.equals.fullName = (((this.fullName)?this.fullName.toLowerCase():null) == ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null)); |
---|
131 | this.comp.equals.email = (((this.email)?this.email.toLowerCase():null) == ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null)); |
---|
132 | |
---|
133 | this.comp.equals.smtp = this.smtp.equal(compareIdentityData.smtp); |
---|
134 | |
---|
135 | |
---|
136 | this.comp.equals.id = this.id.equal(compareIdentityData.id); |
---|
137 | this.comp.equals.extras = this.extras.equal(compareIdentityData.extras); |
---|
138 | |
---|
139 | return (this.comp.equals.fullName && this.comp.equals.email && this.comp.equals.smtp && this.comp.equals.id && this.comp.equals.extras); |
---|
140 | }, |
---|
141 | |
---|
142 | equalsCurrentIdentity : function(getCompareMatrix) { |
---|
143 | var equal = this.equals(document.getElementById("msgIdentity_clone").identityData); |
---|
144 | var compareMatrix = null; |
---|
145 | // generate CompareMatrix only if asked and non-equal |
---|
146 | if (getCompareMatrix && !equal) compareMatrix = this.getCompareMatrix(); |
---|
147 | return { equal : equal, compareMatrix : compareMatrix }; |
---|
148 | }, |
---|
149 | |
---|
150 | getCompareMatrix : function() { |
---|
151 | const Items = Array("fullName", "email", "smtp", "id"); |
---|
152 | var string = ""; |
---|
153 | var saveBaseId = (vI_statusmenu.objSaveBaseIDMenuItem.getAttribute("checked") == "true") |
---|
154 | var saveSMTP = (vI_statusmenu.objSaveSMTPMenuItem.getAttribute("checked") == "true") |
---|
155 | for each (item in Items) { |
---|
156 | var classEqual = (this.comp.equals[item])?"equal":"unequal"; |
---|
157 | var classIgnore = (((!saveBaseId) && (item == "id")) || ((!saveSMTP) && (item == "smtp")))?" ignoreValues":"" |
---|
158 | string += "<tr>" + |
---|
159 | "<td class='col1 " + classEqual + "'>" + this[item+"Label"] + "</td>" + |
---|
160 | "<td class='col2 " + classEqual + classIgnore + "'>" + this.comp.compareID[item+"Html"] + "</td>" + |
---|
161 | "<td class='col3 " + classEqual + classIgnore + "'>" + this[item+"Html"] + "</td>" + |
---|
162 | "</tr>" |
---|
163 | } |
---|
164 | string += this.extras.getCompareMatrix(); |
---|
165 | return string; |
---|
166 | }, |
---|
167 | |
---|
168 | getMatrix : function() { |
---|
169 | const Items = Array("smtp", "id"); |
---|
170 | var string = ""; |
---|
171 | for each (item in Items) if (this[item+"Html"]) |
---|
172 | string += "<tr><td class='col1'>" + this[item+"Label"] + ":</td>" + |
---|
173 | "<td class='col2'>" + this[item+"Html"] + "</td></tr>" |
---|
174 | string += this.extras.getMatrix(); |
---|
175 | return string; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | function vI_identityCollection() { |
---|
180 | this.number = 0; |
---|
181 | this.identityDataCollection = {}; |
---|
182 | this.menuItems = {}; |
---|
183 | } |
---|
184 | vI_identityCollection.prototype = |
---|
185 | { |
---|
186 | number : null, |
---|
187 | identityDataCollection : null, |
---|
188 | menuItems : null, |
---|
189 | |
---|
190 | mergeWithoutDuplicates : function(addIdentityCollection) { |
---|
191 | for (var index = 0; index < addIdentityCollection.number; index++) |
---|
192 | this.addWithoutDuplicates(addIdentityCollection.identityDataCollection[index]) |
---|
193 | }, |
---|
194 | |
---|
195 | dropIdentity : function(index) { |
---|
196 | vI_notificationBar.dump("## identityCollection: dropping address from inputList: " + this.identityDataCollection[index].combinedName + "\n"); |
---|
197 | while (index < (this.number - 1)) { this.identityDataCollection[index] = this.identityDataCollection[++index]; }; |
---|
198 | this.identityDataCollection[--this.number] = null; |
---|
199 | }, |
---|
200 | |
---|
201 | addWithoutDuplicates : function(identityData) { |
---|
202 | for (var index = 0; index < this.number; index++) { |
---|
203 | if (this.identityDataCollection[index].email == identityData.email && |
---|
204 | (!this.identityDataCollection[index].id.key || !identityData.id.key || |
---|
205 | (this.identityDataCollection[index].id.key == identityData.id.key && |
---|
206 | this.identityDataCollection[index].smtp.key == identityData.smtp.key))) { |
---|
207 | // found, so check if we can use the Name of the new field |
---|
208 | if (this.identityDataCollection[index].fullName == "" && identityData.fullName != "") { |
---|
209 | this.identityDataCollection[index].fullName = identityData.fullName; |
---|
210 | vI_notificationBar.dump("## identityCollection: added fullName '" + identityData.fullName |
---|
211 | + "' to stored email '" + this.identityDataCollection[index].email +"'\n") |
---|
212 | } |
---|
213 | // check if id_key, smtp_key or extras can be used |
---|
214 | // only try this once, for the first Identity where id is set) |
---|
215 | if (!this.identityDataCollection[index].id.key && identityData.id.key) { |
---|
216 | this.identityDataCollection[index].id.key = identityData.id.key; |
---|
217 | this.identityDataCollection[index].smtp.key = identityData.smtp.key; |
---|
218 | this.identityDataCollection[index].extras = identityData.extras; |
---|
219 | vI_notificationBar.dump("## identityCollection: added id '" + identityData.id.value |
---|
220 | + "' smtp '" + identityData.smtp.value + "' (+extras) to stored email '" + this.identityDataCollection[index].email +"'\n") |
---|
221 | } |
---|
222 | return; |
---|
223 | } |
---|
224 | } |
---|
225 | vI_notificationBar.dump("## identityCollection: add new address to result: " + identityData.combinedName + "\n") |
---|
226 | this.identityDataCollection[index] = identityData; |
---|
227 | this.number = index + 1; |
---|
228 | }, |
---|
229 | |
---|
230 | // this is used to completely use the conten of another identityCollection, but without changing all pointers |
---|
231 | // see for instance vI_smartIdentity.__filterAddresses |
---|
232 | takeOver : function(newIdentityCollection) { |
---|
233 | this.number = newIdentityCollection.number |
---|
234 | this.identityDataCollection = newIdentityCollection.identityDataCollection |
---|
235 | } |
---|
236 | }; |
---|
237 | |
---|
238 | const DEFAULT_SMTP_TAG = "vI_useDefaultSMTP" |
---|
239 | const NO_SMTP_TAG = "vI_noStoredSMTP" |
---|
240 | |
---|
241 | function vI_smtpObj(key) { |
---|
242 | this._key = key; |
---|
243 | this.DEFAULT_TAG = document.getElementById("bundle_messenger").getString("defaultServerTag"); |
---|
244 | } |
---|
245 | vI_smtpObj.prototype = { |
---|
246 | DEFAULT_TAG : null, |
---|
247 | _key : null, |
---|
248 | _value : null, |
---|
249 | |
---|
250 | set key(key) { this._key = key; this._value = null; }, |
---|
251 | get key() { |
---|
252 | var dummy = this.value; // just to be sure key is adapted if SMTP is not available |
---|
253 | return this._key |
---|
254 | }, |
---|
255 | get keyNice() { // the same as key but with "" for DEFAULT_SMTP_TAG |
---|
256 | if (this.key == DEFAULT_SMTP_TAG) return ""; // this is the key used for default server |
---|
257 | return this.key |
---|
258 | }, |
---|
259 | get value() { |
---|
260 | if (this._value == null) { |
---|
261 | this._value = ""; |
---|
262 | if (this._key == null || this._key == "") this._key = DEFAULT_SMTP_TAG; |
---|
263 | if (this._key == DEFAULT_SMTP_TAG) this._value = this.DEFAULT_TAG; |
---|
264 | else if (!this._key) this._value = null; |
---|
265 | else if (this._key) { |
---|
266 | var servers = Components.classes["@mozilla.org/messengercompose/smtp;1"] |
---|
267 | .getService(Components.interfaces.nsISmtpService).smtpServers; |
---|
268 | if (typeof(servers.Count) == "undefined") // TB 3.x |
---|
269 | while (servers && servers.hasMoreElements()) { |
---|
270 | var server = servers.getNext(); |
---|
271 | if (server instanceof Components.interfaces.nsISmtpServer && |
---|
272 | !server.redirectorType && this._key == server.key) { |
---|
273 | this._value = server.description?server.description:server.hostname; |
---|
274 | break; |
---|
275 | } |
---|
276 | } |
---|
277 | else // TB 2.x |
---|
278 | for (var i=0 ; i < servers.Count(); i++) { |
---|
279 | var server = servers.QueryElementAt(i, |
---|
280 | Components.interfaces.nsISmtpServer); |
---|
281 | if (!server.redirectorType && this._key == server.key) { |
---|
282 | this._value = server.description?server.description:server.hostname; |
---|
283 | break; |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | } |
---|
288 | if (!this._value) this._key = NO_SMTP_TAG; // if non-existant SMTP handle like non available |
---|
289 | return this._value; |
---|
290 | }, |
---|
291 | equal : function(compareSmtpObj) { |
---|
292 | if (this.key == NO_SMTP_TAG || compareSmtpObj.key == NO_SMTP_TAG) return true; |
---|
293 | return (this.keyNice == compareSmtpObj.keyNice); |
---|
294 | }, |
---|
295 | hasNoDefinedSMTP : function() { |
---|
296 | return (this.key == NO_SMTP_TAG); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | function vI_idObj(key) { this._key = key; } |
---|
301 | vI_idObj.prototype = { |
---|
302 | _key : null, |
---|
303 | _value : null, |
---|
304 | |
---|
305 | set key(key) { this._key = key; this._value = null; }, |
---|
306 | get key() { if (this._value == null) var dummy = this.value; return this._key }, |
---|
307 | get value() { |
---|
308 | if (this._value == null) { |
---|
309 | this._value = ""; |
---|
310 | if (this._key) { |
---|
311 | var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
312 | .getService(Components.interfaces.nsIMsgAccountManager); |
---|
313 | for (var i = 0; i < accountManager.accounts.Count(); i++) { |
---|
314 | var account = accountManager.accounts.GetElementAt(i) |
---|
315 | .QueryInterface(Components.interfaces.nsIMsgAccount); |
---|
316 | for (var j = 0; j < account.identities.Count(); j++) { |
---|
317 | var identity = account.identities.GetElementAt(j) |
---|
318 | .QueryInterface(Components.interfaces.nsIMsgIdentity); |
---|
319 | if (this._key == identity.key) { |
---|
320 | this._value = identity.identityName; |
---|
321 | break; |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | if (!this._value) this._key = null; |
---|
326 | } |
---|
327 | } |
---|
328 | return this._value; |
---|
329 | }, |
---|
330 | equal : function(compareIdObj) { |
---|
331 | if (!this.key || !compareIdObj.key) return true; |
---|
332 | return (this.key == compareIdObj.key); |
---|
333 | } |
---|
334 | } |
---|