home *** CD-ROM | disk | FTP | other *** search
/ YPA: Your Privacy Assured / YPA.ISO / other_goodies / utilities / memomaster.lha / MM2.1 / source / mm2.c < prev    next >
C/C++ Source or Header  |  1994-09-12  |  13KB  |  473 lines

  1. /*  MM2.C
  2.  *    Looks for files on assigned device PROGDIR:
  3.  *        Files are MEMODATA.DAT and MMASTER.CONFIG
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <intuition/intuition.h>
  9. #include <dos/dos.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. #include <exec/memory.h>
  13. #include <string.h>
  14. #include <exec/nodes.h>
  15. #include <exec/lists.h>
  16. #include <workbench/workbench.h>
  17.  
  18. #include "mm2.h"
  19.  
  20. /* Prototypes */
  21. Prototype int CleanUp(void);
  22. Prototype void Action(void);
  23. Prototype BOOL MemoChk(BOOL);
  24. Prototype int main(int, char **);
  25. Prototype int wbmain(struct WBStartup *);
  26. Prototype void Display_One(int,struct MI_Mem *);
  27. Prototype void Display_Blank(int);
  28. Prototype int Shrink(void);
  29. Prototype struct Window *MainWindow(void);
  30. Prototype struct Window *mm_w;
  31. Prototype struct MinNode *DisplayFirst;
  32. Prototype struct MinList *MemListPtr;
  33. Prototype struct Gadget *mem_t_line[];
  34. Prototype struct Gadget *m_gad_type[];
  35. Prototype struct Gadget *m_gad_notice[];
  36. Prototype struct Gadget *m_gad_date[];
  37. Prototype struct Gadget *m_gad_checked[];
  38.  
  39. const char verstr[] = "\0$VER: MemoMaster 2.1 (12.9.94)";
  40.  
  41. struct IntuitionBase *IntuitionBase;
  42. struct Library *WorkbenchBase;
  43. struct Window *mm_w;
  44. struct Remember *RK;
  45. struct Gadget *mem_t_line[6];
  46. struct Gadget *m_gad_type[6];
  47. struct Gadget *m_gad_notice[6];
  48. struct Gadget *m_gad_date[6];
  49. struct Gadget *m_gad_checked[6];
  50.  
  51. int CPLine;
  52. int Colour[4];
  53. struct MinList *MemListPtr;
  54. struct MinNode *DisplayFirst;
  55.  
  56. extern BOOL DataAmended;
  57.  
  58. struct Gadget gtgads[MEMOS_IN_BLOCK];
  59. struct MM_Config mm_prefs;
  60. struct MsgPort *port;
  61. BOOL User_Config;
  62. BOOL Check;
  63. BOOL WBStart;
  64.  
  65. char date_today[10];
  66. char MWTitle[60];
  67.  
  68. /*========================================================================
  69.  *###======   wbmain() function   ========================================
  70.  * DICE specific function. Program starts here if executed from WorkBench
  71.  *========================================================================*/
  72. int wbmain(struct WBStartup *wbs)
  73.   {
  74.   WBStart = TRUE;
  75.   return main();
  76.   }
  77.  
  78. /*==========================================================================*/
  79. /*###======   main() function   ============================================*/
  80. /*==========================================================================*/
  81. main(int argc, char *argv[])
  82.   {
  83.   BOOL MFound;
  84.  
  85.   WorkbenchBase = OpenLibrary("workbench.library",37);
  86.   if ((IntuitionBase=(struct IntuitionBase *)
  87.         OpenLibrary("intuition.library",37))==NULL)
  88.     {
  89.     printf("Failed to open intuition library\n");
  90.     CleanUp();
  91.     exit(0);
  92.     }
  93.  
  94. /* Create a MsgPort for use when shrunk to an AppIcon */
  95.   port = CreateMsgPort();
  96.  
  97.   MemListPtr = LoadData();
  98.   if (!MemListPtr)
  99.     {
  100.     printf("Failed to create list of memos\n");
  101.     CleanUp();
  102.     exit(0);
  103.     }
  104.   Today(date_today);      /* Set up todays date string */
  105.   sprintf(MWTitle, "MemoMaster V2.1  -  %s", date_today);
  106.   MMWdt = MWTitle;
  107.  
  108. /* If started from WorkBench or if request on command line check for memos
  109.  *  for today.
  110.  */
  111.   MFound=TRUE;
  112.   if (stricmp(argv[1],"check")==0) Check = TRUE;
  113.   if (stricmp(argv[1],"?")==0)
  114.   {
  115.     puts("MemoMaster v2.1 by Jeff Flynn, updated by Simon Dick\n"
  116.          "    Use the CHECK argument to check for any memos for the day\n"
  117.          "    Use no arguments to enter the memo editor.");
  118.     CleanUp();
  119.     exit(0);
  120.   }
  121.   if (((argc > 1) && (Check)) || (WBStart))
  122.     MFound=MemoChk(FALSE);
  123.  
  124.   if (!Check)
  125.     {
  126.     mm_w=MainWindow();
  127.     if(!mm_w)
  128.       {
  129.       printf("Failed to open MemoMaster window\n");
  130.       CleanUp();
  131.       exit(0);
  132.       }
  133.     Action();
  134.     }
  135.   CleanUp();
  136.   }
  137. /*=====================================================================*/
  138. CleanUp()
  139.   {
  140.   CloseMMWindow();
  141.   CloseDownScreen();
  142.   FreeRemember(&RK,TRUE);
  143.   if(port)DeleteMsgPort();
  144.   if(IntuitionBase)CloseLibrary(IntuitionBase);
  145.   if(WorkbenchBase)CloseLibrary(WorkbenchBase);
  146.   return 1;
  147.   }
  148. /*=====================================================================*/
  149. void Action()
  150. {
  151.   int x;
  152.   BOOL finished = FALSE;
  153.   ULONG SignalMask, Signal;
  154.  
  155.   if ((MemListPtr == NULL) || (MemListPtr->mlh_Head == NULL))
  156.     DisplayFirst = NULL;
  157.   else
  158.     DisplayFirst = MemListPtr->mlh_Head;
  159.  
  160.   Display_Block(DisplayFirst);
  161.   SignalMask = 1L << mm_w->UserPort->mp_SigBit;
  162.   while(!finished)
  163.   {
  164.     Signal = Wait(SignalMask);
  165.     if ((Signal & SignalMask) == SignalMask)
  166.     {
  167.       if (HandleMMIDCMP() == -1)
  168.         finished = TRUE;
  169.     }
  170.   }
  171.   if (DataAmended) x=SaveData();
  172. }
  173.  
  174. /*========================================================================
  175.  * BOOL MemoChk(BOOL loud)
  176.  *   Checks through list of memos and displays those specified by date and
  177.  *   notice values compared to todays date (supplied by caller).
  178.  * Rather than display memos individually, which could be annoying if there
  179.  *   several, display in blocks of 5
  180.  * Optionally, if no memos displayed, display a message confirming that none
  181.  *   found (parameter 'loud' is non-zero for confirmation, otherwise zero)
  182.  * Return TRUE if one or more memos displayed, otherwise return FALSE
  183.  */
  184. BOOL MemoChk(BOOL loud)
  185.   {
  186.   char *cp_array[8]; /* One heading, up to 5 memos plus spacing */
  187.   int cp_array_sub;
  188.   char mtype;
  189.   char dbuf[3];
  190.   char ybuf[3];
  191.   int today_days, memo_days, notice_days;
  192.   struct MinNode *n;
  193.   BOOL MFound = FALSE;
  194.   char *empty = "";
  195.   char *heading = "   Memos for today   ";
  196.   char memobuf[5][80];
  197.  
  198.   if (LISTEMPTY)
  199.     {
  200.     if (loud)
  201.       {
  202.       cp_array[0] = "No memo for today";
  203.       cp_array[1] = '\0';
  204.       DisplayT(cp_array);
  205.       }
  206.     return FALSE;
  207.     }
  208.  
  209. /*
  210.  * Work out days from 1 Jan 1970 until today for comparison
  211.  */
  212.   dbuf[0] = *(date_today);
  213.   dbuf[1] = *(date_today+1);
  214.   dbuf[2] = '\0';
  215.   ybuf[0] = *(date_today+7);
  216.   ybuf[1] = *(date_today+8);
  217.   ybuf[2] = '\0';
  218.   today_days = date2days(atoi(dbuf),
  219.             Month2Num((char *)date_today+3),
  220.             atoi(ybuf));
  221.  
  222.   n=MemListPtr->mlh_Head;
  223.   cp_array_sub = 2;
  224.   while (n->mln_Succ)
  225.     {
  226.     dbuf[0] = ((struct MI_Mem *)n)->mim_MI.mi_Date[0];
  227.     dbuf[1] = ((struct MI_Mem *)n)->mim_MI.mi_Date[1];
  228.     dbuf[2] = '\0';
  229.     mtype=((struct MI_Mem *)n)->mim_MI.mi_Type[0];
  230.     if (( mtype == 'a') || (mtype == 'A'))
  231.       {
  232.       ybuf[0] = *(date_today+7);
  233.       ybuf[1] = *(date_today+8);
  234.       }
  235.     else
  236.       {
  237.       ybuf[0] = ((struct MI_Mem *)n)->mim_MI.mi_Date[7];
  238.       ybuf[1] = ((struct MI_Mem *)n)->mim_MI.mi_Date[8];
  239.       }
  240.     ybuf[2] = '\0';
  241.  
  242.     memo_days = date2days(atoi(dbuf),
  243.           Month2Num((char *)&(((struct MI_Mem *)n)->mim_MI.mi_Date[3])),
  244.           atoi(ybuf));
  245.     if ( (memo_days < today_days) && ( (mtype == 'a') || (mtype == 'A') ))
  246.       {
  247.       sprintf(ybuf, "%d", atoi(ybuf)+1); /* Doesn't cater for going beyond 1999!!*/
  248.       memo_days = date2days(atoi(dbuf),
  249.           Month2Num((char *)&(((struct MI_Mem *)n)->mim_MI.mi_Date[3])),
  250.           atoi(ybuf));
  251.       }
  252.     notice_days = memo_days -
  253.             atoi((char *)&(((struct MI_Mem *)n)->mim_MI.mi_Notice[0]));
  254.  
  255.     if ((today_days < notice_days) || (today_days > memo_days))
  256.       {
  257.       ; /* Do not display */
  258.       }
  259.     else
  260.       {
  261.       /* Add memo to list for display*/
  262.       sprintf(memobuf[cp_array_sub-2], "  %s : %s  ",
  263.         (char *)&(((struct MI_Mem *)n)->mim_MI.mi_Date[0]),
  264.         (char *)&(((struct MI_Mem *)n)->mim_MI.mi_Text[0])   );
  265.       cp_array[cp_array_sub] = (memobuf[cp_array_sub-2]);
  266.       cp_array_sub++;
  267.       if (cp_array_sub > 6)
  268.     {
  269.     cp_array[0] = heading;
  270.     cp_array[1] = empty;
  271.     cp_array[cp_array_sub] = empty;
  272.     cp_array[cp_array_sub+1] = '\0';
  273.     DisplayT(cp_array);
  274.     cp_array_sub = 2;
  275.     }
  276.       MFound = TRUE;
  277.       }
  278.     n = n->mln_Succ;
  279.     }
  280. /* End of loop through memos */
  281.  
  282.   if (MFound)
  283.     {
  284.     /* Display remaining memos on display list */
  285.     if (cp_array_sub > 2)
  286.       {
  287.       cp_array[0] = heading;
  288.       cp_array[1] = empty;
  289.       cp_array[cp_array_sub] = empty;
  290.       cp_array[cp_array_sub+1] = '\0';
  291.       DisplayT(cp_array);
  292.       }
  293.     }
  294.   else
  295.     {
  296.     if (loud)
  297.       {
  298.       cp_array[0] = "No memo for today";
  299.       cp_array[1] = '\0';
  300.       DisplayT(cp_array);
  301.       }
  302.     }
  303.   return MFound;
  304.  
  305.   }
  306.  
  307. /*------------------------------------------------------------------------*/
  308. /*                 Display_One()                            */
  309. /*        Move one memo into IntuiText structures and print      */
  310. /*------------------------------------------------------------------------*/
  311. void Display_One(int gadg_no, struct MI_Mem *mim)
  312.   {
  313.   GT_SetGadgetAttrs(m_gad_date[gadg_no],mm_w,0,
  314.                     GTTX_Text, mim->mim_MI.mi_Date,
  315.                     TAG_DONE);
  316.   GT_SetGadgetAttrs(m_gad_notice[gadg_no],mm_w,0,
  317.                     GTTX_Text, mim->mim_MI.mi_Notice,
  318.                     TAG_DONE);
  319.   GT_SetGadgetAttrs(m_gad_type[gadg_no],mm_w,0,
  320.                     GTTX_Text, mim->mim_MI.mi_Type,
  321.                     TAG_DONE);
  322.   GT_SetGadgetAttrs(mem_t_line[gadg_no],mm_w,0,
  323.                     GTTX_Text, mim->mim_MI.mi_Text,
  324.                     TAG_DONE);
  325.   if (mim->mim_Select == 0)
  326.     GT_SetGadgetAttrs(m_gad_checked[gadg_no],mm_w,0,
  327.                       GA_Disabled, FALSE,
  328.                       GTCB_Checked, FALSE,
  329.                       TAG_DONE);
  330.   else
  331.     GT_SetGadgetAttrs(m_gad_checked[gadg_no],mm_w,0,
  332.                       GA_Disabled, FALSE,
  333.                       GTCB_Checked, TRUE,
  334.                       TAG_DONE);
  335.   }
  336.  
  337. /*------------------------------------------------------------------------*/
  338. /*                   Display_Blank()                            */
  339. /*        Move blank 'memo' into IntuiText structures and print         */
  340. /*------------------------------------------------------------------------*/
  341. void Display_Blank(int gadg_no )
  342.   {
  343.     GT_SetGadgetAttrs(m_gad_date[gadg_no],mm_w,0,
  344.                       GTTX_Text, "",
  345.                       TAG_DONE);
  346.     GT_SetGadgetAttrs(m_gad_notice[gadg_no],mm_w,0,
  347.                       GTTX_Text, 0,
  348.                       TAG_DONE);
  349.     GT_SetGadgetAttrs(m_gad_type[gadg_no],mm_w,0,
  350.                       GTTX_Text, "",
  351.                       TAG_DONE);
  352.     GT_SetGadgetAttrs(mem_t_line[gadg_no],mm_w,0,
  353.                       GTTX_Text, "",
  354.                       TAG_DONE);
  355.     GT_SetGadgetAttrs(m_gad_checked[gadg_no],mm_w,0,
  356.                       GTCB_Checked, FALSE,
  357.                       GA_Disabled, TRUE,
  358.                       TAG_DONE);
  359.   }
  360.  
  361. /* ============================================================================
  362.  * Function to display 'iconified' window to user and wait until user
  363.  *   ready to continue with main window.
  364.  *
  365.  * Calling sequence should be something like :-
  366.  *   CloseWindow(mm_w);
  367.  *   Shrink();
  368.  *   mm_w=OpenWindow(nw);
  369.  *   if (!mm_w) panic!!!
  370.  *   Display_Block(DisplayFirst);
  371.  *
  372.  */
  373. Shrink()
  374.   {
  375.   struct DiskObject *dobj;
  376.   struct AppIcon *appicon;
  377.   struct Message *msg;
  378.  
  379.   if (!(dobj = GetDiskObject("PROGDIR:MM2")))
  380.   {
  381.     dobj = GetDefDiskObject(WBTOOL);
  382.   }
  383.  
  384.   if (dobj)
  385.   {
  386.     if (port)
  387.     {
  388.       if (appicon = AddAppIconA(0,0,"MemoMaster",port,NULL,dobj,NULL))
  389.       {
  390.     CloseMMWindow();
  391.     CloseDownScreen();
  392.     WaitPort(port);
  393.     Forbid();
  394.     while (msg = GetMsg(port))
  395.       ReplyMsg(msg);
  396.     RemoveAppIcon(appicon);
  397.     Permit();
  398.     mm_w = MainWindow();
  399.     if (!mm_w)
  400.     {
  401.       puts("Failed reopening main window");
  402.       FreeDiskObject(dobj);
  403.       CleanUp();
  404.       exit(0);
  405.     }
  406.     Display_Block(DisplayFirst);
  407.       }
  408.       else
  409.     ZipWindow(MMWnd);
  410.     }
  411.     else
  412.       ZipWindow(MMWnd);
  413.   }
  414.   else
  415.     ZipWindow(MMWnd);
  416.  
  417.   if (dobj)
  418.     FreeDiskObject(dobj); 
  419.   }
  420.  
  421. /* ======================================================================
  422.  * Function defining and opening main window
  423.  */
  424.  
  425. struct Window *MainWindow()
  426. {
  427.   struct window *wnd = NULL;
  428.  
  429.   if (!SetupScreen())
  430.     if (!OpenMMWindow())
  431.     {
  432.       wnd = MMWnd;
  433.       mem_t_line[0] = MMGadgets[GDX_Message1Gad];
  434.       mem_t_line[1] = MMGadgets[GDX_Message2Gad];
  435.       mem_t_line[2] = MMGadgets[GDX_Message3Gad];
  436.       mem_t_line[3] = MMGadgets[GDX_Message4Gad];
  437.       mem_t_line[4] = MMGadgets[GDX_Message5Gad];
  438.       mem_t_line[5] = MMGadgets[GDX_Message6Gad];
  439.       m_gad_type[0] = MMGadgets[GDX_Type1Gad];
  440.       m_gad_type[1] = MMGadgets[GDX_Type2Gad];
  441.       m_gad_type[2] = MMGadgets[GDX_Type3Gad];
  442.       m_gad_type[3] = MMGadgets[GDX_Type4Gad];
  443.       m_gad_type[4] = MMGadgets[GDX_Type5Gad];
  444.       m_gad_type[5] = MMGadgets[GDX_Type6Gad];
  445.       m_gad_notice[0] = MMGadgets[GDX_Notice1Gad];
  446.       m_gad_notice[1] = MMGadgets[GDX_Notice2Gad];
  447.       m_gad_notice[2] = MMGadgets[GDX_Notice3Gad];
  448.       m_gad_notice[3] = MMGadgets[GDX_Notice4Gad];
  449.       m_gad_notice[4] = MMGadgets[GDX_Notice5Gad];
  450.       m_gad_notice[5] = MMGadgets[GDX_Notice6Gad];
  451.       m_gad_date[0] = MMGadgets[GDX_Date1Gad];
  452.       m_gad_date[1] = MMGadgets[GDX_Date2Gad];
  453.       m_gad_date[2] = MMGadgets[GDX_Date3Gad];
  454.       m_gad_date[3] = MMGadgets[GDX_Date4Gad];
  455.       m_gad_date[4] = MMGadgets[GDX_Date5Gad];
  456.       m_gad_date[5] = MMGadgets[GDX_Date6Gad];
  457.       m_gad_checked[0] = MMGadgets[GDX_Select1Gad];
  458.       m_gad_checked[1] = MMGadgets[GDX_Select2Gad];
  459.       m_gad_checked[2] = MMGadgets[GDX_Select3Gad];
  460.       m_gad_checked[3] = MMGadgets[GDX_Select4Gad];
  461.       m_gad_checked[4] = MMGadgets[GDX_Select5Gad];
  462.       m_gad_checked[5] = MMGadgets[GDX_Select6Gad];
  463.     }
  464.     else
  465.     {
  466.       CloseMMWindow();
  467.       CloseDownScreen();
  468.     }
  469.   else
  470.     CloseDownScreen();
  471.   return(wnd);
  472. }
  473.