home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Productivity / EggTimer-2.5-I / Reminder.m < prev    next >
Encoding:
Text File  |  1998-03-30  |  3.5 KB  |  172 lines

  1. /* Reminder.m created by wjabi on Sat 17-Jan-1998 */
  2.  
  3. #import "Reminder.h"
  4.  
  5. @implementation Reminder
  6.  
  7. - (void) cancelReminder
  8. {
  9.     if(timer)
  10.       {
  11.         [timer invalidate];
  12.       }
  13. }
  14.  
  15. - (void) convertSecondsToTime:(long)totalSeconds :(int *)hrs :(int *)mins :(int *)secs
  16. {
  17.     *hrs = totalSeconds/3600;
  18.     *mins = ((totalSeconds%3600)/60);
  19.     *secs = ((totalSeconds%60));
  20. }
  21.  
  22. - (long) convertTimeToSeconds:(int)hr :(int)min :(int)sec
  23. {
  24.     return (long) ((hr *3600) + (min * 60) + sec);
  25. }
  26.  
  27. - (BOOL) getRepeats
  28. {
  29.     return repeats;
  30. }
  31.  
  32. - (BOOL) getRelative
  33. {
  34.     return relative;
  35. }
  36.  
  37. - (BOOL) getValid
  38. {
  39.     if(timer)
  40.       {
  41.         return [timer isValid];
  42.       }
  43.     else
  44.       {
  45.         return NO;
  46.       }
  47. }
  48.  
  49. -(id) init
  50. {
  51.     [super init];
  52.     reminderString = @"";
  53.     repeats = NO;
  54.     relative = NO;
  55.     return self;
  56. }
  57.  
  58. - (void) remainingTime:(int *)hr minutes:(int *)min seconds:(int *)sec
  59. {
  60.     NSDate *fireDate;
  61.     NSTimeInterval timeInterval = 0;
  62.     if(timer)
  63.       {
  64.         if([timer isValid])
  65.           {
  66.             fireDate = [timer fireDate];
  67.             timeInterval = [fireDate timeIntervalSinceNow];
  68.           }
  69.       }
  70.     if(relative == 0)
  71.       {
  72.         *hr = hours;
  73.         *min = minutes;
  74.         *sec = seconds;
  75.         return;
  76.       }
  77.     else
  78.       {
  79.         if(timeInterval > 0)
  80.           {
  81.             [self convertSecondsToTime:timeInterval :hr :min :sec];
  82.             return;
  83.           }
  84.         else
  85.           {
  86.             *hr  = 0;
  87.             *min = 0;
  88.             *sec = 0;
  89.             return;
  90.           }
  91.  
  92.       }
  93. }
  94.  
  95. - (void) setRelative:(BOOL)relativeValue
  96. {
  97.     relative = relativeValue;
  98. }
  99.  
  100. - (id) setReminder:(int)hr :(int)min :(int)sec :aTarget :(SEL)aSelector :(BOOL)repeatsValue :(BOOL)relativeValue :(BOOL)soundValue :(BOOL)alertPanelValue :(NSString *)aString
  101. {
  102.     long timeInterval, todayTimeInterval, newTimeInterval;
  103.     NSDate *newDate, *today;
  104.     repeats = repeatsValue;
  105.     relative = relativeValue;
  106.     shouldEmitSound = soundValue;
  107.     shouldShowAlertPanel = alertPanelValue;
  108.     [self setStringValue:aString];
  109.     if(timer)
  110.       {
  111.         [timer invalidate];
  112.         [timer autorelease];
  113.       }
  114.     if (relative == NO)
  115.       {
  116.         hours = hr;
  117.         minutes = min;
  118.         seconds = sec;
  119.         newDate = [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@"today %02d:%02d:%02d",hr, min, sec] locale:nil];
  120.         newTimeInterval = [newDate timeIntervalSince1970];
  121.         today = [NSDate date];
  122.         todayTimeInterval = [today timeIntervalSince1970];
  123.         if (newTimeInterval < todayTimeInterval)
  124.           {
  125.             newTimeInterval += 12*60*60;
  126.           }
  127.         if (newTimeInterval < todayTimeInterval)
  128.           {
  129.             newTimeInterval += 12*60*60;
  130.           }
  131.         timeInterval = newTimeInterval - todayTimeInterval;
  132.       }
  133.     else
  134.       {
  135.         timeInterval = [self convertTimeToSeconds:hr :min :sec];
  136.       }
  137.     timer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval target:aTarget selector:aSelector userInfo:self repeats:(BOOL)repeats];
  138.     [timer retain];
  139.     return self;
  140. }
  141.  
  142. - (void) setRepeats:(BOOL)repeatsValue
  143. {
  144.     repeats = repeatsValue;
  145. }
  146.  
  147. - (void) setStringValue:(NSString *)aString
  148. {
  149.     if(reminderString)
  150.       {
  151.         [reminderString autorelease];
  152.       }
  153.     reminderString = [aString copy];
  154. }
  155.  
  156. - (BOOL) shouldEmitSound
  157. {
  158.     return shouldEmitSound;
  159. }
  160.  
  161. - (BOOL) shouldShowAlertPanel
  162. {
  163.     return shouldShowAlertPanel;
  164. }
  165.  
  166. - (NSString *) stringValue
  167. {
  168.     return reminderString;
  169. }
  170.  
  171. @end
  172.