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 / remote.xp / xpprov.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  12.3 KB  |  361 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  File Name 
  4. //      XPPROV.CPP
  5. //
  6. //  Description
  7. //      This file implements the IXPProvider interface with the methods 
  8. //      specified in the MAPI SPI 1.0 specifications. Also some helper
  9. //      functions are implemented in this file.
  10. //
  11. //  Author
  12. //      Irving De la Cruz
  13. //
  14. //  Revision: 1.7
  15. //
  16. // Written for Microsoft Windows Developer Support
  17. // Copyright (c) 1995-1996 Microsoft Corporation. All rights reserved.
  18. //
  19. #define INITGUID
  20. #define USES_IID_IXPProvider
  21. #define USES_IID_IXPLogon
  22. #define USES_IID_IMAPIStatus
  23. #define USES_IID_IMAPIProp
  24. #define USES_IID_IMAPIPropData
  25. #define USES_IID_IMAPIControl
  26. #define USES_IID_IMAPIContainer
  27. #define USES_IID_IMAPIFolder
  28. #define USES_IID_IMAPITableData
  29.  
  30. #include "XPWDSR.H"
  31.  
  32. // Remark this line to turn verbose tracing OFF
  33. #define DO_INFO_TRACES
  34. #ifdef DO_INFO_TRACES
  35. #define InfoTrace(a)   TraceInfoMessage(a)
  36. #else
  37. #define InfoTrace(a)   
  38. #endif // DO_INFO_TRACES
  39.  
  40. ///////////////////////////////////////////////////////////////////////////////
  41. //    CXPProvider::CXPProvider()
  42. //
  43. //    Parameters
  44. //      hInst                   Handle to instance of this XP DLL
  45. //
  46. //    Purpose
  47. //      Constructor of the object. Parameters are passed to initialize the
  48. //      data members with the appropiate values.
  49. //
  50. //    Return Value
  51. //      None
  52. //
  53. CXPProvider::CXPProvider (HINSTANCE hInst)
  54. {
  55.     InfoTrace ("CXPProvider: Constructor called");
  56.     m_hInstance    = hInst;
  57.     m_cRef         = 1;
  58.     // Initialize critical sections for this transport
  59.     InitializeCriticalSection (&m_csTransport);
  60. }
  61.  
  62. ///////////////////////////////////////////////////////////////////////////////
  63. //    CXPProvider::~CXPProvider()
  64. //
  65. //    Parameters
  66. //      None
  67. //
  68. //    Purpose
  69. //      Close down and release resources and libraries
  70. //
  71. //    Return Value
  72. //      None
  73. //
  74. CXPProvider::~CXPProvider()
  75. {
  76.     InfoTrace ("CXPProvider: Destructor called");
  77.     m_hInstance = NULL;
  78.  
  79.     // This is the last method called on a transport, close down the traces
  80.     UnInitTraces();
  81.     DeleteCriticalSection (&m_csTransport);
  82. }
  83.  
  84. ///////////////////////////////////////////////////////////////////////////////
  85. //    CXPProvider::QueryInterface()
  86. //
  87. //    Parameters
  88. //      { Refer to OLE Documentation on this method }
  89. //
  90. //    Purpose
  91. //      Returns a pointer to a interface requested if the interface is 
  92. //      supported and implemented by this object. If it is not supported, it 
  93. //      returns NULL
  94. //
  95. //    Return Value
  96. //      An HRESULT
  97. //
  98. STDMETHODIMP CXPProvider::QueryInterface (REFIID riid, LPVOID * ppvObj)
  99. {
  100.     // OLE requires NULLing parameter
  101.     *ppvObj = NULL;
  102.     // If this is one of the two IID return an interface pointer to it
  103.     if (riid == IID_IXPProvider || riid == IID_IUnknown)
  104.     {
  105.         *ppvObj = (LPVOID)this;
  106.         // Increase usage count of this object
  107.         AddRef();
  108.         return S_OK;
  109.     }
  110.     // This object does not support the interface requested
  111.     return E_NOINTERFACE;
  112. }
  113.  
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // IXPProvider virtual member functions implementation
  116. //
  117.  
  118. ///////////////////////////////////////////////////////////////////////////////
  119. //    CXPProvider::Shutdown()
  120. //
  121. //    Parameters
  122. //      { Refer to MAPI Documentation on this method }
  123. //
  124. //    Purpose
  125. //      Stub method.
  126. //
  127. //    Return Value
  128. //      An HRESULT
  129. //
  130. STDMETHODIMP CXPProvider::Shutdown (ULONG * pulFlags)
  131. {
  132.     InfoTrace ("CXPProvider::Shutdown method called");
  133.     CheckParameters_IXPProvider_Shutdown (this, pulFlags);
  134.     return S_OK;
  135. }
  136.  
  137. ///////////////////////////////////////////////////////////////////////////////
  138. //    CXPProvider::TransportLogon()
  139. //
  140. //    Parameters
  141. //      { Refer to MAPI Documentation on this method }
  142. //
  143. //    Purpose
  144. //      Display the logon dialog to show the options saved in the profile for
  145. //      this provider and allow changes to it. Save new configuration settings
  146. //      back in the profile.
  147. //      Create a new CXPLogon object and return it to the spooler. Also, 
  148. //      initialize the properties array for each address type handled
  149. //      by this transport. Check all the flags and return them to the spooler
  150. //
  151. //    Return Value
  152. //      An HRESULT
  153. //
  154. STDMETHODIMP CXPProvider::TransportLogon (LPMAPISUP     pSupObj,
  155.                                           ULONG         ulUIParam,
  156.                                           LPTSTR        pszProfileName,
  157.                                           ULONG *       pulFlags,
  158.                                           LPMAPIERROR * ppMAPIError,
  159.                                           LPXPLOGON *   ppXPLogon)
  160. {
  161.     InfoTrace ("CXPProvider::TransportLogon method called");
  162.     CheckParameters_IXPProvider_TransportLogon (this,
  163.                                                 pSupObj,
  164.                                                 ulUIParam,
  165.                                                 pszProfileName,
  166.                                                 pulFlags,
  167.                                                 ppMAPIError,
  168.                                                 ppXPLogon);
  169.     CXPLogon * pXPLogon = NULL;
  170.     ULONG ulPropCount;
  171.     LPSPropValue pProps = NULL;
  172.     MAILBOX_INFO UserMBInfo = { 0 };
  173.     CFGDLG CfgDialog = { 0 };
  174.     LPPROFSECT pProfileObj;
  175.     HRESULT hResult = OpenServiceProfileSection (pSupObj, &pProfileObj, gpfnFreeBuffer);
  176.     if (hResult)
  177.     {
  178.         TraceResult ("CXPProvider::TransportLogon: Failed to open the service profile section", hResult);
  179.         return hResult;
  180.     }
  181.  
  182.     HANDLE hUIMutex = CreateMutex (NULL, FALSE, CONFIG_UI_MUTEX);
  183.     if (NULL == hUIMutex)
  184.     {
  185.         HRESULT hResult = HRESULT_FROM_WIN32 (GetLastError());
  186.         TraceResult ("CXPProvider::TransportLogon: Failed to create UI mutext", hResult);
  187.     }
  188.     
  189.     hResult = pProfileObj->GetProps ((LPSPropTagArray)&sptLogonProps,
  190.                                      fMapiUnicode,
  191.                                      &ulPropCount,
  192.                                      &pProps);
  193.     if (FAILED(hResult))
  194.     {
  195.         TraceResult ("CXPProvider::TransportLogon: Failed to get the logon props", hResult);
  196.         goto ErrorExit;
  197.     }
  198.  
  199.     // Fill in the logon UI structure
  200.     CfgDialog.hInst    = m_hInstance;
  201.     CfgDialog.hWnd     = (HWND)ulUIParam;
  202.     CfgDialog.ppProps  = &pProps;
  203.     CfgDialog.pSupObj  = pSupObj;
  204.     CfgDialog.hUIMutex = hUIMutex;
  205.  
  206.     // In case we get MAPI_W_ERRORS_RETURNED, ignore it and reset 
  207.     // to S_OK. Now display the logon configuration dialog
  208.     if (MAPI_W_ERRORS_RETURNED == hResult)
  209.     {
  210.         if (PR_SMP_MAILBOX_ID       != pProps[MAILBOX_ID].ulPropTag  ||
  211.             PR_SMP_CONNECTION_TYPE  != pProps[NET_CON].ulPropTag     ||
  212.             PR_SMP_UPLOAD_TIME      != pProps[UPLOAD_TIME].ulPropTag ||
  213.             PR_SMP_HEADERS_FILENAME != pProps[HEADER_FILE].ulPropTag ||
  214.             PR_SMP_GET_HEADERS      != pProps[GET_HEADERS].ulPropTag)
  215.         {
  216.             TraceMessage ("CXPProvider::TransportLogon: Where are the provider properties?");
  217.             hResult = MAPI_E_UNCONFIGURED;
  218.             if (!(LOGON_NO_DIALOG & *pulFlags))
  219.             {
  220.                 PrivInitialize3DCtl (m_hInstance);
  221.                 PrivateMessageBox (IDS_MSG_SERVICE_NOT_PROPERLY_CFG, (HWND)ulUIParam);
  222.                 PrivUninitialize3DCtl (m_hInstance);
  223.             }
  224.             goto ErrorExit;
  225.         }
  226.  
  227.         if (PR_SMP_REMOTE_SERVER != pProps[SERVER_NAME].ulPropTag ||
  228.             PR_SMP_MAILBOX_NAME != pProps[MAILBOX_NAME].ulPropTag ||
  229.             PR_SMP_USER_NAME != pProps[USER_NAME].ulPropTag ||
  230.             PR_SMP_MAILBOX_PASSWORD != pProps[PASSWORD].ulPropTag)
  231.         {
  232.             if (LOGON_NO_DIALOG & *pulFlags)
  233.             {
  234.                 TraceMessage ("CXPProvider::TransportLogon: UI not allowed but it is needed");
  235.                 hResult = MAPI_E_UNCONFIGURED;
  236.                 goto ErrorExit;
  237.             }
  238.             PrivInitialize3DCtl (m_hInstance);
  239. ReStartLogonDlg :
  240.             if ((hResult = DoLogonDlg (&CfgDialog)))
  241.             {
  242.                 goto ErrorExit;
  243.             }
  244.             hResult = pProfileObj->SetProps (4, pProps, NULL);
  245.             if (hResult)
  246.             {
  247.                 TraceResult ("CXPProvider::TransportLogon: Failed to set the properties", hResult);
  248.                 goto ErrorExit;
  249.             }
  250.         }
  251.     }
  252.     // Connect to the server with the stored information. If necessary (and allowed) display UI.
  253.     hResult = DoServerLogon (&UserMBInfo,
  254.                              pProps,
  255.                              pProfileObj,
  256.                              !(LOGON_NO_DIALOG & *pulFlags),
  257.                              (HWND)ulUIParam,
  258.                              FALSE,
  259.                              0,
  260.                              NULL,
  261.                              TRUE);
  262.     if (S_FALSE == hResult)
  263.     {
  264.         goto ReStartLogonDlg;
  265.     }
  266.     if (hResult)
  267.     {
  268.         goto ErrorExit;
  269.     }
  270.  
  271.     // Once we have validated the remote credentials, terminate all connections
  272.     TerminateRemoteConnections();
  273.  
  274.     try
  275.     {
  276.         // Allocate the IXPLogon-derived object. Initialize its data members with the necessary values
  277.         pXPLogon = new CXPLogon (m_hInstance,
  278.                                  pSupObj,
  279.                                  pProps[HEADER_FILE].Value.LPSZ,
  280.                                  pProps[GET_HEADERS].Value.b,
  281.                                  &UserMBInfo,
  282.                                  pProps[MAILBOX_ID].Value.l,
  283.                                  pProps[SERVER_NAME].Value.LPSZ,
  284.                                  hUIMutex);
  285.         if (!pXPLogon)
  286.         {
  287.             // New failed, propably due to memory shortage
  288.             TraceMessage ("CXPProvider::TransportLogon: Failed to allocate new CXPLogon object");
  289.             hResult = E_OUTOFMEMORY;
  290.         }
  291.     }
  292.     catch (CException & Exception)
  293.     {
  294.         hResult = Exception.GetError();
  295.     }
  296.     if (hResult)
  297.     {
  298.         goto ErrorExit;
  299.     }
  300.     hUIMutex = NULL;
  301.  
  302.     pXPLogon->CheckForUnfinishedDownloads();
  303.  
  304.     // Lock other threads because we are going to work on data that is 
  305.     // global to all threads (sessions) using this transport
  306.     EnterCriticalSection (&m_csTransport);
  307.     // Check the mode in which MAPI is logging into our transport and set
  308.     // the appropiate internal (this CXPLogon object) transport session flags
  309.     pXPLogon->InitializeTransportStatusFlags (*pulFlags);
  310.     // Initialize the transport ID prop array for this session
  311.     hResult = pXPLogon->SetIdentityProps();
  312.     if (!hResult)
  313.     {
  314.         // Build the transport status row for this session
  315.         hResult = pXPLogon->InitializeStatusRow();
  316.         if (!hResult)
  317.         {
  318.             // Set the session flags returned to MAPI by the transport
  319.             pXPLogon->SetSessionFlags (pulFlags);
  320.             // Set the upload delivery time to the current day, mo, yr and the hour and
  321.             // minute from the config dialog saved in the profile. The profile info
  322.             // is only valid for the hour and minute of day, the date can be old
  323.             pXPLogon->SetUploadTime (pProps[UPLOAD_TIME].Value.ft);
  324.             // Copy our allocated object back to the returned MAPI object pointer
  325.             *ppXPLogon = (LPXPLOGON)pXPLogon;
  326.         }
  327.     }
  328.     // Release the critical section
  329.     LeaveCriticalSection (&m_csTransport);
  330.  
  331.  
  332. ErrorExit:
  333.     // Release pProfileObj. This object was AddRef() by MAPI before it 
  334.     // came to us. The transport is in charge of cleaning it up (Release()ing it).
  335.     if (pProfileObj)
  336.     {
  337.         pProfileObj->Release();
  338.     }
  339.     PrivUninitialize3DCtl (m_hInstance);
  340.  
  341.     if (hUIMutex)
  342.     {
  343.         CloseHandle (hUIMutex);
  344.     }
  345.     gpfnFreeBuffer (pProps);
  346.     if (hResult)
  347.     {   
  348.         // Something failed, so clean the session if it has been allocated.
  349.         // The Release() method of CXPLogon does the memory deallocation 
  350.         // of this object. The destructor of CXPLogon does the rest of 
  351.         // the clean up
  352.         if (pXPLogon)
  353.         {
  354.             pXPLogon->Release();
  355.         }
  356.     }
  357.     return hResult;
  358. }                                        
  359.  
  360. // End of File for XPPROV.CPP
  361.