www.pudn.com > S60_Platform_Bluetooth_OBEX_Example.rar > btobjectexchangeappui_1st.cpp


/* Copyright (c) 2004, Nokia. All rights reserved */ 
 
 
// INCLUDE FILES 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
//These are not published in 1st edition 
//#include  
//#include  
 
#include "BTObjectExchange.pan" 
#include "BTObjectExchangeAppUi.h" 
#include "BTObjectExchangeAppView.h" 
#include "BTObjectExchange.hrh" 
#include "ObjectExchangeClient.h" 
#include "ObjectExchangeServer.h" 
#include  
#include  
 
//The file to send. 
_LIT(KTestFile,"C:\\test.txt"); 
 
// ============================ MEMBER FUNCTIONS =============================== 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::ConstructL() 
// ConstructL is called by the application framework. 
// ----------------------------------------------------------------------------- 
// 
void CBTObjectExchangeAppUi::ConstructL() 
    { 
    BaseConstructL(); 
 
    iAppView = CBTObjectExchangeAppView::NewL( ClientRect() ); 
 
    iAppView->SetMopParent( this );   //  so view can update scroll bars 
 
    AddToStackL( iAppView ); 
 
    iClient = CObjectExchangeClient::NewL( *iAppView ); 
    iServer = CObjectExchangeServer::NewL( *iAppView ); 
 
    // check whether BT is available or not 
    RSdp sdpSession; 
 
    if ( sdpSession.Connect() == KErrNone ) 
        { 
        sdpSession.Close(); 
 
        iBtAvailable = ETrue; 
        } 
    else 
        { 
        iBtAvailable = EFalse; 
        } 
 
    iAppView->LogL(_L("BT OBEX Example")); 
 
    if(iBtAvailable) 
        { 
        //Make sure BT is on 
        TurnBtOnL(); 
        } 
    iSendUi = CSendAppUi::NewL(ESendViaSendUi); 
    } 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::CBTObjectExchangeAppUi() 
// C++ default constructor can NOT contain any code, that might leave. 
// ----------------------------------------------------------------------------- 
// 
CBTObjectExchangeAppUi::CBTObjectExchangeAppUi() 
    { 
    // no implementation required 
    } 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::~CBTObjectExchangeAppUi() 
// Destructor. 
// ----------------------------------------------------------------------------- 
// 
CBTObjectExchangeAppUi::~CBTObjectExchangeAppUi() 
    { 
    if ( iAppView ) 
        { 
        RemoveFromStack( iAppView ); 
        delete iAppView; 
        iAppView = NULL; 
        } 
    delete iClient; 
    iClient = NULL; 
    delete iServer; 
    iServer = NULL; 
 
    delete iDocHandler; 
    } 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::HandleCommandL() 
// Handle any menu commands. 
// ----------------------------------------------------------------------------- 
// 
void CBTObjectExchangeAppUi::HandleCommandL( TInt aCommand ) 
    { 
    if ( !iBtAvailable && ( 
        ( aCommand == EBTObjectExchangeStartServer ) || 
        ( aCommand == EBTObjectExchangeConnect ) ) ) 
        { 
        // Load a string from the resource file and display it 
        HBufC* textResource = StringLoader::LoadLC( R_BTOB_NO_BT ); 
        CAknErrorNote* errorNote; 
 
        errorNote = new ( ELeave ) CAknErrorNote; 
 
        // Show the information Note with 
        // textResource loaded with StringLoader. 
        errorNote->ExecuteLD( *textResource); 
 
        // Pop HBuf from CleanUpStack and Destroy it. 
        CleanupStack::PopAndDestroy( textResource ); 
        } 
    else 
        { 
        switch ( aCommand ) 
            { 
            case EAknSoftkeyExit: 
                iClient->StopL(); 
                iServer->DisconnectL(); 
                Exit(); 
                break; 
 
            case EBTObjectExchangeStartServer: 
                iServer->StartL(); 
                break; 
 
            case EBTObjectExchangeStopServer: 
                iServer->DisconnectL(); 
                break; 
 
            case EBTObjectExchangeConnect: 
 
                iClient->ConnectL(); 
                break; 
 
            case EBTObjectExchangeSendMessage: 
                { 
                TFileName filename = KTestFile(); 
 
                iClient->SendObjectL(filename); 
                } 
                break; 
 
            case EBTObjectExchangeDisconnect: 
                iClient->DisconnectL(); 
                break; 
 
            case EBTObjectExchangeClearList: 
                iAppView->ClearMessageListL(); 
                break; 
 
            case ESendViaSendUi: 
            case ESendViaSendUi1:  //FALLTHROUGH 
            case ESendViaSendUi2:  //FALLTHROUGH 
            case ESendViaSendUi3:  //FALLTHROUGH 
            case ESendViaSendUi4:  //FALLTHROUGH 
            case ESendViaSendUi5:  //FALLTHROUGH 
            case ESendViaSendUi6:  //FALLTHROUGH 
            case ESendViaSendUi7:  //FALLTHROUGH 
            case ESendViaSendUi8:  //FALLTHROUGH 
            case ESendViaSendUi9: 
                { 
                SendFileViaSendUiL(aCommand); 
                break; 
                } 
 
            default: 
                Panic( EBTObjectExchangeBasicUi ); 
                break; 
            } 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::DynInitMenuPaneL() 
// This function is called by the EIKON framework just before it displays 
// a menu pane. Its default implementation is empty, and by overriding it, 
// the application can set the state of menu items dynamically according 
// to the state of application data. 
// ----------------------------------------------------------------------------- 
// 
void CBTObjectExchangeAppUi::DynInitMenuPaneL( TInt aResourceId, 
                                               CEikMenuPane* aMenuPane ) 
    { 
    if ( aResourceId == R_BTOBJECTEXCHANGE_MENU ) 
        { 
        if ( !iClient->IsConnected() && !iServer->IsConnected() ) 
            { 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStartServer, EFalse ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStopServer, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeConnect, EFalse ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeSendMessage, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeDisconnect, ETrue ); 
            } 
        else if ( iServer->IsConnected() ) 
            { 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStartServer, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStopServer, EFalse ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeConnect, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeSendMessage, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeDisconnect, ETrue ); 
            } 
        else if ( iClient->IsConnected() ) 
            { 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStartServer, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeStopServer, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeConnect, ETrue ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeSendMessage, 
                iClient->IsBusy() ); 
            aMenuPane->SetItemDimmed( EBTObjectExchangeDisconnect, 
                iClient->IsBusy() ); 
            } 
        aMenuPane->SetItemDimmed( EBTObjectExchangeClearList, 
            !iAppView->ContainsEntries() ); 
 
        TSendingCapabilities capabilities(0,0, TSendingCapabilities::ESupportsAttachments ); 
 
        TInt position = 0; 
        aMenuPane->ItemAndPos( ESendViaSendUi, position ); 
        iSendUi->DisplaySendMenuItemL (*aMenuPane, position, capabilities ); 
        aMenuPane->DeleteMenuItem(ESendViaSendUi);       
        } 
    else if ( aResourceId == R_BTOBEX_SENDUI_MENU) // The SendUI cascade menu 
            { 
            iSendUi->DisplaySendCascadeMenuL(*aMenuPane, NULL); 
            }    } 
 
void CBTObjectExchangeAppUi::ShowMessageL( const TDesC& aMsg ) 
   { 
   CAknInformationNote* informationNote; 
   informationNote = new ( ELeave ) CAknInformationNote; 
   informationNote->ExecuteLD( aMsg); 
   } 
 
//after the joystick is pressed the current line (item) is 
//passed here. If the line matches a file name in current 
//working directory then launch it using document handler 
TBool CBTObjectExchangeAppUi::HandleJoystickPressL(TDesC& aLine) 
    { 
    TBool fileLaunched = EFalse; 
    //First figure out the CWD: 
    TParsePtrC parsePtr( KTempFile ); 
    TFileName filename = parsePtr.DriveAndPath(); 
 
    //If the CWD + aLine lenght is too long then 
    //aLine can't be a filename 
    if ( filename.Length() + aLine.Length() > KMaxFileName ) 
        return fileLaunched; //can't be a filename 
 
    //Otherwise check if it's a filename 
    filename.Append(aLine); 
    if( !BaflUtils::FileExists(CEikonEnv::Static()->FsSession(),  filename) ) 
      return fileLaunched; 
 
    //If the file exists in CWD then launch it using Document Handler API 
    if (!iDocHandler ) 
        { 
        #ifdef __SERIES60_3X__ 
        //In 3rd ed. the process is not given as a parameter 
        iDocHandler = CDocumentHandler::NewL(); 
        #else 
        //In older SDKs the process is given 
        iDocHandler = CDocumentHandler::NewL( CEikonEnv::Static()->Process() ); 
        #endif 
        } 
 
    //Finally try to launch the file. 
    TDataType empty = TDataType(); 
 
    iDocHandler->OpenFileEmbeddedL(filename,empty ); 
 
    fileLaunched = ETrue; 
    return fileLaunched; 
    } 
 
//Uses the Notifier API to ask the user to turn on Bluetooth 
//if it's not on already. 
void CBTObjectExchangeAppUi::TurnBtOnL() 
    { 
    //the constant is from btnotifierapi.h which is not in all SDKs 
    //so it's hard coded here 
    const TUid KPowerModeSettingNotifierUid = {0x100059E2}; 
    //const TUid KBTPowerStateNotifierUid = {0x101F808E}; //S80 and 7710 
 
    RNotifier notifier; 
    User::LeaveIfError( notifier.Connect() ); 
    TPckgBuf dummy(ETrue); 
    TPckgBuf reply(EFalse); 
    TRequestStatus stat; 
    notifier.StartNotifierAndGetResponse(stat, KPowerModeSettingNotifierUid, dummy, reply); 
    User::WaitForRequest(stat); 
    notifier.CancelNotifier(KPowerModeSettingNotifierUid); 
    notifier.Close(); 
    } 
 
void CBTObjectExchangeAppUi::HandleResourceChangeL(TInt aType) 
    { 
    CAknAppUi::HandleResourceChangeL(aType); //call to upper class 
 
    // ADDED FOR SCALABLE UI SUPPORT 
    // ***************************** 
    //if ( aType == KEikDynamicLayoutVariantSwitch ) 
    //hardcoded constant so it can be compiled with 1st edition 
    if ( aType == 0x101F8121 ) 
        { 
        iAppView->SetRect( ClientRect() ); 
        } 
    } 
 
void CBTObjectExchangeAppUi::SendFileViaSendUiL(TInt aCommand) 
    { 
    TFileName path(KTestFile); //Since the file query dialog not working in 1srt edition 
     
    TSendingCapabilities capabs( 0, 1024, TSendingCapabilities::ESupportsAttachments );  
    const TInt KGranularity = 2; 
    //1st and 2nd edition 
    CDesC16ArrayFlat* array = new ( ELeave ) CDesC16ArrayFlat( KGranularity ); 
    CleanupStack::PushL( array ); 
 
    array->AppendL( path ); 
 
    //This would show the bearer selection: 
    //iSendUi->CreateAndSendMessagePopupQueryL( _L("Send UI"), capabs, NULL, array ),  
    TRAPD(err,iSendUi->CreateAndSendMessageL(aCommand, NULL, array) ); 
    if( err ) //error occured during sending 
        { 
        iAppView->LogL(KSenduiError, err); 
        } 
    else 
        { 
        //This is shown even if the user has selected cancel during sending, 
        //for example, an MMS. 
        iAppView->LogL(KSenduiOk); 
        } 
 
    CleanupStack::PopAndDestroy(); // array        
    } 
 
// End of File