home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / filesbbs / os2 / plnk065.arj / PLNK065.ZIP / pilot-link.0.6.5 / read-ical.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-23  |  6.9 KB  |  277 lines

  1. /* read-ical.c:  Translate Pilot ToDo and Datebook databases into ical 2.0 format
  2.  *
  3.  * Copyright (c) 1996, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-todo.h"
  15. #include "pi-datebook.h"
  16. #include "pi-dlp.h"
  17.  
  18. char * tclquote(char * in)
  19. {
  20.   static char * buffer = 0;
  21.   char * out;
  22.   char * pos;
  23.   int len;
  24.   
  25.   len = 3;
  26.   pos = in;
  27.   while(*pos) {
  28.     if((*pos == '\\') || (*pos == '"') || (*pos == '[') || (*pos == '{') || (*pos == '$'))
  29.       len++;
  30.     len++;
  31.     pos++;
  32.   }
  33.   
  34.   if (buffer)
  35.     free(buffer);
  36.   buffer = (char*)malloc(len);
  37.   out = buffer;
  38.  
  39.   pos = in;
  40.   *out++ = '"';
  41.   while(*pos) {
  42.     if((*pos == '\\') || (*pos == '"') || (*pos == '[') || (*pos == '{') || (*pos == '$'))
  43.       *out++ = '\\';
  44.     *out++=*pos++;
  45.   }
  46.   *out++ = '"';
  47.   *out++ = '\0';
  48.   
  49.   return buffer;
  50. }
  51.  
  52. int main(int argc, char *argv[])
  53. {
  54.   struct pi_sockaddr addr;
  55.   int db;
  56.   int sd;
  57.   int i;
  58.   FILE *ical;
  59.   struct PilotUser U;
  60.   int ret;
  61.   unsigned char buffer[0xffff];
  62.   char cmd[128];
  63.   struct ToDoAppInfo tai;
  64.  
  65.   if (argc < 3) {
  66.     fprintf(stderr,"usage:%s %s calfile # Calfile will be overwritten!\n",argv[0],TTYPrompt);
  67.     exit(2);
  68.   }
  69.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  70.     perror("pi_socket");
  71.     exit(1);
  72.   }
  73.     
  74.   addr.pi_family = PI_AF_SLP;
  75.   addr.pi_port = 3;
  76.   strcpy(addr.pi_device,argv[1]);
  77.   
  78.   ret = pi_bind(sd, &addr, sizeof(addr));
  79.   if(ret == -1) {
  80.     perror("pi_bind");
  81.     exit(1);
  82.   }
  83.  
  84.   ret = pi_listen(sd,1);
  85.   if(ret == -1) {
  86.     perror("pi_listen");
  87.     exit(1);
  88.   }
  89.  
  90.   sd = pi_accept(sd, 0, 0);
  91.   if(sd == -1) {
  92.     perror("pi_accept");
  93.     exit(1);
  94.   }
  95.  
  96.   /* Ask the pilot who it is. */
  97.   dlp_ReadUserInfo(sd,&U);
  98.   
  99.   /* Tell user (via Pilot) that we are starting things up */
  100.   dlp_OpenConduit(sd);
  101.   
  102.   /* Open the ToDo database, store access handle in db */
  103.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "ToDoDB", &db) < 0) {
  104.     puts("Unable to open ToDoDB");
  105.     dlp_AddSyncLogEntry(sd, "Unable to open ToDoDB.\n");
  106.     exit(1);
  107.   }
  108.   
  109.   dlp_ReadAppBlock(sd, db, 0, buffer, 0xffff);
  110.   unpack_ToDoAppInfo(&tai, buffer, 0);
  111.   
  112.   unlink(argv[2]);
  113.   sprintf(cmd, "ical -f - -calendar %s", argv[2]);
  114.   ical = popen(cmd, "w");
  115.   
  116.   fprintf(ical,"calendar cal $ical(calendar)\n");
  117.   
  118.   for (i=0;1;i++) {
  119.       struct ToDo t;
  120.       int attr, category;
  121.                                  
  122.       int len = dlp_ReadRecordByIndex(sd, db, i, buffer, 0, 0, &attr, &category);
  123.       if(len<0)
  124.           break;
  125.           
  126.       /* Skip deleted records */
  127.       if((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
  128.           continue;
  129.           
  130.     unpack_ToDo(&t, buffer, len);
  131.     
  132.     fprintf(ical,"set n [notice]\n");
  133.     fprintf(ical,"$n text %s\n", tclquote(t.description));
  134.     fprintf(ical,"$n date [date today]\n");
  135.     fprintf(ical,"$n todo 1\n");
  136.     fprintf(ical,"$n option Priority %d\n", t.priority);
  137.     fprintf(ical,"$n done %d\n", t.complete ? 1 : 0);
  138.     fprintf(ical,"cal add $n\n");
  139.     
  140.     free_ToDo(&t);
  141.   }
  142.  
  143.   /* Close the database */
  144.   dlp_CloseDB(sd, db);
  145.  
  146.   dlp_AddSyncLogEntry(sd, "Read todos from Pilot.\n");
  147.  
  148.   /* Open the Datebook's database, store access handle in db */
  149.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "DatebookDB", &db) < 0) {
  150.     puts("Unable to open DatebookDB");
  151.     dlp_AddSyncLogEntry(sd, "Unable to open DatebookDB.\n");
  152.     pi_close(sd);
  153.     exit(1);
  154.   }
  155.   
  156.   
  157.  
  158.   for (i=0;1;i++) {
  159.       int j;
  160.       struct Appointment a;
  161.       int attr;
  162.                                  
  163.       int len = dlp_ReadRecordByIndex(sd, db, i, buffer, 0, 0, &attr, 0);
  164.       if(len<0)
  165.           break;
  166.           
  167.       /* Skip deleted records */
  168.       if((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
  169.           continue;
  170.           
  171.     unpack_Appointment(&a, buffer, len);
  172.     
  173.     if (a.event) {
  174.       fprintf(ical,"set i [notice]\n");
  175.  
  176.     } else {
  177.       int start,end;
  178.  
  179.       fprintf(ical,"set i [appointment]\n");
  180.       
  181.       start = a.begin.tm_hour*60 + a.begin.tm_min;
  182.       end   = a.end.tm_hour*60 + a.end.tm_min;
  183.  
  184.       fprintf(ical,"$i starttime %d\n", start);
  185.       fprintf(ical,"$i length %d\n", end-start+1);
  186.     }
  187.     
  188.     fprintf(ical,"$i text %s\n", tclquote(a.description));
  189.     
  190.     fprintf(ical,"set begin [date make %d %d %d]\n", a.begin.tm_mday,a.begin.tm_mon+1,a.begin.tm_year+1900);
  191.     
  192.     if (a.repeatFreq) {
  193.       if (a.repeatType == repeatDaily) {
  194.         fprintf(ical,"$i dayrepeat %d $begin\n", a.repeatFreq);
  195.       } else if(a.repeatType == repeatMonthlyByDate) {
  196.         fprintf(ical,"$i month_day %d $begin %d\n",a.begin.tm_mon+1,a.repeatFreq);
  197.       } else if(a.repeatType == repeatMonthlyByDay) {
  198.         if (a.repeatOn>=domLastSun) {
  199.           fprintf(ical,"$i month_last_week_day %d 1 $begin %d\n", a.repeatOn % 7 + 1,
  200.                                                         a.repeatFreq);
  201.         } else {
  202.           fprintf(ical,"$i month_week_day %d %d $begin %d\n", a.repeatOn % 7 + 1,
  203.                                                         a.repeatOn / 7 + 1,
  204.                                                         a.repeatFreq);
  205.         }
  206.       } else if(a.repeatType == repeatWeekly) {
  207.         /*
  208.          * Handle the case where the user said weekly repeat, but
  209.          * really meant daily repeat every n*7 days.  Note: We can't
  210.          * do days of the week and a repeat-frequency > 1, so do the
  211.          * best we can and go on.
  212.          */
  213.         if (a.repeatFreq > 1) {
  214.         int ii, found;
  215.         for (ii = 1, found = 0; ii < 128; ii <<= 1) {
  216.             if (a.repeatOn & i)
  217.             found++;
  218.         }
  219.         if (found > 1)
  220.             fprintf(stderr, "Incomplete translation of %s\n",
  221.                 a.description);
  222.         fprintf(ical,"$i dayrepeat %d $begin\n", a.repeatFreq * 7);
  223.         } else {
  224.         fprintf(ical,"$i weekdays ");
  225.         if(a.repeatOn & 1)
  226.           fprintf(ical,"1 ");
  227.         if(a.repeatOn & 2)
  228.           fprintf(ical,"2 ");
  229.         if(a.repeatOn & 4)
  230.           fprintf(ical,"3 ");
  231.         if(a.repeatOn & 8)
  232.           fprintf(ical,"4 ");
  233.         if(a.repeatOn & 16)
  234.           fprintf(ical,"5 ");
  235.         if(a.repeatOn & 32)
  236.           fprintf(ical,"6 ");
  237.         if(a.repeatOn & 64)
  238.           fprintf(ical,"7 ");
  239.         fprintf(ical,"\n");
  240.         }
  241.       } else if(a.repeatType == repeatYearly) {
  242.         fprintf(ical,"$i monthrepeat %d $begin\n", 12 * a.repeatFreq);
  243.       }
  244.       fprintf(ical,"$i start $begin\n");
  245.       if (!a.repeatForever)
  246.         fprintf(ical,"$i finish [date make %d %d %d]\n", a.repeatEnd.tm_mday, a.repeatEnd.tm_mon+1, 
  247.                                                    a.repeatEnd.tm_year+1900);
  248.       if (a.exceptions)
  249.         for (j=0;j<a.exceptions;j++)
  250.           fprintf(ical,"$i deleteon [date make %d %d %d]\n", a.exception[j].tm_mday,
  251.                                                        a.exception[j].tm_mon+1,
  252.                                                        a.exception[j].tm_year+1900);
  253.     } else 
  254.           fprintf(ical,"$i date $begin\n");
  255.     
  256.     fprintf(ical,"cal add $i\n");
  257.  
  258.     free_Appointment(&a);
  259.         
  260.   }
  261.   
  262.   fprintf(ical,"cal save [cal main]\n");
  263.   fprintf(ical,"exit\n");
  264.   
  265.   pclose(ical);
  266.  
  267.   /* Close the database */
  268.   dlp_CloseDB(sd, db);
  269.  
  270.   dlp_AddSyncLogEntry(sd, "Read datebook from Pilot.\n");
  271.  
  272.   pi_close(sd);  
  273.   
  274.   return 0;
  275. }
  276.  
  277.