home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / memsz231.zip / PROFILE.CPP < prev    next >
Text File  |  1994-06-08  |  15KB  |  361 lines

  1. /**************************************************************** PROFILE.CPP
  2.  *                                                                          *
  3.  *                     Profile (INI) File Object Class                      *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_PM
  9. #include <os2.h>
  10.  
  11. #include <string.h>
  12.  
  13. #include "debug.h"
  14. #include "support.h"
  15.  
  16. #include "profile.h"
  17.  
  18.  
  19. /****************************************************************************
  20.  *                                                                          *
  21.  *                     Definitions & Declarations                           *
  22.  *                                                                          *
  23.  ****************************************************************************/
  24.  
  25.   // Constants
  26.  
  27. enum { ENTRY, ERR } ;
  28.  
  29.  
  30.   // Type Definitions
  31.  
  32. typedef struct {
  33.   SHORT   id ;
  34.   HWND    hwndHelp ;
  35.   PBYTE   Path ;
  36.   int     PathSize ;
  37. } PROFILE_PARMS, *PPROFILE_PARMS ;
  38.  
  39.  
  40.   // Function Prototypes
  41.  
  42. static FNWP ProfileProcessor ;
  43.  
  44. static METHODFUNCTION InitDlg ;
  45. static METHODFUNCTION Command ;
  46. static METHODFUNCTION OK ;
  47. static METHODFUNCTION Cancel ;
  48.  
  49.  
  50. /****************************************************************************
  51.  *                                                                          *
  52.  *      Profile Class Constructor                                           *
  53.  *                                                                          *
  54.  ****************************************************************************/
  55.  
  56. Profile::Profile
  57. (
  58.   PSZ     name,
  59.   HAB     Anchor,
  60.   HMODULE Library,
  61.   int     DialogID,
  62.   HWND    HelpInstance,
  63.   BOOL    ResetFlag
  64. ) {
  65.  
  66.   /**************************************************************************
  67.    * Save the names.                                                        *
  68.    **************************************************************************/
  69.  
  70.    Name = new BYTE [ strlen(PCHAR(name)) + 1 ] ;
  71.    strcpy ( PCHAR(Name), PCHAR(name) ) ;
  72.  
  73.   /**************************************************************************
  74.    * If resetting the profile, clear the system profile information now.    *
  75.    **************************************************************************/
  76.  
  77.    if ( ResetFlag ) {
  78.       PrfWriteProfileData ( HINI_USERPROFILE, Name, PSZ(NULL), PSZ(NULL), 0 ) ;
  79.    }
  80.  
  81.   /**************************************************************************
  82.    * Query the system INI for the profile file's path.                      *
  83.    **************************************************************************/
  84.  
  85.    PSZ ProfilePath = PSZ(NULL) ;
  86.    ULONG Size ;
  87.  
  88.    if ( PrfQueryProfileSize ( HINI_USERPROFILE, Name, PSZ("INIPATH"), &Size ) ) {
  89.  
  90.       // The info exists.  Fetch it.
  91.       ProfilePath = new BYTE [ Size ] ;
  92.       PrfQueryProfileData ( HINI_USERPROFILE, Name,
  93.          PSZ("INIPATH"), ProfilePath, &Size ) ;
  94.  
  95.       // Build the profile file name.
  96.       BYTE FullPath [_MAX_PATH] ;
  97.       strcpy ( PCHAR(FullPath), PCHAR(ProfilePath) ) ;
  98.       strcat ( PCHAR(FullPath), "\\" ) ;
  99.       strcat ( PCHAR(FullPath), PCHAR(Name) ) ;
  100.       strcat ( PCHAR(FullPath), ".INI" ) ;
  101.  
  102.       // Clean the name up and expand it to a full path.
  103.       BYTE Path [256] ;
  104.       DosQueryPathInfo ( FullPath, FIL_QUERYFULLNAME, Path, sizeof(Path) ) ;
  105.  
  106.       // Does the file exist?  If not, discard the name.
  107.       FILESTATUS3 Status ;
  108.       if ( DosQueryPathInfo ( Path, FIL_STANDARD, &Status, sizeof(Status) ) ) {
  109.          delete [] ProfilePath ;
  110.          ProfilePath = PSZ(NULL) ;
  111.       }
  112.    }
  113.  
  114.   /**************************************************************************
  115.    * If the profile file couldn't be found, ask the user for a path.        *
  116.    **************************************************************************/
  117.  
  118.    if ( ProfilePath == NULL ) {
  119.       // Set the default path.
  120.       BYTE Path [256] ;
  121.       DosQueryPathInfo ( PSZ("."), FIL_QUERYFULLNAME, Path, sizeof(Path) ) ;
  122.  
  123.       // Call up the entry dialog.
  124.       PROFILE_PARMS Parms ;
  125.       Parms.id = DialogID ;
  126.       Parms.hwndHelp = HelpInstance ;
  127.       Parms.Path = Path ;
  128.       Parms.PathSize = sizeof(Path) ;
  129.       if ( WinDlgBox ( HWND_DESKTOP, HWND_DESKTOP, PFNWP(ProfileProcessor), Library, DialogID, &Parms ) ) {
  130.          // If OK, save the approved path in the system profile.
  131.          ProfilePath = new BYTE [ strlen(PCHAR(Path)) + 1 ] ;
  132.          strcpy ( PCHAR(ProfilePath), PCHAR(Path) ) ;
  133.  
  134.          PrfWriteProfileData ( HINI_USERPROFILE, Name, PSZ("INIPATH"),
  135.             ProfilePath, strlen(PCHAR(ProfilePath))+1 ) ;
  136.       }
  137.    }
  138.  
  139.   /**************************************************************************
  140.    * Reset profile handle.  If the path could be determined . . .           *
  141.    **************************************************************************/
  142.  
  143.    Handle = 0 ;
  144.  
  145.    if ( ProfilePath ) {
  146.  
  147.      /***********************************************************************
  148.       * Build the full profile file name.                                   *
  149.       ***********************************************************************/
  150.  
  151.       BYTE ProfileName [_MAX_PATH] ;
  152.       strcpy ( PCHAR(ProfileName), PCHAR(ProfilePath) ) ;
  153.       strcat ( PCHAR(ProfileName), "\\"  ) ;
  154.       strcat ( PCHAR(ProfileName), PCHAR(Name) ) ;
  155.       strcat ( PCHAR(ProfileName), ".INI" ) ;
  156.  
  157.      /***********************************************************************
  158.       * Release the memory previously allocated to store the path.          *
  159.       ***********************************************************************/
  160.  
  161.       if ( ProfilePath ) {
  162.          delete [] ProfilePath ;
  163.       }
  164.  
  165.      /***********************************************************************
  166.       * Open/Create the profile file and return the resultant handle.       *
  167.       ***********************************************************************/
  168.  
  169.       Handle = PrfOpenProfile ( Anchor, ProfileName ) ;
  170.  
  171.      /***********************************************************************
  172.       * If resetting, then clean this profile out.                          *
  173.       ***********************************************************************/
  174.  
  175.       if ( ResetFlag ) {
  176.          PrfWriteProfileData ( Handle, Name, PSZ(NULL), PSZ(NULL), 0 ) ;
  177.       }
  178.    }
  179. }
  180.  
  181. /****************************************************************************
  182.  *                                                                          *
  183.  *      Profile Class Destructor                                            *
  184.  *                                                                          *
  185.  ****************************************************************************/
  186.  
  187. Profile::~Profile ( ) {
  188.  
  189.  /***************************************************************************
  190.   * Release allocated memory.                                               *
  191.   ***************************************************************************/
  192.  
  193.   delete [] Name ;
  194.  
  195.  /***************************************************************************
  196.   * Close the profile.                                                      *
  197.   ***************************************************************************/
  198.  
  199.   if ( Handle )
  200.      PrfCloseProfile ( Handle ) ;
  201. }
  202.  
  203. /****************************************************************************
  204.  *                                                                          *
  205.  *      Dialog Message Processor                                            *
  206.  *                                                                          *
  207.  ****************************************************************************/
  208.  
  209. extern MRESULT EXPENTRY ProfileProcessor ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  210.  
  211.  /***************************************************************************
  212.   * Dispatch the message according to the method table and return the       *
  213.   *   result.  Any messages not defined above get handled by the system     *
  214.   *   default dialog processor.                                             *
  215.   ***************************************************************************/
  216.  
  217.   static METHOD Methods [] = {
  218.     { WM_INITDLG, InitDlg },
  219.     { WM_COMMAND, Command }
  220.   } ;
  221.  
  222.   return ( DispatchMessage ( hwnd, msg, mp1, mp2, Methods, sizeof(Methods)/sizeof(Methods[0]), WinDefDlgProc ) ) ;
  223. }
  224.  
  225. /****************************************************************************
  226.  *                                                                          *
  227.  *      Initialize Dialog                                                   *
  228.  *                                                                          *
  229.  ****************************************************************************/
  230.  
  231. static MRESULT APIENTRY InitDlg ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  232.  
  233.  /***************************************************************************
  234.   * Get parameters from initialization message.                             *
  235.   ***************************************************************************/
  236.  
  237.   PPROFILE_PARMS Parms = (PPROFILE_PARMS) ( PVOIDFROMMP ( mp2 ) ) ;
  238.  
  239.   WinSetWindowPtr ( hwnd, QWL_USER, Parms ) ;
  240.  
  241.  /***************************************************************************
  242.   * Set the dialog help instance.                                           *
  243.   ***************************************************************************/
  244.  
  245.   WinSetWindowUShort ( hwnd, QWS_ID, Parms->id ) ;
  246.   if ( Parms->hwndHelp ) {
  247.     WinAssociateHelpInstance ( Parms->hwndHelp, hwnd ) ;
  248.   }
  249.  
  250.  /***************************************************************************
  251.   * Set the entry field contents.                                           *
  252.   ***************************************************************************/
  253.  
  254.   WinSetDlgItemText ( hwnd, Parms->id+ENTRY, Parms->Path ) ;
  255.  
  256.  /***************************************************************************
  257.   * Return no error.                                                        *
  258.   ***************************************************************************/
  259.  
  260.   return ( MRFROMSHORT ( FALSE ) ) ;
  261. }
  262.  
  263. /****************************************************************************
  264.  *                                                                          *
  265.  *      Process commands received by the dialog.                            *
  266.  *                                                                          *
  267.  ****************************************************************************/
  268.  
  269. static MRESULT APIENTRY Command ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  270.  
  271.  /***************************************************************************
  272.   * Dispatch the message without a default message processor.               *
  273.   ***************************************************************************/
  274.  
  275.   static METHOD Methods [] = {
  276.     { DID_OK,     OK     },
  277.     { DID_CANCEL, Cancel },
  278.   } ;
  279.  
  280.   return ( DispatchMessage ( hwnd, SHORT1FROMMP(mp1), mp1, mp2, Methods, sizeof(Methods)/sizeof(Methods[0]), 0 ) ) ;
  281. }
  282.  
  283. /****************************************************************************
  284.  *                                                                          *
  285.  *      Process the dialog's OK button being pressed.                       *
  286.  *                                                                          *
  287.  ****************************************************************************/
  288.  
  289. static MRESULT APIENTRY OK ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  290.  
  291.  /***************************************************************************
  292.   * Find the instance data.                                                 *
  293.   ***************************************************************************/
  294.  
  295.   PPROFILE_PARMS Parms = PPROFILE_PARMS ( WinQueryWindowPtr ( hwnd, QWL_USER ) ) ;
  296.  
  297.  /***************************************************************************
  298.   * Verify the entered path.                                                *
  299.   ***************************************************************************/
  300.  
  301.   BYTE Name [256] ;
  302.   WinQueryDlgItemText ( hwnd, Parms->id+ENTRY, sizeof(Name), Name ) ;
  303.  
  304.   BYTE FullPath [256] ;
  305.   if ( DosQueryPathInfo ( Name, FIL_QUERYFULLNAME, FullPath, sizeof(FullPath) ) ) {
  306.     PSZ Message = PSZ ( "ERROR: Not a valid path." ) ;
  307.     WinSetDlgItemText ( hwnd, Parms->id+ERR, Message ) ;
  308.     WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  309.     WinSetFocus ( HWND_DESKTOP, WinWindowFromID ( hwnd, Parms->id+ENTRY ) ) ;
  310.     return ( 0 ) ;
  311.   }
  312.  
  313.   FILESTATUS3 Status ;
  314.   if ( DosQueryPathInfo ( FullPath, FIL_STANDARD, &Status, sizeof(Status) ) ) {
  315.     PSZ Message = PSZ ( "ERROR: Path does not exist." ) ;
  316.     WinSetDlgItemText ( hwnd, Parms->id+ERR, Message ) ;
  317.     WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  318.     WinSetFocus ( HWND_DESKTOP, WinWindowFromID ( hwnd, Parms->id+ENTRY ) ) ;
  319.     return ( 0 ) ;
  320.   }
  321.  
  322.   if ( ! ( Status.attrFile & FILE_DIRECTORY ) ) {
  323.     PSZ Message = PSZ ( "ERROR: Specified path is not a directory." ) ;
  324.     WinSetDlgItemText ( hwnd, Parms->id+ERR, Message ) ;
  325.     WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  326.     WinSetFocus ( HWND_DESKTOP, WinWindowFromID ( hwnd, Parms->id+ENTRY ) ) ;
  327.     return ( 0 ) ;
  328.   }
  329.  
  330.  /***************************************************************************
  331.   * Return the full path to the caller.                                     *
  332.   ***************************************************************************/
  333.  
  334.   strncpy ( PCHAR(Parms->Path), PCHAR(FullPath), Parms->PathSize ) ;
  335.  
  336.  /***************************************************************************
  337.   * Dismiss the dialog with a TRUE status.                                  *
  338.   ***************************************************************************/
  339.  
  340.   WinDismissDlg ( hwnd, TRUE ) ;
  341.  
  342.   return ( 0 ) ;
  343. }
  344.  
  345. /****************************************************************************
  346.  *                                                                          *
  347.  *      Process the dialog's being cancelled.                               *
  348.  *                                                                          *
  349.  ****************************************************************************/
  350.  
  351. static MRESULT APIENTRY Cancel ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  352.  
  353.  /***************************************************************************
  354.   * Dismiss the dialog with a TRUE status.                                  *
  355.   ***************************************************************************/
  356.  
  357.   WinDismissDlg ( hwnd, FALSE ) ;
  358.  
  359.   return ( 0 ) ;
  360. }
  361.