📄 voipcalllogdb.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
// VoIPCallLogDB.cpp : Implementation of CVoIPCallLogDB
#include "VoIPStore.h"
#include "VoIPCallLogDB.h"
#include "VoIPCallRecord.h"
#include "VoIPCallLogDBEnum.h"
#include "auto_xxx.hxx"
#define MAX_STRING 512
/////////////////////////////////////////////////////////////////////////////
// CVoIPCallLogDB
/*----------------------------
Constructor - does nothing -
The object must be initialized before use
------------------------------*/
CVoIPCallLogDB::CVoIPCallLogDB()
{
m_pIncomingDB = NULL;
m_pOutgoingDB = NULL;
m_pMissedDB = NULL;
m_fInit = FALSE;
m_Capacity = INFINITE;
} //Constructor
/*----------------------------
Destructor
Deletes the DB's
------------------------------*/
CVoIPCallLogDB::~CVoIPCallLogDB()
{
if (m_pIncomingDB)
{
delete m_pIncomingDB;
m_pIncomingDB = NULL;
}
if (m_pOutgoingDB)
{
delete m_pOutgoingDB;
m_pOutgoingDB = NULL;
}
if (m_pMissedDB)
{
delete m_pMissedDB;
m_pMissedDB = NULL;
}
}
/*---------------------------------------
Init ()
Params:[in]BSTR - uri for which these logs will be saved
Initializes all member variables
Returns: HRESULT
---------------------------------------*/
STDMETHODIMP CVoIPCallLogDB::Init(
BSTR bstrURI
)
{
WCHAR Name[MAX_STRING];
HRESULT hr = S_OK;
SORTORDERSPECEX Sort = {0};
//if already initialized we fail
if (m_fInit)
{
return VOIP_E_ALREADYINITIALIZED;
}
if (bstrURI == NULL)
{
return E_POINTER;
}
Sort.rgdwFlags[0] = CEDB_SORT_DESCENDING;
Sort.rgPropID[0] = VOIP_TIME_STAMP;
Sort.wNumProps = 1;
Sort.wKeyFlags = 0;
Sort.wVersion = 1;
//Create and initialize the 3 databases
StringCchPrintf(
Name,
_countof(Name),
L"In:%s",
bstrURI
);
m_pIncomingDB = new CVoipDB(CALL_LOG_DB_NUMBER_PROPS);
if (! m_pIncomingDB)
{
hr = E_OUTOFMEMORY;
goto exit;
}
hr = m_pIncomingDB->Initialize(
Name,
&Sort
);
if (FAILED(hr))
{
goto exit;
}
StringCchPrintf(
Name,
_countof(Name),
L"Ot:%s",
bstrURI
);
m_pOutgoingDB = new CVoipDB(CALL_LOG_DB_NUMBER_PROPS);
if (! m_pOutgoingDB)
{
goto exit;
}
hr = m_pOutgoingDB->Initialize(
Name,
&Sort
);
if (FAILED(hr))
{
goto exit;
}
StringCchPrintf(
Name,
_countof(Name),
L"Ms:%s",
bstrURI
);
m_pMissedDB = new CVoipDB(CALL_LOG_DB_NUMBER_PROPS);
if (! m_pMissedDB)
{
goto exit;
}
hr = m_pMissedDB->Initialize(
Name,
&Sort
);
if (FAILED(hr))
{
goto exit;
}
//Yeah we passed
m_fInit = TRUE;
hr = S_OK;
exit:
if (FAILED(hr))
{
//Clean up if we fail to init
if (m_pIncomingDB)
{
delete m_pIncomingDB;
m_pIncomingDB = NULL;
}
if (m_pOutgoingDB)
{
delete m_pOutgoingDB;
m_pOutgoingDB = NULL;
}
if (m_pMissedDB)
{
delete m_pMissedDB;
m_pMissedDB = NULL;
}
}
return hr;
} //Init
/*---------------------------------------
Create Record
Params: [in]Call Type, [out]Call Record **
Creates a new interface pointer to a IVoIPCallRecord
Returns: HRESULT
---------------------------------------*/
STDMETHODIMP CVoIPCallLogDB::CreateRecord(
VoIPCallType vctType,
IVoIPCallRecord **ppiRecord
)
{
HRESULT hr = S_OK;
CComObject<CVoIPCallLogRecord> *pRecord = NULL;
//Can't use unless you have init'ed
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//Check param and NULL out ppiRecord
if (SUCCEEDED(hr))
{
if (ppiRecord == NULL)
return E_POINTER;
*ppiRecord = NULL;
}
//params are checked in internal create record
if (SUCCEEDED(hr))
{
hr = InternalCreateRecord(vctType, &pRecord);
}
//Query and init
if (SUCCEEDED(hr))
{
//Query to get the correct interface to make the compiler happy
hr = pRecord->QueryInterface(IID_IVoIPCallRecord, (void **)ppiRecord);
}
if (pRecord) //We add ref'ed 2x
pRecord->Release();
//Cleanup on failure
if (FAILED(hr))
{
if (ppiRecord && *ppiRecord)
{
(*ppiRecord)->Release();
(*ppiRecord) = NULL;
}
}
return hr;
}
/*---------------------------------------
Internal Create Record
Params: [in]Call Type, [out]COM Object - CVoIPCallLogRecord - internal class
Allocates space and initializes members for a log record
Returns: HRESULT
---------------------------------------*/
HRESULT CVoIPCallLogDB::InternalCreateRecord(VoIPCallType vctType, CComObject<CVoIPCallLogRecord> **ppRecord)
{
CVoipDB *pDB = NULL;
HRESULT hr = S_OK;
//Can't use unless you have init'ed
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//Check param
if (SUCCEEDED(hr))
{
if (ppRecord == NULL)
hr = E_POINTER;
}
//Check param and set which DB to create a record for
if (SUCCEEDED(hr))
{
switch (vctType)
{
case e_vctIncoming:
pDB = m_pIncomingDB;
break;
case e_vctOutgoing:
pDB = m_pOutgoingDB;
break;
case e_vctMissed:
pDB = m_pMissedDB;
break;
default:
//not a recognized call type
hr = E_INVALIDARG;
break;
}
}
//Fill ppRecord with a CallLogRecord COM Object
if (SUCCEEDED(hr))
{
//Create Instance
hr = CComObject<CVoIPCallLogRecord>::CreateInstance(ppRecord);
}
if (SUCCEEDED(hr))
{
(*ppRecord)->AddRef();
//set the correct db as the owner of the record
(*ppRecord)->SetDB(pDB);
}
if (SUCCEEDED(hr))
{
//Initialize call type of record
hr = (*ppRecord)->put_CallType(vctType);
}
if (FAILED(hr))
{
if (ppRecord && *ppRecord)
{
(*ppRecord)->Release();
(*ppRecord) = NULL;
}
}
return hr;
}
/*---------------------------------------
put capacity
Params: [in]new capacity
Sets the capacity of all three internal databases.
Returns: HRESULT
---------------------------------------*/
STDMETHODIMP CVoIPCallLogDB::put_Capacity(int cRecords)
{
HRESULT hr = S_OK;
//Can't use the function unless initialized
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//Check params
if (cRecords <= 0 && cRecords != INFINITE)
{
return E_INVALIDARG;
}
//Check internal db's
if (!m_pIncomingDB || !m_pOutgoingDB || !m_pMissedDB)
{
return E_FAIL;
}
m_pIncomingDB->Resize(cRecords);
m_pOutgoingDB->Resize(cRecords);
m_pMissedDB->Resize(cRecords);
return hr;
}
/*---------------------------------------
get capacity
Params: [out]capacity *
Gets the capacity of each internal db
Returns: HRESULT
---------------------------------------*/
STDMETHODIMP CVoIPCallLogDB::get_Capacity(int *pcRecords)
{
//Can't use the function unless initialized
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//Check params
if (pcRecords == NULL)
{
return E_POINTER;
}
*pcRecords = m_pIncomingDB->GetCapacity();
return S_OK;
}
/*---------------------------------------
FillRecord
Params: [in]CallRecord, [in]array of propvals, [in]array size
Internal function (not exposed through the interface) that copies properties from
CEPROPVAL array into a record
Returns: HRESULT
---------------------------------------*/
HRESULT CVoIPCallLogDB::FillRecord(CVoIPCallLogRecord *piRecord, CEPROPVAL *pvals, int cNumProps)
{
int i = 0;
HRESULT hr = S_OK;
SYSTEMTIME systemtime = {0};
FILETIME filetime = {0};
//Can't use the function unless initialized
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//Check params
if (piRecord == NULL || pvals == NULL)
return E_POINTER;
//Check params
if (cNumProps < 0)
return E_INVALIDARG;
//For each prop
for (i = 0; i < cNumProps; i++)
{
//Call appropriate put_ATTRIBUTE on record
switch(pvals[i].propid)
{
case CALL_LOG_CALLTYPE:
hr = piRecord->put_CallType((VoIPCallType)pvals[i].val.lVal);
break;
case CALL_LOG_ENDTIME:
filetime = pvals[i].val.filetime;
FileTimeToSystemTime(&filetime, &systemtime);
hr = piRecord->put_EndTime(systemtime);
break;
case CALL_LOG_FRIENDLYNAME:
{
ce::auto_bstr bstrName = SysAllocString(pvals[i].val.lpwstr);
hr = piRecord->put_FriendlyName(bstrName);
}
break;
case CALL_LOG_STARTTIME:
filetime = pvals[i].val.filetime;
FileTimeToSystemTime(&filetime, &systemtime);
hr = piRecord->put_StartTime(systemtime);
break;
case CALL_LOG_URI:
{
ce::auto_bstr bstrURI = SysAllocString(pvals[i].val.lpwstr);
hr = piRecord->put_URI(bstrURI);
}
break;
case CALL_LOG_VOIPNAME:
{
ce::auto_bstr bstrName = SysAllocString(pvals[i].val.lpwstr);
hr = piRecord->put_VoIPName(bstrName);
}
break;
case VOIP_TIME_STAMP:
filetime = pvals[i].val.filetime;
hr = piRecord->put_TimeStamp(filetime);
break;
default:
// Unexpected property ID
ASSERT(FALSE);
break;
}
if (FAILED(hr))
break;
}
return hr;
}
/*---------------------------------------
GetEnumerator
Params: [in]type of enumerator, [out] enumerator interface double pointer
Instantiates inner IVoIPCallLogDBEnum interface class and initializes the Enumerator.
Returns: HRESULT
---------------------------------------*/
HRESULT CVoIPCallLogDB::GetEnumerator(VoIPCallType vct, IVoIPCallLogDBEnum **ppiEnum)
{
HRESULT hr = S_OK;
CComObject<CVoIPCallLogDBEnum> *pCallLogObject = NULL;
CVoipDB *pDB = NULL;
BOOL fSelfReferenced = FALSE;
//If not initialized can't call this function
if (!m_fInit)
{
return VOIP_E_NOTINITIALIZED;
}
//check params
if (ppiEnum == NULL)
{
return E_POINTER;
}
//check params and set up which db to use
switch (vct)
{
case e_vctIncoming:
pDB = m_pIncomingDB;
break;
case e_vctOutgoing:
pDB = m_pOutgoingDB;
break;
case e_vctMissed:
pDB = m_pMissedDB;
break;
default:
hr = E_INVALIDARG;
break;
}
//Create an internal calllogenum
if (SUCCEEDED(hr))
{
hr = CComObject<CVoIPCallLogDBEnum>::CreateInstance(&pCallLogObject);
}
//Initialize the calllogenum with 'this' as the callback object
//Init also addref's 'this'
if (SUCCEEDED(hr))
{
hr = pCallLogObject->Init(vct, pDB, (CVoIPCallLogDB *)this);
}
//Also, Query for the interface
if (SUCCEEDED(hr))
{
hr = pCallLogObject->QueryInterface(IID_IVoIPCallLogDBEnum, (void **)ppiEnum);
}
if (SUCCEEDED(hr))
{
if (*ppiEnum == NULL)
hr = E_NOINTERFACE;
}
//Reset the enumeration to start at 0
if (SUCCEEDED(hr))
{
hr = (*ppiEnum)->Reset();
}
//Clean up allocated resources on failure
if (FAILED(hr))
{
if (*ppiEnum != NULL)
{
(*ppiEnum)->Release();
(*ppiEnum) = NULL;
}
}
return hr;
}
//Get Enumerator functions all call into GetEnumerator() directly
STDMETHODIMP CVoIPCallLogDB::get_IncomingEnumerator(IVoIPCallLogDBEnum **ppiEnum)
{
return GetEnumerator(e_vctIncoming, ppiEnum);
}
STDMETHODIMP CVoIPCallLogDB::get_OutgoingEnumerator(IVoIPCallLogDBEnum **ppiEnum)
{
return GetEnumerator(e_vctOutgoing, ppiEnum);
}
STDMETHODIMP CVoIPCallLogDB::get_MissedEnumerator(IVoIPCallLogDBEnum **ppiEnum)
{
return GetEnumerator(e_vctMissed, ppiEnum);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -