//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2013  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Extented MAPI environent CMAPEx class implementation
//  NOTES: Encapsulates all subordinate MAPI resources
//      

#include "stdafx.h"
#include "P2Pevent.h"
#include "MAPIEx.h"
#include "MAPI_Ext.h"

/////////////////////////////////////////////////////////////
//  CMAPIEx constructors and destructor
//
CMAPIEx::CMAPIEx()
{
    m_bInitialized  = false;
}

CMAPIEx::~CMAPIEx()
{
    if ( m_bInitialized )
      Uninitialize ( );
}

//  Initialise MAPI environment
//  NOTES: Refer complimentary Initialize() for initialising the MAPI
//         environment.  MAPI environment may be managed external to
//         this object.
//       : When running from the debugger with exceptions turned on the
//         subsequent call generates internals exceptions.  {Continue}
//         through exception list and all appears OK.
//
//  Parameters:  MAPINIT_0 *pMAPINIT
//               Initializatio structure, refer MAPIInitialize() for
//               further details.
//
//  Returns:     HRESULT
//               Standard error error.  Refer GetP2Pevent() for retrieval of
//               full description
HRESULT
CMAPIEx::Initialize ( MAPIINIT_0 *pMAPINIT )
{
    ASSERT(!m_bInitialized);
    if ( m_bInitialized )
      return S_OK;
    // Expect 3 exceptions in debug mode at this point
    HRESULT hr = MAPIInitialize ( pMAPINIT );
    if ( hr == S_OK )
      m_bInitialized = true;
    else
      EVERR_MAPI(hr,"MAPIInitialise() failed")->SetLast();
    return hr;
}

//  Uninitialise MAPI environment
//  NOTES: Refer complimentary Initialize() for initialising the MAPI
//         environment.  MAPI environment may be managed external to
//         this object.
//
void
CMAPIEx::Uninitialize ( )
{
    //DropSession ( ); //LogoffEx ( );
    if ( m_bInitialized )
	    MAPIUninitialize ( );
    m_bInitialized = false;
}

CMAPISession*
CMAPIEx::Query4Session ( )
{
    if ( !m_bInitialized )
      EVERR_MAPI(0,"MAPI not initialised")->Throw();
    return new CMAPISession ( this );
}

///////////////////////////////////////
//  Property exposure

bool
CMAPIEx::IsInitialized ( )
{
    return m_bInitialized;
}
