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


/* Copyright (c) 2004, Nokia. All rights reserved */ 
 
 
// INCLUDE FILES 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
 
#include  
#include   
 
#include "BTObjectExchange.pan" 
#include "BTObjectExchangeAppUi.h" 
#include "BTObjectExchangeAppView.h" 
#include "BTObjectExchange.hrh" 
#include "ObjectExchangeClient.h" 
#include "ObjectExchangeServer.h" 
#include  
 
#include  
 
#ifdef __SERIES60_3X__ 
#include   
#endif 
 
#define KEnableSkinFlag 0x1000 
 
// ============================ MEMBER FUNCTIONS =============================== 
 
// ----------------------------------------------------------------------------- 
// CBTObjectExchangeAppUi::ConstructL()  
// ConstructL is called by the application framework. 
// ----------------------------------------------------------------------------- 
// 
void CBTObjectExchangeAppUi::ConstructL() 
    { 
#ifdef __SERIES60_3X__ 
    BaseConstructL(EAknEnableSkin); 
#else 
    BaseConstructL(KEnableSkinFlag); 
#endif 
 
    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(KTitle); 
     
    //make sure that BT is on 
    if(iBtAvailable) 
        { 
        //Make sure BT is on 
        TurnBtOnL(); 
        } 
     
    //Create the sendui object 
    #ifdef __SERIES60_3X__ 
    iSendUi = CSendUi::NewL(); 
    #else 
    iSendUi = CSendAppUi::NewL(ESendViaSendUi); 
    #endif 
             
    } 
 
// ----------------------------------------------------------------------------- 
// 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; 
    iDocHandler = NULL; 
     
    delete iSendUi; 
    iSendUi = NULL; 
    } 
 
// ----------------------------------------------------------------------------- 
// 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; 
	             
	            if( AskFileL(filename) )	             
	                iClient->SendObjectL(filename); 
	            else 
	               ShowMessageL( KCanceled ); 
	            } 
	            break; 
 
	        case EBTObjectExchangeDisconnect: 
	            iClient->DisconnectL(); 
	            break; 
 
	        case EBTObjectExchangeClearList: 
	            iAppView->ClearMessageListL(); 
	            break; 
 
 
            case ESendViaSendUi: 
            //These are the commands from Send cascade menu  
            //(actually needed only in 1st and 2nd edition) 
            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: 
                { 
                //With all the commands we send a file. 
                //Pass the command to the function. It's required 
                //by CSendAppUi sending function 
                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() ); 
            } 
         
        // Display the Send menu item (if there are possible bearers) 
 
        // Show only the bearers which support attachments 
        TSendingCapabilities capabilities(0,0, TSendingCapabilities::ESupportsAttachments ); 
         
         
        #ifdef __SERIES60_3X__ 
            //In 3rd edition the menu item is not created in the rss 
            TInt position = 0; 
            //so find a desired command 
            aMenuPane->ItemAndPos( EBTObjectExchangeClearList, position ); 
            //and add the Send menu item next to it 
            iSendUi->AddSendMenuItemL(*aMenuPane, position-1, ESendViaSendUi, capabilities ); 
             
            //this could be used if the menu item was introduced in the rss 
            //aMenuPane->ItemAndPos( ESendViaSendUi, position ); 
            //iSendUi->AddSendMenuItemL(*aMenuPane, position, ESendViaSendUi, capabilities ); 
        #else 
            //In 1st and 2nd edition we have the Send menu item with its cascade 
            //menu in the rss 
            TInt position = 0; 
            aMenuPane->ItemAndPos( ESendViaSendUi, position ); 
            iSendUi->DisplaySendMenuItemL (*aMenuPane, position, capabilities ); 
            aMenuPane->DeleteMenuItem(ESendViaSendUi);       
        #endif 
        } 
    #ifndef __SERIES60_3X__ //this is not needed in 3rd ed. 
     
    else if ( aResourceId == R_BTOBEX_SENDUI_MENU) // Show the SendUI cascade menu 
            { 
            iSendUi->DisplaySendCascadeMenuL(*aMenuPane, NULL); 
            } 
    #endif 
 
    } 
 
TBool CBTObjectExchangeAppUi::AskFileL(TFileName& aFileName) 
    { 
    // Select memory 
    CAknMemorySelectionDialog* memSelectionDialog =  
        CAknMemorySelectionDialog::NewL(ECFDDialogTypeNormal, EFalse); 
    CleanupStack::PushL(memSelectionDialog); 
    CAknMemorySelectionDialog::TMemory mem(CAknMemorySelectionDialog::EPhoneMemory); 
 
    TInt ret = memSelectionDialog->ExecuteL(mem); 
    CleanupStack::PopAndDestroy(memSelectionDialog); 
    if (!ret)  
        {         
        return EFalse; 
        } 
    //Select file from the chosen memory 
    CAknFileSelectionDialog* fileSelectionDialog = NULL;  
    if (mem == CAknMemorySelectionDialog::EMemoryCard) 
        {   
        fileSelectionDialog = CAknFileSelectionDialog::NewL(ECFDDialogTypeNormal,R_FILE_SELECTION_DIALOG_E ); 
        } 
    else 
        {   
        fileSelectionDialog= CAknFileSelectionDialog::NewL(ECFDDialogTypeNormal,R_FILE_SELECTION_DIALOG_C ); 
        }  
 
    TBool result = fileSelectionDialog->ExecuteL(aFileName); 
    delete fileSelectionDialog; 
    return result; 
    } 
     
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; 
     
    if (AskFileL(path) ) 
        { 
        TSendingCapabilities capabs( 0, 1024, TSendingCapabilities::ESupportsAttachments );  
 
        #ifdef __SERIES60_3X__    
        RFs fs; 
        CleanupClosePushL(fs); 
        User::LeaveIfError( fs.Connect() ); 
        fs.ShareProtected(); 
         
        TInt error( KErrNone ); 
         
        RFile temp; 
        User::LeaveIfError( temp.Open( fs, path, EFileShareReadersOnly | EFileRead ) ); 
        CleanupClosePushL(temp); 
                 
        CMessageData* messageData = CMessageData::NewL(); 
        CleanupStack::PushL(messageData); 
        messageData->AppendAttachmentHandleL(temp); 
         
        TRAPD(err, iSendUi->ShowQueryAndSendL(messageData, capabs) ); 
 
        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(messageData); 
         
        CleanupStack::PopAndDestroy(&temp); 
        CleanupStack::PopAndDestroy(&fs);         
         
        #else //1st and 2nd edition 
        const TInt KGranularity = 2; 
        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 
        #endif 
        } 
    else 
        { 
        //File Selection Canceled by user 
        iAppView->LogL(KCanceled); 
        } 
    } 
// End of File