ansphoneengine.cpp

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 551 行 · 第 1/2 页

CPP
551
字号
{
    iObserver.HandleCallChangeL(RCall::EStatusUnknown);
    delete iCallMaker;
    iCallMaker = NULL;

    if(!iCallWatcher)
    {
        iLine.Close();
        iSession.Close();
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// MAnsPhoneCallLogObserver
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::HandlePhoneNumberL(const TDesC& aNumber)
// the logger has completed with the phone number that called, so
// copy the file to the new name for the number and update the messages list with this new name
{
    // get the next file in the list
    TFileName file;
    GetNextMessageFileName(file, aNumber);

    TMessage& message = iMessageList->At(0);
    message.iNumber = file;
    file.Insert(0, KMessagesDir);

    TFileName fileName (KTheirMessageFileName);
    CompleteWithAppPath (fileName);
    iFs.Rename(fileName, file);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::AnsweringStartL()
// starts the answering machine
{
    TelStartL();
    iCallWatcher = CAnsPhoneCallWatcher::NewL(*this, iLine);
    iCallLog = CAnsPhoneCallLog::NewL(*this);
}

void CAnsPhoneEngine::AnsweringStop()
// stops the answering machine
{
    TelephonyCleanup();
}

void CAnsPhoneEngine::PlayMessageL(TBool aIsLocal, TInt aIndex, TBool aIsUsers)
{
    SoundCleanup();
    iSound = CMdaAudioRecorderUtility::NewL(*this);

    if(aIsUsers)
    {
        TFileName fileName (KYourMessageFileName);
        CompleteWithAppPath (fileName);
        iSound->OpenFileL(fileName);
    }
    else
    {
        TFileName file = iMessageList->At(aIndex).iNumber;
        file.Insert(0, KMessagesDir);
        iSound->OpenFileL(file);
    }

    iState = EPlayInit;
    iIsLocal = aIsLocal;
}

void CAnsPhoneEngine::RecordMessageL(TBool aIsLocal)
// records a message;
//    if aIsLocal = ETrue, then record to the user's message;        use the telephony line
//    if aIsLocal = EFalse, then record to aFileName;                use the device microphone
//
//    there are several ways to record to a file by the CMdaAudioRecorderUtility:
//    ->    use OpenFileL()
//        if we use this method, then the file must all ready exist, and just creating a new file using
//        RFile.CreateL() or RFile.ReplaceL() will not be accepted by the recorder utility
//        therefore, this would put a limit on how many messages could be kept
//    ->    use OpenL()
//        if we use this method, then the file name passed to it, will lead it to construct a new file
//        with this filename or it will overwrite an existing file with that name
//        however, in order to use this function, we need to pass in several objects:
//        ->    file location
//        ->    file format to be recorded, this is populated by the function
//        ->    recording codec to use
//        ->    audio settings, such as sample rate and number of channels
{
    SoundCleanup();

    iSound = CMdaAudioRecorderUtility::NewL(*this);

    iMessageCodec.iBits = TMdaPcmWavCodec::E16BitPcm;
    iMessageSettings.iCaps = TMdaAudioDataSettings::ESampleRateFixed | TMdaAudioDataSettings::ESampleRate8000Hz | TMdaAudioDataSettings::EChannelsMono;
    iMessageSettings.iSampleRate = 8000;
    iMessageSettings.iChannels = 1;

    if(aIsLocal)
    {
        TFileName fileName (KYourMessageFileName);
        CompleteWithAppPath (fileName);
        iMessageLocation.iName = fileName;
    }
    else
    {
        TFileName fileName (KTheirMessageFileName);
        CompleteWithAppPath (fileName);
        iMessageLocation.iName = fileName;
    }

    iSound->OpenL(&iMessageLocation, &iMessageFormat, &iMessageCodec, &iMessageSettings);

    iState = ERecordInit;
    iIsLocal = aIsLocal;
}

void CAnsPhoneEngine::Stop()
{
    SoundCleanup();
}

void CAnsPhoneEngine::TelStartL()
// starts the telephony server and line;
// this does not start up the call watcher
// this is used when dialling
{
    // if the call watcher is up and running, then we have all ready started the telephony server and line
    if(iCallWatcher)
        return;

    User::LeaveIfError(iSession.Connect());
    // load the appropriate tsy
    User::LeaveIfError(iSession.LoadPhoneModule(KTSY));

    // in order to get a handle on a line, we must get a handle on an RPhone object
    TInt numberPhones = 0;
    User::LeaveIfError(iSession.EnumeratePhones(numberPhones));
    if(!numberPhones)
        User::Leave(KErrNotFound);

    // we use the 1st available phone
    RTelServer::TPhoneInfo phoneInfo;
    User::LeaveIfError(iSession.GetPhoneInfo(0, phoneInfo));
    User::LeaveIfError(iPhone.Open(iSession, phoneInfo.iName));

    // we must now find a line that will accept voice calls
    TInt numberLines = 0;
    User::LeaveIfError(iPhone.EnumerateLines(numberLines));
    RPhone::TLineInfo lineInfo;
    TBool foundLine = EFalse;
    for(TInt a = 0; a < numberLines; a++)
    {
        User::LeaveIfError(iPhone.GetLineInfo(a, lineInfo));
        if(lineInfo.iLineCapsFlags & RLine::KCapsVoice)
        {
            foundLine = ETrue;
            break;
        }
    }

    if(!foundLine)
        User::Leave(KErrNotFound);

    User::LeaveIfError(iLine.Open(iPhone, lineInfo.iName));
}

void CAnsPhoneEngine::SoundCleanup()
{
    if(iSound)
    {
        iSound->Stop();
        iSound->Close();
        delete iSound;
        iSound = NULL;
    }
    iState = ENoState;
}

void CAnsPhoneEngine::TelephonyCleanup()
{
    delete iCallWatcher;
    iCallWatcher = NULL;

    delete iCallMaker;
    iCallMaker = NULL;

    delete iCallLog;
    iCallLog = NULL;

    iPhone.Close();
    iLine.Close();
    iSession.Close();
}

void CAnsPhoneEngine::DeleteMessage(TInt aIndex)
{
    TFileName file;
    file.Append(KMessagesDir);
    file.Append(iMessageList->At(aIndex).iNumber);
    iFs.Delete(file);
    iMessageList->Delete(aIndex);
}

void CAnsPhoneEngine::GetNextMessageFileName(TDes& aFileName, const TDesC& aNumber)
// works out what the appropriate file name is for the next message to be recorded
{
    TBool foundNumber = EFalse;
    TInt highestIndex = 0;
    TInt numberMessages = iMessageList->Count();
    for(TInt a = 0; a < numberMessages; a++)
    {
        const TMessage& message = iMessageList->At(a);

        TInt index = 0;
        TBuf<KNumberMaxLength> buf;
        TrimIndex(message.iNumber, buf, index);

        if(aNumber.Compare(buf) == 0)
        {
            foundNumber = ETrue;

            if(index > highestIndex)
                highestIndex = index;
        }
    }

    if(!foundNumber)
    {
        aFileName = aNumber;
    }
    else
    {
        aFileName = aNumber;
        aFileName.Append(KSpace);
        aFileName.AppendNum(highestIndex + 1);
    }
}

void CAnsPhoneEngine::TrimIndex(const TDesC& aBuffer, TDes& aNumber, TInt& aIndex)
// takes aBuffer and works out the number from this and aIndex and populates these arguments
// e.g. "07779238045 5" gives aNumber = "07779239045" and aIndex = 5
{
    TInt locateSpace = aBuffer.LocateReverse(KSpace);
    if(locateSpace <= 0)
    {
        aNumber = aBuffer;
        aIndex = 0;
    }
    else
    {
        aNumber = aBuffer.Left(locateSpace);
        TBuf<KNumberMaxLength> buf = aBuffer.Right(aBuffer.Length() - locateSpace - 1);
        TLex lex(buf);
        lex.Val(aIndex);
    }
}

void CAnsPhoneEngine::DialNumberL(TInt aIndex)
// dials the number of the message's number at aIndex in iMessageList
{
    TelStartL();
    iCallMaker = CAnsPhoneCallMaker::NewL(*this, iLine);
    TBuf<KNumberMaxLength> file = iMessageList->At(aIndex).iNumber;
    TBuf<KNumberMaxLength> number;
    TInt index = 0;
    TrimIndex(file, number, index);

    iCallMaker->MakeCallL(number);
}

⌨️ 快捷键说明

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