home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / prtsampl.zip / PRTPROF.C < prev    next >
C/C++ Source or Header  |  1998-04-20  |  14KB  |  432 lines

  1. /****************************************************************************
  2.  * OS/2 Sample Print Application PRTSAMP
  3.  *
  4.  * File name: prtprof.c
  5.  *
  6.  * Description:  In this module are all the functions needed to save and
  7.  *               restore the application defaults to/from the PRTSAMP.INI.
  8.  *
  9.  *               This source file contains the following functions:
  10.  *
  11.  *               GetProfile(pmp)
  12.  *               SaveProfile(pmp)
  13.  *               SaveVersion(pmp)
  14.  *               SaveProfileOnly(pmp)
  15.  *               SaveDriverData(pmp)
  16.  *               PutProfileInfo(pmp)
  17.  *               GetProfileInfo(pmp)
  18.  *
  19.  * Concepts:   profile file
  20.  *
  21.  * API's:      PrfQueryProfileData
  22.  *             PrfWriteProfileData
  23.  *             PrfQueryProfileSize
  24.  *             WinQueryWindowPos
  25.  *             WinQueryWindowUShort
  26.  *             WinSetWindowPos
  27.  *
  28.  *    Files    :  OS2.H, PRTSAMP.H, PRTSDLG.H, PMASSERT.H
  29.  *
  30.  *  Copyright (C) 1991-1993 IBM Corporation
  31.  *
  32.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  33.  *      sample code created by IBM Corporation. This sample code is not
  34.  *      part of any standard or IBM product and is provided to you solely
  35.  *      for  the purpose of assisting you in the development of your
  36.  *      applications.  The code is provided "AS IS", without
  37.  *      warranty of any kind.  IBM shall not be liable for any damages
  38.  *      arising out of your use of the sample code, even if they have been
  39.  *      advised of the possibility of such damages.                                                    *
  40.  ****************************************************************************/
  41.  
  42. /* os2 includes */
  43. #define INCL_WINSTDFILE
  44. #define INCL_WINSTDFONT
  45. #define INCL_WINWINDOWMGR
  46. #define INCL_WINSHELLDATA
  47. #define INCL_GPITRANSFORMS
  48. #define INCL_DEV
  49. #define INCL_SPL
  50. #define INCL_SPLDOSPRINT
  51. #include <os2.h>
  52.  
  53. /* c language includes */
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <ctype.h>
  58. #include <stddef.h>
  59. #include <process.h>
  60. #include <memory.h>
  61. #include <sys\types.h>
  62. #include <sys\stat.h>
  63.  
  64. /* application includes */
  65. #include "prtsamp.h"
  66. #include "prtsdlg.h"
  67. #include "pmassert.h"
  68.  
  69. static const LONG lProgramVersion = 2100L;
  70.  
  71. static const PSZ pszApplication = "PRTSAMP";
  72. static const PSZ pszProfileKey = "Profile";
  73. static const PSZ pszVersionKey = "Version";
  74. static const PSZ pszDriverDataKey = "DriverData";
  75.  
  76. VOID GetProfileInfo(PMAIN_PARM pmp);
  77. VOID PutProfileInfo(PMAIN_PARM pmp);
  78. BOOL SaveDriverData(PMAIN_PARM pmp);
  79. BOOL SaveProfileOnly(PMAIN_PARM pmp);
  80. BOOL SaveVersion(PMAIN_PARM pmp);
  81.  
  82. /***************************************************************************
  83.  * Name    : GetDefaults
  84.  *
  85.  * Description:  Get the application defaults from OS2.INI (HINI_USERPROFILE)
  86.  *
  87.  * Concepts:  profile file
  88.  *
  89.  * API's:      PrfQueryProfileData
  90.  *             PrfWriteProfileData
  91.  *             PrfQueryProfileSize
  92.  *
  93.  * Parameters:  pmp = a pointer to the application main data structure
  94.  *
  95.  * Return:   [none]
  96.  *
  97.  ***************************************************************************/
  98. VOID GetProfile(PMAIN_PARM pmp)
  99. {
  100.    LONG    lCount, lVersion;
  101.    BOOL    fNeedDefaults;
  102.  
  103.    fNeedDefaults = TRUE;
  104.    lVersion = 0;
  105.    pmp->pDriverData = (PDRIVDATA)NULL;
  106.    pmp->cbDriverDataLen = 0;
  107.  
  108.    /*
  109.     * First determine if the profile information in OS2.INI matches
  110.     * the version number of this program.  If so, we will understand
  111.     * the format of the data.  Otherwise, just use default values.
  112.     */
  113.    lCount = sizeof(LONG);
  114.  
  115.    if (!PrfQueryProfileData(
  116.            HINI_USERPROFILE,
  117.            pszApplication,
  118.            pszVersionKey,
  119.            (PVOID)&lVersion,
  120.            (PULONG)&lCount) ||
  121.        lCount != sizeof(LONG))
  122.    {
  123.        /*
  124.         * The profile data for the version does not exist, so clean up
  125.         * and other data. This deletes all keys under the application name.
  126.         */
  127.        PrfWriteProfileData(
  128.            HINI_USERPROFILE,
  129.            pszApplication,
  130.            (PSZ)NULL,
  131.            (PVOID)NULL,
  132.            0L);
  133.    }
  134.    else if (lVersion != lProgramVersion)
  135.    {
  136.        /*
  137.         * The profile data is the wrong version, so we need to clean
  138.         * it up. This deletes all keys under the application name.
  139.         */
  140.        PrfWriteProfileData(
  141.            HINI_USERPROFILE,
  142.            pszApplication,
  143.            (PSZ)NULL,
  144.            (PVOID)NULL,
  145.            0L);
  146.    }
  147.    else
  148.    {
  149.        /*
  150.         * We have a matching version number, so we can proceed to
  151.         * read the rest of the profile information.
  152.         */
  153.  
  154.        /*
  155.         * Look for the "Profile" information. We know the size of this
  156.         * data so we're going to read as many bytes as we need. If for
  157.         * some reason there's more info than we need that's all right
  158.         * because we're relying on the lVersion to tell us we understand
  159.         * the data.
  160.         */
  161.        lCount = sizeof(PRTSAMP_PROFILE);
  162.  
  163.        if (PrfQueryProfileData(
  164.                HINI_USERPROFILE,
  165.                pszApplication,
  166.                pszProfileKey,
  167.                (PVOID)&pmp->Profile,
  168.                (PULONG)&lCount) &&
  169.            lCount == sizeof(PRTSAMP_PROFILE))
  170.        {
  171.            fNeedDefaults = FALSE;
  172.  
  173.            /*
  174.             * We found our profile data, which includes what
  175.             * print object we want to use. Now look for the
  176.             * "DriverData" data we want to use for this
  177.             * print object.
  178.             */
  179.            if (PrfQueryProfileSize(
  180.                    HINI_USERPROFILE,
  181.                    pszApplication,
  182.                    pszDriverDataKey,
  183.                    (PULONG)&lCount) &&
  184.                lCount >= sizeof(DRIVDATA))
  185.            {
  186.                /*
  187.                 * Found the size of the driver data. It can
  188.                 * only make sense if it is at least as big
  189.                 * as the DRIVDATA structure. Allocate some
  190.                 * space for it, and read it in.
  191.                 */
  192.                pmp->pDriverData = (PDRIVDATA)malloc(lCount);
  193.                if (pmp->pDriverData)
  194.                {
  195.                    pmp->cbDriverDataLen = lCount;
  196.  
  197.                    if (!PrfQueryProfileData(
  198.                            HINI_USERPROFILE,
  199.                            pszApplication,
  200.                            pszDriverDataKey,
  201.                            (PVOID)pmp->pDriverData,
  202.                            (PULONG)&lCount) ||
  203.                        lCount != pmp->cbDriverDataLen)
  204.                    {
  205.                        /*
  206.                         * Didn't find any driver data or the size
  207.                         * was wrong. Back out of our allocation.
  208.                         */
  209.                        free(pmp->pDriverData);
  210.                        pmp->pDriverData = (PDRIVDATA)NULL;
  211.                        pmp->cbDriverDataLen = 0;
  212.                    }
  213.                }
  214.            }
  215.        }
  216.  
  217.    } /* end handle correct profile version info */
  218.  
  219.    if (fNeedDefaults)
  220.    {
  221.        memset((PVOID)&pmp->Profile, 0, sizeof(PRTSAMP_PROFILE));
  222.    }
  223.    else
  224.    {
  225.        GetProfileInfo(pmp);
  226.    }
  227. }  /*  end of GetProfile() */
  228.  
  229. /**************************************************************************
  230.  * Name:     SaveProfile
  231.  *
  232.  * Description: saves profile data PRTSAMP_PROFILE Application defaults
  233.  *
  234.  * API's:  [none]
  235.  *
  236.  * Parameters:  pmp = a pointer to the application main data structure
  237.  *
  238.  * Return:  [none]
  239.  *
  240.  ***************************************************************************/
  241. VOID SaveProfile(PMAIN_PARM pmp)
  242. {
  243.     PutProfileInfo(pmp);
  244.     SaveVersion(pmp);
  245.     if (SaveProfileOnly(pmp))
  246.         SaveDriverData(pmp);
  247.  
  248.     return;
  249. } /* end of SaveProfile() */
  250.  
  251. /**************************************************************************
  252.  * Name:     SaveVersion
  253.  *
  254.  * Description: saves program version to user profile
  255.  *
  256.  * API's:  PrfWriteProfileData
  257.  *
  258.  * Parameters:  pmp = a pointer to the application main data structure
  259.  *
  260.  * Return:  value from call to PrfWriteProfileData
  261.  *
  262.  ***************************************************************************/
  263. BOOL SaveVersion(PMAIN_PARM pmp)
  264. {
  265.     return PrfWriteProfileData(
  266.         HINI_USERPROFILE,
  267.         pszApplication,
  268.         pszVersionKey,
  269.         (PVOID)&lProgramVersion,
  270.         (ULONG)sizeof(lProgramVersion));
  271. } /*  end of SaveVersion()  */
  272.  
  273. /**************************************************************************
  274.  * Name :    SaveProfileOnly
  275.  *
  276.  * Description: saves program profile to user profile
  277.  *
  278.  * API's:  PrfWriteProfileData
  279.  *
  280.  * Parameters:  pmp = a pointer to the application main data structure
  281.  *
  282.  * Return:  value from call to PrfWriteProfileData
  283.  *
  284.  ***************************************************************************/
  285. BOOL SaveProfileOnly(PMAIN_PARM pmp)
  286. {
  287.     return PrfWriteProfileData(
  288.         HINI_USERPROFILE,
  289.         pszApplication,
  290.         pszProfileKey,
  291.         (PVOID)&pmp->Profile,
  292.         (ULONG)sizeof(pmp->Profile));
  293. } /* end of SaveProfileOnly()  */
  294.  
  295. /**************************************************************************
  296.  * Name :    SaveDriverData
  297.  *
  298.  * Description: saves driver data to user profile
  299.  *
  300.  * API's:  PrfWriteProfileData
  301.  *
  302.  * Parameters:  pmp = a pointer to the application main data structure
  303.  *
  304.  * Return:  value from call to PrfWriteProfileData
  305.  *
  306.  ***************************************************************************/
  307. BOOL SaveDriverData(PMAIN_PARM pmp)
  308. {
  309.    return pmp->pDriverData == (PDRIVDATA)NULL ? FALSE :
  310.         PrfWriteProfileData(
  311.             HINI_USERPROFILE,
  312.             pszApplication,
  313.             pszDriverDataKey,
  314.             (PVOID)pmp->pDriverData,
  315.             (ULONG)pmp->cbDriverDataLen);
  316. }  /*  end of SaveDriverData()  */
  317.  
  318. /**************************************************************************
  319.  * Name :    PutProfileInfo
  320.  *
  321.  * Description:  writes data to the profile file
  322.  *
  323.  * API's:  WinQueryWindowPos
  324.  *         WinQueryWindowUShort
  325.  *
  326.  * Parameters:  pmp = a pointer to the application main data structure
  327.  *
  328.  * Return:  [none]
  329.  *
  330.  ***************************************************************************/
  331. VOID PutProfileInfo(PMAIN_PARM pmp)
  332. {
  333.    SWP     swpPos;
  334.  
  335.    memset((PVOID)&pmp->Profile, 0, sizeof(PRTSAMP_PROFILE));
  336.  
  337.    /*
  338.     * Query window position and size.
  339.     */
  340.    WinQueryWindowPos(pmp->hwndFrame, &swpPos);
  341.  
  342.    if ((swpPos.fl & SWP_MAXIMIZE) != 0)
  343.    {
  344.        pmp->Profile.flOptions = SWP_MAXIMIZE;
  345.        pmp->Profile.cy = WinQueryWindowUShort(pmp->hwndFrame, QWS_CYRESTORE);
  346.        pmp->Profile.cx = WinQueryWindowUShort(pmp->hwndFrame, QWS_CXRESTORE);
  347.        pmp->Profile.y = WinQueryWindowUShort(pmp->hwndFrame, QWS_YRESTORE);
  348.        pmp->Profile.x = WinQueryWindowUShort(pmp->hwndFrame, QWS_XRESTORE);
  349.    }
  350.    else if ((swpPos.fl & SWP_MINIMIZE) != 0)
  351.    {
  352.        pmp->Profile.flOptions = SWP_MINIMIZE;
  353.        pmp->Profile.cy = WinQueryWindowUShort(pmp->hwndFrame, QWS_CYRESTORE);
  354.        pmp->Profile.cx = WinQueryWindowUShort(pmp->hwndFrame, QWS_CXRESTORE);
  355.        pmp->Profile.y = WinQueryWindowUShort(pmp->hwndFrame, QWS_YRESTORE);
  356.        pmp->Profile.x = WinQueryWindowUShort(pmp->hwndFrame, QWS_XRESTORE);
  357.    }
  358.    else
  359.    {
  360.        pmp->Profile.flOptions = 0;
  361.        pmp->Profile.cy = swpPos.cy;
  362.        pmp->Profile.cx = swpPos.cx;
  363.        pmp->Profile.y = swpPos.y;
  364.        pmp->Profile.x = swpPos.x;
  365.    }
  366.  
  367.    pmp->Profile.fxPointSize = pmp->fontdlg.fxPointSize;
  368.    memcpy((PVOID)&pmp->Profile.fAttrs,
  369.           (PVOID)&pmp->fontdlg.fAttrs,
  370.           sizeof(pmp->Profile.fAttrs));
  371.  
  372.    memcpy((PVOID)&pmp->Profile.form,
  373.           (PVOID)&pmp->form,
  374.           sizeof(pmp->Profile.form));
  375.  
  376.    pmp->Profile.ulNextMode = pmp->ulMode;
  377.    strcpy(pmp->Profile.szNextFilename, pmp->szFilename);
  378.  
  379.    strcpy(pmp->Profile.achQueueName, pmp->achQueueName);
  380.    strcpy(pmp->Profile.achDriverName, pmp->achDriverName);
  381.  
  382.    pmp->Profile.usCopies = pmp->usCopies;
  383.    pmp->Profile.fPrintAllPages = pmp->fPrintAllPages;
  384.    pmp->Profile.usFirstPage = pmp->usFirstPage;
  385.    pmp->Profile.usLastPage = pmp->usLastPage;
  386.  
  387.    return;
  388. }  /*  end of PutProfileInfo()  */
  389.  
  390. /**************************************************************************
  391.  * Function: GetProfileInfo
  392.  *
  393.  * Description:  reads data which had been saved in the profile
  394.  *
  395.  * API's:  WinSetWindowPos
  396.  *
  397.  * Parameters:  pmp = a pointer to the application main data structure
  398.  *
  399.  * Result: [none]
  400.  *
  401.  ***************************************************************************/
  402. VOID GetProfileInfo(PMAIN_PARM pmp)
  403. {
  404.    /* restore Window position */
  405.    WinSetWindowPos( pmp->hwndFrame, HWND_TOP, pmp->Profile.x, pmp->Profile.y,
  406.                     pmp->Profile.cx, pmp->Profile.cy,
  407.                     pmp->Profile.flOptions |
  408.                               SWP_ACTIVATE | SWP_MOVE | SWP_SIZE | SWP_SHOW );
  409.  
  410.    pmp->fontdlg.fxPointSize = pmp->Profile.fxPointSize;
  411.    memcpy((PVOID)&pmp->fontdlg.fAttrs,
  412.           (PVOID)&pmp->Profile.fAttrs,
  413.           sizeof(pmp->Profile.fAttrs));
  414.  
  415.    memcpy((PVOID)&pmp->form,
  416.           (PVOID)&pmp->Profile.form,
  417.           sizeof(pmp->Profile.form));
  418.  
  419.    pmp->ulNextMode = pmp->Profile.ulNextMode;
  420.    strcpy(pmp->szNextFilename, pmp->Profile.szNextFilename);
  421.    strcpy(pmp->achQueueName, pmp->Profile.achQueueName);
  422.    strcpy(pmp->achDriverName, pmp->Profile.achDriverName);
  423.  
  424.    pmp->usCopies = pmp->Profile.usCopies;
  425.    pmp->fPrintAllPages = pmp->Profile.fPrintAllPages;
  426.    pmp->usFirstPage = pmp->Profile.usFirstPage;
  427.    pmp->usLastPage = pmp->Profile.usLastPage;
  428.  
  429.    return;
  430. }  /*  end of GetProfileInfo()  */
  431. /***************************  End of prtprof.c ****************************/
  432.