summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorniko <niko>2012-10-26 13:33:41 (GMT)
committerniko <niko>2012-10-26 13:33:41 (GMT)
commitcbcc1d1672fd98e4088f6274d40daf2b74eb1eac (patch)
treeb2423f8fe7b212b680bc9709a8bd6af1aab7b3a7
parentca36877b242ddafe173e348bfbf7da095d034009 (diff)
downloadsynckolab-cbcc1d1672fd98e4088f6274d40daf2b74eb1eac.tar.gz
write photo back
-rw-r--r--src/chrome/content/synckolab/addressbook.js2
-rw-r--r--src/chrome/content/synckolab/addressbookTools.js79
-rw-r--r--src/chrome/content/synckolab/tools.js19
-rw-r--r--src/chrome/content/synckolab/tools/text.js30
-rw-r--r--src/chrome/content/synckolab/wndConfig.js4
-rw-r--r--test/synckolab/parser/kolab3/xml/contact.complex.mime.xml34
-rw-r--r--test/synckolab/parser/kolab3/xml/contact.test.mime.xml2
7 files changed, 140 insertions, 30 deletions
diff --git a/src/chrome/content/synckolab/addressbook.js b/src/chrome/content/synckolab/addressbook.js
index 7ea3803..f536fe8 100644
--- a/src/chrome/content/synckolab/addressbook.js
+++ b/src/chrome/content/synckolab/addressbook.js
@@ -640,7 +640,7 @@ synckolab.AddressBook = {
if (pNameA && pNameA !== "" && pNameA !== "null")
{
// delete actual image
- var fileTo = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var fileTo = synckolab.tools.getProfileFolder();
fileTo.append("Photos");
if (!fileTo.exists()) {
fileTo.create(1, parseInt("0775", 8));
diff --git a/src/chrome/content/synckolab/addressbookTools.js b/src/chrome/content/synckolab/addressbookTools.js
index c7443d8..8ba7bf8 100644
--- a/src/chrome/content/synckolab/addressbookTools.js
+++ b/src/chrome/content/synckolab/addressbookTools.js
@@ -1547,7 +1547,7 @@ synckolab.addressbookTools.copyImage = function (newName) {
return false;
}
- var fileTo = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var fileTo = synckolab.tools.getProfileFolder();
fileTo.append("Photos");
if (!fileTo.exists()) {
fileTo.create(1, parseInt("0775", 8));
@@ -1686,21 +1686,28 @@ synckolab.addressbookTools.card2Kolab3 = function (card, skipHeader, fields) {
var adate;
if (this.haveCardProperty(card, "BirthYear") && this.haveCardProperty(card, "BirthMonth") && this.haveCardProperty(card, "BirthDay")) {
- adate = this.getCardProperty(card, "BirthYear") + this.getCardProperty(card, "BirthMonth") + this.getCardProperty(card, "BirthDay");
- xml += " <bday>" + synckolab.tools.text.nodeWithContent("date-time", adate, false) + "</bday>\n";
+ adate = synckolab.tools.text.splitDate2String (card, "Birth", true);
+ xml += " <bday>" + synckolab.tools.text.nodeWithContent("date-time", adate + "T000000", false) + "</bday>\n";
}
if (this.haveCardProperty(card, "AnniversaryYear") && this.haveCardProperty(card, "AnniversaryMonth") && this.haveCardProperty(card, "AnniversaryDay")) {
- adate = this.getCardProperty(card, "AnniversaryYear") + "-" + this.getCardProperty(card, "AnniversaryMonth") + "-" + this.getCardProperty(card, "AnniversaryDay");
- xml += " <anniversary>" + synckolab.tools.text.nodeWithContent("date-time", adate, false) + "</anniversary>\n";
+ adate = synckolab.tools.text.splitDate2String (card, "Anniversary", true);
+ xml += " <anniversary>" + synckolab.tools.text.nodeWithContent("date", adate, false) + "</anniversary>\n";
}
// TODO photo name = photo - base64-Encoded
+ var ptype = this.getCardProperty(card, "PhotoType");
+ if(ptype === "inline") {
+ var fileName = this.getCardProperty(card, "PhotoName");
+ // based on the name - get the MIME
+ ptype = synckolab.tools.file.getMimeType(fileName.substring(fileName.lastIndexOf(".")+1));
+ xml += " <photo>\n <uri>data:" + ptype + ";base64," + synckolab.tools.text.splitInto(this.getCardProperty(card, "PhotoData"), 72) +
+ "</uri>\n </photo>";
+ }
/*
xml += synckolab.tools.text.nodeWithContent("picture", this.getCardProperty(card, "PhotoName"), false);
// we can probably ignore that
- var ptype = this.getCardProperty(card, "PhotoType");
if (ptype === "web" || ptype === "file") {
* kolab:
* 1. read the file: FILENAME = this.getCardProperty(card, "PhotoName")
@@ -1840,8 +1847,7 @@ synckolab.addressbookTools.card2Pojo = function (card, uid, fields) {
"AnniversaryYear", "AnniversaryMonth", "AnniversaryDay", "HomeAddress",
"HomeAddress2", "HomeCity", "HomeState", "HomeZipCode", "HomeCountry", "WorkAddress",
"WorkAddress2", "WorkCity", "WorkState", "WorkZipCode", "WorkCountry",
- "PhotoName", "PhotoType", "PhotoURI", "Notes", "Department",
- "WebPage1", "WebPage2", "WebPage3",
+ "Notes", "Department", "WebPage1", "WebPage2", "WebPage3",
"AimScreenName", "Custom1", "Custom2", "Custom3", "Custom4", "AllowRemoteContent", "PreferMailFormat",
"Profesion"];
var i;
@@ -1850,6 +1856,56 @@ synckolab.addressbookTools.card2Pojo = function (card, uid, fields) {
}
this.setUID(pojo, uid);
+
+ // handle photo
+ var uri = this.getCardProperty(card, "PhotoURI");
+ if(uri) {
+ // we got a local file
+ if(uri.indexOf("file:") === 0) {
+ /*
+ * 1. read the file: FILENAME = this.getCardProperty(card, "PhotoName")
+ * found in ~profil/Photos/FILENAME
+ * 2. create an attachment name FILENAME with the content (base64 encoded)
+ */
+
+ // image is the last part of the uri
+ var image = uri.substring(uri.lastIndexOf("/"));
+
+ var file = synckolab.tools.getProfileFolder();
+ try {
+ file.append("Photos");
+ if (!file.exists()) {
+ file.create(1, parseInt("0775", 8));
+ }
+
+ // fix newName: we can have C:\ - file:// and more - remove all that and put it in the photos folder
+ var imageName = image.replace(/[^A-Za-z0-9._ \-]/g, "");
+ imageName = imageName.replace(/ /g, "_");
+
+ file.append(imageName);
+ // file actually exists - we can try to read it and attach it
+ var fileContent = this.readFileIntoBase64(file);
+
+ // if we have a file - put it in
+ if (fileContent !== null) {
+ this.setCardProperty("PhotoType", "inline");
+ this.setCardProperty("PhotoData", fileContent);
+ // last part of the path is the name
+ this.setCardProperty("PhotoName", uri.substring(uri.lastIndexOf("/")));
+ }
+ }
+ catch (ex)
+ {
+ this.logMessage("Unable to read image: "+image+"\n" + ex, synckolab.global.LOG_WARNING);
+ return null;
+ }
+ } else {
+ this.setCardProperty("PhotoType", "web");
+ this.setCardProperty("PhotoData", uri);
+ }
+ }
+
+
/*
case "PHOTO": // kolab3
// handle photo VERY special... TODO
@@ -2030,12 +2086,7 @@ synckolab.addressbookTools.card2Xml = function (card, fields) {
// we can probably ignore that
var ptype = this.getCardProperty(card, "PhotoType");
- if (ptype === "web" || ptype === "file") {
- /* kolab:
- * 1. read the file: FILENAME = this.getCardProperty(card, "PhotoName")
- * found in ~profil/Photos/FILENAME
- * 2. create an attachment name FILENAME with the content (base64 encoded)
- */
+ if (ptype === "web") {
xml += synckolab.tools.text.nodeWithContent("picture-uri", this.getCardProperty(card, "PhotoURI"), false); // we can distinguish between file: and http: anyways
}
diff --git a/src/chrome/content/synckolab/tools.js b/src/chrome/content/synckolab/tools.js
index d4c4e9c..4b01825 100644
--- a/src/chrome/content/synckolab/tools.js
+++ b/src/chrome/content/synckolab/tools.js
@@ -540,6 +540,13 @@ stripMailHeader: function (skcontent) {
},
/**
+ * get the profile folder object
+ */
+getProfileFolder: function () {
+ return Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+},
+
+/**
* Create a message to be stored on the Kolab server
*
* @param cid the id of the card/event
@@ -548,7 +555,7 @@ stripMailHeader: function (skcontent) {
* @param part true if this is a multipart message
* @param content the content for the message
* @param hr human Readable Part (optional)
- * @param profileimage optional image attachment (the name of the image - it always resides in profile/Photos/XXX.jpg!)
+ * @param image optional image attachment (the name of the image - it always resides in profile/Photos/XXX.jpg!)
*/
generateMail: function (cid, mail, adsubject, mime, part, skcontent, hr, image){
// sometime we just do not want a new message :)
@@ -640,7 +647,7 @@ generateMail: function (cid, mail, adsubject, mime, part, skcontent, hr, image){
// if we have an image try to read it and create a new part (ONLY for xml)
if (part && image) {
- var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var file = this.getProfileFolder();
try {
file.append("Photos");
if (!file.exists()) {
@@ -656,7 +663,7 @@ generateMail: function (cid, mail, adsubject, mime, part, skcontent, hr, image){
var fileContent = this.readFileIntoBase64(file);
if (fileContent !== null) {
- this.logMessage("got " + fileContent.length + " bytes", synckolab.global.LOG_WARNING);
+ this.logMessage("got " + fileContent.length + " bytes", synckolab.global.LOG_INFO);
// now we got the image into fileContent - lets attach
@@ -676,7 +683,7 @@ generateMail: function (cid, mail, adsubject, mime, part, skcontent, hr, image){
}
if (part) {
- msg += '--Boundary-00='+bound+'--\n';
+ msg += '\n--Boundary-00='+bound+'--\n';
} else {
msg += '\n';
}
@@ -970,7 +977,7 @@ synckolab.tools.file = {
synckolab.tools.logMessage("syncDbFile: (" +synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "/" + config.type + "_" + config.name + "/" + id + ")", synckolab.global.LOG_ERROR);
- var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var file = synckolab.tools.getProfileFolder();
try {
file.append("synckolab");
if (!file.exists()) {
@@ -1097,7 +1104,7 @@ synckolab.tools.readSyncDBFile = function (file, direct)
*/
synckolab.tools.file.getHashDataBaseFile = function (config)
{
- var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var file = synckolab.tools.getProfileFolder();
file.append("synckolab." + synckolab.tools.text.fixNameToMiniCharset(config.serverKey) + "." + config.name + "." + config.type+".hdb");
return file;
};
diff --git a/src/chrome/content/synckolab/tools/text.js b/src/chrome/content/synckolab/tools/text.js
index 76e7dc5..2eaa683 100644
--- a/src/chrome/content/synckolab/tools/text.js
+++ b/src/chrome/content/synckolab/tools/text.js
@@ -340,6 +340,17 @@ synckolab.tools.text = {
calDateTime.jsDate = jsDate;
return calDateTime;
},
+
+
+ splitDate2String: function(obj, prefix, compact) {
+ if(!obj || !obj[prefix+ "Year"]) {
+ return null;
+ }
+ return (Number(obj[prefix+ "Year"]) < 1800 ? "20" : "") + (Number(obj[prefix+ "Year"]) < 10 ? "0" : "") + obj[prefix+ "Year"] + (compact?'':'-') +
+ (Number(obj[prefix+ "Month"]) < 10 ? "0" : "") + obj[prefix+ "Month"] + (compact?'':'-') +
+ (Number(obj[prefix+ "Day"]) < 10 ? "0" : "") + obj[prefix+ "Day"];
+
+ },
// produces: 2005-03-30
date2String : function (datetime, normal, compact) {
@@ -517,6 +528,25 @@ synckolab.tools.text = {
// final
return t.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
},
+ /**
+ * @param text text to split
+ * @param width the line width to split into
+ */
+ splitInto: function(text, width) {
+ var out = "";
+ var n = 0;
+ for (n= 0; n < text.length; )
+ {
+ if (n + width < text.length) {
+ out += text.slice(n, n+width);
+ } else {
+ out += text.slice(n);
+ }
+ out += "\n";
+ n+=width;
+ }
+ return out;
+ },
URLDecode : function (encoded) {
// Replace + with ' '
diff --git a/src/chrome/content/synckolab/wndConfig.js b/src/chrome/content/synckolab/wndConfig.js
index a29425a..e6fc711 100644
--- a/src/chrome/content/synckolab/wndConfig.js
+++ b/src/chrome/content/synckolab/wndConfig.js
@@ -1269,13 +1269,13 @@ synckolab.settings.resetConfiguration = function (account, type, config)
{
synckolab.tools.logMessage("Resetting " + account + "!", synckolab.global.LOG_INFO);
- var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ var file = synckolab.tools.getProfileFolder();
file.append("synckolab." + synckolab.tools.text.fixNameToMiniCharset(account) + "." + type + "." + config + ".hdb");
if (file.exists()) {
file.remove(true);
}
- file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
+ file = synckolab.tools.getProfileFolder();
file.append("synckolab");
if (!file.exists()) {
diff --git a/test/synckolab/parser/kolab3/xml/contact.complex.mime.xml b/test/synckolab/parser/kolab3/xml/contact.complex.mime.xml
index 4eab87d..1892809 100644
--- a/test/synckolab/parser/kolab3/xml/contact.complex.mime.xml
+++ b/test/synckolab/parser/kolab3/xml/contact.complex.mime.xml
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<vcards xmlns="urn:ietf:params:xml:ns:vcard-4.0">
<vcard>
- <uid><urn>urn:uuid:Huu5X7hfYy</urn></uid>
+ <uid><uri>urn:uuid:Huu5X7hfYy</uri></uid>
<x-kolab-version><text>3.0dev1</text></x-kolab-version>
<prodid><text>SyncKolab 3.0.0, Kolab resource</text></prodid>
<rev><timestamp>19700101T000000ZZ</timestamp></rev>
- <kind>
- <text>individual</text>
- </kind>
+ <kind><text>individual</text></kind>
<fn><text>Displayname</text></fn>
<n>
<surname>Firstname</surname>
@@ -45,11 +43,35 @@
<text>Nickname</text>
</nickname>
<bday>
- <date-time>2043</date-time>
+ <date-time>20090727T000000</date-time>
</bday>
<anniversary>
- <date-time>2009-7-28</date-time>
+ <date>20090728</date>
</anniversary>
+ <photo>
+ <uri>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAA
+Bw4pVUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAOTQAADpwB3vacVwAABAVJREFUeJztn
+cFrFGcYxt/szLrJmk2yWTeojQktQluiqNFeKglSQmsOHhRyUApKPbeHQC8taKl4E0Hw4sGT
+oAcvgojVQCg0B+2ph0DprTlYs7SQsNlkdzKzmf4DzxvYsCRP9Pkdn5n53iG/fPDy7TczZkI
+IIYQQov107PQNtI0vrR/mOfveveap/eAcSXeqRsYdSOwIEkKGhJAhIWRICBkSQsbua3u/sl
+EUj346OoPyUrGEW1Uzm5mfycMDNRtB8ciHIy9RXswXi16NuYU5XOOx1VGsGUKGhJAhIWRIC
+BkSQka40zfgcs6uoHjis4l7KB8fG8+ifPa3Wb9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P4TXAAAAABJRU5ErkJggg</uri>
+ </photo>
<tel>
<parameters><type><text>home</text></type></parameters>
<text>+4930-homephone</text>
diff --git a/test/synckolab/parser/kolab3/xml/contact.test.mime.xml b/test/synckolab/parser/kolab3/xml/contact.test.mime.xml
index 9c5370d..19e7b22 100644
--- a/test/synckolab/parser/kolab3/xml/contact.test.mime.xml
+++ b/test/synckolab/parser/kolab3/xml/contact.test.mime.xml
@@ -18,7 +18,7 @@
notes
with empty</text>
</note>
- <bday> <date-time>2042</date-time>
+ <bday> <date-time>20121020T000000</date-time>
</bday>
<tel>
<parameters><type><text>home</text></type></parameters>