/core/externals/update-engine/externals/gdata-objectivec-client/Source/Clients/Calendar/GDataEntryEvent.m

http://macfuse.googlecode.com/ · Objective C · 318 lines · 223 code · 65 blank · 30 comment · 27 complexity · a0d0337c1cfddec09601b577a760182f MD5 · raw file

  1. /* Copyright (c) 2007 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. //
  16. // GDataEntryEvent.m
  17. //
  18. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_CALENDAR_SERVICE
  19. #define GDATAENTRYEVENT_DEFINE_GLOBALS 1
  20. #import "GDataEntryEvent.h"
  21. #import "GDataTextConstruct.h"
  22. // extensions
  23. // EventEntry extensions
  24. // These extensions are just subclasses of GDataValueConstruct.
  25. // We're creating them as unique subclasses so they can exist
  26. // separately in the extensions list, which stores found extensions
  27. // by class.
  28. @implementation GDataEventStatus
  29. + (NSString *)extensionElementURI { return kGDataNamespaceGData; }
  30. + (NSString *)extensionElementPrefix { return kGDataNamespaceGDataPrefix; }
  31. + (NSString *)extensionElementLocalName { return @"eventStatus"; }
  32. @end
  33. @implementation GDataVisibility
  34. + (NSString *)extensionElementURI { return kGDataNamespaceGData; }
  35. + (NSString *)extensionElementPrefix { return kGDataNamespaceGDataPrefix; }
  36. + (NSString *)extensionElementLocalName { return @"visibility"; }
  37. @end
  38. @implementation GDataTransparency
  39. + (NSString *)extensionElementURI { return kGDataNamespaceGData; }
  40. + (NSString *)extensionElementPrefix { return kGDataNamespaceGDataPrefix; }
  41. + (NSString *)extensionElementLocalName { return @"transparency"; }
  42. @end
  43. // We're defining Reminders as extensions to When elements.
  44. // This category on When elements will simpify access to those
  45. // reminders that are found inside When elements.
  46. @implementation GDataWhen (GDataCalendarEntryEventExtensions)
  47. - (NSArray *)reminders {
  48. return [self objectsForExtensionClass:[GDataReminder class]];
  49. }
  50. - (void)setReminders:(NSArray *)arr {
  51. [self setObjects:arr forExtensionClass:[GDataReminder class]];
  52. }
  53. - (void)addReminder:(GDataReminder *)obj {
  54. [self addObject:obj forExtensionClass:[GDataReminder class]];
  55. }
  56. @end
  57. @implementation GDataEntryEvent
  58. - (void)addExtensionDeclarations {
  59. [super addExtensionDeclarations];
  60. Class entryClass = [self class];
  61. // EventEntry extensions
  62. [self addExtensionDeclarationForParentClass:entryClass
  63. childClasses:
  64. [GDataRecurrenceException class], [GDataReminder class],
  65. [GDataRecurrence class], [GDataWhere class],
  66. [GDataEventStatus class], [GDataVisibility class],
  67. [GDataTransparency class], [GDataWho class],
  68. [GDataWhen class], [GDataOriginalEvent class],
  69. [GDataComment class], nil];
  70. // a reminder may be at the event entry level (declared above) for
  71. // recurrence events, or inside a GDataWhen for single events
  72. [self addExtensionDeclarationForParentClass:[GDataWhen class]
  73. childClass:[GDataReminder class]];
  74. }
  75. - (NSString *)suffixAfterPoundSign:(NSString *)str {
  76. if (str != nil) {
  77. NSRange range = [str rangeOfString:@"#" options:NSBackwardsSearch];
  78. if (range.location != NSNotFound) {
  79. return [str substringFromIndex:(1 + range.location)];
  80. }
  81. }
  82. return nil;
  83. }
  84. #if !GDATA_SIMPLE_DESCRIPTIONS
  85. - (NSMutableArray *)itemsForDescription {
  86. NSString *visibility = [self suffixAfterPoundSign:[[self visibility] stringValue]];
  87. NSString *transparency = [self suffixAfterPoundSign:[[self transparency] stringValue]];
  88. NSString *eventStatus = [self suffixAfterPoundSign:[[self eventStatus] stringValue]];
  89. NSArray *times = [self times];
  90. NSUInteger numberOfTimes = [times count];
  91. NSString *timesStr;
  92. if (numberOfTimes == 1) {
  93. GDataWhen *when = [times objectAtIndex:0];
  94. timesStr = [NSString stringWithFormat:@"(%@..%@)",
  95. [[when startTime] stringValue], [[when endTime] stringValue]];
  96. } else {
  97. // just show the number of times, pretty rare
  98. timesStr = [NSString stringWithFormat:@"%lu", (unsigned long) numberOfTimes];
  99. }
  100. struct GDataDescriptionRecord descRecs[] = {
  101. { @"recurrence", @"recurrence.stringValue", kGDataDescValueLabeled },
  102. { @"visibility", visibility, kGDataDescValueIsKeyPath },
  103. { @"transparency", transparency, kGDataDescValueIsKeyPath },
  104. { @"eventStatus", eventStatus, kGDataDescValueIsKeyPath },
  105. { @"times", timesStr, kGDataDescValueIsKeyPath },
  106. { @"recExc", @"recurrenceExceptions", kGDataDescArrayCount },
  107. { @"reminders", @"reminders", kGDataDescArrayCount },
  108. { @"comment", @"comment", kGDataDescLabelIfNonNil },
  109. { nil, nil, (GDataDescRecTypes)0 }
  110. };
  111. NSMutableArray *items = [super itemsForDescription];
  112. [self addDescriptionRecords:descRecs toItems:items];
  113. return items;
  114. }
  115. #endif
  116. #pragma mark Actual iVars
  117. - (GDataEventStatus *)eventStatus {
  118. return (GDataEventStatus *) [self objectForExtensionClass:[GDataEventStatus class]];
  119. }
  120. - (void)setEventStatus:(GDataEventStatus *)val {
  121. [self setObject:val forExtensionClass:[GDataEventStatus class]];
  122. }
  123. - (GDataTransparency *)transparency {
  124. return (GDataTransparency *) [self objectForExtensionClass:[GDataTransparency class]];
  125. }
  126. - (void)setTransparency:(GDataTransparency *)val {
  127. [self setObject:val forExtensionClass:[GDataTransparency class]];
  128. }
  129. - (GDataVisibility *)visibility {
  130. return (GDataVisibility *) [self objectForExtensionClass:[GDataVisibility class]];
  131. }
  132. - (void)setVisibility:(GDataVisibility *)val {
  133. [self setObject:val forExtensionClass:[GDataVisibility class]];
  134. }
  135. - (GDataRecurrence *)recurrence {
  136. return (GDataRecurrence *) [self objectForExtensionClass:[GDataRecurrence class]];
  137. }
  138. - (void)setRecurrence:(GDataRecurrence *)obj {
  139. BOOL hadRecurrence = ([self recurrence] != nil);
  140. BOOL willHaveRecurrence = (obj != nil);
  141. if (hadRecurrence && !willHaveRecurrence) {
  142. [self setNonRecurrenceReminders:[self recurrenceReminders]];
  143. [self setRecurrenceReminders:nil];
  144. } else if (!hadRecurrence && willHaveRecurrence) {
  145. [self setRecurrenceReminders:[self nonRecurrenceReminders]];
  146. [self setNonRecurrenceReminders:nil];
  147. }
  148. [self setObject:obj forExtensionClass:[GDataRecurrence class]];
  149. }
  150. - (NSArray *)recurrenceExceptions {
  151. return [self objectsForExtensionClass:[GDataRecurrenceException class]];
  152. }
  153. - (void)setRecurrenceExceptions:(NSArray *)arr {
  154. [self setObjects:arr forExtensionClass:[GDataRecurrenceException class]];
  155. }
  156. - (void)addRecurrenceException:(GDataRecurrenceException *)obj {
  157. [self addObject:obj forExtensionClass:[obj class]];
  158. }
  159. - (GDataOriginalEvent *)originalEvent {
  160. return (GDataOriginalEvent *) [self objectForExtensionClass:[GDataOriginalEvent class]];
  161. }
  162. - (void)setOriginalEvent:(GDataOriginalEvent *)obj {
  163. [self setObject:obj forExtensionClass:[GDataOriginalEvent class]];
  164. }
  165. - (GDataComment *)comment {
  166. return (GDataComment *) [self objectForExtensionClass:[GDataComment class]];
  167. }
  168. - (void)setComment:(GDataComment *)obj {
  169. [self setObject:obj forExtensionClass:[GDataComment class]];
  170. }
  171. - (NSArray *)recurrenceReminders {
  172. return [self objectsForExtensionClass:[GDataReminder class]];
  173. }
  174. - (void)setRecurrenceReminders:(NSArray *)array {
  175. [self setObjects:array forExtensionClass:[GDataReminder class]];
  176. }
  177. - (void)addRecurrenceReminder:(GDataReminder *)obj {
  178. [self addObject:obj forExtensionClass:[GDataReminder class]];
  179. }
  180. - (NSArray *)nonRecurrenceReminders {
  181. NSArray *whens = [self times];
  182. if ([whens count] > 0) {
  183. GDataWhen *when = [whens objectAtIndex:0];
  184. NSArray *reminders = [when objectsForExtensionClass:[GDataReminder class]];
  185. return reminders;
  186. }
  187. return nil;
  188. }
  189. - (void)setNonRecurrenceReminders:(NSArray *)array {
  190. NSArray *whens = [self times];
  191. if ([whens count] > 0) {
  192. GDataWhen *when = [whens objectAtIndex:0];
  193. [when setReminders:array];
  194. }
  195. }
  196. - (void)addNonRecurrenceReminder:(GDataReminder *)obj {
  197. NSArray *whens = [self times];
  198. if ([whens count] > 0) {
  199. GDataWhen *when = [whens objectAtIndex:0];
  200. [when addReminder:obj];
  201. }
  202. }
  203. - (NSArray *)reminders {
  204. if ([self recurrence] != nil) {
  205. return [self recurrenceReminders];
  206. } else {
  207. return [self nonRecurrenceReminders];
  208. }
  209. return nil;
  210. }
  211. - (void)setReminders:(NSArray *)array {
  212. if ([self recurrence] != nil) {
  213. [self setRecurrenceReminders:array];
  214. } else {
  215. [self setNonRecurrenceReminders:array];
  216. }
  217. }
  218. - (void)addReminder:(GDataReminder *)obj {
  219. if ([self recurrence] != nil) {
  220. [self addRecurrenceReminder:obj];
  221. } else {
  222. [self addNonRecurrenceReminder:obj];
  223. }
  224. }
  225. - (NSArray *)times {
  226. return [self objectsForExtensionClass:[GDataWhen class]];
  227. }
  228. - (void)setTimes:(NSArray *)array {
  229. [self setObjects:array forExtensionClass:[GDataWhen class]];
  230. }
  231. - (void)addTime:(GDataWhen *)obj {
  232. [self addObject:obj forExtensionClass:[obj class]];
  233. }
  234. - (NSArray *)participants {
  235. return [self objectsForExtensionClass:[GDataWho class]];
  236. }
  237. - (void)setParticipants:(NSArray *)array {
  238. [self setObjects:array forExtensionClass:[GDataWho class]];
  239. }
  240. - (void)addParticipant:(GDataWho *)obj {
  241. [self addObject:obj forExtensionClass:[obj class]];
  242. }
  243. - (NSArray *)locations {
  244. return [self objectsForExtensionClass:[GDataWhere class]];
  245. }
  246. - (void)setLocations:(NSArray *)array {
  247. [self setObjects:array forExtensionClass:[GDataWhere class]];
  248. }
  249. - (void)addLocation:(GDataWhere *)obj {
  250. [self addObject:obj forExtensionClass:[obj class]];
  251. }
  252. @end
  253. #endif // !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_CALENDAR_SERVICE