From 0d89f02410b16a9dc27eb899fafdf0db3b14f599 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Mon, 19 Apr 2010 21:28:03 +0000 Subject: [PATCH] Added missing files for class iCalByDayMask. Monotone-Parent: ab2613d820779df99f34c6d0ae2ec38908cae05f Monotone-Revision: b6b676075804a429d82d9d9a9fa7c68dd27cef65 Monotone-Author: flachapelle@inverse.ca Monotone-Date: 2010-04-19T21:28:03 Monotone-Branch: ca.inverse.sogo --- SOPE/NGCards/iCalByDayMask.h | 75 ++++++++ SOPE/NGCards/iCalByDayMask.m | 343 +++++++++++++++++++++++++++++++++++ 2 files changed, 418 insertions(+) create mode 100644 SOPE/NGCards/iCalByDayMask.h create mode 100644 SOPE/NGCards/iCalByDayMask.m diff --git a/SOPE/NGCards/iCalByDayMask.h b/SOPE/NGCards/iCalByDayMask.h new file mode 100644 index 000000000..1ca57ee34 --- /dev/null +++ b/SOPE/NGCards/iCalByDayMask.h @@ -0,0 +1,75 @@ +/* iCalByDayMask.h - this file is part of SOPE + * + * Copyright (C) 2010 Wolfgang Sourdeau + * + * Author: Wolfgang Sourdeau + * + * 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. + */ + +#ifndef ICALBYDAYMASK_H +#define ICALBYDAYMASK_H + +#import + +#import "iCalRecurrenceRule.h" + +typedef enum { + iCalWeekOccurrenceFirst = 0x001, // order + iCalWeekOccurrenceSecond = 0x002, // 2^(order - 1) + iCalWeekOccurrenceThird = 0x004, + iCalWeekOccurrenceFourth = 0x008, + iCalWeekOccurrenceFifth = 0x010, + iCalWeekOccurrenceAll = 0x3ff, + iCalWeekOccurrenceLast = 0x020, // 2^(order - 1) >> 5 + iCalWeekOccurrenceSecondLast = 0x040, + iCalWeekOccurrenceThirdLast = 0x080, + iCalWeekOccurrenceFourthLast = 0x100, + iCalWeekOccurrenceFifthLast = 0x200, +} iCalWeekOccurrence; + +typedef iCalWeekOccurrence iCalWeekOccurrences[7]; + +// extern NSString *iCalWeekOccurrenceString[]; + +@interface iCalByDayMask : NSObject +{ + iCalWeekOccurrences days; +} + ++ (id) byDayMaskWithDays: (iCalWeekOccurrences) theDays; ++ (id) byDayMaskWithWeekDays; +- (id) initWithDays: (iCalWeekOccurrences) theDays; ++ (id) byDayMaskWithRuleString: (NSString *) byDayRule; +- (id) initWithRuleString: (NSString *) byDayRule; + +- (BOOL) occursOnDay: (iCalWeekDay) weekDay; +- (BOOL) occursOnDay: (iCalWeekDay) weekDay + withWeekOccurrence: (iCalWeekOccurrence) occurrence; +- (BOOL) occursOnDay: (iCalWeekDay) weekDay + withWeekNumber: (int) week; +- (BOOL) isWeekDays; + +//- (iCalWeekOccurrences *) allDays; +- (iCalWeekDay) firstDay; +- (int) firstOccurrence; + +- (NSString *) asRuleString; +- (NSString *) asRuleStringWithIntegers; + +@end + +#endif /* ICALBYDAYMASK_H */ diff --git a/SOPE/NGCards/iCalByDayMask.m b/SOPE/NGCards/iCalByDayMask.m new file mode 100644 index 000000000..9eb313a4c --- /dev/null +++ b/SOPE/NGCards/iCalByDayMask.m @@ -0,0 +1,343 @@ +/* iCalByDayMask.m - this file is part of SOPE + * + * Copyright (C) 2010 Inverse inc. + * + * Author: Wolfgang Sourdeau + * + * 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 +#import + +#import "iCalByDayMask.h" + +#import +#import + +@interface iCalByDayMask (PrivateAPI) + +- (int) _iCalWeekOccurrenceIntValue: (iCalWeekOccurrence) weekOccurrence; + +@end + +@implementation iCalByDayMask + ++ (id) byDayMaskWithDays: (iCalWeekOccurrences) theDays +{ + id o; + + o = [[self alloc] initWithDays: theDays]; + AUTORELEASE(o); + + return o; +} + +- (id) initWithDays: (iCalWeekOccurrences) theDays +{ + self = [super init]; + + if (self) + { + memcpy (days, theDays, sizeof(iCalWeekOccurrences)); + } + + return self; +} + ++ (id) byDayMaskWithWeekDays +{ + id o; + + iCalWeekOccurrences days; + days[iCalWeekDaySunday] = 0; + days[iCalWeekDayMonday] = iCalWeekOccurrenceAll; + days[iCalWeekDayTuesday] = iCalWeekOccurrenceAll; + days[iCalWeekDayWednesday] = iCalWeekOccurrenceAll; + days[iCalWeekDayThursday] = iCalWeekOccurrenceAll; + days[iCalWeekDayFriday] = iCalWeekOccurrenceAll; + days[iCalWeekDaySaturday] = 0; + o = [[self alloc] initWithDays: days]; + AUTORELEASE(o); + + return o; +} + ++ (id) byDayMaskWithRuleString: (NSString *) byDayRule +{ + id o; + + o = [[self alloc] initWithRuleString: byDayRule]; + AUTORELEASE(o); + + return o; +} + +- (id) initWithRuleString: (NSString *) byDayRule +{ + NSArray *values; + + unsigned int count, max; + NSString *value; + unichar c, chars[2]; + unsigned int valueLength, i, digitStart, order; + iCalWeekDay day; + BOOL reverse; + + self = [super init]; + + if (self) + { + memset(days, 0, 7 * sizeof(iCalWeekOccurrence)); + + if ([byDayRule length] > 0) + { + values = [byDayRule componentsSeparatedByString: @","]; + max = [values count]; + for (count = 0; count < max; count++) + { + value = [[values objectAtIndex: count] uppercaseString]; + valueLength = [value length]; + if (valueLength > 1) + { + day = iCalWeekDayUnknown; + reverse = NO; + digitStart = 0; + order = 0; + + [value getCharacters: chars + range: NSMakeRange(valueLength - 2, 2)]; + + switch (chars[0]) + { + case 'M': day = iCalWeekDayMonday; + break; + case 'W': day = iCalWeekDayWednesday; + break; + case 'F': day = iCalWeekDayFriday; + break; + case 'T': + if (chars[1] == 'U') + day = iCalWeekDayTuesday; + else if (chars[1] == 'H') + day = iCalWeekDayThursday; + break; + case 'S': + if (chars[1] == 'A') + day = iCalWeekDaySaturday; + else if (chars[1] == 'U') + day = iCalWeekDaySunday; + break; + } + + if (day != iCalWeekDayUnknown) + { + c = [value characterAtIndex: 0]; + if (c == '-') + { + digitStart = 1; + reverse = YES; + c = [value characterAtIndex: 1]; + } + else if (c == '+') + { + digitStart = 1; + c = [value characterAtIndex: 1]; + } + + i = digitStart; + + while (i < valueLength && isdigit(c)) + { + i++; + c = [value characterAtIndex: i]; + } + + if (i != digitStart) + order = [[value substringWithRange: NSMakeRange(digitStart, (i - digitStart))] intValue]; + + if (order > 0 && order < 6) + { + order = pow (2, order - 1); + if (reverse) + order = order << 5; + days[day] |= order; + //NSLog(@"*** iCalByDayMask [%i] %@ : day = %i, order = %i, result = %i", count, byDayRule, day, order, days[day]); + } + else + { + days[day] = iCalWeekOccurrenceAll; + } + } + } + } + } + } + + return self; +} + +- (void) dealloc +{ + [super dealloc]; +} + +/** + * The week occurrence has no meaning for a DAILY or WEEKLY frequency; + * therefore, this method ignores the week occurrence. + */ +- (BOOL) occursOnDay: (iCalWeekDay) weekDay +{ + return days[weekDay] > 0; +} + +- (BOOL) occursOnDay: (iCalWeekDay) weekDay + withWeekOccurrence: (iCalWeekOccurrence) occurrence +{ + return (days[weekDay] | occurrence) > 0; +} + +- (BOOL) occursOnDay: (iCalWeekDay) weekDay + withWeekNumber: (int) week +{ + unsigned int absWeek, order; + + absWeek = abs (week); + order = 0; + if (absWeek > 0 && absWeek < 6) + { + order = pow (2, absWeek - 1); + if (week < 0) + order = order << 5; + } + + return ((days[weekDay] & order) > 0); +} + +- (BOOL) isWeekDays +{ + return (days[iCalWeekDaySunday] == 0 && + days[iCalWeekDayMonday] == iCalWeekOccurrenceAll && + days[iCalWeekDayTuesday] == iCalWeekOccurrenceAll && + days[iCalWeekDayWednesday] == iCalWeekOccurrenceAll && + days[iCalWeekDayThursday] == iCalWeekOccurrenceAll && + days[iCalWeekDayFriday] == iCalWeekOccurrenceAll && + days[iCalWeekDaySaturday] == 0); +} + +- (iCalWeekDay) firstDay +{ + int i; + iCalWeekDay day; + + day = -1; + for (i = 0; i < 7; i++) + { + if (days[i]) + { + day = i; + break; + } + } + + return day; +} + +- (int) firstOccurrence +{ + int occurrence; + iCalWeekDay day; + + occurrence = 0; + day = [self firstDay]; + + if (day > -1 && days[day] != iCalWeekOccurrenceAll) + occurrence = [self _iCalWeekOccurrenceIntValue: days[day]]; + + return occurrence; +} + +- (int) _iCalWeekOccurrenceIntValue: (iCalWeekOccurrence) weekOccurrence +{ + int i = 0; + + switch (weekOccurrence) + { + case iCalWeekOccurrenceFirst: i = 1; + break; + case iCalWeekOccurrenceSecond: i = 2; + break; + case iCalWeekOccurrenceThird: i = 3; + break; + case iCalWeekOccurrenceFourth: i = 4; + break; + case iCalWeekOccurrenceFifth: i = 5; + break; + case iCalWeekOccurrenceLast: i = -1; + break; + case iCalWeekOccurrenceSecondLast: i = -2; + break; + case iCalWeekOccurrenceThirdLast: i = -3; + break; + case iCalWeekOccurrenceFourthLast: i = -4; + break; + case iCalWeekOccurrenceFifthLast: i = -5; + break; + } + + return i; +} + +- (NSString *) asRuleString +{ + NSMutableArray *rules; + NSMutableString *rule; + int i; + + rules = [NSMutableArray array]; + for (i = 0; i < 7; i++) + { + if (days[i]) + { + rule = [NSMutableString string]; + if (days[i] != iCalWeekOccurrenceAll) + [rule appendFormat: @"%i", [self _iCalWeekOccurrenceIntValue: days[i]]]; + [rule appendString: iCalWeekDayString[i]]; + [rules addObject: rule]; + } + } + + return [rules componentsJoinedByString: @","]; +} + +- (NSString *) asRuleStringWithIntegers +{ + unsigned int i; + NSMutableString *s; + + s = [NSMutableString string]; + + for (i = 0; i < 7; i++) + if (days[i] > 0) + { + [s appendFormat: @"%d,", i]; + } + [s deleteSuffix: @","]; + + return s; +} + +@end /* iCalByDayMask */