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

source: content/modules/vI_identityData.js @ 184c6c

ng_0.9
Last change on this file since 184c6c was 184c6c, checked in by rene <rene@…>, 11 years ago

rearranged tree structure / added build-script

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