home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / RZToDoList / Source / ToDoItemPublic.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.6 KB  |  229 lines

  1. /* 
  2.  * ToDoItemPublic - public implementation of ToDoItem, does not contain
  3.  *        priority calculation code
  4.  *
  5.  * You may freely copy, distribute and reuse the code in this example.
  6.  * This code is provided AS IS without warranty of any kind, expressed 
  7.  * or implied, as to its fitness for any particular use.
  8.  *
  9.  * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
  10.  *
  11.  */
  12.  
  13. #import "ToDoItemPublic.h"
  14. #import <appkit/appkit.h>
  15.  
  16. @implementation ToDoItemPublic
  17.  
  18. static char *RZCopyStringBuffer(const char *s)
  19. {
  20.     return s ? NXCopyStringBuffer(s) : NULL;
  21. }
  22.  
  23. static char *RZReplaceStringBuffer(char **s, const char *new)
  24. {
  25.     if(s && *s) {
  26.         NX_FREE(*s);
  27.         *s = NULL;
  28.     }
  29.     
  30.     *s = RZCopyStringBuffer(new);
  31.     
  32.     return *s;
  33. }
  34.  
  35. + initialize
  36. {
  37.     if([self class] == [ToDoItemPublic class]) {
  38.         [self setVersion:1];
  39.     }
  40.     return self;
  41. }
  42.  
  43. - initSubject:(char *)subj startDate:(long)start dueDate:(long)due
  44.     completeDate:(long)completed type:(char)type isPrivate:(BOOL)privateFlag
  45.     isCompleted:(BOOL)completeFlag data:(char *)buf dataLen:(int)len
  46. {
  47.     if(self = [super init]) {
  48.         RZReplaceStringBuffer(&subject, subj);
  49.         
  50.         startDate = start ? start : time(NULL);
  51.         dueDate = due ? due : time(NULL)+60;
  52.         dateCompleted = completed;
  53.         
  54.         _flags.type = type;
  55.         _flags.private = privateFlag;
  56.         _flags.completed = completeFlag;
  57.         
  58.         RZReplaceStringBuffer(&data, buf);
  59.         dataLen = len;
  60.         
  61.     }
  62.     return self;
  63. }
  64.     
  65. - initFromItem:(id <ToDoItems>)anItem
  66. {
  67.     char *buf;
  68.     int len;
  69.     
  70.     [anItem getData:&buf length:&len];
  71.     
  72.     return [self initSubject:[anItem subject] startDate:[anItem startDate]
  73.                 dueDate:[anItem dueDate] completeDate:[anItem dateCompleted]
  74.                 type:[anItem type] isPrivate:[anItem isPrivate]
  75.                 isCompleted:[anItem isCompleted] data:buf dataLen:len];
  76. }
  77.  
  78. - init
  79. {
  80.     return [self notImplemented:_cmd];
  81. }
  82.  
  83. - free
  84. {
  85.     if(subject) {
  86.         NX_FREE(subject);
  87.         subject = NULL;
  88.     }
  89.     if(data) {
  90.         NX_FREE(data);
  91.         data = NULL;
  92.         dataLen = 0;
  93.     }
  94.     
  95.     return [super free];
  96. }
  97.  
  98. - (char *)subject                        { return subject; }
  99. - (long)startDate                        { return startDate; }
  100. - (long)dueDate                        { return dueDate; }
  101. - (long)dateCompleted                { return dateCompleted; }
  102. - (char)type                            { return _flags.type; }
  103. - (BOOL)isPrivate                        { return _flags.private; }
  104. - (BOOL)isCompleted                    { return _flags.completed; }
  105.  
  106. - (const char *)asciiStartDate
  107.     static char buf[40];
  108.     
  109.     if(!startDate) {
  110.         return NULL;
  111.     }
  112.     
  113.     strftime(buf, 40, "%m/%d/%y", localtime(&startDate));
  114.  
  115.     return buf;
  116. }
  117.  
  118. - (const char *)asciiCompletedDate
  119.     static char buf[40];
  120.     
  121.     if(!dateCompleted) {
  122.         return NULL;
  123.     }
  124.     
  125.     strftime(buf, 40, "%m/%d/%y", localtime(&dateCompleted));
  126.  
  127.     return buf;
  128. }
  129.  
  130. - (const char *)asciiDueDate
  131.     static char buf[40];
  132.     
  133.     switch(_flags.type) {
  134.         case TODO_TYPE_NORMAL :
  135.         case TODO_TYPE_APPOINTMENT :
  136.             strftime(buf, 40, "%m/%d/%y", localtime(&dueDate));
  137.             break;
  138.         case TODO_TYPE_HIGHPRIORITY :
  139.             sprintf(buf, "ASAP!");
  140.             break;
  141.         case TODO_TYPE_LOWPRIORITY :
  142.             sprintf(buf, "LATER");
  143.             break;
  144.     }
  145.     
  146.     return buf;
  147. }
  148.  
  149. - getData:(char **)d length:(int *)len
  150. {
  151.     *d = data;
  152.     *len = dataLen;
  153.     return self;
  154. }
  155.     
  156. /*** archival methods ***/
  157.  
  158. - read:(NXTypedStream *)ts
  159. {
  160.     [super read:ts];
  161.     
  162.     switch(NXTypedStreamClassVersion(ts, [[self class] name])) {
  163.         case 1 :
  164.             NXReadTypes(ts, "*lll*iI",
  165.                 &subject, &startDate, &dueDate, &dateCompleted, 
  166.                 &data, &dataLen, _flags);
  167.             break;    
  168.     }
  169.     
  170.     return self;
  171. }
  172.  
  173. - write:(NXTypedStream *)ts
  174. {
  175.     [super write:ts];
  176.     NXWriteTypes(ts, "*lll*iI",
  177.         &subject, &startDate, &dueDate, &dateCompleted, 
  178.         &data, &dataLen, _flags);
  179.     return self;
  180. }
  181.  
  182. @end
  183.  
  184. @implementation ToDoItemPublic(Transport)
  185.  
  186. - encodeUsing:(id <NXEncoding>)stream
  187. {
  188.     [stream encodeData:&subject ofType:"*"];
  189.     [stream encodeData:&startDate ofType:"l"];
  190.     [stream encodeData:&dueDate ofType:"l"];
  191.     [stream encodeData:&dateCompleted ofType:"l"];
  192.     [stream encodeData:&data ofType:"*"];
  193.     [stream encodeData:&dataLen ofType:"i"];
  194.     [stream encodeData:&_flags ofType:"I"];
  195.     return self;
  196. }
  197.  
  198. - decodeUsing:(id <NXDecoding>)stream
  199. {
  200.     [stream decodeData:&subject ofType:"*"];
  201.     [stream decodeData:&startDate ofType:"l"];
  202.     [stream decodeData:&dueDate ofType:"l"];
  203.     [stream decodeData:&dateCompleted ofType:"l"];
  204.     [stream decodeData:&data ofType:"*"];
  205.     [stream decodeData:&dataLen ofType:"i"];
  206.     [stream decodeData:&_flags ofType:"I"];
  207.     return self;
  208. }
  209.  
  210. - encodeRemotelyFor:(NXConnection *)connection
  211.     freeAfterEncoding:(BOOL *)flagp
  212.     isBycopy:(BOOL)isBycopy
  213. {
  214.     if (isBycopy) {
  215.         *flagp = YES;        // if bycopy, free the local instance
  216.         return self;
  217.     }
  218.  
  219.     // otherwise, super's behavior is to encode a proxy
  220.    return [super encodeRemotelyFor:connection
  221.         freeAfterEncoding:flagp
  222.         isBycopy:isBycopy];
  223. }
  224.  
  225. @end
  226.