home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / portwin.zip / portwin.c next >
C/C++ Source or Header  |  1996-04-15  |  25KB  |  834 lines

  1. /* portwin.c */
  2.  
  3. /* This program maintains a small number of buttons as a
  4.    management gadget for UUPC/Extended.
  5.  
  6.    A button is provided to suspend/resume the use of the comport
  7.    and show its current status.
  8.  
  9.    A 2nd button displays the contents of a profile-specified log file,
  10.      via a profile-specified display program (needs to give list).
  11.  
  12.    Another button is provided that displays the spool status in a
  13.    list box, allowing deletions of individual spool files.
  14. */
  15.  
  16. #include "portwin.h"
  17.  
  18. static HWND hwndBase;  /* invisible frame */
  19. static HWND hwndDlg;   /* Button dialog */
  20. static HWND hwndObject;   /* Object window for util_task */
  21. static HWND hwndSpool;    /* Spool dialog */
  22. static HAB hab;           /* PM anchor */
  23. static TID util_thread;   /* utility task ID */
  24. static FILE *errf;        /* error file handle */
  25. static HINI prfinit;      /* profile handle */
  26. static BOOL portfree;     /* COMx available? */
  27. static  char  comstr[5];  /* COMx string */
  28. static  char *qprocbuf;   /* DosQProcStatus buffer ptr */
  29. PROF_INI globals;         /* portwin.ini globals */
  30. SWP    buttonpos;         /* window positions */
  31. SWP    spoolpos;          /* window positions */
  32. BOOL   buttonsaved, spoolsaved;  /* positions saved ? */
  33.  
  34. /* declare mystery API */
  35. extern unsigned _Far16 _Pascal DosQProcStatus( ULONG * _Seg16 buffer,
  36.                                            USHORT buffer_size );
  37.  
  38. /*  check for SLIP.EXE or PPP.EXE running: returns TRUE if one is */
  39. BOOL query_tcp( void )
  40. {
  41.  APIRET rc;
  42.  
  43.  rc = DosQProcStatus( (ULONG *  _Seg16 )qprocbuf, 64*1024 );
  44.  
  45.  if ( rc != 0 ) return FALSE;
  46.  
  47.  return FALSE;
  48. }
  49.  
  50. /*------------------- log PM error code to pmastro.err -----------------*/
  51. static VOID logWinErr( void )
  52.   {
  53.   USHORT sev, id;
  54.   ERRORID ecode;
  55.  
  56.  
  57.   ecode = WinGetLastError( hab );
  58.   id = ERRORIDERROR( ecode );
  59.   sev  = ERRORIDSEV( ecode );
  60.   if ( errf == NULL )
  61.       errf = fopen( "portwin.err", "w" );
  62.   fprintf( errf, "PM Window error %x severity %x  \n", id, sev );
  63.   }
  64.  
  65. /*-------------------- issue message box ------------------------------*/
  66. USHORT message( PSZ text, PSZ label, ULONG mstyle )
  67. {
  68.    USHORT tmpret;
  69.  
  70.    tmpret =
  71.    WinMessageBox(HWND_DESKTOP,         /* Parent window is desk top */
  72.                  hwndBase,             /* Owner window is our frame */
  73.                  text,                 /* PMWIN Error message       */
  74.                  label,                /* Title bar message         */
  75.                  MSGBOXID,             /* Message identifier        */
  76.                  MB_MOVEABLE | MB_CUACRITICAL | mstyle ); /* Flags */
  77.    if (tmpret == MBID_ERROR)
  78.       logWinErr();
  79.    return tmpret;
  80. }
  81.  
  82. /* see if specified com port is available or not;
  83.    just open it exclusive */
  84. int  check_com_port( void )
  85. {
  86.   HFILE hfCom;
  87.   ULONG ulAction;
  88.   APIRET retc;
  89.   BOOL retval;
  90.   RGB  color;
  91.  
  92.   strcpy( comstr, "COMx" );
  93.   /* make the com num into an ascii char */
  94.   comstr[3] = globals.comport | 0x30;
  95.  
  96.   retc = DosOpen( comstr, &hfCom, &ulAction, 0, FILE_NORMAL, FILE_OPEN,
  97.                   OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE,
  98.                   (PEAOP2)NULL );
  99.   if ( retc == 0 )
  100.      {
  101.        DosClose( hfCom );
  102.        retval = 0;  /* says port is free */
  103.        color.bGreen = 255;
  104.        color.bBlue = 0;
  105.        color.bRed = 0;
  106.      }
  107.   else
  108.      {
  109.        char  tcpath[128];
  110.        char  *cp;
  111.  
  112.        retval = 2;  /* says port has tcpip connect */
  113.        color.bGreen = 0;
  114.        color.bBlue = 255; /* assume tcpip has it */
  115.        color.bRed = 0;
  116.  
  117.        cp = getenv("ETC");
  118.        if ( cp != NULL )
  119.           {
  120.           /* determine if SLIP.EXE or PPP.EXE are running.
  121.              If so, change color to Blue */
  122.           strcpy( tcpath, cp );
  123.           cp = strrchr( tcpath, '\\' );
  124.           if ( cp == NULL )
  125.              cp = strrchr( tcpath, ':' );
  126.           cp++;
  127.           *cp = '\0';  /* terminate string */
  128.           strcat( tcpath, "bin\\slip.exe" );
  129.  
  130.           retc = DosOpen( tcpath, &hfCom, &ulAction,
  131.                         0, FILE_NORMAL, FILE_OPEN,
  132.                         OPEN_ACCESS_READONLY | OPEN_SHARE_DENYREADWRITE,
  133.                         (PEAOP2)NULL );
  134.  
  135.           if ( retc == 0 )
  136.              { /* we were able to open it, so it's not in use */
  137.              DosClose( hfCom );
  138.              *cp = '\0';  /* reset to path */
  139.              strcat( tcpath, "bin\\ppp.exe" );
  140.  
  141.              retc = DosOpen( tcpath, &hfCom, &ulAction,
  142.                         0, FILE_NORMAL, FILE_OPEN,
  143.                         OPEN_ACCESS_READONLY | OPEN_SHARE_DENYREADWRITE,
  144.                         (PEAOP2)NULL );
  145.              if ( retc == 0 )
  146.                 { /* both pgms not in use, must be UUPC */
  147.                  DosClose( hfCom );
  148.                  retval = 1; /* says port in use by uupc */
  149.                  color.bGreen = 0;
  150.                  color.bBlue = 0;
  151.                  color.bRed = 255;
  152.                 }
  153.              }
  154.           }
  155.      }
  156.  
  157.   /* set button background */
  158.   WinSetPresParam( WinWindowFromID( hwndDlg, BID_COM2 ),
  159.                     PP_BACKGROUNDCOLOR, (ULONG)sizeof(RGB),
  160.                     (PVOID)&color);
  161.   return retval;
  162. }
  163.  
  164. static UU_JOB *jobanc;
  165.  
  166. void insert_spool_line( HWND lbox, char * dline, UU_JOB * njob )
  167. {
  168.   MRESULT res;
  169.   USHORT  itx;
  170.  
  171.      /* add the spool status line to listbox,
  172.      save the result which contains index num */
  173.      res = WinSendMsg( lbox, LM_INSERTITEM,
  174.                MPFROM2SHORT( LIT_SORTASCENDING, 0 ),
  175.                MPFROMP( dline ) );
  176.      itx = SHORT1FROMMR( res );
  177.  
  178.      /* push the rest of the data onto a linked-list */
  179.      njob->next = jobanc;
  180.      jobanc = njob;
  181.  
  182.      /* now save this address with the dialog item */
  183.      WinSendMsg( lbox, LM_SETITEMHANDLE,
  184.                MPFROMSHORT( itx ),
  185.                MPFROMP( njob ) );
  186.      return;
  187. }
  188.  
  189. static  HAB        uhab;  /* hab for utility task */
  190.  
  191. /* Client Window for util_task */
  192. static
  193. MRESULT EXPENTRY UtilWinProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  194. {
  195.   ULONG         retc;
  196.   BOOL          prevfree;
  197.   char          cmd[80];
  198.  
  199.  
  200.   switch( msg )
  201.      {
  202.  
  203.  
  204.      case WM_CREATE:
  205.           /* send a WM_TIMER message for 10 sec. interval */
  206.           retc = WinStartTimer( uhab, hwnd, 1L, 10000UL );
  207.           return (MRESULT) 0;
  208.  
  209.      case WM_TIMER:
  210.           prevfree = portfree;
  211.           portfree = check_com_port();
  212.           break;
  213.  
  214.  
  215.      case WM_USER_TOGGLE_PORT:
  216.           {
  217.           /* obtain the current state of the port */
  218.  
  219.           prevfree = portfree;
  220.           portfree = check_com_port();
  221.  
  222.           /* suspend or resume the COM port as required */
  223.           if ( portfree == 1 )
  224.              { /* held by uupc, so suspend it */
  225.              sprintf( cmd, "uuport -s %s", comstr );
  226.              system( cmd );
  227.              }
  228.           else
  229.           if ( portfree == 0 )
  230.              { sprintf( cmd, "uuport -r %s", comstr );
  231.              system( cmd );
  232.              }
  233.           else
  234.              message( "COM port in use by Network", "UUPC",
  235.                        MB_CANCEL );
  236.  
  237.           portfree = check_com_port();
  238.  
  239.           WinPostMsg( hwndBase, WM_USER_ENABLE, 0, 0 );
  240.           return (MRESULT) 0;
  241.           }
  242.  
  243.      case WM_USER_DISPLAY_LOG:
  244.           sprintf( cmd, "start /f %s %s", globals.disp_pgm, globals.log_file );
  245.           system(cmd);
  246.           /* no Ack required */
  247.           WinPostMsg( hwndBase, WM_USER_ENABLE, 0, 0 );
  248.           return (MRESULT) 0;
  249.  
  250.      /* for display run the uustat to a tmp file,
  251.         and read it into a listbox */
  252.      case WM_USER_DISPLAY_SPOOL:
  253.           {
  254.           HWND lbox;
  255.           FILE *uustat;
  256.           char *uutarg, *uusize;
  257.           char  cbuf[128];
  258.           char  dline[128];
  259.  
  260.           sprintf( cmd, "uustat -sall > %s 2>NUL", globals.tmp_file );
  261.           retc = system(cmd);
  262.           /* if ok return msg to open the listbox dialog */
  263.           if ( retc != 0 )
  264.              {
  265.              WinPostMsg( hwndBase, WM_USER_ENABLE, 0, 0 );
  266.              message("Unable to run uustat", "Error", MB_OK );
  267.              WinDismissDlg( hwndSpool, 0L );
  268.              return (MRESULT) 0;
  269.              }
  270.  
  271.           lbox = WinWindowFromID( hwndSpool, DID_SPOOL_BOX );
  272.  
  273.           uustat = fopen( globals.tmp_file, "r" );
  274.           jobanc = NULL;
  275.  
  276.           while ( fgets( cbuf, sizeof(cbuf)-1, uustat ) != NULL )
  277.               {
  278.               char   *cp, *datime;
  279.               UU_JOB *njob;
  280.  
  281.               if ( strncmp( cbuf, "uustat: No jobs", 15 ) == 0 )
  282.                  {
  283.                  fclose( uustat );
  284.                  message( "Spool is empty", "UUPC", MB_OK );
  285.                  WinPostMsg( hwndBase, WM_USER_ENABLE, 0, 0 );
  286.                  WinDismissDlg( hwndSpool, 0L );
  287.                  return (MRESULT)0;
  288.                  }
  289.  
  290.               /* parse the line(s) and build 1 display line */
  291.               cp = strtok( cbuf, " \n" );
  292.               if ( cbuf[0] > ' ' ) /* has jobID */
  293.                  {
  294.                     njob = (UU_JOB *)malloc( sizeof(UU_JOB) );
  295.                     strcpy( njob->jobid, cp );
  296.                     datime = strtok( NULL, " \n" );
  297.                     strtok( NULL, " \n" );  /* skip field */
  298.                     uutarg = strtok( NULL, " \n" );
  299.                     strtok( NULL, " \n" );  /* skip field */
  300.                     uusize = strtok( NULL, " \n" );
  301.                     cp = strtok( NULL, " \n" );
  302.                     sprintf( dline, "%-9s %-12s %-8s ", uutarg, datime, uusize );
  303.                     /* 2-liner has chars 'x.' to begin this token */
  304.                     if ( ( cp != NULL ) && ( *(cp+1) == '.' ) ) /* 2-liner */
  305.                        strncpy( njob->uufile, cp, sizeof(njob->uufile)-1 );
  306.                     else /* 1-liner */
  307.                        { if ( cp != NULL )
  308.                             strcat( dline, cp );  /* add UUCP file to copy */
  309.                          njob->uufile[0] = '\0';
  310.                          insert_spool_line( lbox, dline, njob );
  311.                        }
  312.                  }
  313.               else
  314.                  {
  315.                    /* skip to 5th and 6th tokens */
  316.                    strtok( NULL, " \n" );  /* skip field */
  317.                    strtok( NULL, " \n" );  /* skip field */
  318.                    strtok( NULL, " \n" );  /* skip field */
  319.                    uutarg = strtok( NULL, " \n" );  /* command */
  320.                    cp = strtok( NULL, " \n" );  /* mail addr */
  321.                    strcat( dline, uutarg );
  322.                    strcat( dline, " " );
  323.                    strcat( dline, cp );
  324.                    insert_spool_line( lbox, dline, njob );
  325.                  }
  326.  
  327.               }  /* end while fgets() */
  328.  
  329.           /* close the temp file  */
  330.           fclose(uustat);
  331.           /* send msg to SpoolDlg to continue */
  332.           WinEnableWindow( hwndSpool, TRUE );
  333.           return (MRESULT) 0;
  334.           }
  335.  
  336.      /* if user chooses to delete a spool file */
  337.      case WM_USER_DELETE_SPOOL:
  338.           {
  339.           /* use pointer in mp1 */
  340.           UU_JOB  *jp;
  341.  
  342.           jp = (UU_JOB *)mp1;
  343.           sprintf( cmd, "uustat -k %s >NUL 2>NUL", jp->jobid );
  344.           system( cmd );
  345.  
  346.           WinEnableWindow( hwndSpool, TRUE );
  347.           return (MRESULT) 0;
  348.           }
  349.      }
  350.  
  351.   return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  352.  
  353. }
  354.  
  355.  
  356. /* Utility Thread */
  357. void util_task( void * parm )
  358. {
  359.   BOOL       fSuccess;
  360.   HMQ        uhmq;
  361.   QMSG       uqmsg;
  362.  
  363.  
  364.   /* thread initialization */
  365.   uhab = WinInitialize( 0 );
  366.   uhmq = WinCreateMsgQueue( uhab, 0 );
  367.  
  368.   /* prevent system from posting object window a WM_QUIT
  369.      I'll post WM_QUIT when it's time.  */
  370.   fSuccess = WinCancelShutdown( uhmq, TRUE );
  371.   pmassert( uhab, fSuccess );
  372.  
  373.   fSuccess = WinRegisterClass( uhab, UTIL_OBJECT,
  374.                   (PFNWP)UtilWinProc, 0, 0 );
  375.   pmassert( uhab, fSuccess );
  376.  
  377.   hwndObject = WinCreateWindow( HWND_OBJECT, UTIL_OBJECT, "",
  378.              0, 0, 0, 0, 0, HWND_OBJECT, HWND_BOTTOM, 0, NULL, NULL );
  379.  
  380.   /* created OK, ack Client to start Dlg */
  381.   WinPostMsg( hwndBase, WM_USER_ENABLE, 0, 0 );
  382.  
  383.   /* get/dispatch messages; user messages, for the most part */
  384.   while( WinGetMsg ( uhab, &uqmsg, 0, 0, 0 ) )
  385.   {
  386.     WinDispatchMsg ( uhab, &uqmsg );
  387.   }
  388.  
  389.   /* tell client window to quit */
  390.   WinPostMsg( hwndDlg, WM_QUIT, 0, 0 );
  391.  
  392.   /* clean up */
  393.   WinDestroyWindow( hwndObject );
  394.   WinDestroyMsgQueue( uhmq );
  395.   WinTerminate( uhab );
  396.   return;
  397. }
  398.  
  399. MRESULT EXPENTRY SpoolDlg( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  400. {
  401.   USHORT command;
  402.  
  403.   command = (COMMANDMSG(&msg)->cmd);
  404.  
  405.   switch( msg )
  406.   {
  407.    case WM_INITDLG:
  408.      {
  409.      /* send msg to object window to fill the listbox */
  410.      hwndSpool = hwnd;
  411.      WinPostMsg( hwndObject, WM_USER_DISPLAY_SPOOL, 0, 0 );
  412.      if ( spoolsaved )
  413.         { WinSetWindowPos( hwnd, HWND_TOP, spoolpos.x, spoolpos.y,
  414.                            0, 0, SWP_MOVE | SWP_ZORDER );
  415.         }
  416.      WinEnableWindow( hwnd, FALSE );  /* disable this dialog until
  417.                      we fill the box */
  418.      return (MRESULT)FALSE;
  419.      }
  420.  
  421.    case WM_CLOSE:
  422.      command = DID_SPOOL_DISMISS;
  423.  
  424.    case WM_COMMAND:
  425.      {
  426.      HWND  lbox;    /* listbox handle */
  427.      SHORT spfile, numitem;  /* item index, count */
  428.      UU_JOB *fjob;
  429.  
  430.      lbox =  WinWindowFromID( hwnd, DID_SPOOL_BOX );
  431.      switch( command )
  432.          {
  433.          case DID_SPOOL_DELETE:
  434.             spfile = WinQueryLboxSelectedItem( lbox );
  435.             numitem = 1;
  436.             if ( spfile >= 0 )
  437.                {  /* retrieve pointer to job */
  438.                fjob = (UU_JOB *) WinSendMsg( lbox,
  439.                         LM_QUERYITEMHANDLE,
  440.                         MPFROMSHORT( spfile ), NULL );
  441.                /* remove entry from list */
  442.                numitem = WinDeleteLboxItem( lbox, spfile );
  443.  
  444.                /* post msg to util-task to delete this one */
  445.                WinPostMsg( hwndObject, WM_USER_DELETE_SPOOL, fjob, 0 );
  446.                }
  447.  
  448.             if ( numitem != 0 )
  449.                return (MRESULT)FALSE;
  450.  
  451.          case DID_SPOOL_DISMISS:
  452.  
  453.             /* free the chain of jobs */
  454.             fjob = jobanc;
  455.             while ( fjob != NULL )
  456.                {  jobanc = fjob;
  457.                   fjob = fjob->next;
  458.                   free(jobanc);
  459.                }
  460.  
  461.             spoolsaved =  WinQueryWindowPos( hwnd, &spoolpos );
  462.             WinDismissDlg( hwnd, 0L );
  463.             return (MRESULT)FALSE;
  464.          }
  465.      return (MRESULT)FALSE;
  466.      }
  467.   }
  468.  
  469.   return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  470. }
  471.  
  472.  
  473. MRESULT EXPENTRY ButtonDlg( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  474. {
  475.   switch( msg )
  476.   {
  477.    case WM_INITDLG:
  478.      {
  479.      /* attach utility task to be used by object window for long-running
  480.        activity */
  481.      util_thread = _beginthread( util_task, NULL, 16384, NULL );
  482.      WinSetDlgItemText( hwnd, BID_COM2, comstr );
  483.      if ( buttonsaved )
  484.         { WinSetWindowPos( hwnd, HWND_TOP, buttonpos.x, buttonpos.y,
  485.                            0, 0, SWP_MOVE | SWP_ZORDER );
  486.         }
  487.      return (MRESULT)FALSE;
  488.      }
  489.  
  490.    case WM_COMMAND:
  491.      {
  492.       switch (COMMANDMSG(&msg)->cmd)
  493.         {
  494.  
  495.         /**********************************/
  496.         /* Process Button requests        */
  497.         /**********************************/
  498.         case BID_COM2:
  499.            {  /* request to toggle COM2 port state */
  500.            WinEnableWindow( hwndDlg, FALSE );
  501.            WinPostMsg( hwndObject, WM_USER_TOGGLE_PORT, 0, 0 );
  502.            break;
  503.            }
  504.         case BID_LOG:
  505.            { /* request to display UUCP log */
  506.            WinEnableWindow( hwndDlg, FALSE );
  507.            WinPostMsg( hwndObject, WM_USER_DISPLAY_LOG, 0, 0 );
  508.            break;
  509.            }
  510.         case BID_SPOOL:
  511.            { /* request to display UUCP spool in listbox */
  512.            WinDlgBox( HWND_DESKTOP, hwndBase, SpoolDlg,
  513.                    NULLHANDLE, DID_SPOOL, NULL );
  514.            break;
  515.            }
  516.         }
  517.  
  518.  
  519.      return (MRESULT)TRUE;
  520.      }
  521.  
  522.    /* if we get a close, tell object to go away, then
  523.       tell the frame to hike out */
  524.    case WM_CLOSE:
  525.       /* save the window position */
  526.       buttonsaved =  WinQueryWindowPos( hwnd, &buttonpos );
  527.  
  528.       /* tell object window to quit, then exit its thread */
  529.       WinPostMsg( hwndObject, WM_QUIT, 0, 0 );
  530.       /* may need to send ourselves a WM_QUIT here */
  531.       WinSendMsg( hwndBase, WM_CLOSE, 0L, 0L );
  532.   }
  533.  
  534.   return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  535.  
  536. }
  537.  
  538. BOOL verify_file( char * fname )
  539. {
  540.   return TRUE;
  541. }
  542.  
  543.  
  544. /* Profile initialization dialog */
  545. MRESULT EXPENTRY prof_init_dlg( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  546. {
  547.   USHORT  fld, act;
  548.   LONG    portn;
  549.   BOOL    tst;
  550.   char    texta[PATH_LEN];
  551.   static  PROF_INI temprof;
  552.  
  553.   switch( msg )
  554.   {
  555.     case WM_INITDLG:
  556.  
  557.     /* setup COM port spin button values */
  558.     WinSendMsg( WinWindowFromID( hwnd, DID_UUPC_COM_PORT ),
  559.                 SPBM_SETLIMITS, MPFROMLONG(4), MPFROMLONG(1) );
  560.     /* set text length limits */
  561.     WinSendMsg( WinWindowFromID( hwnd, DID_TMP_FILE ), EM_SETTEXTLIMIT,
  562.             MPFROMLONG( PATH_LEN - 1 ), MPFROMLONG( 0 ) );
  563.     WinSendMsg( WinWindowFromID( hwnd, DID_LOG_FILE ), EM_SETTEXTLIMIT,
  564.             MPFROMLONG( PATH_LEN - 1 ), MPFROMLONG( 0 ) );
  565.     WinSendMsg( WinWindowFromID( hwnd, DID_DISPLAY_PGM ), EM_SETTEXTLIMIT,
  566.             MPFROMLONG( PATH_LEN - 1 ), MPFROMLONG( 0 ) );
  567.  
  568.     /* setup temp file path */
  569.     WinSetDlgItemText( hwnd, DID_TMP_FILE, globals.tmp_file );
  570.     /* setup log file path */
  571.     WinSetDlgItemText( hwnd, DID_LOG_FILE, globals.log_file );
  572.     /* setup display pgm  path */
  573.     WinSetDlgItemText( hwnd, DID_DISPLAY_PGM, globals.disp_pgm );
  574.  
  575.     /* setup COM port spin button current value */
  576.     portn = (LONG)globals.comport;
  577.     tst = (BOOL)WinSendMsg( WinWindowFromID(hwnd, DID_UUPC_COM_PORT),
  578.                 SPBM_SETCURRENTVALUE, MPFROMLONG( portn ), 0L );
  579.     pmassert( hab, tst );
  580.  
  581.     memcpy( &temprof, &globals, sizeof(PROF_INI) );
  582.     return (MRESULT)FALSE;
  583.     /*----------------------------------------------------------------*/
  584.  
  585.     case WM_CONTROL:
  586.  
  587.     fld = SHORT1FROMMP( mp1 );  /* file resource ID */
  588.     act = SHORT2FROMMP( mp1 );  /* event */
  589.     switch ( act )
  590.       {
  591.       case  EN_KILLFOCUS:
  592.          { /* verify the current field contents */
  593.  
  594.          WinQueryDlgItemText( hwnd, fld,
  595.                     PATH_LEN, texta );
  596.          if ( texta[0] == '\0' )
  597.             break;
  598.  
  599.          switch ( fld )
  600.             {
  601.             case DID_TMP_FILE:
  602.                  if ( verify_file( texta ) )
  603.                     strcpy( temprof.tmp_file, texta );
  604.                  break;
  605.             case DID_LOG_FILE:
  606.                  if ( verify_file( texta ) )
  607.                     strcpy( temprof.log_file, texta );
  608.                  break;
  609.             case DID_DISPLAY_PGM:
  610.                  if ( verify_file( texta ) )
  611.                     strcpy( temprof.disp_pgm, texta );
  612.                  break;
  613.             default: break;
  614.             }
  615.          break;
  616.          }
  617.       }
  618.     break;
  619.     /*----------------------------------------------------------------*/
  620.     case WM_COMMAND:
  621.       {
  622.       switch (COMMANDMSG(&msg)->cmd)
  623.         {
  624.         case DID_PROF_OK:
  625.            {  /* user accepts profile change */
  626.            ULONG fx;
  627.  
  628.            /* pick up spin button value */
  629.            fx = 0;
  630.            WinSendMsg( WinWindowFromID( hwnd, DID_UUPC_COM_PORT ),
  631.                        SPBM_QUERYVALUE, MPFROMP(&fx), 0 );
  632.            memcpy( &globals, &temprof, sizeof(PROF_INI) );
  633.            globals.comport = fx;
  634.            /* save the profile */
  635.            PrfWriteProfileData( prfinit,
  636.             "portwin", "profini", &(globals), sizeof(PROF_INI) );
  637.  
  638.            WinDismissDlg(hwnd, (ULONG)sizeof(PROF_INI) );
  639.            return(MRESULT)TRUE;
  640.            }
  641.  
  642.         case DID_PROF_CANCEL:
  643.            {  /* ignore profile change */
  644.            WinDismissDlg(hwnd, FALSE);
  645.            return(MRESULT)TRUE;
  646.            }
  647.         }
  648.       break;
  649.       }
  650.  
  651.     /*----------------------------------------------------------------*/
  652.  
  653.     case WM_CLOSE:
  654.       {
  655.  
  656.       WinDismissDlg(hwnd, FALSE);
  657.       return(MRESULT)TRUE;
  658.       }
  659.  
  660.   }
  661.  
  662.   return WinDefDlgProc( hwnd, msg, mp1, mp2 );
  663. }
  664.  
  665. BOOL portwin_profile( void )
  666. {
  667.    ULONG lsize;
  668.  
  669.    if ( ( prfinit = PrfOpenProfile( hab, "uucppm.ini" ) )  == (HINI)0L )
  670.        {
  671.        message( "Unable to open portwin.ini", "Sorry", MB_CANCEL );
  672.        return FALSE;
  673.        }
  674.    /* load .ini data */
  675.    PrfQueryProfileSize( prfinit, "portwin", "profini", &lsize );
  676.    if ( lsize != sizeof(PROF_INI) ) return FALSE;
  677.  
  678.    PrfQueryProfileSize( prfinit, "portwin", "buttonpos", &lsize );
  679.    if ( lsize == sizeof(SWP) )
  680.       { PrfQueryProfileData( prfinit, "portwin", "buttonpos",
  681.                              &buttonpos, &lsize );
  682.         buttonsaved = TRUE;
  683.       }
  684.    PrfQueryProfileSize( prfinit, "portwin", "spoolpos", &lsize );
  685.    if ( lsize == sizeof(SWP) )
  686.       { PrfQueryProfileData( prfinit, "portwin", "spoolpos",
  687.                              &spoolpos, &lsize );
  688.         spoolsaved = TRUE;
  689.       }
  690.  
  691.    /* load globals with profile values */
  692.    lsize = sizeof(PROF_INI);
  693.    PrfQueryProfileData( prfinit, "portwin", "profini", &globals, &lsize );
  694.  
  695.    return TRUE;
  696. }
  697.  
  698.  
  699. MRESULT EXPENTRY UUPCbuttonWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  700. {
  701.  
  702.  
  703.    switch( msg )
  704.    {
  705.    case WM_CREATE:
  706.      {
  707.      /* If profile is not present schedule dialog  */
  708.      if (!(portwin_profile()) )
  709.         { /* set default profile data */
  710.         strcpy( globals.tmp_file, "C:\\TMP" );
  711.         strcpy( globals.tmp_file, getenv( "TMP" ) );
  712.         strcpy( globals.log_file, "c:\\uupc\\spool\\rmail.log" );
  713.         strcpy( globals.disp_pgm, "c:\\os2\\e.exe" );
  714.         globals.comport = 1;
  715.         WinPostMsg( hwnd, WM_USER_PROFILE, 0, 0 );
  716.         }
  717.      else
  718.         {
  719.         portfree = check_com_port();
  720.         }
  721.  
  722.      hwndDlg = WinLoadDlg( HWND_DESKTOP, hwnd, ButtonDlg, NULLHANDLE,
  723.                            FID_UUPC, NULL );
  724.      pmassert( hab, hwndDlg );
  725.  
  726.      return (MRESULT)0;
  727.      }
  728.  
  729.    case WM_USER_PROFILE:
  730.      {
  731.  
  732.         if ( WinDlgBox( HWND_DESKTOP, hwndBase, prof_init_dlg,
  733.                    NULLHANDLE, DID_PROF, NULL ) != sizeof(PROF_INI) )
  734.            { WinPostMsg( hwndDlg, WM_CLOSE, 0, 0 );
  735.            }
  736.         return (MRESULT)TRUE;
  737.      }
  738.  
  739.    case WM_USER_ENABLE:
  740.      {  /* util thread is ready for work */
  741.         WinEnableWindow( hwndDlg, TRUE );
  742.         return (MRESULT)TRUE;
  743.      }
  744.  
  745.    case WM_USER_DISABLE:
  746.      {  /* util thread is ready for work */
  747.         WinEnableWindow( hwndDlg, FALSE );
  748.         return (MRESULT)TRUE;
  749.      }
  750.  
  751.  
  752.    }
  753.  
  754.   return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  755. }
  756.  
  757.  
  758. /* Start here. */
  759.  
  760. int main (void)
  761. {
  762.  
  763.   ULONG flFrameFlags;
  764.   HMQ hmq;
  765.   QMSG qmsg;
  766.   APIRET  retc;
  767.  
  768.   errf = NULL;
  769.   portfree = FALSE;
  770.   buttonsaved = FALSE;
  771.   spoolsaved = FALSE;
  772.  
  773.   /* obtain DosQProcStatus 64k buffer */
  774.   retc = DosAllocMem( (PVOID *) &(qprocbuf), 64*1024,
  775.          OBJ_TILE | PAG_READ | PAG_WRITE );
  776.   if ( retc != 0 )
  777.      exit( 0x7A );
  778.  
  779.   /* Initialize Presentation Manager. */
  780.  
  781.   hab = WinInitialize (0);
  782.   hmq = WinCreateMsgQueue (hab, 0);
  783.  
  784.  
  785.  
  786.   /* Create client window class. */
  787.  
  788.   WinRegisterClass (hab, szUUPCbuttonClass, UUPCbuttonWndProc,
  789.                     CS_SIZEREDRAW, 0);
  790.  
  791.   /* Create the frame to hold the buttons.
  792.      Window is initially invisible. */
  793.  
  794.   flFrameFlags = (FCF_TITLEBAR | FCF_SYSMENU );
  795.  
  796.   /* Create and the frame window. */
  797.  
  798.   hwndBase = WinCreateStdWindow (HWND_DESKTOP, 0,
  799.                                  &flFrameFlags, szUUPCbuttonClass,
  800.                                  "UUPC Status",
  801.                                  0L, 0, 1, NULL);
  802.   pmassert( hab, hwndBase );
  803.  
  804.  
  805.   /* The message loop. */
  806.  
  807.   while (WinGetMsg (hab, &qmsg, 0L, 0, 0))
  808.     WinDispatchMsg (hab, &qmsg);
  809.  
  810.   /* Clean up. */
  811.   if ( buttonsaved )
  812.      {
  813.       PrfWriteProfileData( prfinit,
  814.      "portwin", "buttonpos", &(buttonpos), sizeof(SWP) );
  815.      }
  816.   if ( spoolsaved )
  817.      {
  818.       PrfWriteProfileData( prfinit,
  819.      "portwin", "spoolpos", &(spoolpos), sizeof(SWP) );
  820.      }
  821.  
  822.   WinDestroyWindow (hwndBase);
  823.   WinDestroyMsgQueue (hmq);
  824.   WinTerminate (hab);
  825.  
  826.   retc = DosWaitThread( &util_thread, DCWW_WAIT );
  827.  
  828.   if ( errf != NULL ) fclose( errf );
  829.  
  830.   DosFreeMem( qprocbuf );
  831.  
  832.   return retc;
  833. }
  834.