home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / manager.sh / smhmdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  7.8 KB  |  294 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 = hrSuccess;
  147.     LPMDB lpmdb = NULL;
  148.  
  149.     if (!lpsto->lpstoe->lpmdb)
  150.     {
  151.         hr = lpsess->lpVtbl->OpenMsgStore (lpsess,
  152.                                 0,
  153.                                 lpsto->lpProps[ipEid].Value.bin.cb,
  154.                                 (LPENTRYID)lpsto->lpProps[ipEid].Value.bin.lpb,
  155.                                 NULL,
  156.                                 MDB_WRITE | MDB_NO_DIALOG | MDB_NO_MAIL,
  157.                                 &lpmdb);
  158.         if (!HR_FAILED (hr))
  159.             lpsto->lpstoe->lpmdb = lpmdb;
  160.         
  161.         *lppmdb = lpmdb;
  162.     }
  163.     else
  164.     {
  165.         *lppmdb = lpsto->lpstoe->lpmdb;
  166.     }
  167.  
  168.     DebugTraceResult (HrOpenStoEntry(), hr);
  169.     return hr;
  170. }
  171.  
  172.  
  173. /*
  174.  *  HrOpenMdbFromEid()
  175.  *
  176.  *  Purpose:
  177.  *
  178.  *      Opens a message store by entryid.  The entryid is looked up in
  179.  *      the stores table to find the proper STOENTRY.  If no entry is
  180.  *      found, then the store is not opened.  If one is found, then the
  181.  *      MDB is retrieved from the STOENTRY.  If it has not yet been opened,
  182.  *      the MDB is opened at this time and stored (no pun intended).
  183.  *
  184.  *      IMPORTANT: the caller does not end up owning the store reference
  185.  *      and should NOT call Release() on the store unless they also call
  186.  *      AddRef().  The referece is owned by the stores table and is
  187.  *      released upon the tables destruction.
  188.  *
  189.  *  Arguments:
  190.  *
  191.  *      lpsmh           the SMH parent object
  192.  *      cbeid           size of the stores entryid
  193.  *      lpeid           the entryid to be opened
  194.  *      lppmdb  [OUT]   contains the LPMDB iff the call succeeds
  195.  *
  196.  *  Returns:
  197.  *
  198.  *      (HRESULT)
  199.  */
  200. HRESULT
  201. HrOpenMdbFromEid (LPSMH lpsmh, ULONG cbeid, LPENTRYID lpeid, LPMDB FAR * lppmdb)
  202. {
  203.     HRESULT hr = ResultFromScode (MAPI_E_NOT_FOUND);
  204.     LPSTOTABLE lpstotbl = lpsmh->lpstotbl;
  205.     UINT i;
  206.  
  207.     if (lpstotbl)
  208.     {
  209.         for (i = 0; i < lpstotbl->cSto; i++)
  210.         {
  211.             if ((cbeid == lpstotbl->aSto[i].lpProps[ipEid].Value.bin.cb) &&
  212.                 !memcmp (lpeid, lpstotbl->aSto[i].lpProps[ipEid].Value.bin.lpb, (UINT)cbeid))
  213.             {
  214.                 if (lpstotbl->aSto[i].lpstoe->lpmdb)
  215.                 {
  216.                     *lppmdb = lpstotbl->aSto[i].lpstoe->lpmdb;
  217.                     hr = hrSuccess;
  218.                 }
  219.                 else
  220.                     hr = HrOpenStoEntry (lpsmh->lpsess, &lpstotbl->aSto[i], lppmdb);
  221.  
  222.                 break;
  223.             }
  224.         }
  225.     }
  226.  
  227.     if (HR_FAILED (hr))
  228.         *lppmdb = NULL;
  229.  
  230.     DebugTraceResult (HrOpenMdbFromEid(), hr);
  231.     return hr;
  232. }
  233.  
  234.  
  235. /*
  236.  *  HrOpenMdbFromName()
  237.  *
  238.  *  Purpose:
  239.  *
  240.  *      Opens a message store by PR_DISPLAY_NAME value.  The name is
  241.  *      looked up in the stores table to find the proper STOENTRY.  If no
  242.  *      entry is found, then the store is not opened.  If one is found,
  243.  *      then the MDB is retrieved from the STOENTRY.  If it has not yet
  244.  *      been opened, the MDB is opened at this time and stored (no pun
  245.  *      intended).
  246.  *
  247.  *      IMPORTANT: the caller does not end up owning the store reference
  248.  *      and should NOT call Release() on the store unless they also call
  249.  *      AddRef().  The referece is owned by the stores table and is
  250.  *      released upon the tables destruction.
  251.  *
  252.  *  Arguments:
  253.  *
  254.  *      lpsmh           the SMH parent object
  255.  *      lpszName        the name of the store to open
  256.  *      lppmdb  [OUT]   contains the LPMDB iff the call succeeds
  257.  *
  258.  *  Returns:
  259.  *
  260.  *      (HRESULT)
  261.  */
  262. HRESULT
  263. HrOpenMdbFromName (LPSMH lpsmh, LPTSTR lpszName, LPMDB FAR * lppmdb)
  264. {
  265.     HRESULT hr = ResultFromScode (MAPI_E_NOT_FOUND);
  266.     LPSTOTABLE lpstotbl = lpsmh->lpstotbl;
  267.     UINT i;
  268.  
  269.     if (lpstotbl)
  270.     {
  271.         for (i = 0; i < lpstotbl->cSto; i++)
  272.         {
  273.             if (!lstrcmpi (lpszName, lpstotbl->aSto[i].lpProps[ipDispNm].Value.LPSZ))
  274.             {
  275.                 if (lpstotbl->aSto[i].lpstoe->lpmdb)
  276.                 {
  277.                     *lppmdb = lpstotbl->aSto[i].lpstoe->lpmdb;
  278.                     hr = hrSuccess;
  279.                 }
  280.                 else
  281.                     hr = HrOpenStoEntry (lpsmh->lpsess, &lpstotbl->aSto[i], lppmdb);
  282.  
  283.                 break;
  284.             }
  285.         }
  286.     }
  287.  
  288.     if (HR_FAILED (hr))
  289.         *lppmdb = NULL;
  290.  
  291.     DebugTraceResult (HrOpenMdbFromEid(), hr);
  292.     return hr;
  293. }
  294.