⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialengine.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    }

    if (m_pAutoDialRules != NULL)
    {
        hr = CleanUpAutoDialRuleQueue(m_pAutoDialRules); 
        delete m_pAutoDialRules; 
        m_pAutoDialRules = NULL; 
    }

    return hr;
}

/*------------------------------------------------------------------------------
    DialEngine_t::CleanUpDialingRuleQueue

    Cleans up a DialingRuleQueue

    Parameters:
        : IN - DialingRuleQueue* - The queue to be cleaned up

    Returns:
        - S_OK returned if the queue was succesfully cleaned up
        - S_FALSE returned if the queue is already empty
        - E_UNEXPECTED returned if errors occur
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::CleanUpDialingRuleQueue(
    DialingRuleQueue*   pThisRules
    )
{
    return DialPlanParser_t::CleanUpDialingRuleQueue(pThisRules);
}

/*------------------------------------------------------------------------------
    DialEngine_t::CleanUpAutoDialRuleQueue

    Cleans up a AutoDialRuleQueue

    Parameters:
        : IN - AutoDialRuleQueue* - The queue to be cleaned up

    Returns:
        - S_OK returned if the queue was succesfully cleaned up
        - S_FALSE returned if the queue is already empty
        - E_UNEXPECTED returned if errors occur
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::CleanUpAutoDialRuleQueue(
    AutoDialRuleQueue*  pThisRules
    )
{
    return DialPlanParser_t::CleanUpAutoDialRuleQueue(pThisRules); 
}
/*------------------------------------------------------------------------------
    DialEngine_t::FindMatch

    Finds dialing rule which matches the user dial string

    Parameters:
        : IN - const WCHAR* - The user dial string
        : IN - int - The length of dial string
        : OUT - DialingRule_t** - Pointer to the dialing rule

    Returns:
        - (HRESULT) indicating success or failure
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::FindMatch(
    const WCHAR* pDialString,
    int DialStringLength,
    const DialingRule_t **ppRuleFound
    )
{
    HRESULT hr = E_FAIL;
    DialingRuleQueue::iterator Item;

    ASSERT(ppRuleFound);
    *ppRuleFound = NULL;

    if ((m_pDialingRules == NULL) || (m_pDialingRules->empty()))
    {
        return hr;
    }

    // Go through all dialing rules until first match is found
    // NOTE: regular expressions are created and compiled on demand
    for (Item = m_pDialingRules->begin(); Item != m_pDialingRules->end(); Item++)
    {
        bool bMatchFound;

        // Create the regular expression if necessary
        if ((*Item)->m_pRegex == NULL)
        {
            IRegularExpression_t *pIRegularExpression;

            hr = CreateRegularExpression(&pIRegularExpression);
            if (FAILED(hr))
            {
                PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"OOM - Failed to allocate rule's regular expression"));
                // Try the other rules
                continue;
            }

            hr = pIRegularExpression->SetExpression((*Item)->m_RegexString);
            if (FAILED(hr))
            {
                pIRegularExpression->Release();
                // Try the other rules
                continue;
            }

            (*Item)->m_pRegex = pIRegularExpression;
        }

        // See if it matches
        hr = (*Item)->m_pRegex->Matches(pDialString, &bMatchFound, NULL);
        if (SUCCEEDED(hr) && bMatchFound)
        {
            *ppRuleFound = static_cast<DialingRule_t*>(*Item);
            return hr;
        }
    }

    return E_FAIL;
}

/*------------------------------------------------------------------------------
    DialEngine_t::FormatPhoneNumber

    Formats dial string based on dialing rules
        FormatStringToDial - formats string for dial
        FormatStringToDisplay - formats string for display
        FormatStringToTransfer - formats string for transfer

    Parameters:
        : IN - const WCHAR* - The dial string
        : IN - int - The length of the dial string (it can be -1 if string is null terminated)
        : IN - FormatString_e - Format string type
        : IN(opt) IRTCProfile* - Profile to use for matching host-name/server address (if present)
        : OUT - BSTR* - The formatted phone number

    Returns:
        - S_OK returned if no errors occur
        - E_INVALIDARG returned if caller passed a NULL pointer or string
        - E_OUTOFMEMORY returned if it cannot allocate the formatted string
        - Error code returned if failed to use dialing rules otherwise
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::FormatPhoneNumber(
    const WCHAR* pDialString,
    int DialStringLength,
    FormatString_e Format,
    IRTCProfile* pProfileToUse,
    BSTR* pFormattedstring
    )
{
    // TRACE(ZONE_VOIP_CALLERID);

    HRESULT hr;
    const DialingRule_t* pDialingRule;
    BSTR ReplaceTemplate;
    WCHAR* pStringToDial;
    bool bMatches;

    ASSERT(Format <= FormatStringLast);

    if ((pFormattedstring == NULL) ||
        (pDialString == NULL))
    {
        return E_INVALIDARG;
    }

    *pFormattedstring = NULL;

    //caller can specify -1 for the dial string length if the string is null-terminated
    //and wants this API to calculate the length for them
    if (DialStringLength == -1)
    {
        DialStringLength = wcslen(pDialString);
    }

    if (DialStringLength <= 0)
    {
        ASSERT(FALSE);
        return E_INVALIDARG;
    }

    if (! pProfileToUse)
    {
        pProfileToUse = (GetApp()->GetProxyServices()).GetDefaultSIPProfile();
    }

    // Default to dial string passed in
    pStringToDial = const_cast<WCHAR*>(pDialString);

    hr = FindMatch(pDialString, DialStringLength, &pDialingRule);
    if (FAILED(hr))
    {
        // use default string
        goto exit;
    }

    // Choose the right template
    switch (Format)
    {
        case FormatStringToDial:
            ReplaceTemplate = pDialingRule->m_DialString;
            break;
        case FormatStringToDisplay:
            ReplaceTemplate = pDialingRule->m_DisplayString;
            break;
        case FormatStringToTransfer:
            ReplaceTemplate = pDialingRule->m_TransferString;
            break;
        default:
            ReplaceTemplate = NULL;
            break;
    }

    if ((ReplaceTemplate == NULL) ||
        (SysStringLen(ReplaceTemplate) == 0))
    {
        // use default string
        goto exit;
    }

    hr = pDialingRule->m_pRegex->Replace(
                    ReplaceTemplate,
                    pDialString,
                    &bMatches,
                    &pStringToDial
                    );
    if (FAILED(hr))
    {
        // Do not allocate
        pStringToDial = NULL;
    }

exit:
    if (pStringToDial == pDialString)
    {
        *pFormattedstring = SysAllocStringLen(pDialString, DialStringLength);
    }
    else if (pStringToDial != NULL)
    {
        const WCHAR* pKeywordFound = NULL;
        int Index;

        CASSERT(SIPServerKeywordNone == _countof(sc_SIPServerKeywords));

        // Look for special keywords
        for (Index = 0; Index < _countof(sc_SIPServerKeywords); Index++)
        {
            pKeywordFound = wcsstr(pStringToDial, sc_SIPServerKeywords[Index]);
            if (pKeywordFound)
            {
                break;
            }
        }

        if (pKeywordFound == NULL)
        {
            // Template formatting is complete
            *pFormattedstring = SysAllocString(pStringToDial);
        }
        else if (pProfileToUse)
        {
            //try to replace the template with the server address or host name
            ce::auto_bstr LocalBuffer;
            const WCHAR* pKeywordValue = NULL;

            switch (Index)
            {
                case SIPServerKeywordAddress:
                {

                    CComPtr<IRTCProfile3> cpRTCProfile3;

                    // Query the IRTCProfile3 object
                    hr = pProfileToUse->QueryInterface(
                                    IID_IRTCProfile3,
                                    reinterpret_cast<void**>(&cpRTCProfile3)
                                    );
                    if (SUCCEEDED(hr))
                    {
                        LONG Transport;
                        hr = cpRTCProfile3->GetServer(&LocalBuffer, &Transport);
                        if (LocalBuffer)
                        {
                            pKeywordValue = LocalBuffer;
                        }
                    }

                    break;
                }

                case SIPServerKeywordHostName:
                {
                    //get the URI we originally registered with
                    hr = pProfileToUse->get_UserURI(&LocalBuffer);

                    //the domain portion of the URI, comes after the last '@' symbol in the URI
                    if (LocalBuffer)
                    {
                        pKeywordValue = wcsrchr(LocalBuffer, URI_DOMAIN_SEPARATOR);

                        //if we found the '@' symbol, skip over it
                        if (pKeywordValue)
                        {
                            pKeywordValue++;
                        }
                    }
                    break;
                }

                default:
                    ASSERT(0);
                    hr = E_UNEXPECTED;
                    break;
            }

            if (pKeywordValue)
            {
                ce::wistring CopyOfString;
                if (CopyOfString.append(pStringToDial) &&
                    CopyOfString.replace(
                                    (pKeywordFound - pStringToDial),
                                    wcslen(sc_SIPServerKeywords[Index]),
                                    pKeywordValue
                                    ))
                {
                    *pFormattedstring = SysAllocString(CopyOfString.get_buffer());
                }
            }
            else
            {
                PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Failed to append sipsrv address or hostname. Error 0x%08x -- ", hr));

                // Default to the dial string from user
                *pFormattedstring = SysAllocStringLen(pDialString, DialStringLength);
            }
        }
        else
        {
            //default to dial string from user
            *pFormattedstring = SysAllocStringLen(pDialString, DialStringLength); 
        }

        LocalFree(pStringToDial);
    }

    if (pStringToDial)
    {
        hr = (*pFormattedstring == NULL) ? E_OUTOFMEMORY : S_OK;
        PHONEAPP_DEBUGMSG(FAILED(hr) && ZONE_PHONEAPP_ERROR, (L"OOM - Failed to allocate phone number to dial"));
    }

    return hr;
}

/*------------------------------------------------------------------------------
    DialEngine_t::GetPhoneNumberRestrictedServices

    Gets restricted services for user dial string

    Parameters:
        : IN - const WCHAR* - The user dial string
        : IN - int - The length of the user dial string
        : OUT - DWORD* - The restricted services

    Returns:
        - (HRESULT) indicating success or failure
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::GetPhoneNumberRestrictedServices(
    const WCHAR * pDialString,
    int DialStringLength,
    DWORD* pRestrictedServices
    )
{
    HRESULT hr;
    const DialingRule_t *pDialingRule;

    if (pRestrictedServices == NULL)
    {
        return E_POINTER;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -