2007-11-13 23:38:05 +01:00
|
|
|
/* UIxMailPartICalActions.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Inverse groupe conseil
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSCalendarDate.h>
|
|
|
|
|
|
|
|
#import <NGObjWeb/WOContext+SoObjects.h>
|
|
|
|
#import <NGObjWeb/WOResponse.h>
|
|
|
|
|
2008-02-05 23:26:39 +01:00
|
|
|
#import <NGExtensions/NSNull+misc.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
2007-11-13 23:38:05 +01:00
|
|
|
#import <NGCards/iCalCalendar.h>
|
2007-11-18 11:16:25 +01:00
|
|
|
#import <NGCards/iCalEvent.h>
|
2007-11-13 23:38:05 +01:00
|
|
|
#import <NGCards/iCalPerson.h>
|
|
|
|
|
|
|
|
#import <UI/Common/WODirectAction+SOGo.h>
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
#import <NGImap4/NGImap4EnvelopeAddress.h>
|
|
|
|
|
|
|
|
#import <SoObjects/Appointments/iCalPerson+SOGo.h>
|
2007-11-13 23:38:05 +01:00
|
|
|
#import <SoObjects/Appointments/SOGoAppointmentObject.h>
|
|
|
|
#import <SoObjects/Appointments/SOGoAppointmentFolder.h>
|
2007-11-18 11:16:25 +01:00
|
|
|
#import <SoObjects/Mailer/SOGoMailObject.h>
|
2007-11-13 23:38:05 +01:00
|
|
|
#import <SoObjects/SOGo/SOGoUser.h>
|
|
|
|
#import <SoObjects/SOGo/iCalEntityObject+Utilities.h>
|
|
|
|
#import <SoObjects/Mailer/SOGoMailBodyPart.h>
|
|
|
|
|
|
|
|
#import "UIxMailPartICalActions.h"
|
|
|
|
|
|
|
|
@implementation UIxMailPartICalActions
|
|
|
|
|
|
|
|
- (iCalEvent *) _emailEvent
|
|
|
|
{
|
|
|
|
NSData *content;
|
|
|
|
NSString *eventString;
|
|
|
|
iCalCalendar *emailCalendar;
|
|
|
|
|
|
|
|
content = [[self clientObject] fetchBLOB];
|
|
|
|
eventString = [[NSString alloc] initWithData: content
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
if (!eventString)
|
|
|
|
eventString = [[NSString alloc] initWithData: content
|
|
|
|
encoding: NSISOLatin1StringEncoding];
|
|
|
|
emailCalendar = [iCalCalendar parseSingleFromSource: eventString];
|
|
|
|
|
|
|
|
return (iCalEvent *) [emailCalendar firstChildWithTag: @"vevent"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SOGoAppointmentObject *) _eventObjectWithUID: (NSString *) uid
|
2007-11-18 11:16:25 +01:00
|
|
|
forUser: (SOGoUser *) user
|
2007-11-13 23:38:05 +01:00
|
|
|
{
|
|
|
|
SOGoAppointmentFolder *personalFolder;
|
|
|
|
SOGoAppointmentObject *eventObject;
|
2007-11-19 16:39:44 +01:00
|
|
|
NSString *cname;
|
|
|
|
|
|
|
|
eventObject = nil;
|
2007-11-13 23:38:05 +01:00
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
personalFolder = [user personalCalendarFolderInContext: context];
|
2007-11-19 16:39:44 +01:00
|
|
|
cname = [personalFolder resourceNameForEventUID: uid];
|
|
|
|
if (cname)
|
|
|
|
{
|
|
|
|
eventObject = [personalFolder lookupName: cname
|
|
|
|
inContext: context acquire: NO];
|
|
|
|
if (![eventObject isKindOfClass: [SOGoAppointmentObject class]])
|
|
|
|
eventObject = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eventObject)
|
2008-08-09 10:17:13 +02:00
|
|
|
{
|
|
|
|
eventObject = [SOGoAppointmentObject objectWithName: uid
|
|
|
|
inContainer: personalFolder];
|
|
|
|
[eventObject setIsNew: YES];
|
|
|
|
}
|
|
|
|
|
2007-11-13 23:38:05 +01:00
|
|
|
return eventObject;
|
|
|
|
}
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
- (SOGoAppointmentObject *) _eventObjectWithUID: (NSString *) uid
|
|
|
|
{
|
|
|
|
return [self _eventObjectWithUID: uid forUser: [context activeUser]];
|
|
|
|
}
|
|
|
|
|
2008-02-05 23:26:39 +01:00
|
|
|
- (void) _fixOrganizerInEvent: (iCalEvent *) brokenEvent
|
|
|
|
{
|
|
|
|
iCalPerson *organizer;
|
|
|
|
SOGoMailObject *mail;
|
|
|
|
NSArray *addresses;
|
|
|
|
NGImap4EnvelopeAddress *from;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
organizer = nil;
|
|
|
|
|
|
|
|
mail = [[self clientObject] mailObject];
|
|
|
|
|
|
|
|
addresses = [mail replyToEnvelopeAddresses];
|
|
|
|
if (![addresses count])
|
|
|
|
addresses = [mail fromEnvelopeAddresses];
|
|
|
|
if ([addresses count] > 0)
|
|
|
|
{
|
|
|
|
from = [addresses objectAtIndex: 0];
|
|
|
|
value = [from baseEMail];
|
|
|
|
if ([value isNotNull])
|
|
|
|
{
|
|
|
|
organizer = [iCalPerson elementWithTag: @"organizer"];
|
|
|
|
[organizer setEmail: value];
|
|
|
|
value = [from personalName];
|
|
|
|
if ([value isNotNull])
|
|
|
|
[organizer setCn: value];
|
|
|
|
[brokenEvent setOrganizer: organizer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!organizer)
|
|
|
|
[self errorWithFormat: @"no organizer could be found, SOGo will crash"
|
|
|
|
@" if the user replies"];
|
|
|
|
}
|
|
|
|
|
2007-11-13 23:38:05 +01:00
|
|
|
- (iCalEvent *)
|
|
|
|
_setupChosenEventAndEventObject: (SOGoAppointmentObject **) eventObject
|
|
|
|
{
|
|
|
|
iCalEvent *emailEvent, *calendarEvent, *chosenEvent;
|
2008-02-05 23:26:39 +01:00
|
|
|
iCalPerson *organizer;
|
2007-11-13 23:38:05 +01:00
|
|
|
|
|
|
|
emailEvent = [self _emailEvent];
|
|
|
|
if (emailEvent)
|
|
|
|
{
|
|
|
|
*eventObject = [self _eventObjectWithUID: [emailEvent uid]];
|
|
|
|
if ([*eventObject isNew])
|
|
|
|
chosenEvent = emailEvent;
|
|
|
|
else
|
|
|
|
{
|
2007-11-19 16:39:44 +01:00
|
|
|
calendarEvent = (iCalEvent *) [*eventObject component: NO
|
|
|
|
secure: NO];
|
2007-11-13 23:38:05 +01:00
|
|
|
if ([calendarEvent compare: emailEvent] == NSOrderedAscending)
|
|
|
|
chosenEvent = emailEvent;
|
|
|
|
else
|
|
|
|
chosenEvent = calendarEvent;
|
|
|
|
}
|
2008-02-05 23:26:39 +01:00
|
|
|
|
|
|
|
organizer = [chosenEvent organizer];
|
|
|
|
if (![[organizer rfc822Email] length])
|
|
|
|
[self _fixOrganizerInEvent: chosenEvent];
|
2007-11-13 23:38:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
chosenEvent = nil;
|
|
|
|
|
|
|
|
return chosenEvent;
|
|
|
|
}
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
#warning this is code copied from SOGoAppointmentObject...
|
|
|
|
- (void) _updateAttendee: (iCalPerson *) attendee
|
|
|
|
withSequence: (NSNumber *) sequence
|
|
|
|
andCalUID: (NSString *) calUID
|
|
|
|
forUID: (NSString *) uid
|
|
|
|
{
|
|
|
|
SOGoAppointmentObject *eventObject;
|
|
|
|
iCalEvent *event;
|
|
|
|
iCalPerson *otherAttendee;
|
|
|
|
NSString *iCalString;
|
|
|
|
|
|
|
|
eventObject = [self _eventObjectWithUID: calUID
|
|
|
|
forUser: [SOGoUser userWithLogin: uid roles: nil]];
|
|
|
|
if (![eventObject isNew])
|
|
|
|
{
|
|
|
|
event = [eventObject component: NO secure: NO];
|
|
|
|
if ([[event sequence] compare: sequence]
|
|
|
|
== NSOrderedSame)
|
|
|
|
{
|
|
|
|
otherAttendee
|
|
|
|
= [event findParticipantWithEmail: [attendee rfc822Email]];
|
|
|
|
[otherAttendee setPartStat: [attendee partStat]];
|
|
|
|
iCalString = [[event parent] versitString];
|
|
|
|
[eventObject saveContentString: iCalString];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-13 23:38:05 +01:00
|
|
|
- (WOResponse *) _changePartStatusAction: (NSString *) newStatus
|
|
|
|
{
|
|
|
|
WOResponse *response;
|
|
|
|
SOGoAppointmentObject *eventObject;
|
|
|
|
iCalEvent *chosenEvent;
|
|
|
|
iCalPerson *user;
|
2007-11-18 11:16:25 +01:00
|
|
|
iCalCalendar *emailCalendar, *calendar;
|
|
|
|
NSString *rsvp, *method, *organizerUID;
|
2007-11-13 23:38:05 +01:00
|
|
|
|
|
|
|
chosenEvent = [self _setupChosenEventAndEventObject: &eventObject];
|
|
|
|
if (chosenEvent)
|
|
|
|
{
|
|
|
|
user = [chosenEvent findParticipant: [context activeUser]];
|
|
|
|
[user setPartStat: newStatus];
|
|
|
|
calendar = [chosenEvent parent];
|
2007-11-18 11:16:25 +01:00
|
|
|
emailCalendar = [[self _emailEvent] parent];
|
|
|
|
method = [[emailCalendar method] lowercaseString];
|
2007-11-13 23:38:05 +01:00
|
|
|
if ([method isEqualToString: @"request"])
|
|
|
|
{
|
|
|
|
[calendar setMethod: @""];
|
|
|
|
rsvp = [[user rsvp] lowercaseString];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rsvp = nil;
|
|
|
|
[eventObject saveContentString: [calendar versitString]];
|
2007-11-18 11:16:25 +01:00
|
|
|
if ([rsvp isEqualToString: @"true"])
|
2008-07-10 17:11:01 +02:00
|
|
|
[eventObject sendResponseToOrganizer];
|
2007-11-18 11:16:25 +01:00
|
|
|
organizerUID = [[chosenEvent organizer] uid];
|
|
|
|
if (organizerUID)
|
|
|
|
[self _updateAttendee: user withSequence: [chosenEvent sequence]
|
|
|
|
andCalUID: [chosenEvent uid] forUID: organizerUID];
|
2007-11-13 23:38:05 +01:00
|
|
|
response = [self responseWith204];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = [context response];
|
|
|
|
[response setStatus: 409];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) acceptAction
|
|
|
|
{
|
|
|
|
return [self _changePartStatusAction: @"ACCEPTED"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) declineAction
|
|
|
|
{
|
|
|
|
return [self _changePartStatusAction: @"DECLINED"];
|
|
|
|
}
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
- (WOResponse *) deleteFromCalendarAction
|
|
|
|
{
|
|
|
|
iCalEvent *emailEvent;
|
|
|
|
SOGoAppointmentObject *eventObject;
|
|
|
|
WOResponse *response;
|
|
|
|
|
|
|
|
emailEvent = [self _emailEvent];
|
|
|
|
if (emailEvent)
|
|
|
|
{
|
|
|
|
eventObject = [self _eventObjectWithUID: [emailEvent uid]];
|
2007-11-19 16:39:44 +01:00
|
|
|
[eventObject delete];
|
2007-11-18 11:16:25 +01:00
|
|
|
response = [self responseWith204];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = [context response];
|
|
|
|
[response setStatus: 409];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (iCalPerson *) _emailParticipantWithEvent: (iCalEvent *) event
|
|
|
|
{
|
|
|
|
NSString *emailFrom;
|
|
|
|
SOGoMailObject *mailObject;
|
|
|
|
NGImap4EnvelopeAddress *address;
|
|
|
|
|
|
|
|
mailObject = [[self clientObject] mailObject];
|
|
|
|
address = [[mailObject fromEnvelopeAddresses] objectAtIndex: 0];
|
|
|
|
emailFrom = [address baseEMail];
|
|
|
|
|
|
|
|
return [event findParticipantWithEmail: emailFrom];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) _updateParticipantStatusInEvent: (iCalEvent *) calendarEvent
|
|
|
|
fromEvent: (iCalEvent *) emailEvent
|
|
|
|
inObject: (SOGoAppointmentObject *) eventObject
|
|
|
|
{
|
|
|
|
iCalPerson *calendarParticipant, *mailParticipant;
|
|
|
|
NSString *partStat;
|
|
|
|
BOOL result;
|
|
|
|
|
|
|
|
calendarParticipant = [self _emailParticipantWithEvent: calendarEvent];
|
|
|
|
mailParticipant = [self _emailParticipantWithEvent: emailEvent];
|
|
|
|
if (calendarParticipant && mailParticipant)
|
|
|
|
{
|
|
|
|
result = YES;
|
|
|
|
partStat = [mailParticipant partStat];
|
|
|
|
if ([partStat caseInsensitiveCompare: [calendarParticipant partStat]]
|
|
|
|
!= NSOrderedSame)
|
|
|
|
{
|
|
|
|
[calendarParticipant setPartStat: [partStat uppercaseString]];
|
|
|
|
[eventObject saveComponent: calendarEvent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = NO;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WOResponse *) updateUserStatusAction
|
|
|
|
{
|
|
|
|
iCalEvent *emailEvent, *calendarEvent;
|
|
|
|
SOGoAppointmentObject *eventObject;
|
|
|
|
WOResponse *response;
|
|
|
|
|
|
|
|
response = nil;
|
|
|
|
|
|
|
|
emailEvent = [self _emailEvent];
|
|
|
|
if (emailEvent)
|
|
|
|
{
|
|
|
|
eventObject = [self _eventObjectWithUID: [emailEvent uid]];
|
|
|
|
calendarEvent = [eventObject component: NO secure: NO];
|
|
|
|
if (([[emailEvent sequence] compare: [calendarEvent sequence]]
|
2008-06-02 22:05:38 +02:00
|
|
|
!= NSOrderedAscending)
|
2007-11-18 11:16:25 +01:00
|
|
|
&& ([self _updateParticipantStatusInEvent: calendarEvent
|
|
|
|
fromEvent: emailEvent
|
|
|
|
inObject: eventObject]))
|
|
|
|
response = [self responseWith204];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response)
|
|
|
|
{
|
|
|
|
response = [context response];
|
|
|
|
[response setStatus: 409];
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2007-11-13 23:38:05 +01:00
|
|
|
// - (WOResponse *) markTentativeAction
|
|
|
|
// {
|
|
|
|
// return [self _changePartStatusAction: @"TENTATIVE"];
|
|
|
|
// }
|
|
|
|
|
|
|
|
// - (WOResponse *) addToCalendarAction
|
|
|
|
// {
|
|
|
|
// return [self responseWithStatus: 404];
|
|
|
|
// }
|
|
|
|
|
|
|
|
// - (WOResponse *) deleteFromCalendarAction
|
|
|
|
// {
|
|
|
|
// return [self responseWithStatus: 404];
|
|
|
|
// }
|
|
|
|
|
|
|
|
@end
|