diff options
author | Matěj Cepl <mcepl@redhat.com> | 2013-05-15 00:07:15 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2013-05-18 20:57:29 +0200 |
commit | f395dfeb002d0840b5e92d23158801d15d632d9e (patch) | |
tree | 22a205cd7a34b8f17b6cd1f799c00364c140df8c | |
parent | 9831804da870874e6c2e6b37a1ad5a3c47b691ea (diff) | |
download | importLDIF-f395dfeb002d0840b5e92d23158801d15d632d9e.tar.gz |
The first draft of the complete program.
Completely untested.
-rw-r--r-- | importAddrBook.js | 281 | ||||
m--------- | libs/parseLDIF | 0 |
2 files changed, 275 insertions, 6 deletions
diff --git a/importAddrBook.js b/importAddrBook.js index 8c1894b..395e900 100644 --- a/importAddrBook.js +++ b/importAddrBook.js @@ -1,9 +1,282 @@ +/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, + bitwise:true, strict:true, undef:true, curly:true, browser:true, + devel:true, indent:2, maxerr:50, moz:true, newcap:false, moz:true */ + +/* global Contact: false, ContactAddress: false, ContactField: false, + ContactTelField: false, parseLDIF: false */ + (function () { "use strict"; + // Shim for the intersection of two Arrays. + if (!Array.prototype.intersection) { + Array.prototype.intersection = function (anotherArr) { + return this.filter(function(n) { + if (anotherArr.indexOf(n) === -1) { return false; } + return true; + }); + }; + } + + const phoneFields = [ + "mobile", "facsimiletelephonenumber", "homePhone", "telephoneNumber" + ]; + + const emailFields = [ "mail" ]; + + function ValueError(message) { + this.name = "ValueError"; + this.message = message || "Unknown Value"; + } + ValueError.prototype = new Error(); + ValueError.prototype.constructor = ValueError; + + /** + * Translate objects created from LDIF files to Contacts + * + * @param Array of LDIF objects + * @return Contact with all fields filled + * @throws ValueError for unknown field in the LDIF object + * + * ContactsAPI: + * @see https://wiki.mozilla.org/WebAPI/ContactsAPI + * @spec http://www.w3.org/TR/contacts-manager-api/ + * + * Thunderbid LDAP Schema: + * @see https://developer.mozilla.org/docs/Thunderbird/LDAP_Support + * @see https://tools.ietf.org/html/rfc4519 + */ + function translateObjectToContact(inRec) { + var contact = new Contact(), pomStr = "", curRec = null; + + /** + * Find the proper record (or create new one) in the multi-value + * attribute + * + * @param idx name of the attribute to which the record belongs + * @param subClass Function constructor of the record if there is + * none + * @param subType String with the type of the record + * @return record of the proper class + * + * Uses global variable contact. + */ + function findSubElement(idx, subClass, subType) { + var cAddr = null; + + // No contact.adr at all + if (!contact.hasOwnProperty(idx)) { + cAddr = new subClass(); + cAddr.type = [subType]; + contact[idx] = cAddr; + } + // Single-element property + else if (!Array.isArray(contact[idx])) { + if (contact[idx].type.indexOf(subType) === -1) { + contact[idx] = [contact[idx]]; + cAddr = new subClass(); + cAddr.type = [subType]; + contact[idx].push(cAddr); + } + else { + cAddr = contact[idx]; + } + } + // Array + else { + cAddr = contact[idx].filter(function (addr) { + return addr.type.indexOf(subType) !== -1; + }); + if (cAddr.length === 0) { + cAddr = new subClass(); + cAddr.type = [subType]; + contact[idx].push(cAddr); + } + else { + cAddr = cAddr[0]; + } + } + + return cAddr; + } + + for (var key in inRec) { + if (key === "birthyear") { + pomStr = inRec[key]; + contact.bday = new Date(pomStr.slice(0,4), pomStr.slice(4,6), + pomStr.slice(6,8)); + } + else if (key === "c") { + curRec = findSubElement("adr", ContactAddress, "work"); + curRec.adr.countryName = inRec[key]; + } + else if (key === "cn") { + contact.name = [inRec[key]]; + } + else if (key === "description") { + contact.note = [inRec[key]]; + } + else if (key === "facsimiletelephonenumber") { + curRec = findSubElement("tel", ContactTelField, "fax"); + curRec.tel.value = inRec[key]; + } + else if (key === "givenName") { + contact.givenName = [inRec[key]]; + } + else if (key === "homePhone") { + curRec = findSubElement("tel", ContactTelField, "home"); + curRec.tel.value = inRec[key]; + } + else if (key === "l") { + curRec = findSubElement("adr", ContactAddress, "work"); + curRec.adr.locality = inRec[key]; + } + else if (key === "mail") { + curRec = findSubElement("email", ContactField, "work"); + curRec.email.value = inRec[key]; + } + else if (key === "mobile") { + curRec = findSubElement("tel", ContactTelField, "mobile"); + curRec.tel.value = inRec[key]; + } + else if (key === "mozillaHomeCountryName") { + curRec = findSubElement("adr", ContactAddress, "home"); + curRec.adr.countryName = inRec[key]; + } + else if (key === "mozillaHomeLocalityName") { + curRec = findSubElement("adr", ContactAddress, "home"); + curRec.adr.locality = inRec[key]; + } + else if (key === "mozillaHomePostalCode") { + curRec = findSubElement("adr", ContactAddress, "home"); + curRec.adr.postalCode = inRec[key]; + } + else if (key === "mozillaHomeState") { + curRec = findSubElement("adr", ContactAddress, "home"); + curRec.adr.region = inRec[key]; + } + else if (key === "mozillaHomeStreet") { + curRec = findSubElement("adr", ContactAddress, "home"); + curRec.adr.streetAddress = inRec[key]; + } + else if (key === "mozillaHomeUrl") { + curRec = findSubElement("url", ContactField, "home"); + curRec.email.value = inRec[key]; + } + else if (key === "mozillaNickname") { + contact.nickname = [inRec[key]]; + } + else if (key === "o") { + contact.org = [inRec[key]]; + } + else if (key === "sn") { + contact.familyName = [inRec[key]]; + } + else if (key === "st") { + curRec = findSubElement("adr", ContactAddress, "work"); + curRec.adr.region = inRec[key]; + } + else if (key === "street") { + curRec = findSubElement("adr", ContactAddress, "work"); + curRec.adr.streetAddress = inRec[key]; + } + else if (key === "telephoneNumber") { + curRec = findSubElement("tel", ContactTelField, "work"); + curRec.tel.value = inRec[key]; + } + else if (key === "title") { + contact.jobTitle = [inRec[key]]; + } + // Unknown attribute + else { + throw new ValueError("Unknown attribute " + key + + " with value:\n" + inRec[key]); + } + } + + return contact; + } + + /** + * Not used presently and not debugged. + */ + function whenContactAlreadyPresent(rec, cb_not_found, cb_found) { + var rec_keys = Object.keys(rec); + + // Finding whether the contact has email or telephone + var email_fields = rec_keys.intersection(emailFields); + var phone_fields = rec_keys.intersection(phoneFields); + var search_keys = []; + + if (email_fields.length > 0) { + search_keys = email_fields; + } + else if (phone_fields.length > 0) { + search_keys = phone_fields; + } + + if (search_keys.length > 0) { + var search_opts = { + filterValue : rec(search_keys[0]), + filterBy : search_keys, + filterOp : "contains", + filterLimit : 1 + }; + + var search = navigator.mozContacts.find(search_opts); + + // Possible duplicate found ... bail out, rather do nothing! + search.onsuccess = function() { + // search.result is found record (Array of length 1) + cb_found(rec, search.result); + }; + + // No duplicates, go ahead and create new record + search.onerror = function() { + cb_not_found(rec); + }; + + } + } + function insertData(ldifText) { - var data = parseLDIF(ldifText.split("\n")); - console.log("loaded " + Object.keys(data).length + " records."); + var progressEl = document.querySelector("#progress-div progress"); + + var records = parseLDIF(ldifText.split("\n")); + + if (records.length > 0) { + if (!window.confirm("THIS WILL ERASE ALL CONTACTS ON YOUR PHONE!\n" + + "Are you cool with that?")) { + return false; + } + } + + var cl_req = navigator.mozContacts.clear(); + + cl_req.onerror = function() { + throw new Error("Cannot clear whole Contacts database?"); + }; + + cl_req.onsuccess = function() { + progressEl.max = records.length; + progressEl.value = 0; + + records.forEach(function (rec) { + var add_contact = translateObjectToContact(rec); + + var sav_req = navigator.mozContacts.save(add_contact); + + sav_req.onsuccess = function() { + progressEl.value += 1; + console.log("Contact added:\n" + add_contact.id); + }; + + sav_req.onerror = function() { + console.error("Cannot save record " + add_contact.id); + }; + }); + }; + } window.onload = function() { @@ -11,13 +284,9 @@ document.body.addEventListener("submit", function(evt) { var URL = document.getElementsByName("URL")[0].value; - //var login = document.getElementsByName("user-name")[0].value; - //var passwd = document.getElementsByName("password")[0].value; var progressForm = document.getElementById("progress-div"); console.log("URL = " + URL); - //console.log("login = " + login); - //console.log("passwd = " + passwd); document.getElementById("URL-form").style.display = "none"; progressForm.style.display = "block"; diff --git a/libs/parseLDIF b/libs/parseLDIF -Subproject cf2b86f588beb9e1964f773e2fbca4ca1174709 +Subproject 7f91596ca5fbe55831d587cdb746b4da595a15f |