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