mirror of
https://github.com/moparisthebest/davmail
synced 2025-03-03 18:59:53 -05:00
Caldav: implement task support over EWS
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1736 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
6ce33f23b9
commit
7036f3fd51
@ -93,6 +93,7 @@ public abstract class ExchangeSession {
|
||||
|
||||
protected static final String PUBLIC_ROOT = "/public/";
|
||||
protected static final String CALENDAR = "calendar";
|
||||
protected static final String TASKS = "tasks";
|
||||
/**
|
||||
* Contacts folder logical name
|
||||
*/
|
||||
@ -2602,6 +2603,20 @@ public abstract class ExchangeSession {
|
||||
* @throws IOException on error
|
||||
*/
|
||||
public List<Event> getAllEvents(String folderPath) throws IOException {
|
||||
boolean caldavDisableTasks = Settings.getBooleanProperty("davmail.caldavDisableTasks");
|
||||
List<Event> results = searchEvents(folderPath, getCalendarItemCondition(caldavDisableTasks, getPastDelayCondition()));
|
||||
|
||||
if (isMainCalendar(folderPath)) {
|
||||
// retrieve tasks from main tasks folder
|
||||
results.addAll(searchTasksOnly("tasks"));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
protected abstract Condition getCalendarItemCondition(boolean excludeTasks, Condition dateCondition);
|
||||
|
||||
protected Condition getPastDelayCondition() throws IOException {
|
||||
int caldavPastDelay = Settings.getIntProperty("davmail.caldavPastDelay");
|
||||
Condition dateCondition = null;
|
||||
if (caldavPastDelay != 0) {
|
||||
@ -2609,15 +2624,9 @@ public abstract class ExchangeSession {
|
||||
cal.add(Calendar.DAY_OF_MONTH, -caldavPastDelay);
|
||||
dateCondition = gt("dtstart", formatSearchDate(cal.getTime()));
|
||||
}
|
||||
|
||||
boolean caldavDisableTasks = Settings.getBooleanProperty("davmail.caldavDisableTasks");
|
||||
Condition condition = getCalendarItemCondition(caldavDisableTasks, dateCondition);
|
||||
|
||||
return searchEvents(folderPath, condition);
|
||||
return dateCondition;
|
||||
}
|
||||
|
||||
protected abstract Condition getCalendarItemCondition(boolean excludeTasks, Condition dateCondition);
|
||||
|
||||
protected Condition getRangeCondition(String timeRangeStart, String timeRangeEnd) throws IOException {
|
||||
try {
|
||||
SimpleDateFormat parser = getZuluDateFormat();
|
||||
@ -3042,6 +3051,14 @@ public abstract class ExchangeSession {
|
||||
*/
|
||||
public abstract boolean isSharedFolder(String folderPath);
|
||||
|
||||
/**
|
||||
* Test if folderPath is main calendar.
|
||||
*
|
||||
* @param folderPath absolute folder path
|
||||
* @return true if folderPath is a public or shared folder
|
||||
*/
|
||||
public abstract boolean isMainCalendar(String folderPath);
|
||||
|
||||
static final String MAILBOX_BASE = "/cn=";
|
||||
|
||||
protected void getEmailAndAliasFromOptions() {
|
||||
|
@ -91,7 +91,7 @@ public class VCalendar extends VObject {
|
||||
@Override
|
||||
public void addVObject(VObject vObject) {
|
||||
super.addVObject(vObject);
|
||||
if (firstVevent == null && "VEVENT".equals(vObject.type)) {
|
||||
if (firstVevent == null && ("VEVENT".equals(vObject.type)|| "VTODO".equals(vObject.type))) {
|
||||
firstVevent = vObject;
|
||||
}
|
||||
if ("VTIMEZONE".equals(vObject.type)) {
|
||||
@ -536,6 +536,10 @@ public class VCalendar extends VObject {
|
||||
firstVevent.addProperty(vProperty);
|
||||
}
|
||||
|
||||
public boolean isTodo() {
|
||||
return "VTODO".equals(firstVevent.type);
|
||||
}
|
||||
|
||||
/**
|
||||
* VCalendar recipients for notifications
|
||||
*/
|
||||
|
@ -203,6 +203,17 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
return !getFolderPath(folderPath).toLowerCase().startsWith(mailPath.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if folderPath is main calendar.
|
||||
*
|
||||
* @param folderPath absolute folder path
|
||||
* @return true if folderPath is a public or shared folder
|
||||
*/
|
||||
@Override
|
||||
public boolean isMainCalendar(String folderPath) {
|
||||
return getFolderPath(folderPath).toLowerCase().equals(getFolderPath("calendar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build base path for cmd commands (galfind, gallookup).
|
||||
*
|
||||
|
@ -1180,7 +1180,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
|
||||
@Override
|
||||
public ItemResult createOrUpdate() throws IOException {
|
||||
byte[] itemContent = Base64.encodeBase64(vCalendar.toString().getBytes("UTF-8"));
|
||||
if (vCalendar.isTodo() && isMainCalendar(folderPath)) {
|
||||
// task item, move to tasks folder
|
||||
folderPath = "tasks";
|
||||
}
|
||||
|
||||
ItemResult itemResult = new ItemResult();
|
||||
EWSMethod createOrUpdateItemMethod;
|
||||
@ -1207,6 +1210,29 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
return itemResult;
|
||||
}
|
||||
}
|
||||
if (vCalendar.isTodo()) {
|
||||
// create or update task method
|
||||
EWSMethod.Item newItem = new EWSMethod.Item();
|
||||
newItem.type = "Task";
|
||||
List<FieldUpdate> updates = new ArrayList<FieldUpdate>();
|
||||
updates.add(Field.createFieldUpdate("calendaruid", vCalendar.getFirstVeventPropertyValue("UID")));
|
||||
// force urlcompname
|
||||
updates.add(Field.createFieldUpdate("urlcompname", convertItemNameToEML(itemName)));
|
||||
updates.add(Field.createFieldUpdate("subject", vCalendar.getFirstVeventPropertyValue("SUMMARY")));
|
||||
updates.add(Field.createFieldUpdate("description", vCalendar.getFirstVeventPropertyValue("DESCRIPTION")));
|
||||
if (currentItemId != null) {
|
||||
// update
|
||||
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
|
||||
ConflictResolution.AutoResolve,
|
||||
SendMeetingInvitationsOrCancellations.SendToNone,
|
||||
currentItemId, updates);
|
||||
} else {
|
||||
newItem.setFieldUpdates(updates);
|
||||
// create
|
||||
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, SendMeetingInvitations.SendToNone, getFolderId(folderPath), newItem);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if (currentItemId != null) {
|
||||
/*Set<FieldUpdate> updates = new HashSet<FieldUpdate>();
|
||||
@ -1224,7 +1250,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
// create
|
||||
EWSMethod.Item newItem = new EWSMethod.Item();
|
||||
newItem.type = "CalendarItem";
|
||||
newItem.mimeContent = itemContent;
|
||||
newItem.mimeContent = Base64.encodeBase64(vCalendar.toString().getBytes("UTF-8"));
|
||||
ArrayList<FieldUpdate> updates = new ArrayList<FieldUpdate>();
|
||||
if (!vCalendar.hasVAlarm()) {
|
||||
updates.add(Field.createFieldUpdate("reminderset", "false"));
|
||||
@ -1282,7 +1308,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
newItem.setFieldUpdates(updates);
|
||||
createOrUpdateItemMethod = new CreateItemMethod(MessageDisposition.SaveOnly, SendMeetingInvitations.SendToNone, getFolderId(folderPath), newItem);
|
||||
//}
|
||||
|
||||
}
|
||||
executeMethod(createOrUpdateItemMethod);
|
||||
|
||||
itemResult.status = createOrUpdateItemMethod.getStatusCode();
|
||||
@ -1290,7 +1316,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
//noinspection VariableNotUsedInsideIf
|
||||
if (currentItemId == null) {
|
||||
itemResult.status = HttpStatus.SC_CREATED;
|
||||
LOGGER.debug("Updated event " + getHref());
|
||||
LOGGER.debug("Created event " + getHref());
|
||||
} else {
|
||||
LOGGER.warn("Overwritten event " + getHref());
|
||||
}
|
||||
@ -1313,8 +1339,16 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
LOGGER.debug("Get event: " + itemName);
|
||||
}
|
||||
try {
|
||||
GetItemMethod getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true);
|
||||
if (!"Message".equals(type)) {
|
||||
GetItemMethod getItemMethod;
|
||||
if ("Task".equals(type)) {
|
||||
getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false);
|
||||
getItemMethod.addAdditionalProperty(Field.get("subject"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("created"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("lastmodified"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("calendaruid"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("description"));
|
||||
} else if (!"Message".equals(type)) {
|
||||
getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true);
|
||||
getItemMethod.addAdditionalProperty(Field.get("reminderset"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("calendaruid"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("requiredattendees"));
|
||||
@ -1323,9 +1357,28 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
getItemMethod.addAdditionalProperty(Field.get("xmozlastack"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("xmozsnoozetime"));
|
||||
getItemMethod.addAdditionalProperty(Field.get("xmozsendinvitations"));
|
||||
} else {
|
||||
getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, true);
|
||||
}
|
||||
|
||||
executeMethod(getItemMethod);
|
||||
if ("Task".equals(type)) {
|
||||
VCalendar localVCalendar = new VCalendar();
|
||||
VObject vTodo = new VObject();
|
||||
vTodo.type = "VTODO";
|
||||
vTodo.setPropertyValue("LAST-MODIFIED", convertDateFromExchange(getItemMethod.getResponseItem().get(Field.get("lastmodified").getResponseName())));
|
||||
vTodo.setPropertyValue("CREATED", convertDateFromExchange(getItemMethod.getResponseItem().get(Field.get("created").getResponseName())));
|
||||
String calendarUid = getItemMethod.getResponseItem().get(Field.get("calendaruid").getResponseName());
|
||||
if (calendarUid == null) {
|
||||
// use item id as uid for Exchange created tasks
|
||||
calendarUid = itemId.id;
|
||||
}
|
||||
vTodo.setPropertyValue("UID", calendarUid);
|
||||
vTodo.setPropertyValue("SUMMARY", getItemMethod.getResponseItem().get(Field.get("subject").getResponseName()));
|
||||
vTodo.setPropertyValue("DESCRIPTION", getItemMethod.getResponseItem().get(Field.get("description").getResponseName()));
|
||||
localVCalendar.addVObject(vTodo);
|
||||
content = localVCalendar.toString().getBytes("UTF-8");
|
||||
} else {
|
||||
content = getItemMethod.getMimeContent();
|
||||
if (content == null) {
|
||||
throw new IOException("empty event body");
|
||||
@ -1383,7 +1436,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
// overwrite method
|
||||
// localVCalendar.setPropertyValue("METHOD", "REQUEST");
|
||||
content = localVCalendar.toString().getBytes("UTF-8");
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw buildHttpException(e);
|
||||
} catch (MessagingException e) {
|
||||
@ -1428,6 +1481,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
for (EWSMethod.Item response : responses) {
|
||||
Event event = new Event(response);
|
||||
if ("Message".equals(event.type)) {
|
||||
// TODO: just exclude
|
||||
// need to check body
|
||||
try {
|
||||
event.getEventContent();
|
||||
@ -1489,6 +1543,11 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
@Override
|
||||
public Item getItem(String folderPath, String itemName) throws IOException {
|
||||
EWSMethod.Item item = getEwsItem(folderPath, itemName);
|
||||
if (item == null && isMainCalendar(folderPath)) {
|
||||
// look for item in task folder
|
||||
item = getEwsItem("tasks", itemName);
|
||||
}
|
||||
|
||||
if (item == null) {
|
||||
throw new HttpNotFoundException(itemName + " not found in " + folderPath);
|
||||
}
|
||||
@ -1509,6 +1568,7 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
return new Contact(item);
|
||||
} else if ("CalendarItem".equals(itemType)
|
||||
|| "MeetingRequest".equals(itemType)
|
||||
|| "Task".equals(itemType)
|
||||
// VTODOs appear as Messages
|
||||
|| "Message".equals(itemType)) {
|
||||
return new Event(item);
|
||||
@ -1600,6 +1660,11 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
return folderPath.startsWith("/") && !folderPath.toLowerCase().startsWith(currentMailboxPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMainCalendar(String folderPath) {
|
||||
return "calendar".equals(folderPath) || (currentMailboxPath + "/calendar").equals(folderPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFreeBusyData(String attendee, String start, String end, int interval) throws IOException {
|
||||
GetUserAvailabilityMethod getUserAvailabilityMethod = new GetUserAvailabilityMethod(attendee, start, end, interval);
|
||||
@ -1747,6 +1812,9 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
} else if (folderPath.startsWith(CALENDAR)) {
|
||||
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.calendar);
|
||||
folderNames = folderPath.substring(CALENDAR.length()).split("/");
|
||||
} else if (folderPath.startsWith(TASKS)) {
|
||||
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.tasks);
|
||||
folderNames = folderPath.substring(TASKS.length()).split("/");
|
||||
} else if (folderPath.startsWith(CONTACTS)) {
|
||||
currentFolderId = DistinguishedFolderId.getInstance(mailbox, DistinguishedFolderId.Name.contacts);
|
||||
folderNames = folderPath.substring(CONTACTS.length()).split("/");
|
||||
|
@ -36,6 +36,7 @@ public final class Field {
|
||||
FIELD_MAP.put("displayname", new ExtendedFieldURI(0x3001, ExtendedFieldURI.PropertyType.String));
|
||||
FIELD_MAP.put("urlcompname", new ExtendedFieldURI(0x10f3, ExtendedFieldURI.PropertyType.String));
|
||||
FIELD_MAP.put("lastmodified", new ExtendedFieldURI(0x3008, ExtendedFieldURI.PropertyType.SystemTime));
|
||||
FIELD_MAP.put("created", new ExtendedFieldURI(0x3007, ExtendedFieldURI.PropertyType.SystemTime));
|
||||
|
||||
// folder
|
||||
FIELD_MAP.put("ctag", new ExtendedFieldURI(0x670a, ExtendedFieldURI.PropertyType.SystemTime)); // PR_LOCAL_COMMIT_TIME_MAX
|
||||
|
Loading…
x
Reference in New Issue
Block a user