btobexprofiles/obexreceiveservices/bip/src/BIPController.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Obex Server image receiver module implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "BIPController.h"
       
    21 #include    "BIPCapabilityHandler.h"
       
    22 #include    "BIPImageHandler.h"
       
    23 
       
    24 
       
    25 #include    <e32base.h>
       
    26 #include    <obexutilsmessagehandler.h>
       
    27 #include    <sysutil.h>
       
    28 #include    <bautils.h>
       
    29 #include    <driveinfo.h>                   
       
    30 #include    <btengdomaincrkeys.h> 
       
    31 #include    <e32math.h> 
       
    32 #include    <es_sock.h>
       
    33 #include    <bt_sock.h>
       
    34 #include    <msvids.h>
       
    35 #include    "debug.h"
       
    36 #include    <bluetoothdevicedialogs.h>
       
    37 #include <hbtextresolversymbian.h>
       
    38         
       
    39 // CONSTANTS
       
    40 _LIT8(KBipCapabilityType, "x-bt/img-capabilities\0");
       
    41 const TInt KFileManagerUID3 = 0x101F84EB; /// File Manager application UID3
       
    42 const TInt    KBufferSize = 0x10000;  // 64 kB
       
    43 _LIT( KBTDevDialogId, "com.nokia.hb.btdevicedialog/1.0" );
       
    44 _LIT(KLocFileName, "btdialogs_");
       
    45 _LIT(KPath, "z:/resource/qt/translations/");  
       
    46 
       
    47 
       
    48 // ================= MEMBER FUNCTIONS =======================
       
    49 
       
    50 CBIPController* CBIPController::NewL()
       
    51     {
       
    52     CBIPController* self = new ( ELeave ) CBIPController();
       
    53 	CleanupStack::PushL( self );
       
    54 	self->ConstructL();
       
    55 	CleanupStack::Pop(self); 
       
    56 	return self;
       
    57     }
       
    58     
       
    59 // ---------------------------------------------------------
       
    60 // CBIPController()
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 CBIPController::CBIPController()
       
    64     {
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // ConstructL()
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 void CBIPController::ConstructL()
       
    72     {
       
    73     TRACE_FUNC_ENTRY
       
    74     iBIPImageHandler = CBIPImageHandler::NewL();
       
    75     iLowMemoryActiveCDrive = CObexUtilsPropertyNotifier::NewL(this, ECheckPhoneMemory);
       
    76     iLowMemoryActiveMMC = CObexUtilsPropertyNotifier::NewL(this, ECheckMMCMemory);
       
    77     iDevMan = CBTEngDevMan::NewL(this);
       
    78     iResultArray = new(ELeave) CBTDeviceArray(1);
       
    79     // Get default folder from CenRep 
       
    80     TObexUtilsMessageHandler::GetCenRepKeyStringValueL(KCRUidBluetoothEngine, KLCReceiveFolder, iCenRepFolder);
       
    81     iDialog = CObexUtilsDialog::NewL(this);
       
    82     TBool ok = HbTextResolverSymbian::Init(KLocFileName, KPath);
       
    83     if (!ok) 
       
    84         {
       
    85         User::Leave( KErrNotFound );
       
    86         }
       
    87     TRACE_FUNC_EXIT
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // ~CBIPController()
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 CBIPController::~CBIPController()
       
    95     {   
       
    96     TRACE_FUNC_ENTRY    
       
    97     delete iGetObject;    
       
    98     delete iBIPCapabilityHandler;    
       
    99     delete iBIPImageHandler;    
       
   100     delete iLowMemoryActiveCDrive;    
       
   101     delete iLowMemoryActiveMMC;    
       
   102     delete iBuf;
       
   103     delete iBTObject;
       
   104     delete iDevMan;
       
   105     if (iResultArray)
       
   106         {
       
   107         iResultArray->ResetAndDestroy();
       
   108         delete iResultArray;
       
   109         }
       
   110     iFs.Close();
       
   111     delete iDialog;
       
   112     delete iProgressDialog;
       
   113     delete iRecvDoneDialog;
       
   114     TRACE_FUNC_EXIT    
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------
       
   118 // ErrorIndication()
       
   119 // ---------------------------------------------------------
       
   120 //
       
   121 void CBIPController::ErrorIndication(TInt TRACE_ONLY(aError))
       
   122     {
       
   123     TRACE_FUNC_ENTRY
       
   124     TRACE_ERROR((_L("[obexreceiveservicebip] CBIPController: ErrorIndication error:\t %d"), aError));
       
   125     HandleError(EFalse); // false because this is not an explicit abort
       
   126     TRACE_FUNC_EXIT
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // AbortIndication()
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 void CBIPController::AbortIndication()
       
   134     {
       
   135     TRACE_FUNC_ENTRY
       
   136     HandleError(ETrue); // true because this is an explicit abort
       
   137     TRACE_FUNC_EXIT
       
   138     }
       
   139 
       
   140 void CBIPController::HandleError(TBool aAbort)
       
   141     {
       
   142     TRACE_FUNC_ENTRY
       
   143     iReceivingFailed = ETrue;
       
   144     iShowRecvCompleteDialog = EFalse;
       
   145     if( iBTTransferState == ETransferPut || (!aAbort && iBTTransferState == ETransferPutDiskError) )
       
   146         {
       
   147         if(iBTObject)
       
   148             {
       
   149             iBTObject->Reset();
       
   150             }
       
   151         CancelTransfer();
       
   152 
       
   153         TRAP_IGNORE( 
       
   154                 HBufC* note = HbTextResolverSymbian::LoadLC(_L("txt_bt_dpophead_receiving_failed"));
       
   155                 iDialog->ShowErrorNoteL(note->Des());
       
   156                 CleanupStack::PopAndDestroy(note); 
       
   157                 );
       
   158         } 
       
   159     delete iBuf;
       
   160     iBuf = NULL;
       
   161     
       
   162     iBTTransferState = ETransferIdle;
       
   163     TRAP_IGNORE(TObexUtilsMessageHandler::RemoveInboxEntriesL(iBTObject,iMsvIdParent));
       
   164     TRAP_IGNORE(TObexUtilsMessageHandler::RemoveTemporaryRFileL(iFullPathFilename));
       
   165     
       
   166     TRACE_FUNC_EXIT
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------
       
   170 // CancelTransfer()
       
   171 // ---------------------------------------------------------
       
   172 //
       
   173 void CBIPController::CancelTransfer()
       
   174     {
       
   175     TRACE_FUNC_ENTRY
       
   176     CloseReceivingIndicator();
       
   177     if(iBTTransferState == ETransferPut)
       
   178         {
       
   179         iBTTransferState = ETransferPutCancel;
       
   180         }
       
   181     else // go to idle for all other states
       
   182         {
       
   183         iBTTransferState = ETransferIdle;
       
   184         }
       
   185     }
       
   186 // ---------------------------------------------------------
       
   187 // TransportUpIndication()
       
   188 // ---------------------------------------------------------
       
   189 //
       
   190 void CBIPController::TransportUpIndication()
       
   191     {
       
   192     TRACE_FUNC
       
   193     iReceivingFailed = EFalse;
       
   194     if (!iFs.Handle())
       
   195         {
       
   196         TRACE_INFO( (_L( "[bipreceiveservice] TransportUpIndication iFs.Connect()" )) ); 
       
   197         if (iFs.Connect())   // error value not checked, iFs.Handle() checked one more time before first useage
       
   198             {
       
   199             TRACE_INFO( (_L( "[bipreceiveservice] TransportUpIndication iFs.Connect() failed" )) ); 
       
   200             }
       
   201         }
       
   202     
       
   203     iFile = RFile();
       
   204     iFullPathFilename.Zero();
       
   205     iCapabilityFileName.Zero();
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // ObexConnectIndication()
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 TInt CBIPController::ObexConnectIndication( const TObexConnectInfo& aRemoteInfo, const TDesC8& aInfo )
       
   213     {
       
   214     TRACE_FUNC
       
   215     
       
   216     (void) aRemoteInfo;
       
   217     (void) aInfo;
       
   218     
       
   219     // Get remote device socket address and bluetooth name
       
   220     // Remote bluetooth name will be displayed in the new message in inbox.
       
   221     //
       
   222     TSockAddr addr;
       
   223     iBTObexServer->RemoteAddr(addr);
       
   224     TBTDevAddr tBTDevAddr = static_cast<TBTSockAddr>(addr).BTAddr();
       
   225     
       
   226     TBTRegistrySearch nameSearch;
       
   227     nameSearch.FindAddress(tBTDevAddr);
       
   228     
       
   229     iResultArray->Reset();
       
   230     // Ignore any errors here, if we don't get the name, we don't get the name.
       
   231     // It is also possible that the name is received too late....
       
   232     static_cast<void>(iDevMan->GetDevices(nameSearch, iResultArray));
       
   233     
       
   234     return KErrNone;
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------
       
   238 // ObexDisconnectIndication()
       
   239 // ---------------------------------------------------------
       
   240 //
       
   241 void CBIPController::ObexDisconnectIndication(const TDesC8& aInfo)
       
   242     {
       
   243     TRACE_FUNC
       
   244     (void) aInfo;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------
       
   248 // TransportDownIndication()
       
   249 // ---------------------------------------------------------
       
   250 //
       
   251 void CBIPController::TransportDownIndication()
       
   252     {
       
   253     TRACE_FUNC  
       
   254     if(!iReceivingFailed && iShowRecvCompleteDialog)
       
   255         {
       
   256         //Launch recevice completed dialog.
       
   257         iRecvDoneDialog = CHbDeviceDialogSymbian::NewL();
       
   258         iRecvDoneDialog->SetObserver(this);
       
   259     
       
   260         CHbSymbianVariantMap* variantMap = CHbSymbianVariantMap::NewL();
       
   261         CleanupStack::PushL(variantMap);
       
   262         
       
   263         TInt dialogIdx = TBluetoothDialogParams::EReceiveDone;
       
   264         CHbSymbianVariant* dialogType = CHbSymbianVariant::NewL( (TAny*) &(dialogIdx), 
       
   265                                                             CHbSymbianVariant::EInt );
       
   266         CleanupStack::PushL(dialogType);
       
   267         TBuf16<6> dialogTypeKey;
       
   268         dialogTypeKey.Num(TBluetoothDialogParams::EDialogType);
       
   269         User::LeaveIfError(variantMap->Add(dialogTypeKey, dialogType));
       
   270         CleanupStack::Pop(dialogType);
       
   271         
       
   272         CHbSymbianVariant* deviceName = CHbSymbianVariant::NewL( (TAny*) (&iRemoteDeviceName), 
       
   273                                                             CHbSymbianVariant::EDes );
       
   274         CleanupStack::PushL(deviceName);
       
   275         TBuf16<6> deviceNameKey;
       
   276         deviceNameKey.Num(TBluetoothDeviceDialog::EDeviceName);
       
   277         User::LeaveIfError(variantMap->Add(deviceNameKey, deviceName));
       
   278         CleanupStack::Pop(deviceName);
       
   279         
       
   280         CHbSymbianVariant* fileName = CHbSymbianVariant::NewL( (TAny*) (&iReceivingFileName), 
       
   281                                                             CHbSymbianVariant::EDes );
       
   282         CleanupStack::PushL(fileName);
       
   283         TBuf16<6> fileNameKey;
       
   284         fileNameKey.Num(TBluetoothDeviceDialog::EReceivingFileName);
       
   285         User::LeaveIfError(variantMap->Add(fileNameKey, fileName));
       
   286         CleanupStack::Pop(fileName);
       
   287         
       
   288         CHbSymbianVariant* fileSz = CHbSymbianVariant::NewL( (TAny*) &iTotalSizeByte, 
       
   289                                                             CHbSymbianVariant::EInt );
       
   290         CleanupStack::PushL(fileSz);
       
   291         TBuf16<6> fileSzKey;
       
   292         fileSzKey.Num(TBluetoothDeviceDialog::EReceivingFileSize);
       
   293         User::LeaveIfError(variantMap->Add(fileSzKey, fileSz));
       
   294         CleanupStack::Pop(fileSz);
       
   295         
       
   296         CHbSymbianVariant* fileCnt = CHbSymbianVariant::NewL( (TAny*) &iFileCount, 
       
   297                                                             CHbSymbianVariant::EInt );
       
   298         CleanupStack::PushL(fileCnt);
       
   299         TBuf16<6> fileCntKey;
       
   300         fileCntKey.Num(TBluetoothDeviceDialog::EReceivedFileCount);
       
   301         User::LeaveIfError(variantMap->Add(fileCntKey, fileCnt));
       
   302         CleanupStack::Pop(fileCnt);
       
   303         
       
   304         iRecvDoneDialog->Show( KBTDevDialogId(), *variantMap, this );
       
   305         CleanupStack::PopAndDestroy(variantMap);
       
   306         
       
   307         iShowRecvCompleteDialog = EFalse;
       
   308         }
       
   309     
       
   310     // Remove receiving buffer and files used during file receiving.
       
   311     //
       
   312     delete iBTObject;
       
   313     iBTObject = NULL;
       
   314     TRAP_IGNORE(TObexUtilsMessageHandler::RemoveTemporaryRFileL (iFullPathFilename));  
       
   315     iFs.Close();
       
   316     iFileCount = 0;
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------
       
   320 // PutRequestIndication()
       
   321 // ---------------------------------------------------------
       
   322 //
       
   323 CObexBufObject* CBIPController::PutRequestIndication()
       
   324     {   
       
   325     TRACE_FUNC_ENTRY
       
   326     iLengthHeaderReceived = EFalse; // New put request so clear header based state
       
   327     iBTTransferState = ETransferPut;
       
   328     
       
   329     // Checking if backup is running now - if backup process is active, then we
       
   330     // need to cancel transfer - otherwise phone will freeze during receiving
       
   331     // data
       
   332     if ( IsBackupRunning() )
       
   333         {
       
   334         TRACE_INFO ( _L ("Backup in progress! Canceling incoming transfer."));
       
   335         iBTTransferState = ETransferPutInitError;
       
   336         return NULL;
       
   337         }
       
   338         
       
   339     TRAPD(err, HandlePutImageRequestL());
       
   340     if (err == KErrNone)
       
   341         {
       
   342         return iBTObject;
       
   343         }
       
   344     if (iBTTransferState != ETransferPutInitError)
       
   345         {
       
   346         iBTTransferState = ETransferPutDiskError;
       
   347         }
       
   348     TRACE_FUNC_EXIT
       
   349     return NULL;
       
   350     }
       
   351 
       
   352 // ---------------------------------------------------------
       
   353 // PutPacketIndication() 
       
   354 // ---------------------------------------------------------
       
   355 //
       
   356 TInt CBIPController::PutPacketIndication()
       
   357     {
       
   358     TRACE_FUNC_ENTRY
       
   359     if(iBTTransferState == ETransferPutCancel)
       
   360         {
       
   361         // User cancelled the put request, so error the next packet to terminate the put request.
       
   362         // BIP considers the Unauthorized error response suitable for this...
       
   363         HandleError(ETrue); // reset state and clear up
       
   364         return KErrIrObexRespUnauthorized;
       
   365         }
       
   366     
       
   367     if (iBTObject)
       
   368         {
       
   369         iTotalSizeByte = iBTObject->Length();     // get size of receiving file
       
   370         iReceivingFileName = iBTObject->Name();   // get name of receiving file
       
   371         
       
   372         // Check that capacity is suitable as soon as possible
       
   373         if(!iLengthHeaderReceived && iTotalSizeByte > 0)
       
   374             {
       
   375             iLengthHeaderReceived = ETrue; // total size value is from length header
       
   376             TBool capacity = ETrue;
       
   377             TRAPD(retTrap, capacity = CheckCapacityL());
       
   378             if(retTrap != KErrNone)
       
   379                 {
       
   380                 return KErrGeneral;
       
   381                 }
       
   382             if(!capacity)
       
   383                 {
       
   384                 //TRAP_IGNORE(TObexUtilsUiLayer::ShowGlobalConfirmationQueryL(R_OUT_OF_MEMORY));
       
   385                 //todo: Need to use Localized string.
       
   386                 _LIT(KText, "Not enough memory to execute operation. Delete some documents and try again.");
       
   387                 TRAP_IGNORE(iDialog->ShowErrorNoteL(KText));
       
   388     
       
   389                 return KErrDiskFull;
       
   390                 }
       
   391             }
       
   392         if(iBTObject->Name().Length() > KMaxFileName)
       
   393             {
       
   394             return KErrAccessDenied;
       
   395             }
       
   396         if(iBTTransferState == ETransferPutDiskError)
       
   397             {
       
   398             return KErrDiskFull;
       
   399             }
       
   400         // successfully received put packet if we reached here
       
   401         iBTTransferState = ETransferPut;
       
   402         
       
   403         // Now we need to either create (in the first instance) or update the dialog on the UI.
       
   404         if(ReceivingIndicatorActive())
       
   405             {
       
   406             TRAPD(err, UpdateReceivingIndicatorL());
       
   407             if(err < KErrNone)
       
   408                 {
       
   409                 return err;
       
   410                 }
       
   411             }
       
   412         else if(!iNoteDisplayed)
       
   413             {
       
   414             // No note launched yet, so try to launch
       
   415             TRAPD(err, LaunchReceivingIndicatorL());
       
   416             iNoteDisplayed = (err == KErrNone);
       
   417             }
       
   418         }
       
   419     
       
   420     TRACE_FUNC_EXIT
       
   421     return KErrNone;
       
   422     }
       
   423 
       
   424 // ---------------------------------------------------------
       
   425 // PutCompleteIndication() 
       
   426 // ---------------------------------------------------------
       
   427 //
       
   428 TInt CBIPController::PutCompleteIndication()  // Once receive has completed.
       
   429     {
       
   430     TRACE_FUNC_ENTRY
       
   431     TInt retVal = KErrNone;
       
   432     if(iBTTransferState == ETransferPutCancel)
       
   433         {
       
   434         retVal = KErrIrObexRespUnauthorized;
       
   435         HandleError(ETrue);
       
   436         }
       
   437     else
       
   438         {
       
   439         retVal = HandlePutCompleteIndication();
       
   440         iBTTransferState = ETransferIdle;
       
   441         CloseReceivingIndicator();
       
   442         iFileCount++;
       
   443         iShowRecvCompleteDialog = ETrue;
       
   444         }
       
   445     TRACE_FUNC_EXIT
       
   446     return retVal;
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------
       
   450 // GetRequestIndication()
       
   451 // ---------------------------------------------------------
       
   452 //
       
   453 CObexBufObject* CBIPController::GetRequestIndication( CObexBaseObject* aRequiredObject )
       
   454     {
       
   455     TRACE_FUNC_ENTRY
       
   456     iBTTransferState = ETransferGet;
       
   457     TInt err = RemoveCapabilityObject();
       
   458     if (err == KErrNone)
       
   459         {
       
   460         if( aRequiredObject->Type() == KBipCapabilityType )
       
   461             {
       
   462             TRAP( err, HandleGetCapabilityRequestL( ) );
       
   463             if( err == KErrNone )
       
   464                 {
       
   465                 return iGetObject;
       
   466                 }
       
   467             }
       
   468         }
       
   469     TRACE_FUNC_EXIT
       
   470     return NULL;
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------
       
   474 // GetPacketIndication()
       
   475 // ---------------------------------------------------------
       
   476 //
       
   477 TInt CBIPController::GetPacketIndication()
       
   478     {
       
   479     TRACE_FUNC_ENTRY
       
   480     return KErrNone;
       
   481     }
       
   482 
       
   483 // ---------------------------------------------------------
       
   484 // GetCompleteIndication()
       
   485 // ---------------------------------------------------------
       
   486 //
       
   487 TInt CBIPController::GetCompleteIndication()
       
   488     {
       
   489     TRACE_FUNC_ENTRY
       
   490     delete iGetObject;
       
   491     iGetObject=NULL;
       
   492     TInt err = RemoveCapabilityObject();
       
   493     if (err != KErrNone)
       
   494         {
       
   495         err = KErrGeneral;
       
   496         }
       
   497     iBTTransferState = ETransferIdle;
       
   498     TRACE_FUNC_EXIT
       
   499     return err;
       
   500     }
       
   501 
       
   502 // ---------------------------------------------------------
       
   503 // SetPathIndication()
       
   504 // ---------------------------------------------------------
       
   505 //
       
   506 TInt CBIPController::SetPathIndication( const CObex::TSetPathInfo& aPathInfo, 
       
   507                                         const TDesC8& aInfo)
       
   508     {
       
   509     TRACE_FUNC
       
   510     // SetPath is not implemented in BIP - so following IrOBEX guidance, return
       
   511     // the Forbidden response code.
       
   512     (void) aPathInfo;
       
   513     (void) aInfo;
       
   514     
       
   515     return KErrIrObexRespForbidden;
       
   516     }
       
   517 
       
   518 /**
       
   519 * This function is implementation for mixin-class for Obexutils.
       
   520 * It is called every time another instance modifies disk status,
       
   521 * for example when it cross warning/critical level.
       
   522 * Parameters:
       
   523 * @param aCheckType             Disktype changes.
       
   524 */
       
   525 void CBIPController::HandleNotifyL(TMemoryPropertyCheckType aCheckType)
       
   526     {    
       
   527     TRACE_FUNC_ENTRY    
       
   528     // Only interested on this notification if we are receiving something
       
   529     if ( iBTTransferState == ETransferPut )
       
   530         {
       
   531        // Check the keys, what has been changed.
       
   532        if ( aCheckType == ECheckPhoneMemory )
       
   533            {
       
   534            if ( SysUtil::FFSSpaceBelowCriticalLevelL( NULL, 0 ) )
       
   535                {                
       
   536                TRACE_INFO( _L( "[obexreceiveservicebip] CBIPController: Obex Server error diskfull:\t" ) );
       
   537                iBTTransferState = ETransferPutDiskError;
       
   538                }
       
   539            }
       
   540        else if ( aCheckType == ECheckMMCMemory )
       
   541            {                
       
   542            if ( SysUtil::MMCSpaceBelowCriticalLevelL( NULL, 0 ) )
       
   543                {                    
       
   544                TRACE_INFO( _L( "[obexreceiveservicebip] CBIPController: Obex Server error diskfull:\t" ) );
       
   545                iBTTransferState = ETransferPutDiskError;
       
   546                }
       
   547            }            
       
   548         }        
       
   549     TRACE_FUNC_EXIT    
       
   550     }
       
   551     
       
   552 // ---------------------------------------------------------
       
   553 // HandleGetCapabilityRequestL()
       
   554 // ---------------------------------------------------------
       
   555 //
       
   556 void CBIPController::HandleGetCapabilityRequestL()
       
   557     {
       
   558     TRACE_FUNC_ENTRY    
       
   559     CBIPCapabilityHandler* capHandler = CBIPCapabilityHandler::NewL();
       
   560     CleanupStack::PushL(capHandler);
       
   561     capHandler->CreateCapabilityObjectL(iCapabilityFileName);
       
   562     delete iGetObject;
       
   563     iGetObject = NULL; 
       
   564     iGetObject = CObexBufObject::NewL(NULL);
       
   565     iGetObject->SetDataBufL(iCapabilityFileName);
       
   566     CleanupStack::PopAndDestroy(capHandler);
       
   567     TRACE_FUNC_EXIT
       
   568     }
       
   569 
       
   570 // ---------------------------------------------------------
       
   571 // HandlePutImageRequest()
       
   572 // ---------------------------------------------------------
       
   573 //
       
   574 void CBIPController::HandlePutImageRequestL()
       
   575     {
       
   576     TRACE_FUNC_ENTRY
       
   577     
       
   578     delete iBTObject;
       
   579     iBTObject = NULL;
       
   580 
       
   581     if (!iFs.Handle())
       
   582         {
       
   583         User::Leave(KErrGeneral);
       
   584         }
       
   585     
       
   586     // Assign an initial value to iDrive
       
   587     iDrive = GetDriveWithMaximumFreeSpaceL();    
       
   588     
       
   589     // If iDrive is at critical space level, we immediately show out_of_memory.
       
   590     //
       
   591     if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, iDrive))
       
   592         {
       
   593         //TRAP_IGNORE(TObexUtilsUiLayer::ShowGlobalConfirmationQueryL(R_OUT_OF_MEMORY));
       
   594         //todo: Need to use Localized string.
       
   595         _LIT(KText, "Not enough memory to execute operation. Delete some documents and try again.");
       
   596         TRAP_IGNORE(iDialog->ShowErrorNoteL(KText));
       
   597     
       
   598         User::Leave(KErrGeneral);
       
   599         }
       
   600     
       
   601     iBTObject = CObexBufObject::NewL(NULL);    
       
   602     
       
   603     delete iBuf;
       
   604     iBuf = NULL;
       
   605     
       
   606     TChar driveLetter;
       
   607     iDefaultFolder.Zero();
       
   608     iFs.DriveToChar(iDrive, driveLetter);
       
   609     iDefaultFolder.Append(driveLetter);
       
   610     if ( iDrive == EDriveC )
       
   611         {
       
   612         iDefaultFolder.Append(_L(":\\data\\"));
       
   613         }
       
   614     else
       
   615         {
       
   616         iDefaultFolder.Append(_L(":\\"));
       
   617         }
       
   618     
       
   619     iDefaultFolder.Append(iCenRepFolder);
       
   620     
       
   621     iFile = RFile();
       
   622     iFullPathFilename.Zero();
       
   623     TObexUtilsMessageHandler::CreateReceiveBufferAndRFileL(iFile, iDefaultFolder, iFullPathFilename, iBuf, KBufferSize);
       
   624     User::LeaveIfError(iFile.Open(iFs, iFullPathFilename, EFileWrite));
       
   625     TObexRFileBackedBuffer bufferdetails(*iBuf, iFile, CObexBufObject::EDoubleBuffering);
       
   626     TRAPD(err, iBTObject->SetDataBufL(bufferdetails));
       
   627     if (err != KErrNone)
       
   628         {
       
   629         iBTTransferState = ETransferPutInitError;
       
   630         User::Leave(KErrGeneral); 
       
   631         }
       
   632         
       
   633     TRACE_FUNC_EXIT
       
   634     }
       
   635 
       
   636 
       
   637 // ---------------------------------------------------------
       
   638 // HandlePutCompleteIndication()
       
   639 // ---------------------------------------------------------
       
   640 //
       
   641 TInt CBIPController::HandlePutCompleteIndication()
       
   642 	{
       
   643     TRACE_FUNC_ENTRY
       
   644     TInt retVal = KErrNone;
       
   645 
       
   646     iDefaultFolder.Zero();
       
   647     TChar driveLetter;
       
   648     if ( iDrive == EDriveC )
       
   649         {
       
   650         iFs.DriveToChar(iDrive, driveLetter);
       
   651         iDefaultFolder.Append(driveLetter);
       
   652         iDefaultFolder.Append(_L(":\\data\\"));
       
   653         iDefaultFolder.Append(iCenRepFolder);
       
   654         }
       
   655     else
       
   656         {
       
   657         iFs.DriveToChar(iDrive, driveLetter);
       
   658         iDefaultFolder.Append(driveLetter);
       
   659         iDefaultFolder.Append(_L(":\\"));
       
   660         iDefaultFolder.Append(iCenRepFolder);
       
   661         }
       
   662     
       
   663     iFullPathFilename.Zero();
       
   664     iFullPathFilename.Append(iDefaultFolder);
       
   665     TRAP ( retVal, TObexUtilsMessageHandler::SaveFileToFileSystemL(iBTObject,
       
   666                                                                    KUidMsgTypeBt,
       
   667                                                                    iMsvIdParent,
       
   668                                                                    iFullPathFilename,
       
   669                                                                    iFile,
       
   670                                                                    iRemoteDeviceName));
       
   671     if ( retVal == KErrNone)
       
   672         {
       
   673         TRAP (retVal, TObexUtilsMessageHandler::AddEntryToInboxL(iMsvIdParent, iFullPathFilename));
       
   674         }
       
   675     
       
   676     
       
   677     if( retVal != KErrNone )
       
   678         {
       
   679         TRACE_ERROR((_L( "[obexreceiveservicebip] CBIPController: HandlePutCompleteIndication error:\t %d" ), retVal ) );              
       
   680         TRAP( retVal, TObexUtilsMessageHandler::RemoveInboxEntriesL(iBTObject, iMsvIdParent));
       
   681         retVal = KErrDiskFull;
       
   682         }
       
   683     // Even if the object saving fails we must return image handle with error code
       
   684     TRAP_IGNORE( iBIPImageHandler->AddImageHandleHeaderL( iBTObexServer ) );            
       
   685     TRACE_INFO( _L( "[obexreceiveservicebip] HandlePutCompleteIndication Done\t" ) );
       
   686     delete iBTObject;
       
   687     iBTObject = NULL;
       
   688     delete iBuf;
       
   689     iBuf = NULL;
       
   690     iPreviousDefaultFolder = iDefaultFolder;  // save the last file path where file is successfully saved to file system.
       
   691     iMsvIdParent = KMsvNullIndexEntryId;
       
   692     TRACE_FUNC_EXIT     
       
   693     return retVal;
       
   694 	}
       
   695 // ---------------------------------------------------------
       
   696 // RemoveCapabilityObject()
       
   697 // ---------------------------------------------------------
       
   698 //		
       
   699 TInt CBIPController::RemoveCapabilityObject()
       
   700     {
       
   701     TRACE_FUNC_ENTRY
       
   702     if ( (iCapabilityFileName.Compare(KNullDesC)) == 0 )
       
   703         {
       
   704         return KErrNone; 
       
   705         }     
       
   706     if ( !iFs.Handle() )
       
   707         {
       
   708         TRACE_INFO( (_L( "[bipreceiveservice] RemoveCapabilityObject return" )) ); 
       
   709         return KErrGeneral;
       
   710         }               
       
   711     iFs.Delete(iCapabilityFileName);        
       
   712     iCapabilityFileName=KNullDesC; 
       
   713     TRACE_FUNC_EXIT 
       
   714     return KErrNone;    
       
   715     }
       
   716 	 
       
   717 // ---------------------------------------------------------
       
   718 // CheckCapacity()
       
   719 // ---------------------------------------------------------
       
   720 //	    
       
   721 TBool CBIPController::CheckCapacityL()
       
   722     {
       
   723     TRACE_FUNC_ENTRY   
       
   724     
       
   725     iDrive = EDriveZ; // Intialize iDrive to Z
       
   726     TInt filesize = iBTObject->Length();
       
   727     
       
   728     TInt mmcDrive = KDefaultDrive;   // External memory card  
       
   729     TInt imsDrive = KDefaultDrive;   // Internal mass storage
       
   730     
       
   731     User::LeaveIfError(DriveInfo::GetDefaultDrive(DriveInfo::EDefaultMassStorage, imsDrive));
       
   732     User::LeaveIfError(DriveInfo::GetDefaultDrive(DriveInfo::EDefaultRemovableMassStorage, mmcDrive));      
       
   733      
       
   734     TRACE_INFO( (_L( "[oppreceiveservice] CheckCapacityL imsDrive=%d; mmcDrive=%d\t" ),imsDrive, mmcDrive ) );
       
   735 
       
   736     TVolumeInfo volumeInfo;
       
   737     TInt err = iFs.Volume(volumeInfo, imsDrive);
       
   738     
       
   739     // If err != KErrNone, Drive is not available.
       
   740     //
       
   741     if ( !err )
       
   742         {
       
   743         // Check capacity on Internal mass storage            
       
   744         TRACE_INFO( (_L( "[obexreceiveservicebip] CheckCapacityL Internal mass storage \t" )) );
       
   745         if ( !SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, filesize, imsDrive ) )
       
   746             {
       
   747             iDrive = imsDrive;            
       
   748             }
       
   749         }
       
   750     if ( iDrive == EDriveZ )
       
   751         {
       
   752         err = iFs.Volume(volumeInfo, mmcDrive);
       
   753         if ( !err )
       
   754             {
       
   755             // Check capacity on memory card    
       
   756             TRACE_INFO( (_L( "[obexreceiveservicebip] CheckCapacityL Checking memory card\t" )) );
       
   757             if ( !SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, filesize, mmcDrive ) )
       
   758                 {                    
       
   759                 iDrive = mmcDrive;
       
   760                 }   
       
   761             }
       
   762         }           
       
   763     if ( iDrive == EDriveZ )
       
   764         {
       
   765         TRACE_INFO( (_L( "[obexreceiveservicebip] CheckCapacityL Checking phone memory\t" )) );
       
   766         // Phone memory
       
   767         if( !SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, filesize, EDriveC ))
       
   768             {
       
   769             iDrive = EDriveC;
       
   770             }
       
   771         }
       
   772   
       
   773     TRACE_INFO( (_L( "[obexreceiveservicebip] CheckCapacityL iDrive = %d\t" ),iDrive ) );                   
       
   774     TRACE_FUNC_EXIT
       
   775     
       
   776     if (iDrive == EDriveZ)
       
   777         {
       
   778         // If there is no free space for receiving file, we need to set iPreviousDefaultFolder back to iDefaultFolder.
       
   779         // In order to show the file receveing dialog correctly.
       
   780         iDefaultFolder = iPreviousDefaultFolder;
       
   781         return EFalse;
       
   782         }
       
   783     return ETrue;
       
   784     }    
       
   785 
       
   786 
       
   787 // ---------------------------------------------------------
       
   788 // IsOBEXActive()
       
   789 // ---------------------------------------------------------
       
   790 //
       
   791 TBool CBIPController::IsOBEXActive()
       
   792     {    
       
   793     TRACE_FUNC    
       
   794     return ETrue;
       
   795     }
       
   796 
       
   797 // ---------------------------------------------------------
       
   798 // SetMediaType()
       
   799 // ---------------------------------------------------------
       
   800 //
       
   801 void CBIPController::SetMediaType( TSrcsMediaType __DEBUG_ONLY(aMediaType) ) 
       
   802     {
       
   803     TRACE_FUNC
       
   804     // BIP is only for Bluetooth, so that is all we expect
       
   805     __ASSERT_DEBUG(aMediaType == ESrcsMediaBT, User::Panic(KBipPanicCategory, EBipPanicNotBluetoothMediaType));
       
   806     }
       
   807 
       
   808 // ---------------------------------------------------------
       
   809 // SetObexServer()
       
   810 // ---------------------------------------------------------
       
   811 //
       
   812 TInt CBIPController::SetObexServer( CObexServer* aServer)
       
   813     {
       
   814     TInt retVal=KErrNone;
       
   815     if (aServer)
       
   816         {
       
   817         iBTObexServer=aServer;
       
   818         retVal=aServer->Start(this);
       
   819         }
       
   820     return retVal;
       
   821     }    
       
   822 
       
   823 void CBIPController::LaunchReceivingIndicatorL()
       
   824     {
       
   825     if(ReceivingIndicatorActive())
       
   826         {
       
   827         return;
       
   828         }
       
   829     
       
   830     if(iTotalSizeByte > 0)
       
   831         {
       
   832         if(iReceivingFileName.Length() > 0)
       
   833             {
       
   834             iProgressDialog = CHbDeviceDialogSymbian::NewL();
       
   835             iProgressDialog->SetObserver(this);
       
   836     
       
   837             CHbSymbianVariantMap* variantMap = CHbSymbianVariantMap::NewL();
       
   838             CleanupStack::PushL(variantMap);
       
   839             
       
   840             TInt dialogIdx = TBluetoothDialogParams::EReceiveProgress;
       
   841             CHbSymbianVariant* dialogType = CHbSymbianVariant::NewL( (TAny*) &(dialogIdx), 
       
   842                                                                 CHbSymbianVariant::EInt );
       
   843             CleanupStack::PushL(dialogType);
       
   844             TBuf16<6> dialogTypeKey;
       
   845             dialogTypeKey.Num(TBluetoothDialogParams::EDialogType);
       
   846             User::LeaveIfError(variantMap->Add(dialogTypeKey, dialogType));
       
   847             CleanupStack::Pop(dialogType);
       
   848             
       
   849             CHbSymbianVariant* deviceName = CHbSymbianVariant::NewL( (TAny*) (&iRemoteDeviceName), 
       
   850                                                                 CHbSymbianVariant::EDes );
       
   851             CleanupStack::PushL(deviceName);
       
   852             TBuf16<6> deviceNameKey;
       
   853             deviceNameKey.Num(TBluetoothDeviceDialog::EDeviceName);
       
   854             User::LeaveIfError(variantMap->Add(deviceNameKey, deviceName));
       
   855             CleanupStack::Pop(deviceName);
       
   856             
       
   857             CHbSymbianVariant* fileName = CHbSymbianVariant::NewL( (TAny*) (&iReceivingFileName), 
       
   858                                                                 CHbSymbianVariant::EDes );
       
   859             CleanupStack::PushL(fileName);
       
   860             TBuf16<6> fileNameKey;
       
   861             fileNameKey.Num(TBluetoothDeviceDialog::EReceivingFileName);
       
   862             User::LeaveIfError(variantMap->Add(fileNameKey, fileName));
       
   863             CleanupStack::Pop(fileName);
       
   864             
       
   865             CHbSymbianVariant* fileSz = CHbSymbianVariant::NewL( (TAny*) &iTotalSizeByte, 
       
   866                                                                 CHbSymbianVariant::EInt );
       
   867             CleanupStack::PushL(fileSz);
       
   868             TBuf16<6> fileSzKey;
       
   869             fileSzKey.Num(TBluetoothDeviceDialog::EReceivingFileSize);
       
   870             User::LeaveIfError(variantMap->Add(fileSzKey, fileSz));
       
   871             CleanupStack::Pop(fileSz);
       
   872             
       
   873             CHbSymbianVariant* fileCnt = CHbSymbianVariant::NewL( (TAny*) &iFileCount, 
       
   874                                                                 CHbSymbianVariant::EInt );
       
   875             CleanupStack::PushL(fileCnt);
       
   876             TBuf16<6> fileCntKey;
       
   877             fileCntKey.Num(TBluetoothDeviceDialog::EReceivedFileCount);
       
   878             User::LeaveIfError(variantMap->Add(fileCntKey, fileCnt));
       
   879             CleanupStack::Pop(fileCnt);
       
   880         
       
   881             iDialogActive = ETrue;
       
   882             iProgressDialog->Show( KBTDevDialogId(), *variantMap, this );
       
   883             CleanupStack::PopAndDestroy(variantMap);
       
   884             }
       
   885         else
       
   886             {
       
   887             // TODO
       
   888             iDialogActive = ETrue;
       
   889             }
       
   890         }
       
   891     else
       
   892         {
       
   893         iDialogActive = ETrue;
       
   894         /*
       
   895          * TODO - The functionality provided by CGlobalDialog will be removed
       
   896          * TODO - and this will be provided by CGlobalProgressDialog.
       
   897          */
       
   898         
       
   899         }
       
   900     }
       
   901 
       
   902 void CBIPController::UpdateReceivingIndicatorL()
       
   903     {
       
   904     if(iProgressDialog)
       
   905         {
       
   906         CHbSymbianVariantMap* variantMap = CHbSymbianVariantMap::NewL();
       
   907         CleanupStack::PushL(variantMap);
       
   908         
       
   909         TInt bytesReceived = iBTObject->BytesReceived();
       
   910         CHbSymbianVariant* progress = CHbSymbianVariant::NewL( (TAny*) &bytesReceived, CHbSymbianVariant::EInt );
       
   911         CleanupStack::PushL(progress);
       
   912         User::LeaveIfError(variantMap->Add(_L("progress"), progress));
       
   913         CleanupStack::Pop(progress);
       
   914         
       
   915         iProgressDialog->Update(*variantMap);
       
   916         CleanupStack::PopAndDestroy(variantMap);
       
   917         }
       
   918     }
       
   919 
       
   920 void CBIPController::CloseReceivingIndicator(TBool aResetDisplayedState)
       
   921     {
       
   922     TRACE_FUNC
       
   923     if(aResetDisplayedState)
       
   924         {
       
   925         iNoteDisplayed = EFalse;
       
   926         }
       
   927     if(iProgressDialog)
       
   928         {
       
   929         iProgressDialog->Cancel();
       
   930         iDialogActive = EFalse;
       
   931         delete iProgressDialog;
       
   932         iProgressDialog = NULL;
       
   933         }
       
   934     }
       
   935  
       
   936  // ---------------------------------------------------------
       
   937  // GetDriveWithMaximumFreeSpace()
       
   938  // ---------------------------------------------------------
       
   939  // 
       
   940  TInt CBIPController::GetDriveWithMaximumFreeSpaceL()
       
   941      {
       
   942      // Get drive with maximum freespace among phone memory, MMC, internal mass storage.
       
   943      //
       
   944       TRACE_FUNC   
       
   945      
       
   946      TVolumeInfo volumeInfoC;
       
   947      TVolumeInfo volumeInfoE;
       
   948      TVolumeInfo volumeInfoF;
       
   949      TInt64 max = 0;
       
   950      TInt drive = 0;
       
   951      
       
   952      TInt err = iFs.Volume(volumeInfoC, EDriveC);
       
   953     
       
   954      if ( !err )
       
   955          {
       
   956          // set initial values to max and drive.
       
   957          max = volumeInfoC.iFree;
       
   958          drive = EDriveC;
       
   959          }
       
   960           
       
   961      err = iFs.Volume(volumeInfoE, EDriveE);     
       
   962      if ( !err )
       
   963          {
       
   964          if (volumeInfoE.iFree >= max)
       
   965              {
       
   966              max = volumeInfoE.iFree;
       
   967              drive = EDriveE;             
       
   968              }
       
   969          
       
   970          }
       
   971           
       
   972      err = iFs.Volume(volumeInfoF, EDriveF);
       
   973      if ( !err )
       
   974          {
       
   975          if (volumeInfoF.iFree >= max)
       
   976              {
       
   977              max = volumeInfoF.iFree;
       
   978              drive = EDriveF;             
       
   979              }
       
   980          }
       
   981      max = 0;
       
   982      return drive;
       
   983      }
       
   984  
       
   985  // ----------------------------------------------------------
       
   986  // COPPController::HandleDevManComplete
       
   987  // Callback from devman
       
   988  // ----------------------------------------------------------
       
   989  //    
       
   990  // 
       
   991  void CBIPController::HandleGetDevicesComplete(TInt aErr, CBTDeviceArray* aDeviceArray)
       
   992     {
       
   993     (void) aDeviceArray;
       
   994      
       
   995     if ( aErr == KErrNone )
       
   996         {
       
   997         if ( iResultArray->Count())
       
   998             {
       
   999             iRemoteDeviceName.Zero();
       
  1000             if ( iResultArray->At(0)->FriendlyName().Length() > 0 )
       
  1001                 {
       
  1002                 TRACE_INFO( _L( "[CBIPController] HandleGetDevicesComplete: got friendly name \t" ) );
       
  1003                 iRemoteDeviceName.Copy(iResultArray->At(0)->FriendlyName());
       
  1004                 }
       
  1005             else
       
  1006                 {
       
  1007                 TRACE_INFO( _L( "[CBIPController] HandleGetDevicesComplete: got devciename name \t" ) );                
       
  1008                 TRAP_IGNORE(iRemoteDeviceName.Copy(BTDeviceNameConverter::ToUnicodeL(iResultArray->At(0)->DeviceName())));
       
  1009                 }
       
  1010             }
       
  1011         }
       
  1012     }
       
  1013  
       
  1014  TBool CBIPController::IsBackupRunning()
       
  1015     {
       
  1016     const TUint32 KFileManagerBkupStatus = 0x00000001;
       
  1017     
       
  1018     TInt status = EFileManagerBkupStatusUnset;
       
  1019     TBool retValue = EFalse;
       
  1020     TInt err = RProperty::Get( TUid::Uid(KFileManagerUID3), KFileManagerBkupStatus,
       
  1021                               status );
       
  1022     if ( err == KErrNone )
       
  1023         {
       
  1024         if ( status == EFileManagerBkupStatusBackup || 
       
  1025              status == EFileManagerBkupStatusRestore )
       
  1026             {
       
  1027             TSecureId fileManagerSecureId( KFileManagerUID3 );
       
  1028             //only returning ETrue if backup process is still active
       
  1029             retValue = ProcessExists( fileManagerSecureId );
       
  1030             }
       
  1031         }
       
  1032    
       
  1033     return retValue;
       
  1034     }
       
  1035  
       
  1036  TBool CBIPController::ProcessExists( const TSecureId& aSecureId )
       
  1037      {
       
  1038      _LIT( KFindPattern, "*" );
       
  1039      TFindProcess finder(KFindPattern);
       
  1040      TFullName processName;
       
  1041      while( finder.Next( processName ) == KErrNone )
       
  1042          {
       
  1043          RProcess process;
       
  1044          if ( process.Open( processName ) == KErrNone )
       
  1045              {
       
  1046              TSecureId processId( process.SecureId() );
       
  1047              process.Close();
       
  1048              if( processId == aSecureId )
       
  1049                  {
       
  1050                  return ETrue;
       
  1051                  }
       
  1052              }
       
  1053          }
       
  1054      return EFalse;
       
  1055      }
       
  1056  
       
  1057  void CBIPController::DialogDismissed(TInt aButtonId)
       
  1058      {
       
  1059      (void) aButtonId;
       
  1060      }
       
  1061 
       
  1062  void CBIPController::DataReceived(CHbSymbianVariantMap& aData)
       
  1063      {
       
  1064      if(aData.Keys().MdcaPoint(0).Compare(_L("actionResult")) == 0)
       
  1065          {
       
  1066          TInt val = *(static_cast<TInt*>(aData.Get(_L("actionResult"))->Data()));
       
  1067          switch(val)
       
  1068              {
       
  1069              case TBluetoothDialogParams::ECancelReceive:
       
  1070                  {
       
  1071                  //User choose to cancel receiving.
       
  1072                  CancelTransfer();
       
  1073                  }break;
       
  1074                  
       
  1075              case TBluetoothDialogParams::EHide:
       
  1076                  {
       
  1077                  //Use choose to hide the progress dialog.
       
  1078                  CloseReceivingIndicator(EFalse);
       
  1079                  }break;
       
  1080                  
       
  1081              case TBluetoothDialogParams::EShow:
       
  1082              case TBluetoothDialogParams::ECancelShow:
       
  1083                  {
       
  1084                  //In case of Show, the device dialog will handle the opening of conversation view.
       
  1085                  iRecvDoneDialog->Cancel();
       
  1086                  delete iRecvDoneDialog;
       
  1087                  iRecvDoneDialog = NULL;
       
  1088                  }break;
       
  1089              }
       
  1090          }
       
  1091      }
       
  1092  
       
  1093  void CBIPController::DeviceDialogClosed(TInt aCompletionCode)
       
  1094      {
       
  1095      (void) aCompletionCode;
       
  1096      }
       
  1097 
       
  1098  
       
  1099 //////////////////////////// Global part ////////////////////////////
       
  1100 
       
  1101 //  End of File