From 36e39eb7e12c67acbdca4f45316e079c69a35588 Mon Sep 17 00:00:00 2001
From: root <root@example.com>
Date: Mon, 24 Mar 2014 21:57:23 +0100
Subject: [PATCH] basic reminder

---
 ActiveSync/iCalEvent+ActiveSync.m |   65 +++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/ActiveSync/iCalEvent+ActiveSync.m b/ActiveSync/iCalEvent+ActiveSync.m
index 05f18a3..df7a78b 100644
--- a/ActiveSync/iCalEvent+ActiveSync.m
+++ b/ActiveSync/iCalEvent+ActiveSync.m
@@ -55,6 +55,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "NSDate+ActiveSync.h"
 #include "NSString+ActiveSync.h"
 
+#import <NGCards/iCalAlarm.h>
+
+
 @implementation iCalEvent (ActiveSync)
 
 - (int) _attendeeStatus: (iCalPerson *) attendee
@@ -210,6 +213,26 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
   // Reminder -- http://msdn.microsoft.com/en-us/library/ee219691(v=exchg.80).aspx
   // TODO
+  if ([self hasAlarms] ) {
+    NSLog(@"tfu reminder hasAlarm=True");
+    NSLog(@"tfu reminder startDate %@", [[self startDate] activeSyncRepresentationWithoutSeparatorsInContext: context]);
+
+    iCalAlarm *anAlarm;
+    NSCalendarDate  *nextAlarmDate;
+    NSInteger delta;
+	
+    anAlarm = [[self alarms] objectAtIndex: 0];
+    if ([[anAlarm action] caseInsensitiveCompare: @"DISPLAY"] == NSOrderedSame)  {
+          nextAlarmDate = [anAlarm nextAlarmDate];
+          delta = (int)(([[self startDate] timeIntervalSince1970] - [nextAlarmDate timeIntervalSince1970]  )/60);
+
+          NSLog(@"tfu reminder nextAlarmDate %@", [nextAlarmDate activeSyncRepresentationWithoutSeparatorsInContext: context]);
+          NSLog(@"tfu reminder Reminder %d",   delta);
+          // don't send negative reminder - not supported
+          if (delta >0)
+              [s appendFormat: @"<Reminder xmlns=\"Calendar:\">%d</Reminder>", delta];
+    }
+  }
 
   // Recurrence rules
   if ([self isRecurrent])
@@ -390,6 +413,48 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
         }
     }
 
+  // tfu if an alarm is deinfed with an action != DISPLAY ignore the alarm - don't want to overwrite 
+  if ([self hasAlarms] && [[[[self alarms] objectAtIndex: 0] action] caseInsensitiveCompare: @"DISPLAY"] != NSOrderedSame)  {
+     NSLog(@"tfu reminder received hasAlarms != DISPLAY  ignore client");
+  }
+  else {
+       // tfu add an alarm specified in msg
+       if ((o = [theValues objectForKey: @"Reminder"])) {
+           NSLog(@"tfu reminder received %@", o);
+           
+           // tfu NOTE: outlook sends a 15 min reminder (18 hour for allday) if  no reminder is specified  
+           // although no default reminder is defined (File -> Options -> Clendar -> Calendar Options - > Default Reminders)
+           // http://answers.microsoft.com/en-us/office/forum/office_2013_release-outlook/desktop-outlook-calendar-creates-entries-with/9aef72d8-81bb-4a32-a6ab-bf7d216fb811?page=5&tm=1395690285088 
+
+	   // outlook: if reminder is set to 0 minutes before start save it as 1 minute since -> 0 minutes in not accepted by sogo
+           if ([o isEqualToString: @"0"]) 
+		o=@"1";
+
+           iCalAlarm  *alarm = nil;
+           iCalTrigger *trigger;
+
+           alarm = [iCalAlarm new];
+           trigger = [iCalTrigger elementWithTag: @"TRIGGER"];
+           [trigger setValueType: @"DURATION"];
+           [alarm setTrigger: trigger];
+           [alarm setAction: @"DISPLAY"];
+           [trigger setSingleValue: [NSString stringWithFormat: @"-PT%@M", o] forKey: @""];
+
+          
+           [self  removeAllAlarms];
+
+           [self addToAlarms: alarm];
+           [alarm release];
+           //}
+        } else {
+           // tfu remove existing alarm since no reminder in msg
+           NSLog(@"tfu reminder removeAllAlarms");
+	   [self  removeAllAlarms];
+        }
+
+   }
+
+
   // Recurrence
   if ((o = [theValues objectForKey: @"Recurrence"]))
     {
-- 
1.7.9.5

