This is just some static backup of the original site, don't expect every link to work!

source: chrome/content/v_identity/vI_identityData.js @ f7b110

ng_0.6ng_0.8ng_0.9
Last change on this file since f7b110 was f7b110, checked in by rene <rene@…>, 12 years ago

ignore empty email addresses

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