From dface8d205962f9d5529955510d94294cf049b58 Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 20 Jun 2011 10:27:07 +0000 Subject: [PATCH] Caldav: first Task (VTODO) implementation step git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1711 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 85 +++++++++++++++++++ .../davmail/exchange/ICSBufferedWriter.java | 34 ++++++++ src/java/davmail/exchange/VCardWriter.java | 34 -------- 3 files changed, 119 insertions(+), 34 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index e5d13c82..2905889a 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -2118,6 +2118,91 @@ public abstract class ExchangeSession { } + /** + * Contact object + */ + public abstract class Task extends Item { + + /** + * @inheritDoc + */ + public Task(String folderPath, String itemName, Map properties, String etag, String noneMatch) { + super(folderPath, itemName.endsWith(".vcf") ? itemName.substring(0, itemName.length() - 3) + "EML" : itemName, etag, noneMatch); + this.putAll(properties); + } + + /** + * @inheritDoc + */ + protected Task() { + } + + /** + * Convert EML extension to ics. + * + * @return item name + */ + @Override + public String getName() { + String name = super.getName(); + if (name.endsWith(".EML")) { + name = name.substring(0, name.length() - 3) + "ics"; + } + return name; + } + + /** + * Set contact name + * + * @param name contact name + */ + public void setName(String name) { + this.itemName = name; + } + + /** + * Compute vtodo uid from name. + * + * @return uid + * @throws URIException on error + */ + protected String getUid() throws URIException { + String uid = getName(); + int dotIndex = uid.lastIndexOf('.'); + if (dotIndex > 0) { + uid = uid.substring(0, dotIndex); + } + return URIUtil.encodePath(uid); + } + + @Override + public String getContentType() { + return "text/calendar;charset=UTF-8"; + } + + + @Override + public String getBody() throws HttpException { + // build VTODO from task information + ICSBufferedWriter writer = new ICSBufferedWriter(); + writer.writeLine("BEGIN:VCALENDAR"); + writer.writeLine("VERSION:2.0"); + writer.writeLine("BEGIN:VTODO"); + + writer.appendProperty("UID", getUid()); + + writer.appendProperty("SUMMARY", get("subject")); + + writer.appendProperty("LAST-MODIFIED", get("lastmodified")); + writer.appendProperty("DTSTAMP", get("dtstamp")); + writer.appendProperty("CREATED", get("created")); + + writer.writeLine("END:VTODO"); + writer.writeLine("END:VCALENDAR"); + return writer.toString(); + } + } + /** * Calendar event object. */ diff --git a/src/java/davmail/exchange/ICSBufferedWriter.java b/src/java/davmail/exchange/ICSBufferedWriter.java index feed944c..a8980bea 100644 --- a/src/java/davmail/exchange/ICSBufferedWriter.java +++ b/src/java/davmail/exchange/ICSBufferedWriter.java @@ -84,4 +84,38 @@ public class ICSBufferedWriter { return buffer.toString(); } + /** + * Append single value property + * + * @param propertyName property name + * @param propertyValue property value + */ + public void appendProperty(String propertyName, String propertyValue) { + if ((propertyValue != null) && (propertyValue.length() > 0)) { + StringBuilder lineBuffer = new StringBuilder(); + lineBuffer.append(propertyName); + lineBuffer.append(':'); + appendMultilineEncodedValue(lineBuffer, propertyValue); + writeLine(lineBuffer.toString()); + } + + } + + /** + * Append and encode \n to \\n in value. + * + * @param buffer line buffer + * @param value value + */ + protected void appendMultilineEncodedValue(StringBuilder buffer, String value) { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '\n') { + buffer.append("\\n"); + } else { + buffer.append(value.charAt(i)); + } + } + } + } diff --git a/src/java/davmail/exchange/VCardWriter.java b/src/java/davmail/exchange/VCardWriter.java index 194cdff0..cd67b0cd 100644 --- a/src/java/davmail/exchange/VCardWriter.java +++ b/src/java/davmail/exchange/VCardWriter.java @@ -30,40 +30,6 @@ public class VCardWriter extends ICSBufferedWriter { writeLine("VERSION:3.0"); } - /** - * Append single value property - * - * @param propertyName property name - * @param propertyValue property value - */ - public void appendProperty(String propertyName, String propertyValue) { - if ((propertyValue != null) && (propertyValue.length() > 0)) { - StringBuilder lineBuffer = new StringBuilder(); - lineBuffer.append(propertyName); - lineBuffer.append(':'); - appendMultilineEncodedValue(lineBuffer, propertyValue); - writeLine(lineBuffer.toString()); - } - - } - - /** - * Append and encode \n to \\n in value. - * - * @param buffer line buffer - * @param value value - */ - protected void appendMultilineEncodedValue(StringBuilder buffer, String value) { - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - if (c == '\n') { - buffer.append("\\n"); - } else { - buffer.append(value.charAt(i)); - } - } - } - /** * Append compound value *