home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume2 / boss-sparc / part03 / do_schedule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-23  |  7.1 KB  |  234 lines

  1. /************************************************************************/
  2. /*    Copyright 1990 by Chuck Musciano and Harris Corporation        */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.        */
  15. /*                                    */
  16. /*    This code contains data and information that is proprietary    */
  17. /*    to Casio Corporation.  You may be subject to legal action if    */
  18. /*    this information is released without explicit permission from    */
  19. /*    Casio.                                */
  20. /************************************************************************/
  21.  
  22. /************************************************************************/
  23. /*                                    */
  24. /*    do_schedule    add Casio appointments to calentool file    */
  25. /*                                    */
  26. /************************************************************************/
  27.  
  28. #include    <stdio.h>
  29. #include    <sys/param.h>
  30. #include    <sys/types.h>
  31. #include    <xview/xview.h>
  32. #include    <xview/panel.h>
  33. #include    "boss_ui.h"
  34.  
  35. #include    "manifest.h"
  36. #include    "object.h"
  37. #include    "globals.h"
  38.  
  39. #include    "calentool.h"
  40.  
  41. #define        round_minute(x)        (((x) < 30)? 0 : 30)
  42. #define        wc(x, y)        (((x) == WILDCARD) || ((x) == (y)))
  43.  
  44. #define        CALENTOOL        0
  45. #define        CALENDAR_MANAGER    1
  46.  
  47. #define        CALENTOOL_START        (8 * 2) /* 8 AM in half hours */
  48. #define        CALENTOOL_END        (17 * 2 + 1) /* 5:30 PM */
  49.  
  50. PUBLIC    boss_base_objects    *boss_base;
  51.  
  52. PRIVATE    boss_schedule_dialog_objects    *dialog = NULL;
  53.  
  54. /************************************************************************/
  55. PRIVATE    reconcile_schedule_range(hour, minute, len, msg)
  56.  
  57. int    *hour;
  58. int    *minute;
  59. int    *len;
  60. char    **msg;
  61.  
  62. {    char    *p;
  63.     int    start, end, oh, om, new_start = FALSE, new_end = FALSE;
  64.  
  65.     start = *hour * 2 + *minute / 30;
  66.     end = start + *len;
  67.     if (start < CALENTOOL_START) {
  68.        start = CALENTOOL_START;
  69.        new_start = TRUE;
  70.        }
  71.     if (start > CALENTOOL_END) {
  72.        start = CALENTOOL_END;
  73.        new_start = TRUE;
  74.        }
  75.     if (end < CALENTOOL_START) {
  76.        end = CALENTOOL_START;
  77.        new_end = TRUE;
  78.        }
  79.     if (end > CALENTOOL_END) {
  80.        end = CALENTOOL_END;
  81.        new_end = TRUE;
  82.        }
  83.     if (new_start || new_end) {
  84.        p = (char *) malloc(strlen(*msg) + 20);
  85.        oh = *hour + *len / 2;
  86.        om = *minute + (*len % 2 + 1) * 30;
  87.        if (om >= 60) {
  88.           oh++;
  89.           om -= 60;
  90.           }
  91.        if (oh > 12)
  92.           oh -= 12;
  93.        if (new_start && !new_end)
  94.           sprintf(p, "%s (%d:%02d)", *msg, (*hour > 12)? *hour - 12 : *hour, *minute);
  95.        else if (new_start && new_end)
  96.           sprintf(p, "%s (%d:%02d to %d:%02d)", *msg, (*hour > 12)? *hour - 12 : *hour, *minute, oh, om);
  97.        else if (!new_start && new_end)
  98.           sprintf(p, "%s (to %d:%02d)", *msg, oh, om);
  99.        free(*msg);
  100.        *msg = p;
  101.        *hour = start / 2;
  102.        *minute = (start % 2) * 30;
  103.        *len = end - start;
  104.        }
  105. }
  106.  
  107. /************************************************************************/
  108. PRIVATE    int    convert_to_calentool()
  109.  
  110. {    calen    *cal, *app, *new;
  111.     char    *path, *p, *msg;
  112.     object    *obj;
  113.     int    year, hour, minute, len;
  114.  
  115.     path = (char *) xv_get(dialog->appt_file, PANEL_VALUE);
  116.     if (cal = load_calentool(path)) {
  117.        for (obj = data[OBJ_SCHEDULE]; obj; obj = obj->next) {
  118.           msg = strsave(obj->sch_memo);
  119.           for (p = msg; *p; p++)
  120.              if (*p == '\r')
  121.                 *p = ' ';
  122.           year = obj->sch_date.year % 100;
  123.           if (obj->sch_start_time.hour == INVALID_TIME) {
  124.              hour = 99;
  125.              minute = 99;
  126.              }
  127.           else {
  128.              hour = obj->sch_start_time.hour;
  129.              minute = round_minute(obj->sch_start_time.minute);
  130.              }
  131.           if (obj->sch_stop_time.hour == INVALID_TIME)
  132.              len = 0;
  133.           else
  134.              len = ((obj->sch_stop_time.hour * 60 + round_minute(obj->sch_stop_time.minute)) - (hour * 60 + minute)) / 30 - 1;
  135.           if (hour != 99)
  136.              reconcile_schedule_range(&hour, &minute, &len, &msg);
  137.           for (app = cal; app; app = app->next) {
  138.              if (wc(app->year, year) && wc(app->month, obj->sch_date.month) && wc(app->day, obj->sch_date.day)) {
  139.                 if (hour == 99) {
  140.                    if (app->hour == 99 && strcmp(app->message, msg) == 0)
  141.                       break;
  142.                    }
  143.                 else if (hour == app->hour && minute == app->minute && len == app->length && strcmp(app->message, msg) == 0)
  144.                    break;
  145.                 }
  146.              }
  147.           if (app == NULL) {
  148.              new = (calen *) malloc(sizeof(calen));
  149.              bzero(new, sizeof(calen));
  150.              new->kind = LINE_APPOINTMENT;
  151.              new->year = year;
  152.              new->month = obj->sch_date.month;
  153.              new->day = obj->sch_date.day;
  154.              new->hour = hour;
  155.              new->minute = (hour == 99)? 0 : minute;
  156.              new->length = len;
  157.              new->repeat = -1;
  158.              new->message = msg;
  159.              for (app = cal; app->next; app = app->next)
  160.                 ;
  161.              app->next = new;
  162.              }
  163.           else
  164.              free(msg);
  165.           }
  166.        store_calentool(path, cal);
  167.        return(XV_OK);
  168.        }
  169.     return(XV_ERROR);
  170. }
  171.  
  172. /************************************************************************/
  173. PRIVATE    int    convert_to_calendar_manager()
  174.  
  175. {
  176.     error("Calendar Manager conversion is not yet available");
  177.     return(XV_ERROR);
  178. }
  179.  
  180. /************************************************************************/
  181. EXPORT    Menu_item    schedule_dialog(item, op)
  182.  
  183. Menu_item    item;
  184. Menu_generate    op;
  185.  
  186. {
  187.     if (op == MENU_NOTIFY) {
  188.        if (dialog == NULL) {
  189.           dialog = boss_schedule_dialog_objects_initialize(NULL, boss_base->base);
  190.           xv_set(dialog->appt_file,
  191.                     PANEL_VALUE, appts_file,
  192.                     PANEL_NOTIFY_LEVEL, PANEL_SPECIFIED,
  193.                     PANEL_NOTIFY_STRING, "\n\r ",
  194.                     PANEL_INACTIVE, FALSE,
  195.                  NULL);
  196.           xv_set(dialog->schedule_format, PANEL_VALUE, 0, NULL);
  197.           place_dialog(boss_base->base, dialog->schedule_dialog);
  198.           }
  199.        xv_set(dialog->schedule_dialog, XV_SHOW, TRUE, NULL);
  200.        }
  201.     return item;
  202. }
  203.  
  204. /************************************************************************/
  205. EXPORT    void    schedule_notify(item, value, event)
  206.  
  207. Panel_item    item;
  208. int        value;
  209. Event        *event;
  210.  
  211. {
  212.     if (value == CALENTOOL)
  213.        xv_set(dialog->appt_file, PANEL_INACTIVE, FALSE, NULL);
  214.     else if (value == CALENDAR_MANAGER)
  215.        xv_set(dialog->appt_file, PANEL_INACTIVE, TRUE, NULL);
  216. }
  217.  
  218. /************************************************************************/
  219. EXPORT    void    accept_schedule(item, event)
  220.  
  221. Panel_item    item;
  222. Event        *event;
  223.  
  224. {    int    result, format;
  225.  
  226.     lets_get_busy(TRUE, NULL);
  227.     if ((format = (int) xv_get(dialog->schedule_format, PANEL_VALUE)) == CALENTOOL)
  228.        result = convert_to_calentool();
  229.     else if (format == CALENDAR_MANAGER)
  230.        result = convert_to_calendar_manager();
  231.     xv_set(item, PANEL_NOTIFY_STATUS, result, NULL);
  232.     lets_get_busy(FALSE, NULL);
  233. }
  234.