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 @ b466b2

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

prefer full match instead of IgnoreFullName? while searching for existingIdentity

  • Property mode set to 100644
File size: 14.8 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;
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}
40vI_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,"&gt;").replace(/</g,"&lt;"):"" },
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 ignoreFullNameMatchKey = null;
105
106        var accounts = queryISupportsArray(gAccountManager.accounts, Components.interfaces.nsIMsgAccount);
107        for (var i in accounts) {
108            // skip possible active VirtualIdentity Accounts
109            try { vI_account.prefroot.getBoolPref("mail.account."+accounts[i].key+".vIdentity"); continue; } catch (e) { };
110   
111            var identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity);
112            for (var j in identities) {
113//              vI_notificationBar.dump("## vI_identityData comp: fullName.toLowerCase()='" + identities[j].fullName.toLowerCase() + "' email.toLowerCase()='" + identities[j].email.toLowerCase() + "' smtp='" + identities[j].smtpServerKey + "'\n");
114                var email = this.email?this.email:"";               // might be null if no identity is set
115                var idEmail = identities[j].email?identities[j].email:"";   // might be null if no identity is set
116                if (    (email.toLowerCase() == idEmail.toLowerCase()) &&
117                    this.smtp.equal(new vI_smtpObj(identities[j].smtpServerKey))    ) {
118                        // if fullName matches, than this is a final match
119                        if ( this.fullName.toLowerCase() == identities[j].fullName.toLowerCase() ) {
120                            vI_notificationBar.dump("## vI_identityData: isExistingIdentity: " + this.combinedName + " found, id='" + identities[j].key + "'\n");
121                            return identities[j].key; // return key and stop searching
122                        }
123                        // if fullNames don't match, remember the key but continue to search for full match
124                        else if (!ignoreFullNameMatchKey) ignoreFullNameMatchKey = identities[j].key;
125                }
126            }
127        }
128
129        if ( ignoreFullNameWhileComparing && ignoreFullNameMatchKey ) {
130            vI_notificationBar.dump("## vI_identityData: isExistingIdentity: " + this.combinedName + " found, id='" + ignoreFullNameMatchKey + "'\n");
131            return  ignoreFullNameMatchKey;
132        }
133
134        vI_notificationBar.dump("## vI_identityData: isExistingIdentity: " + this.combinedName + " not found\n");
135        return null;
136    },
137   
138    equals : function(compareIdentityData) {
139        this.comp.compareID = compareIdentityData;
140
141        this.comp.equals.fullName = (((this.fullName)?this.fullName.toLowerCase():null) == ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null));
142        this.comp.equals.email = (((this.email)?this.email.toLowerCase():null) == ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null));
143
144        this.comp.equals.smtp = this.smtp.equal(compareIdentityData.smtp);
145
146
147        this.comp.equals.id = this.id.equal(compareIdentityData.id);
148        this.comp.equals.extras = this.extras.equal(compareIdentityData.extras);
149       
150        return (this.comp.equals.fullName && this.comp.equals.email && this.comp.equals.smtp && this.comp.equals.id && this.comp.equals.extras);
151    },
152
153    equalsCurrentIdentity : function(getCompareMatrix) {
154        var equal = this.equals(document.getElementById("msgIdentity_clone").identityData);
155        var compareMatrix = null;
156        // generate CompareMatrix only if asked and non-equal
157        if (getCompareMatrix && !equal) compareMatrix = this.getCompareMatrix();
158        return { equal : equal, compareMatrix : compareMatrix };
159    },
160
161    getCompareMatrix : function() {
162        const Items = Array("fullName", "email", "smtp", "id");
163        var string = "";       
164        var saveBaseId = (vI_statusmenu.objSaveBaseIDMenuItem.getAttribute("checked") == "true")
165        var saveSMTP = (vI_statusmenu.objSaveSMTPMenuItem.getAttribute("checked") == "true")
166        for each (item in Items) {
167            var classEqual = (this.comp.equals[item])?"equal":"unequal";
168            var classIgnore = (((!saveBaseId) && (item == "id")) || ((!saveSMTP) && (item == "smtp")))?" ignoreValues":""
169            string += "<tr>" +
170                "<td class='col1 " + classEqual + "'>" + this[item+"Label"] + "</td>" +
171                "<td class='col2 " + classEqual + classIgnore + "'>" + this.comp.compareID[item+"Html"] + "</td>" +
172                "<td class='col3 " + classEqual + classIgnore + "'>" + this[item+"Html"] + "</td>" +
173                "</tr>"
174        }
175        string += this.extras.getCompareMatrix();
176        return string;
177    },
178
179    getMatrix : function() {
180        const Items = Array("smtp", "id");
181        var string = "";
182        for each (item in Items) if (this[item+"Html"])
183            string += "<tr><td class='col1'>" + this[item+"Label"] + ":</td>" +
184                "<td class='col2'>" + this[item+"Html"] + "</td></tr>"
185        string += this.extras.getMatrix();
186        return string;     
187    }
188}
189
190function vI_identityCollection() {
191    this.number = 0;
192    this.identityDataCollection = {};
193    this.menuItems = {};
194}
195vI_identityCollection.prototype =
196{
197    number : null,
198    identityDataCollection : null,
199    menuItems : null,
200   
201    mergeWithoutDuplicates : function(addIdentityCollection) {
202        for (var index = 0; index < addIdentityCollection.number; index++)
203            this.addWithoutDuplicates(addIdentityCollection.identityDataCollection[index])
204    },
205
206    dropIdentity : function(index) {
207        vI_notificationBar.dump("## identityCollection:   dropping address from inputList: " + this.identityDataCollection[index].combinedName + "\n");
208        while (index < (this.number - 1)) { this.identityDataCollection[index] = this.identityDataCollection[++index]; };
209        this.identityDataCollection[--this.number] = null;
210    },
211
212    addWithoutDuplicates : function(identityData) {
213        for (var index = 0; index < this.number; index++) {
214            if (this.identityDataCollection[index].email == identityData.email &&
215                (!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                // found, so check if we can use the Name of the new field
219                if (this.identityDataCollection[index].fullName == "" && identityData.fullName != "") {
220                    this.identityDataCollection[index].fullName = identityData.fullName;
221                    vI_notificationBar.dump("## identityCollection:   added fullName '" + identityData.fullName
222                        + "' to stored email '" + this.identityDataCollection[index].email +"'\n")
223                }
224                // check if id_key, smtp_key or extras can be used
225                // only try this once, for the first Identity where id is set)
226                if (!this.identityDataCollection[index].id.key && identityData.id.key) {
227                    this.identityDataCollection[index].id.key = identityData.id.key;
228                    this.identityDataCollection[index].smtp.key = identityData.smtp.key;
229                    this.identityDataCollection[index].extras = identityData.extras;
230                    vI_notificationBar.dump("## identityCollection:   added id '" + identityData.id.value
231                        + "' smtp '" + identityData.smtp.value + "' (+extras) to stored email '" + this.identityDataCollection[index].email +"'\n")
232                }
233                return;
234            }
235        }
236        vI_notificationBar.dump("## identityCollection:   add new address to result: " + identityData.combinedName + "\n")
237        this.identityDataCollection[index] = identityData;
238        this.number = index + 1;
239    },
240   
241    // this is used to completely use the conten of another identityCollection, but without changing all pointers
242    // see for instance vI_smartIdentity.__filterAddresses
243    takeOver : function(newIdentityCollection) {
244        this.number = newIdentityCollection.number
245        this.identityDataCollection = newIdentityCollection.identityDataCollection
246    }
247};
248
249const DEFAULT_SMTP_TAG = "vI_useDefaultSMTP"
250const NO_SMTP_TAG = "vI_noStoredSMTP"
251
252function vI_smtpObj(key) {
253    this._key = key;
254    this.DEFAULT_TAG = document.getElementById("bundle_messenger").getString("defaultServerTag");
255}
256vI_smtpObj.prototype = {
257    DEFAULT_TAG : null,
258    _key : null,
259    _value : null,
260   
261    set key(key) { this._key = key; this._value = null; },
262    get key() {
263        var dummy = this.value; // just to be sure key is adapted if SMTP is not available
264        return this._key
265    },
266    get keyNice() { // the same as key but with "" for DEFAULT_SMTP_TAG
267        if (this.key == DEFAULT_SMTP_TAG) return ""; // this is the key used for default server
268        return this.key
269    },
270    get value() {
271        if (this._value == null) {
272            this._value = "";
273            if (this._key == null || this._key == "") this._key = DEFAULT_SMTP_TAG;
274            if (this._key == DEFAULT_SMTP_TAG) this._value = this.DEFAULT_TAG;
275            else if (!this._key) this._value = null;
276            else if (this._key) {
277                var servers = Components.classes["@mozilla.org/messengercompose/smtp;1"]
278                    .getService(Components.interfaces.nsISmtpService).smtpServers;
279                if (typeof(servers.Count) == "undefined")       // TB 3.x
280                    while (servers && servers.hasMoreElements()) {
281                        var server = servers.getNext();
282                        if (server instanceof Components.interfaces.nsISmtpServer && 
283                            !server.redirectorType && this._key == server.key) {
284                            this._value = server.description?server.description:server.hostname;
285                            break;
286                        }
287                    }
288                else                            // TB 2.x
289                    for (var i=0 ; i < servers.Count(); i++) {
290                        var server = servers.QueryElementAt(i,
291                            Components.interfaces.nsISmtpServer);
292                        if (!server.redirectorType && this._key == server.key) {
293                            this._value = server.description?server.description:server.hostname;
294                            break;
295                        }
296                    }
297            }
298        }
299        if (!this._value) this._key = NO_SMTP_TAG; // if non-existant SMTP handle like non available
300        return this._value;
301    },
302    equal : function(compareSmtpObj) {
303        if (this.key == NO_SMTP_TAG || compareSmtpObj.key == NO_SMTP_TAG) return true;
304        return (this.keyNice == compareSmtpObj.keyNice);
305    },
306    hasNoDefinedSMTP : function() {
307        return (this.key == NO_SMTP_TAG);
308    }
309}
310
311function vI_idObj(key) { this._key = key; }
312vI_idObj.prototype = {
313    _key : null,
314    _value : null,
315
316    set key(key) { this._key = key; this._value = null; },
317    get key() { if (this._value == null) var dummy = this.value; return this._key },
318    get value() {
319        if (this._value == null) {
320            this._value = "";
321            if (this._key) {
322                var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"]
323                    .getService(Components.interfaces.nsIMsgAccountManager);
324                for (var i = 0; i < accountManager.accounts.Count(); i++) {
325                    var account = accountManager.accounts.GetElementAt(i)
326                        .QueryInterface(Components.interfaces.nsIMsgAccount);
327                    for (var j = 0; j < account.identities.Count(); j++) {
328                        var identity = account.identities.GetElementAt(j)
329                            .QueryInterface(Components.interfaces.nsIMsgIdentity);
330                        if (this._key == identity.key) {
331                            this._value = identity.identityName;
332                            break;
333                        }
334                    }
335                }
336                if (!this._value) this._key = null;
337            }
338        }
339        return this._value;
340    },
341    equal : function(compareIdObj) {
342        if (!this.key || !compareIdObj.key) return true;
343        return (this.key == compareIdObj.key);
344    }
345}
Note: See TracBrowser for help on using the repository browser.