filemanager/Engine/src/CFileManagerIRReceiver.cpp
changeset 0 6a9f87576119
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  Receives obex object via InfraRed, used to receive files
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <obex.h>
       
    21 #include "CFileManagerIRReceiver.h"
       
    22 #include "MFileManagerProcessObserver.h"
       
    23 #include "CFileManagerEngine.h"
       
    24 #include "CFileManagerUtils.h"
       
    25 #include "CFileManagerCommonDefinitions.h"
       
    26 #include "FileManagerDebug.h"
       
    27 
       
    28 
       
    29 // CONSTANTS
       
    30 const TUint KDefaultObexPort = 65;
       
    31 const TUint KBufferGranularity = 2048;
       
    32 const TInt KFullPercentage = 100;
       
    33 const TInt KInactiveTimeout = 60000000;
       
    34 
       
    35 // Required for IR
       
    36 _LIT( KTransportTinyTp, "IrTinyTP" );
       
    37 _LIT8( KClassNameObex, "OBEX" );
       
    38 _LIT8( KAttName, "IrDA:TinyTP:LsapSel" );
       
    39 _LIT( KTemporaryFile, "__FileManagerIrdaReceive.tmp" );
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CFileManagerEngine::CFileManagerEngine
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CFileManagerIRReceiver::CFileManagerIRReceiver(
       
    50         MFileManagerProcessObserver& aObserver,
       
    51         CFileManagerEngine& aEngine,
       
    52         RFs& aFs ) :
       
    53     iObserver( aObserver ),
       
    54     iEngine( aEngine ),
       
    55     iFs( aFs )
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CFileManagerEngine::NewL
       
    61 // Two-phased constructor.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 EXPORT_C CFileManagerIRReceiver* CFileManagerIRReceiver::NewL(
       
    65         MFileManagerProcessObserver& aObserver,
       
    66         const TDesC& aPath,
       
    67         CFileManagerEngine& aEngine )
       
    68     {
       
    69     FUNC_LOG
       
    70 
       
    71     CFileManagerIRReceiver* self = new( ELeave ) CFileManagerIRReceiver(
       
    72             aObserver,
       
    73             aEngine,
       
    74             aEngine.Fs() );
       
    75     
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL( aPath );
       
    78     CleanupStack::Pop( self );
       
    79 
       
    80     return self;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CFileManagerIRReceiver::ConstructL
       
    85 // 
       
    86 // -----------------------------------------------------------------------------
       
    87 // 
       
    88 void CFileManagerIRReceiver::ConstructL( const TDesC& aPath )
       
    89     {
       
    90     TPtrC ptrPath( CFileManagerUtils::StripFinalBackslash( aPath ) );
       
    91     TInt len( ptrPath.Length() +
       
    92         KFmgrBackslashSpace + KTemporaryFile().Length() );
       
    93     if ( len > KMaxFileName )
       
    94         {
       
    95         User::Leave( KErrBadName );
       
    96         }
       
    97     iTempFile = HBufC::NewL( len );
       
    98     TPtr ptr( iTempFile->Des() );
       
    99     ptr.Append( ptrPath );
       
   100     CFileManagerUtils::EnsureFinalBackslash( ptr );
       
   101     ptr.Append( KTemporaryFile );
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CFileManagerIRReceiver::ReceiveFileL
       
   106 // 
       
   107 // -----------------------------------------------------------------------------
       
   108 // 
       
   109 EXPORT_C void CFileManagerIRReceiver::ReceiveFileL()
       
   110     {
       
   111     FUNC_LOG
       
   112 
       
   113     ResetInactivityTimer();
       
   114 
       
   115     TObexIrProtocolInfo obexIrInfo;
       
   116     obexIrInfo.iAddr.SetPort( KDefaultObexPort );
       
   117     obexIrInfo.iTransport     = KTransportTinyTp;
       
   118     obexIrInfo.iClassName     = KClassNameObex;
       
   119     obexIrInfo.iAttributeName = KAttName;
       
   120 
       
   121     iObexServer = CObexServer::NewL( obexIrInfo );
       
   122 
       
   123     User::LeaveIfError( iObexServer->Start( this ) );
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CFileManagerIRReceiver::~CFileManagerIRReceiver
       
   128 // Destructor
       
   129 // -----------------------------------------------------------------------------
       
   130 // 
       
   131 EXPORT_C CFileManagerIRReceiver::~CFileManagerIRReceiver()
       
   132     {
       
   133     FUNC_LOG
       
   134     
       
   135     if ( iObexServer )
       
   136         {
       
   137         if ( iObexServer->IsStarted() )
       
   138             {
       
   139             iObexServer->Stop();
       
   140             }
       
   141         delete iObexServer;
       
   142         }
       
   143     delete iObexBufObject;
       
   144 
       
   145     if ( iTempFile )
       
   146         {
       
   147         iFs.Delete( *iTempFile );
       
   148         delete iTempFile;
       
   149         }
       
   150     delete iBuffer;
       
   151     delete iInactivityTimer;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CFileManagerIRReceiver::StopReceiving
       
   156 // 
       
   157 // -----------------------------------------------------------------------------
       
   158 // 
       
   159 EXPORT_C void CFileManagerIRReceiver::StopReceiving()
       
   160     {
       
   161     FUNC_LOG
       
   162 
       
   163     iError = KErrCancel;
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CFileManagerIRReceiver::ErrorIndication
       
   168 // 
       
   169 // -----------------------------------------------------------------------------
       
   170 // 
       
   171 void CFileManagerIRReceiver::ErrorIndication(TInt aError )
       
   172     {
       
   173     ERROR_LOG1( "CFileManagerIRReceiver::ErrorIndication-aError=%d", aError )
       
   174 
       
   175     if ( iError == KErrNone )
       
   176         {
       
   177         iError = aError;
       
   178         }
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CFileManagerIRReceiver::TransportUpIndication
       
   183 // 
       
   184 // -----------------------------------------------------------------------------
       
   185 // 
       
   186 void CFileManagerIRReceiver::TransportUpIndication()
       
   187     {
       
   188     FUNC_LOG
       
   189 
       
   190     StopInactivityTimer();
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CFileManagerIRReceiver::TransportDownIndication
       
   195 // 
       
   196 // -----------------------------------------------------------------------------
       
   197 // 
       
   198 void CFileManagerIRReceiver::TransportDownIndication()
       
   199     {
       
   200     FUNC_LOG
       
   201 
       
   202     TInt err( KErrNone );
       
   203     if ( iObexBufObject )
       
   204         {
       
   205         // transfer has been cancelled, set temp buffer so obexobject won't
       
   206         // create temp file again
       
   207         delete iBuffer;
       
   208         iBuffer = NULL;
       
   209         TRAP( err, iBuffer = CBufFlat::NewL( KBufferGranularity ) );
       
   210         if ( err == KErrNone )
       
   211             {
       
   212             TRAP( err, iObexBufObject->SetDataBufL( iBuffer ) );
       
   213             }
       
   214         }
       
   215 
       
   216     if ( iTempFile )
       
   217         {
       
   218         iFs.Delete( *iTempFile );
       
   219         }
       
   220 
       
   221     TRAP( err, iObserver.ProcessFinishedL( iError ) );
       
   222     if ( err != KErrNone )
       
   223         {
       
   224         iObserver.Error( err );
       
   225         }
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CFileManagerIRReceiver::TargetHeaderReceived
       
   230 // 
       
   231 // -----------------------------------------------------------------------------
       
   232 // 
       
   233 TBool CFileManagerIRReceiver::TargetHeaderReceived(TDesC8& /* aTargetHeader */ )
       
   234     {
       
   235     FUNC_LOG
       
   236 
       
   237     return EFalse;
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CFileManagerIRReceiver::ObexConnectIndication 
       
   242 // 
       
   243 // -----------------------------------------------------------------------------
       
   244 // 
       
   245 TInt CFileManagerIRReceiver::ObexConnectIndication
       
   246     ( const TObexConnectInfo& /* aRemoteInfo */, 
       
   247       const TDesC8& /* aInfo */ )
       
   248     {
       
   249     FUNC_LOG
       
   250 
       
   251     if ( iError == KErrCancel )
       
   252         {
       
   253         return KErrCancel;
       
   254         }
       
   255 
       
   256     TRAPD( err, iObserver.ProcessStartedL( 
       
   257         MFileManagerProcessObserver::EIRReceiveProcess ) );
       
   258 
       
   259     return err;
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // CFileManagerIRReceiver::ObexDisconnectIndication
       
   264 // 
       
   265 // -----------------------------------------------------------------------------
       
   266 // 
       
   267 void CFileManagerIRReceiver::ObexDisconnectIndication(const TDesC8& /* aInfo */ )
       
   268     {
       
   269     FUNC_LOG
       
   270     // from MObexServerNotify
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CFileManagerIRReceiver::PutRequestIndication
       
   275 // 
       
   276 // -----------------------------------------------------------------------------
       
   277 // 
       
   278 CObexBufObject* CFileManagerIRReceiver::PutRequestIndication()
       
   279     {
       
   280     FUNC_LOG
       
   281 
       
   282     // delete old buffer if it exists
       
   283     delete iObexBufObject;
       
   284     iObexBufObject = NULL;
       
   285     iFs.Delete( *iTempFile );
       
   286 
       
   287     TRAPD( err, iObexBufObject = CObexBufObject::NewL( NULL ) );
       
   288     if ( err == KErrNone )
       
   289         {
       
   290         TRAP( err, iObexBufObject->SetDataBufL( *iTempFile ) );
       
   291         }
       
   292 
       
   293     if ( err != KErrNone )
       
   294         {
       
   295         return NULL;
       
   296         }
       
   297 
       
   298     return iObexBufObject;
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CFileManagerIRReceiver::PutPacketIndication
       
   303 // 
       
   304 // -----------------------------------------------------------------------------
       
   305 // 
       
   306 TInt CFileManagerIRReceiver::PutPacketIndication()
       
   307     {
       
   308     FUNC_LOG
       
   309 
       
   310     if ( iError == KErrCancel )
       
   311         {
       
   312         return KErrCancel;
       
   313         }
       
   314 
       
   315     const TInt size( iObexBufObject->Length() );
       
   316 
       
   317     if ( !iDiskSpaceChecked )
       
   318         {
       
   319         TParsePtrC parse( *iTempFile );
       
   320         TRAP( iError, iEnoughSpace = iEngine.EnoughSpaceL(
       
   321             parse.DriveAndPath(), size,
       
   322             MFileManagerProcessObserver::EIRReceiveProcess ) );
       
   323         iDiskSpaceChecked = ETrue;
       
   324         }
       
   325 
       
   326     if ( iError == KErrNone && iEnoughSpace )
       
   327         {
       
   328         const TInt received( iObexBufObject->BytesReceived());
       
   329         TInt percent( 0 );
       
   330         if ( size != 0)
       
   331             {
       
   332             percent = KFullPercentage * received / size;
       
   333             }
       
   334         TRAPD( error, iObserver.ProcessAdvanceL( percent ) );
       
   335         if ( iError == KErrNone )
       
   336             {
       
   337             iError = error;
       
   338             }
       
   339         }
       
   340     else if ( !iEnoughSpace )
       
   341         {
       
   342         if ( iError == KErrNone )
       
   343             {
       
   344             iError = KErrDiskFull;
       
   345             }
       
   346         }
       
   347 
       
   348     return iError;
       
   349     }
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CFileManagerIRReceiver::PutCompleteIndication
       
   353 // 
       
   354 // -----------------------------------------------------------------------------
       
   355 // 
       
   356 TInt CFileManagerIRReceiver::PutCompleteIndication()
       
   357     {
       
   358     FUNC_LOG
       
   359 
       
   360     if ( iError == KErrCancel )
       
   361         {
       
   362         iFs.Delete( *iTempFile );
       
   363         iDiskSpaceChecked = EFalse;
       
   364         return KErrNone;
       
   365         }
       
   366 
       
   367     TInt error( KErrNone );
       
   368     TFileName fileName;
       
   369     TParsePtrC parse( *iTempFile );
       
   370     fileName.Append( parse.DriveAndPath() );
       
   371     TPtrC objName( iObexBufObject->Name() );
       
   372 
       
   373     if ( fileName.Length() + objName.Length() > KMaxFileName )
       
   374         {
       
   375         iFs.Delete( *iTempFile );
       
   376         iDiskSpaceChecked = EFalse;
       
   377         iObserver.Error( KErrBadName );
       
   378         return KErrBadName;
       
   379         }
       
   380 
       
   381     fileName.Append( objName );
       
   382     delete iObexBufObject;
       
   383     iObexBufObject = NULL;
       
   384     TBool nameFound( EFalse );
       
   385     TRAP( error, nameFound = iEngine.IsNameFoundL( fileName ) );
       
   386     if ( error == KErrNone && nameFound )
       
   387         {
       
   388         TFileName name;
       
   389         TBool overWrite( ETrue );
       
   390         TRAP( error, overWrite = iObserver.ProcessQueryOverWriteL( 
       
   391             fileName, name, 
       
   392             MFileManagerProcessObserver::EIRReceiveProcess ) );
       
   393         
       
   394         if ( error == KErrNone )
       
   395             {
       
   396             if ( overWrite )
       
   397                 {
       
   398                 error = iFs.Delete( fileName );
       
   399                 if ( error == KErrNone )
       
   400                     {
       
   401                     iFs.Rename( *iTempFile, fileName );
       
   402                     }
       
   403                 if ( error != KErrNone )
       
   404                     {
       
   405                     iObserver.Error( error );
       
   406                     }
       
   407                 }
       
   408             else 
       
   409                 {
       
   410                 TBool askAgain( ETrue );
       
   411                 TRAP( error, nameFound = iEngine.IsNameFoundL( name ) );
       
   412                 while( error == KErrNone && nameFound && askAgain )
       
   413                     {
       
   414                     TFileName newName;
       
   415                     TRAP( error, overWrite = 
       
   416                         iObserver.ProcessQueryOverWriteL( 
       
   417                             name, 
       
   418                             newName, 
       
   419                             MFileManagerProcessObserver::EIRReceiveProcess 
       
   420                             ) );
       
   421                     if ( error != KErrNone )
       
   422                         {
       
   423                         iError = error;
       
   424                         askAgain = EFalse;
       
   425                         name.Zero();
       
   426                         }
       
   427                     else if ( !overWrite )
       
   428                         {
       
   429                         name.Zero();
       
   430                         name.Append( newName );
       
   431                         }
       
   432                     else
       
   433                         {
       
   434                         askAgain = EFalse;
       
   435                         }
       
   436                     }
       
   437                 if ( name.Length() > 0 )
       
   438                     {
       
   439                     TParsePtrC parse( *iTempFile );
       
   440                     name.Insert( 0, parse.DriveAndPath() );
       
   441                     iFs.Rename( *iTempFile, name );
       
   442                     }
       
   443                 }
       
   444             }
       
   445         else
       
   446             {
       
   447             iError = error;
       
   448             }
       
   449         }
       
   450     else
       
   451         {
       
   452 
       
   453         iFs.Rename( *iTempFile, fileName );
       
   454         }
       
   455     iFs.Delete( *iTempFile );
       
   456     iDiskSpaceChecked = EFalse;
       
   457 
       
   458     return error;
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CFileManagerIRReceiver::GetRequestIndication
       
   463 // Empty because needed only for send functionality
       
   464 // -----------------------------------------------------------------------------
       
   465 // 
       
   466 CObexBufObject* CFileManagerIRReceiver::GetRequestIndication
       
   467     (CObexBaseObject* /* aRequiredObject */)
       
   468     {
       
   469     FUNC_LOG
       
   470 
       
   471     return NULL;
       
   472     }
       
   473 
       
   474 // -----------------------------------------------------------------------------
       
   475 // CFileManagerIRReceiver::GetPacketIndication
       
   476 // Empty because needed only for send functionality
       
   477 // -----------------------------------------------------------------------------
       
   478 // 
       
   479 TInt CFileManagerIRReceiver::GetPacketIndication()
       
   480     {
       
   481     FUNC_LOG
       
   482 
       
   483     return KErrNone;
       
   484     }
       
   485 
       
   486 // -----------------------------------------------------------------------------
       
   487 // CFileManagerIRReceiver::GetCompleteIndication
       
   488 // Empty because needed only for send functionality
       
   489 // -----------------------------------------------------------------------------
       
   490 // 
       
   491 TInt CFileManagerIRReceiver::GetCompleteIndication()
       
   492     {
       
   493     FUNC_LOG
       
   494 
       
   495     return KErrNone;
       
   496     }
       
   497 
       
   498 // -----------------------------------------------------------------------------
       
   499 // CFileManagerIRReceiver::SetPathIndication
       
   500 // Empty because setpath not supported
       
   501 // -----------------------------------------------------------------------------
       
   502 // 
       
   503 TInt CFileManagerIRReceiver::SetPathIndication(
       
   504         const CObex::TSetPathInfo& /* aPathInfo*/ , const TDesC8& /* aInfo */)
       
   505     {
       
   506     FUNC_LOG
       
   507 
       
   508     return KErrNone;
       
   509     }
       
   510 
       
   511 // -----------------------------------------------------------------------------
       
   512 // CFileManagerIRReceiver::AbortIndication
       
   513 // Empty because needed only for send functionality
       
   514 // -----------------------------------------------------------------------------
       
   515 // 
       
   516 void CFileManagerIRReceiver::AbortIndication()
       
   517     {
       
   518     FUNC_LOG
       
   519     }
       
   520 
       
   521 // -----------------------------------------------------------------------------
       
   522 // CFileManagerIRReceiver::ResetInactivityTimer
       
   523 //
       
   524 // -----------------------------------------------------------------------------
       
   525 // 
       
   526 void CFileManagerIRReceiver::ResetInactivityTimer()
       
   527     {
       
   528     FUNC_LOG
       
   529 
       
   530     StopInactivityTimer();
       
   531 
       
   532     TRAPD( err, iInactivityTimer =
       
   533         CPeriodic::NewL( CActive::EPriorityStandard ) );
       
   534     if ( err == KErrNone )
       
   535         {
       
   536         iInactivityTimer->Start(
       
   537             KInactiveTimeout,
       
   538             KInactiveTimeout,
       
   539             TCallBack( InactivityTimeout, this ) );
       
   540             
       
   541         INFO_LOG( "CFileManagerIRReceiver::ResetInactivityTimer-Timer started" )
       
   542         }
       
   543     }
       
   544 
       
   545 // -----------------------------------------------------------------------------
       
   546 // CFileManagerIRReceiver::InactivityTimeout
       
   547 //
       
   548 // -----------------------------------------------------------------------------
       
   549 // 
       
   550 TInt CFileManagerIRReceiver::InactivityTimeout( TAny* aPtr )
       
   551     {
       
   552     FUNC_LOG
       
   553 
       
   554     CFileManagerIRReceiver* self =
       
   555         static_cast< CFileManagerIRReceiver* >( aPtr );
       
   556 
       
   557     self->StopInactivityTimer();
       
   558     self->CloseConnection();
       
   559 
       
   560     return KErrNone;
       
   561     }
       
   562 
       
   563 // -----------------------------------------------------------------------------
       
   564 // CFileManagerIRReceiver::StopInactivityTimer
       
   565 //
       
   566 // -----------------------------------------------------------------------------
       
   567 // 
       
   568 void CFileManagerIRReceiver::StopInactivityTimer()
       
   569     {
       
   570     FUNC_LOG
       
   571 
       
   572     delete iInactivityTimer;
       
   573     iInactivityTimer = NULL;
       
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CFileManagerIRReceiver::CloseConnection
       
   578 //
       
   579 // -----------------------------------------------------------------------------
       
   580 // 
       
   581 void CFileManagerIRReceiver::CloseConnection()
       
   582     {
       
   583     FUNC_LOG
       
   584 
       
   585     StopReceiving();
       
   586 
       
   587     if ( iObexServer )
       
   588         {
       
   589         if ( iObexServer->IsStarted() )
       
   590             {
       
   591             INFO_LOG( "CFileManagerIRReceiver::CloseConnection-Stop server" )
       
   592 
       
   593             iObexServer->Stop();
       
   594             }
       
   595         }
       
   596 
       
   597     TRAPD( err, iObserver.ProcessFinishedL( KErrCancel ) );
       
   598     if ( err != KErrNone )
       
   599         {
       
   600         iObserver.Error( err );
       
   601         }
       
   602     }
       
   603 
       
   604 //  End of File