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 @ 30a840

lite_0.1multiEditng_0.6ng_0.6_helpng_0.8ng_0.9
Last change on this file since 30a840 was 30a840, checked in by rene <rene@…>, 13 years ago

indicate if SMTP will not be stored

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