1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
/* UIxAppointmentEditor.m - this file is part of SOGo
*
* Copyright (C) 2007-2014 Inverse inc.
*
* 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.
*/
#include <math.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSTimeZone.h>
#import <Foundation/NSValue.h>
#import <NGObjWeb/SoObject.h>
#import <NGObjWeb/SoPermissions.h>
#import <NGObjWeb/SoSecurityManager.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/WOResponse.h>
#import <NGObjWeb/NSException+HTTP.h>
#import <NGExtensions/NSCalendarDate+misc.h>
#import <NGExtensions/NGCalendarDateRange.h>
#import <NGExtensions/NSString+misc.h>
#import <NGCards/iCalAlarm.h>
#import <NGCards/iCalCalendar.h>
#import <NGCards/iCalEvent.h>
#import <NGCards/iCalPerson.h>
#import <NGCards/iCalTrigger.h>
#import <NGCards/iCalRecurrenceRule.h>
#import <NGCards/iCalTimeZone.h>
#import <NGCards/iCalDateTime.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoContentObject.h>
#import <SOGo/SOGoDateFormatter.h>
#import <SOGo/SOGoPermissions.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserDefaults.h>
#import <Appointments/iCalCalendar+SOGo.h>
#import <Appointments/iCalEntityObject+SOGo.h>
#import <Appointments/iCalPerson+SOGo.h>
#import <Appointments/iCalRepeatableEntityObject+SOGo.h>
#import <Appointments/SOGoAppointmentFolder.h>
#import <Appointments/SOGoAppointmentObject.h>
#import <Appointments/SOGoAppointmentOccurence.h>
#import <Appointments/SOGoComponentOccurence.h>
#import "UIxComponentEditor.h"
#import "UIxAppointmentEditor.h"
@implementation UIxAppointmentEditor
- (id) init
{
SOGoUser *user;
if ((self = [super init]))
{
aptStartDate = nil;
aptEndDate = nil;
item = nil;
event = nil;
isAllDay = NO;
isTransparent = NO;
sendAppointmentNotifications = YES;
componentCalendar = nil;
user = [[self context] activeUser];
ASSIGN (dateFormatter, [user dateFormatterInContext: context]);
}
return self;
}
- (void) dealloc
{
[item release];
[[event parent] release];
[aptStartDate release];
[aptEndDate release];
[dateFormatter release];
[componentCalendar release];
[super dealloc];
}
/* template values */
- (iCalEvent *) event
{
if (!event)
{
event = (iCalEvent *) [[self clientObject] occurence];
[[event parent] retain];
}
return event;
}
- (NSString *) saveURL
{
return [NSString stringWithFormat: @"%@/saveAsAppointment",
[[self clientObject] baseURL]];
}
/* icalendar values */
- (BOOL) isAllDay
{
NSString *hm;
hm = [self queryParameterForKey: @"hm"];
return (isAllDay
|| [hm isEqualToString: @"allday"]);
}
- (void) setIsAllDay: (BOOL) newIsAllDay
{
isAllDay = newIsAllDay;
}
- (BOOL) isTransparent
{
return isTransparent;
}
- (void) setIsTransparent: (BOOL) newIsTransparent
{
isTransparent = newIsTransparent;
}
- (void) setSendAppointmentNotifications: (BOOL) theBOOL
{
sendAppointmentNotifications = theBOOL;
}
- (BOOL) sendAppointmentNotifications
{
return sendAppointmentNotifications;
}
- (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
{
ASSIGN (aptStartDate, newAptStartDate);
}
- (NSCalendarDate *) aptStartDate
{
return aptStartDate;
}
- (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
{
ASSIGN (aptEndDate, newAptEndDate);
}
- (NSCalendarDate *) aptEndDate
{
return aptEndDate;
}
- (void) setItem: (NSString *) newItem
{
ASSIGN (item, newItem);
}
- (NSString *) item
{
return item;
}
- (SOGoAppointmentFolder *) componentCalendar
{
return componentCalendar;
}
- (void) setComponentCalendar: (SOGoAppointmentFolder *) _componentCalendar
{
ASSIGN (componentCalendar, _componentCalendar);
}
/* read-only event */
- (NSString *) aptStartDateText
{
return [dateFormatter formattedDate: aptStartDate];
}
- (NSString *) aptStartDateTimeText
{
return [dateFormatter formattedDateAndTime: aptStartDate];
}
- (NSString *) aptEndDateText
{
return [dateFormatter formattedDate: aptEndDate];
}
- (NSString *) aptEndDateTimeText
{
return [dateFormatter formattedDateAndTime: aptEndDate];
}
- (BOOL) startDateIsEqualToEndDate
{
return [aptStartDate isEqualToDate: aptEndDate];
}
/* actions */
- (NSCalendarDate *) newStartDate
{
NSCalendarDate *newStartDate, *now;
NSTimeZone *timeZone;
SOGoUserDefaults *ud;
int hour, minute;
unsigned int uStart, uEnd;
newStartDate = [self selectedDate];
if (![[self queryParameterForKey: @"hm"] length])
{
ud = [[context activeUser] userDefaults];
timeZone = [ud timeZone];
now = [NSCalendarDate calendarDate];
[now setTimeZone: timeZone];
uStart = [ud dayStartHour];
if ([now isDateOnSameDay: newStartDate])
{
uEnd = [ud dayEndHour];
hour = [now hourOfDay];
minute = [now minuteOfHour];
if (minute % 15)
minute += 15 - (minute % 15);
if (hour < uStart)
newStartDate = [now hour: uStart minute: 0];
else if (hour > uEnd)
newStartDate = [[now tomorrow] hour: uStart minute: 0];
else
newStartDate = [now hour: [now hourOfDay] minute: minute];
}
else
newStartDate = [newStartDate hour: uStart minute: 0];
}
return newStartDate;
}
- (id <WOActionResults>) defaultAction
{
NSCalendarDate *startDate, *endDate;
NSString *duration;
NSTimeZone *timeZone;
unsigned int total, hours, minutes;
signed int offset;
SOGoObject <SOGoComponentOccurence> *co;
SOGoUserDefaults *ud;
[self event];
co = [self clientObject];
ud = [[context activeUser] userDefaults];
timeZone = [ud timeZone];
if ([co isNew]
&& [co isKindOfClass: [SOGoCalendarComponent class]])
{
startDate = [self newStartDate];
duration = [self queryParameterForKey:@"duration"];
if ([duration length] > 0)
{
total = [duration intValue];
hours = total / 100;
minutes = total % 100;
}
else
{
hours = 1;
minutes = 0;
}
endDate
= [startDate dateByAddingYears: 0 months: 0 days: 0
hours: hours minutes: minutes seconds: 0];
sendAppointmentNotifications = YES;
}
else
{
startDate = [event startDate];
isAllDay = [event isAllDay];
endDate = [event endDate];
if (isAllDay)
{
endDate = [endDate dateByAddingYears: 0 months: 0 days: -1];
// Convert the dates to the user's timezone
offset = [timeZone secondsFromGMTForDate: startDate];
startDate = [startDate dateByAddingYears:0 months:0 days:0 hours:0 minutes:0
seconds:-offset];
endDate = [endDate dateByAddingYears:0 months:0 days:0 hours:0 minutes:0
seconds:-offset];
}
isTransparent = ![event isOpaque];
sendAppointmentNotifications = ([event firstChildWithTag: @"X-SOGo-Send-Appointment-Notifications"] ? NO : YES);
}
[startDate setTimeZone: timeZone];
ASSIGN (aptStartDate, startDate);
[endDate setTimeZone: timeZone];
ASSIGN (aptEndDate, endDate);
return self;
}
- (id <WOActionResults>) newAction
{
NSString *objectId, *method, *uri;
id <WOActionResults> result;
SOGoAppointmentFolder *co;
SoSecurityManager *sm;
co = [self clientObject];
objectId = [co globallyUniqueObjectId];
if ([objectId length])
{
sm = [SoSecurityManager sharedSecurityManager];
if (![sm validatePermission: SoPerm_AddDocumentsImagesAndFiles
onObject: co
inContext: context])
method = [NSString stringWithFormat:@"%@/%@.ics/editAsAppointment",
[co soURL], objectId] ;
else
method = [NSString stringWithFormat: @"%@/Calendar/personal/%@.ics/editAsAppointment",
[self userFolderPath], objectId];
uri = [self completeHrefForMethod: method];
result = [self redirectToLocation: uri];
}
else
result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
reason: @"could not create a unique ID"];
return result;
}
- (void) _adjustRecurrentRules
{
iCalRecurrenceRule *rule;
NSEnumerator *rules;
NSCalendarDate *untilDate;
SOGoUserDefaults *ud;
NSTimeZone *timeZone;
rules = [[event recurrenceRules] objectEnumerator];
ud = [[context activeUser] userDefaults];
timeZone = [ud timeZone];
while ((rule = [rules nextObject]))
{
untilDate = [rule untilDate];
if (untilDate)
{
// The until date must match the time of the end date
NSCalendarDate *date;
date = [[event endDate] copy];
[date setTimeZone: timeZone];
[untilDate setTimeZone: timeZone];
untilDate = [untilDate dateByAddingYears:0
months:0
days:0
hours:[date hourOfDay]
minutes:[date minuteOfHour]
seconds:0];
[rule setUntilDate: untilDate];
[date release];
}
}
}
- (id <WOActionResults>) saveAction
{
SOGoAppointmentFolder *previousCalendar;
SOGoAppointmentObject *co;
NSString *jsonResponse;
SoSecurityManager *sm;
NSException *ex;
co = [self clientObject];
if ([co isKindOfClass: [SOGoAppointmentOccurence class]])
co = [co container];
previousCalendar = [co container];
sm = [SoSecurityManager sharedSecurityManager];
ex = nil;
if ([event hasRecurrenceRules])
[self _adjustRecurrentRules];
if ([co isNew])
{
if (componentCalendar
&& ![[componentCalendar ocsPath]
isEqualToString: [previousCalendar ocsPath]])
{
// New event in a different calendar -- make sure the user can
// write to the selected calendar since the rights were verified
// on the calendar specified in the URL, not on the selected
// calendar of the popup menu.
if (![sm validatePermission: SoPerm_AddDocumentsImagesAndFiles
onObject: componentCalendar
inContext: context])
co = [componentCalendar lookupName: [co nameInContainer]
inContext: context
acquire: NO];
}
// Save the event.
ex = [co saveComponent: event];
}
else
{
// The event was modified -- save it.
ex = [co saveComponent: event];
if (componentCalendar
&& ![[componentCalendar ocsPath]
isEqualToString: [previousCalendar ocsPath]])
{
// The event was moved to a different calendar.
if (![sm validatePermission: SoPerm_DeleteObjects
onObject: previousCalendar
inContext: context])
{
if (![sm validatePermission: SoPerm_AddDocumentsImagesAndFiles
onObject: componentCalendar
inContext: context])
ex = [co moveToFolder: componentCalendar];
}
}
}
if (ex)
jsonResponse = [NSDictionary dictionaryWithObjectsAndKeys:
@"failure", @"status",
[ex reason],
@"message",
nil];
else
jsonResponse = [NSDictionary dictionaryWithObjectsAndKeys:
@"success", @"status", nil];
return [self responseWithStatus: 200
andString: [jsonResponse jsonRepresentation]];
}
- (id <WOActionResults>) viewAction
{
WOResponse *result;
NSDictionary *data;
NSCalendarDate *eventStartDate, *eventEndDate;
NSTimeZone *timeZone;
SOGoUserDefaults *ud;
SOGoCalendarComponent *co;
NSString *created_by;
iCalAlarm *anAlarm;
BOOL resetAlarm;
unsigned int snoozeAlarm;
[self event];
result = [self responseWithStatus: 200];
ud = [[context activeUser] userDefaults];
timeZone = [ud timeZone];
eventStartDate = [event startDate];
eventEndDate = [event endDate];
[eventStartDate setTimeZone: timeZone];
[eventEndDate setTimeZone: timeZone];
co = [self clientObject];
if (!componentCalendar)
{
componentCalendar = [co container];
if ([componentCalendar isKindOfClass: [SOGoCalendarComponent class]])
componentCalendar = [componentCalendar container];
[componentCalendar retain];
}
created_by = [event createdBy];
// resetAlarm=yes is set only when we are about to show the alarm popup in the Web
// interface of SOGo. See generic.js for details. snoozeAlarm=X is called when the
// user clicks on "Snooze for" X minutes, when the popup is being displayed.
// If either is set to yes, we must find the right alarm.
resetAlarm = [[[context request] formValueForKey: @"resetAlarm"] boolValue];
snoozeAlarm = [[[context request] formValueForKey: @"snoozeAlarm"] intValue];
if (resetAlarm || snoozeAlarm)
{
iCalEvent *master;
master = event;
[componentCalendar findEntityForClosestAlarm: &event
timezone: timeZone
startDate: &eventStartDate
endDate: &eventEndDate];
anAlarm = [event firstDisplayOrAudioAlarm];
if (resetAlarm)
{
iCalTrigger *aTrigger;
aTrigger = [anAlarm trigger];
[aTrigger setValue: 0 ofAttribute: @"x-webstatus" to: @"triggered"];
[co saveComponent: master];
}
else if (snoozeAlarm)
{
[co snoozeAlarm: snoozeAlarm];
}
}
data = [NSDictionary dictionaryWithObjectsAndKeys:
[[componentCalendar displayName] stringByEscapingHTMLString], @"calendar",
[event tag], @"component",
[dateFormatter formattedDate: eventStartDate], @"startDate",
[dateFormatter formattedTime: eventStartDate], @"startTime",
[dateFormatter formattedDate: eventEndDate], @"endDate",
[dateFormatter formattedTime: eventEndDate], @"endTime",
([event isAllDay] ? @"1": @"0"), @"isAllDay",
[[event summary] stringByEscapingHTMLString], @"summary",
[[event location] stringByEscapingHTMLString], @"location",
[created_by stringByEscapingHTMLString], @"created_by",
[[[event comment] stringByEscapingHTMLString] stringByDetectingURLs], @"description",
nil];
[result appendContentString: [data jsonRepresentation]];
return result;
}
- (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
inContext: (WOContext*) context
{
NSString *actionName;
actionName = [[request requestHandlerPath] lastPathComponent];
return ([[self clientObject] conformsToProtocol: @protocol (SOGoComponentOccurence)]
&& [actionName hasPrefix: @"save"]);
}
- (void) takeValuesFromRequest: (WORequest *) _rq
inContext: (WOContext *) _ctx
{
int nbrDays;
iCalDateTime *startDate;
iCalTimeZone *tz;
NSCalendarDate *allDayStartDate;
NSTimeZone *timeZone;
SOGoUserDefaults *ud;
signed int offset;
id o;
[self event];
[super takeValuesFromRequest: _rq inContext: _ctx];
if (isAllDay)
{
nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
/ 86400) + 1;
// Convert all-day start date to GMT (floating date)
ud = [[context activeUser] userDefaults];
timeZone = [ud timeZone];
offset = [timeZone secondsFromGMTForDate: aptStartDate];
allDayStartDate = [aptStartDate dateByAddingYears:0 months:0 days:0 hours:0 minutes:0
seconds:offset];
[event setAllDayWithStartDate: allDayStartDate
duration: nbrDays];
}
else
{
[event setStartDate: aptStartDate];
[event setEndDate: aptEndDate];
}
if (!isAllDay)
{
// Make sure there's a vTimeZone associated to the event unless it
// is an all-day event.
startDate = (iCalDateTime *)[event uniqueChildWithTag: @"dtstart"];
if (![startDate timeZone])
{
ud = [[context activeUser] userDefaults];
tz = [iCalTimeZone timeZoneForName: [ud timeZoneName]];
if ([[event parent] addTimeZone: tz])
{
[startDate setTimeZone: tz];
[(iCalDateTime *)[event uniqueChildWithTag: @"dtend"] setTimeZone: tz];
}
}
}
else if (![[self clientObject] isNew])
{
// Remove the vTimeZone when dealing with an all-day event.
startDate = (iCalDateTime *)[event uniqueChildWithTag: @"dtstart"];
tz = [startDate timeZone];
if (tz)
{
[startDate setTimeZone: nil];
[(iCalDateTime *)[event uniqueChildWithTag: @"dtend"] setTimeZone: nil];
[[event parent] removeChild: tz];
}
}
[event setTransparency: (isTransparent? @"TRANSPARENT" : @"OPAQUE")];
o = [event firstChildWithTag: @"X-SOGo-Send-Appointment-Notifications"];
if (!sendAppointmentNotifications && !o)
[event addChild: [CardElement simpleElementWithTag: @"X-SOGo-Send-Appointment-Notifications" value: @"NO"]];
else if (sendAppointmentNotifications && o)
[event removeChild: o];
}
- (id) _statusChangeAction: (NSString *) newStatus
{
[[self clientObject] changeParticipationStatus: newStatus
withDelegate: nil];
return [self responseWith204];
}
- (id) acceptAction
{
return [self _statusChangeAction: @"ACCEPTED"];
}
- (id) declineAction
{
return [self _statusChangeAction: @"DECLINED"];
}
- (id) needsActionAction
{
return [self _statusChangeAction: @"NEEDS-ACTION"];
}
- (id) tentativeAction
{
return [self _statusChangeAction: @"TENTATIVE"];
}
- (id) delegateAction
{
// BOOL receiveUpdates;
NSString *delegatedEmail, *delegatedUid;
iCalPerson *delegatedAttendee;
SOGoUser *user;
WORequest *request;
WOResponse *response;
response = nil;
request = [context request];
delegatedEmail = [request formValueForKey: @"to"];
if ([delegatedEmail length])
{
user = [context activeUser];
delegatedAttendee = [iCalPerson new];
[delegatedAttendee autorelease];
[delegatedAttendee setEmail: delegatedEmail];
delegatedUid = [delegatedAttendee uid];
if (delegatedUid)
{
SOGoUser *delegatedUser;
delegatedUser = [SOGoUser userWithLogin: delegatedUid];
[delegatedAttendee setCn: [delegatedUser cn]];
}
[delegatedAttendee setRole: @"REQ-PARTICIPANT"];
[delegatedAttendee setRsvp: @"TRUE"];
[delegatedAttendee setParticipationStatus: iCalPersonPartStatNeedsAction];
[delegatedAttendee setDelegatedFrom:
[NSString stringWithFormat: @"mailto:%@", [[user allEmails] objectAtIndex: 0]]];
// receiveUpdates = [[request formValueForKey: @"receiveUpdates"] boolValue];
// if (receiveUpdates)
// [delegatedAttendee setRole: @"NON-PARTICIPANT"];
response = (WOResponse*)[[self clientObject] changeParticipationStatus: @"DELEGATED"
withDelegate: delegatedAttendee];
}
else
response = [NSException exceptionWithHTTPStatus: 400
reason: @"missing 'to' parameter"];
if (!response)
response = [self responseWith204];
return response;
}
@end
|