home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Examples / AppKit / ToDo / ToDoItem.m < prev    next >
Encoding:
Text File  |  1996-03-29  |  4.1 KB  |  191 lines

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