home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / sdk / mapi / win16 / dev / smh / smhmdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-11  |  7.7 KB  |  286 lines

  1. /*
  2.  *  S M H M D B . C
  3.  *
  4.  *  Sample mail handling hook
  5.  *  Store Tables
  6.  *
  7.  *  Copyright 1992-95 Microsoft Corporation.  All Rights Reserved.
  8.  */
  9.  
  10. #include "_pch.h"
  11.  
  12.  
  13. /*
  14.  *  sptStoTbl
  15.  *
  16.  *  The store table column set and enum
  17.  */
  18. enum { ipNull, ipEid, ipDispNm, ipRsrcFlags, cpStoTblMax };
  19. static const SizedSPropTagArray (cpStoTblMax, sptStoTbl) =
  20. {
  21.     cpStoTblMax,
  22.     {
  23.         PR_NULL,
  24.         PR_ENTRYID,
  25.         PR_DISPLAY_NAME_A,
  26.         PR_RESOURCE_FLAGS
  27.     }
  28. };
  29.  
  30.  
  31. /*
  32.  *  HrInitStoresTable()
  33.  *
  34.  *  Purpose:
  35.  *
  36.  *      Inits the stores table stucture and adds a reference to the SMH
  37.  *      parent object.  This is done by getting the message stores table
  38.  *      from the MAPI session and querying all rows (up to cStoMax) and
  39.  *      mapping the return to an LPSTOTABLE structure.
  40.  *
  41.  *  Arguments:
  42.  *
  43.  *      lpsmh           the SMH parent object
  44.  *      lpsess          the MAPI session that this SMH instance belongs to
  45.  *
  46.  *  Returns:
  47.  *
  48.  *      (HRESULT)
  49.  */
  50. HRESULT
  51. HrInitStoresTable (LPSMH lpsmh, LPMAPISESSION lpsess)
  52. {
  53.     HRESULT hr;
  54.     LPMAPITABLE lptbl;
  55.     LPSRowSet lprws = NULL;
  56.     LPSTOTABLE lpstotbl;
  57.     UINT i;
  58.  
  59.     Assert (lpsmh->lpstotbl == NULL);
  60.     hr = lpsess->lpVtbl->GetMsgStoresTable (lpsess, 0, &lptbl);
  61.     if (!HR_FAILED (hr))
  62.     {
  63.         hr = lptbl->lpVtbl->SetColumns (lptbl, (LPSPropTagArray)&sptStoTbl, 0);
  64.         if (!HR_FAILED (hr))
  65.         {
  66.             hr = lptbl->lpVtbl->QueryRows (lptbl, cStoMax, 0, &lprws);
  67.             if (!HR_FAILED (hr))
  68.             {
  69.                 /*  Zero fill the initial property in all rows.
  70.                  *  This has the nice side effect of NULL'ing the
  71.                  *  lpmdb member of the structure
  72.                  */
  73.                 lpstotbl = (LPSTOTABLE)lprws;
  74.                 for (i = 0; i < lpstotbl->cSto; i++)
  75.                     memset (&lpstotbl->aSto[i].lpstoe->valPad,
  76.                         0, sizeof(SPropValue));
  77.  
  78.                 lpsmh->lpstotbl = lpstotbl;
  79.             }
  80.         }
  81.         UlRelease (lptbl);
  82.     }
  83.  
  84.     DebugTraceResult (HrInitStoresTable(), hr);
  85.     return hr;
  86. }
  87.  
  88.  
  89. /*
  90.  *  ReleaseStoresTable()
  91.  *
  92.  *  Purpose:
  93.  *
  94.  *      Releases all resources owned by the store table and disconnects
  95.  *      the SMH parent from the table.
  96.  *
  97.  *  Arguments:
  98.  *
  99.  *      lpsmh       the SMH parent object
  100.  */
  101. VOID
  102. ReleaseStoresTable (LPSMH lpsmh)
  103. {
  104.     LPSTOTABLE lpstotbl = lpsmh->lpstotbl;
  105.     UINT i;
  106.  
  107.     if (lpstotbl)
  108.     {
  109.         for (i = 0; i < lpstotbl->cSto; i++)
  110.         {
  111.             UlRelease (lpstotbl->aSto[i].lpstoe->lpmdb);
  112.             (*lpsmh->lpfnFree) (lpstotbl->aSto[i].lpProps);
  113.         }
  114.         (*lpsmh->lpfnFree) (lpstotbl);
  115.         lpsmh->lpstotbl = NULL;
  116.     }
  117.     return;
  118. }
  119.  
  120.  
  121. /*
  122.  *  HrOpenStoEntry()
  123.  *
  124.  *  Purpose:
  125.  *
  126.  *      Opens the store associated with a stoenty in the stores table.
  127.  *
  128.  *      IMPORTANT: the caller does not end up owning the store reference
  129.  *      and should NOT call Release() on the store unless they also call
  130.  *      AddRef().  The referece is owned by the stores table and is
  131.  *      released upon the tables destruction.
  132.  *
  133.  *  Arguments:
  134.  *
  135.  *      lpsess          the MAPI session object
  136.  *      lpsto           the STO structure for the MDB to open
  137.  *      lppmdb  [OUT]   contains the LPMDB iff the call succeeds
  138.  *
  139.  *  Returns:
  140.  *
  141.  *      (HRESULT)
  142.  */
  143. HRESULT
  144. HrOpenStoEntry (LPMAPISESSION lpsess, LPSTO lpsto, LPMDB FAR * lppmdb)
  145. {
  146.     HRESULT hr;
  147.     LPMDB lpmdb = NULL;
  148.  
  149.     hr = lpsess->lpVtbl->OpenMsgStore (lpsess,
  150.                             0,
  151.                             lpsto->lpProps[ipEid].Value.bin.cb,
  152.                             (LPENTRYID)lpsto->lpProps[ipEid].Value.bin.lpb,
  153.                             NULL,
  154.                             MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL,
  155.                             &lpmdb);
  156.     if (!HR_FAILED (hr))
  157.         lpsto->lpstoe->lpmdb = lpmdb;
  158.  
  159.     *lppmdb = lpmdb;
  160.     DebugTraceResult (HrOpenStoEntry(), hr);
  161.     return hr;
  162. }
  163.  
  164.  
  165. /*
  166.  *  HrOpenMdbFromEid()
  167.  *
  168.  *  Purpose:
  169.  *
  170.  *      Opens a message store by entryid.  The entryid is looked up in
  171.  *      the stores table to find the proper STOENTRY.  If no entry is
  172.  *      found, then the store is not opened.  If one is found, then the
  173.  *      MDB is retrieved from the STOENTRY.  If it has not yet been opened,
  174.  *      the MDB is opened at this time and stored (no pun intended).
  175.  *
  176.  *      IMPORTANT: the caller does not end up owning the store reference
  177.  *      and should NOT call Release() on the store unless they also call
  178.  *      AddRef().  The referece is owned by the stores table and is
  179.  *      released upon the tables destruction.
  180.  *
  181.  *  Arguments:
  182.  *
  183.  *      lpsmh           the SMH parent object
  184.  *      cbeid           size of the stores entryid
  185.  *      lpeid           the entryid to be opened
  186.  *      lppmdb  [OUT]   contains the LPMDB iff the call succeeds
  187.  *
  188.  *  Returns:
  189.  *
  190.  *      (HRESULT)
  191.  */
  192. HRESULT
  193. HrOpenMdbFromEid (LPSMH lpsmh, ULONG cbeid, LPENTRYID lpeid, LPMDB FAR * lppmdb)
  194. {
  195.     HRESULT hr = ResultFromScode (MAPI_E_NOT_FOUND);
  196.     LPSTOTABLE lpstotbl = lpsmh->lpstotbl;
  197.     UINT i;
  198.  
  199.     if (lpstotbl)
  200.     {
  201.         for (i = 0; i < lpstotbl->cSto; i++)
  202.         {
  203.             if ((cbeid == lpstotbl->aSto[i].lpProps[ipEid].Value.bin.cb) &&
  204.                 !memcmp (lpeid, lpstotbl->aSto[i].lpProps[ipEid].Value.bin.lpb, (UINT)cbeid))
  205.             {
  206.                 if (lpstotbl->aSto[i].lpstoe->lpmdb)
  207.                 {
  208.                     *lppmdb = lpstotbl->aSto[i].lpstoe->lpmdb;
  209.                     hr = hrSuccess;
  210.                 }
  211.                 else
  212.                     hr = HrOpenStoEntry (lpsmh->lpsess, &lpstotbl->aSto[i], lppmdb);
  213.  
  214.                 break;
  215.             }
  216.         }
  217.     }
  218.  
  219.     if (HR_FAILED (hr))
  220.         *lppmdb = NULL;
  221.  
  222.     DebugTraceResult (HrOpenMdbFromEid(), hr);
  223.     return hr;
  224. }
  225.  
  226.  
  227. /*
  228.  *  HrOpenMdbFromName()
  229.  *
  230.  *  Purpose:
  231.  *
  232.  *      Opens a message store by PR_DISPLAY_NAME value.  The name is
  233.  *      looked up in the stores table to find the proper STOENTRY.  If no
  234.  *      entry is found, then the store is not opened.  If one is found,
  235.  *      then the MDB is retrieved from the STOENTRY.  If it has not yet
  236.  *      been opened, the MDB is opened at this time and stored (no pun
  237.  *      intended).
  238.  *
  239.  *      IMPORTANT: the caller does not end up owning the store reference
  240.  *      and should NOT call Release() on the store unless they also call
  241.  *      AddRef().  The referece is owned by the stores table and is
  242.  *      released upon the tables destruction.
  243.  *
  244.  *  Arguments:
  245.  *
  246.  *      lpsmh           the SMH parent object
  247.  *      lpszName        the name of the store to open
  248.  *      lppmdb  [OUT]   contains the LPMDB iff the call succeeds
  249.  *
  250.  *  Returns:
  251.  *
  252.  *      (HRESULT)
  253.  */
  254. HRESULT
  255. HrOpenMdbFromName (LPSMH lpsmh, LPTSTR lpszName, LPMDB FAR * lppmdb)
  256. {
  257.     HRESULT hr = ResultFromScode (MAPI_E_NOT_FOUND);
  258.     LPSTOTABLE lpstotbl = lpsmh->lpstotbl;
  259.     UINT i;
  260.  
  261.     if (lpstotbl)
  262.     {
  263.         for (i = 0; i < lpstotbl->cSto; i++)
  264.         {
  265.             if (!lstrcmpi (lpszName, lpstotbl->aSto[i].lpProps[ipDispNm].Value.LPSZ))
  266.             {
  267.                 if (lpstotbl->aSto[i].lpstoe->lpmdb)
  268.                 {
  269.                     *lppmdb = lpstotbl->aSto[i].lpstoe->lpmdb;
  270.                     hr = hrSuccess;
  271.                 }
  272.                 else
  273.                     hr = HrOpenStoEntry (lpsmh->lpsess, &lpstotbl->aSto[i], lppmdb);
  274.  
  275.                 break;
  276.             }
  277.         }
  278.     }
  279.  
  280.     if (HR_FAILED (hr))
  281.         *lppmdb = NULL;
  282.  
  283.     DebugTraceResult (HrOpenMdbFromEid(), hr);
  284.     return hr;
  285. }
  286.