home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / ToDoItem.m < prev    next >
Encoding:
Text File  |  1996-07-30  |  4.2 KB  |  197 lines

  1. /*
  2.   You may freely copy, distribute, and reuse the code in this example.
  3.   NeXT disclaims any warranty of any kind, expressed or implied, as to its
  4.   fitness for any particular use.
  5. */
  6.  
  7. #import "ToDoItem.h"
  8.  
  9. @implementation ToDoItem
  10.  
  11. - (id)initWithName:(NSString *)name andDate:(NSCalendarDate *)date
  12. {
  13.     NSCalendarDate *tDate;
  14.  
  15.     if (!name && ![name isKindOfClass:[NSString class]]) return nil;
  16.     [super init];
  17.  
  18.     if (!date)
  19.         tDate = [NSCalendarDate date];
  20.     else
  21.         tDate = date;
  22.  
  23.     [self setDay: tDate];
  24.     [self setItemName:name];
  25.     [self setItemStatus:incomplete];
  26.     [self setNotes:@""];
  27.  
  28.     return self;
  29. }
  30.  
  31. - (void)dealloc
  32. {
  33.     [day release];
  34.     [itemName release];
  35.     [notes release];
  36.     if (itemTimer) {
  37.         [itemTimer invalidate];
  38.         [itemTimer release];
  39.     }
  40.     [super dealloc];
  41. }
  42.  
  43. - (id)copyWithZone:(NSZone *)zone
  44. {
  45.     ToDoItem *newobj = [[ToDoItem allocWithZone:zone] initWithName:itemName andDate:day];
  46.     [newobj setNotes:notes];
  47.     [newobj setItemStatus:itemStatus];
  48.     [newobj setSecsUntilDue:secsUntilDue];
  49.     [newobj setSecsUntilNotif:secsUntilNotif];
  50.  
  51.     return newobj;
  52. }
  53.  
  54. - (BOOL)isEqual:(id)anObj
  55. {
  56.     if ([anObj isKindOfClass:[ToDoItem class]] &&
  57.         [itemName isEqualToString:[anObj itemName]] &&
  58.         [day isEqualToDate:[anObj day]])
  59.         return YES;
  60.     else
  61.         return NO;
  62. }
  63.  
  64. - (NSString *)description
  65. {
  66.     NSString *desc = [NSString stringWithFormat:@"%@\n\tName: %@\n\tDate: %@\n\tNotes: %@\n\tCompleted: %@\n\tSecs Until Due: %d\n\tSecs Until Notif: %d",
  67.         [super description],
  68.         [self itemName],
  69.         [self day],
  70.         [self notes],
  71.         (([self itemStatus]==complete)?@"Yes":@"No"),
  72.         [self secsUntilDue],
  73.         [self secsUntilNotif]];
  74.  
  75.     return (desc);
  76. }
  77.  
  78. - (id)initWithCoder:(NSCoder *)coder
  79. {
  80.     day = [coder decodeObject];
  81.     itemName = [coder decodeObject];
  82.     [coder decodeValueOfObjCType:@encode(typeof(secsUntilDue)) at:&secsUntilDue];
  83.     [coder decodeValueOfObjCType:@encode(typeof(secsUntilNotif)) at:&secsUntilNotif];
  84.     [coder decodeValueOfObjCType:@encode(typeof(itemStatus)) at:&itemStatus];
  85.     notes = [coder decodeObject];
  86.  
  87.     day = [day copy];
  88.     itemName = [itemName copy];
  89.     notes = [notes copy];
  90.  
  91.     return self;
  92. }
  93.  
  94. - (void)encodeWithCoder:(NSCoder *)coder
  95. {
  96.     [coder encodeObject:day];
  97.     [coder encodeObject:itemName];
  98.     [coder encodeValueOfObjCType:@encode(typeof(secsUntilDue)) at:&secsUntilDue];
  99.     [coder encodeValueOfObjCType:@encode(typeof(secsUntilNotif)) at:&secsUntilNotif];
  100.     [coder encodeValueOfObjCType:@encode(typeof(itemStatus)) at:&itemStatus];
  101.     [coder encodeObject:notes];
  102. }
  103.  
  104. -(void) setDay:(NSCalendarDate *)newDay
  105. {
  106.     [day autorelease];
  107.     day = [newDay copy];
  108. }
  109.  
  110. - (NSCalendarDate *)day { return day; }
  111.  
  112. - (void)setItemName:(NSString *)newName
  113. {
  114.     [itemName autorelease];
  115.     itemName = [newName copy];
  116. }
  117.  
  118. - (NSString *)itemName { return itemName; }
  119.  
  120. - (void)setNotes:(NSString *)newNotes
  121. {
  122.     [notes autorelease];
  123.     notes = [newNotes copy];
  124. }
  125.  
  126. - (NSString *)notes { return notes; }
  127.  
  128. - (void)setSecsUntilNotif:(long)secs
  129. {
  130.     secsUntilNotif = secs;
  131. }
  132.  
  133. - (long)secsUntilNotif { return secsUntilNotif; }
  134.  
  135. - (void)setSecsUntilDue:(long)secs
  136. {
  137.     secsUntilDue = secs;
  138. }
  139.  
  140. - (long)secsUntilDue { return secsUntilDue; }
  141.  
  142. - (void)setItemStatus:(ToDoItemStatus)newStatus
  143. {
  144.     itemStatus = newStatus;
  145. }
  146.  
  147. - (ToDoItemStatus)itemStatus { return itemStatus; }
  148.  
  149. - (void)setItemTimer:(NSTimer *)aTimer
  150. {
  151.     if (itemTimer) {
  152.         [itemTimer invalidate];
  153.         [itemTimer autorelease];
  154.     }
  155.     if (aTimer) itemTimer = [aTimer retain];
  156. }
  157.  
  158. - (NSTimer *)itemTimer { return itemTimer; }
  159.  
  160.  
  161.  
  162. long ConvertTimeToSeconds(int hr, int min, BOOL flag)
  163. {
  164.     if (flag) { /* PM */
  165.         if (hr >= 1 && hr < 12)
  166.             hr += 12;
  167.     } else {
  168.         if (hr == 12)
  169.             hr = 0;
  170.     }
  171.     return ((hr * hrInSecs) + (min * minInSecs));
  172. }
  173.  
  174.  
  175. BOOL ConvertSecondsToTime(long secs, int *hour, int *minute)
  176. {
  177.     int hr=0;
  178.     BOOL pm=NO;
  179.  
  180.     if (secs) {
  181.         hr = secs / hrInSecs;
  182.         if (hr > 12) {
  183.             *hour = (hr -= 12);
  184.             pm = YES;
  185.         } else {
  186.             pm = NO;
  187.             if (hr == 0)
  188.                 hr = 12;
  189.             *hour = hr;
  190.         }
  191.         *minute = ((secs%hrInSecs) / minInSecs);
  192.     }
  193.     return pm;
  194. }
  195.  
  196. @end
  197.