localconnectivityservice/obexsendservices/obexserviceirsend/src/IRClient.cpp
branchRCL_3
changeset 39 4096754ee773
parent 38 3dcb815346df
child 40 52a167391590
equal deleted inserted replaced
38:3dcb815346df 39:4096754ee773
     1 /*
       
     2 * Copyright (c) 2002 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 client implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <obexclient.h>
       
    22 #include <Obexutils.rsg>
       
    23 #include <obexutilsuilayer.h>
       
    24 
       
    25 #include "IRClient.h"
       
    26 #include "IrSendingServiceDebug.h"
       
    27 
       
    28 
       
    29 const TUint KIRProgressInterval         = 1000000;
       
    30 
       
    31 _LIT( KTransportTinyTp, "IrTinyTP" );
       
    32 _LIT8( KClassNameObex, "OBEX" );
       
    33 _LIT8( KAttName, "IrDA:TinyTP:LsapSel" );
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CBTServiceClient::CBTServiceClient
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CIRClient::CIRClient(  ) 
       
    46     : CActive( EPriorityStandard ), 
       
    47       iClientState( EIRCliDisconnected),                             
       
    48       iObjectIndex( 0 )
       
    49     {
       
    50     CActiveScheduler::Add( this );    
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CBTServiceClient::ConstructL
       
    55 // Symbian 2nd phase constructor can leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CIRClient::ConstructL( )
       
    59     {
       
    60     FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL()"));
       
    61 
       
    62     User::LeaveIfError( iFileSession.Connect() );
       
    63     
       
    64     iDialog = CObexUtilsDialog::NewL( this );
       
    65     iTotalBytesSent = 0;
       
    66     // Create Obex Client
       
    67     //
       
    68     TObexIrProtocolInfo  info;
       
    69     //info.iTransport = KObexIrTTPProtocol;    
       
    70     
       
    71   //  info.iAddr.SetPort( KDefaultObexPort );//default obex server for now
       
    72     info.iTransport     = KTransportTinyTp;
       
    73     info.iClassName     = KClassNameObex;
       
    74     info.iAttributeName = KAttName;
       
    75 
       
    76     
       
    77     iClient = CObexClient::NewL( info );
       
    78     
       
    79 
       
    80     // Create Connect-object
       
    81     //
       
    82     iConnectObject = CObexNullObject::NewL();
       
    83 
       
    84     //Show note
       
    85     //
       
    86     iDialog->LaunchWaitDialogL( R_IR_CONNECTING_WAIT_NOTE);
       
    87     
       
    88     // Establish client connection
       
    89     //
       
    90     iClient->Connect( /**iConnectObject,*/ iStatus );
       
    91     SetActive();
       
    92     iClientState = EIRCliConnecting;
       
    93 
       
    94     FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL() completed"));
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CBTServiceClient::NewL
       
    99 // Two-phased constructor.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CIRClient* CIRClient::NewL()
       
   103     {
       
   104     CIRClient* self = new( ELeave ) CIRClient( );
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL( );
       
   107     CleanupStack::Pop();
       
   108     return self;
       
   109     }
       
   110 
       
   111     
       
   112 // Destructor
       
   113 CIRClient::~CIRClient()
       
   114     { 
       
   115 
       
   116     Cancel();
       
   117     if(iClient)
       
   118         {
       
   119         delete iClient;
       
   120         iClient = NULL;        
       
   121         }    
       
   122     
       
   123     iFileSession.Close();    
       
   124     delete iConnectObject;
       
   125     
       
   126     for (TInt index = 0; index < iFileArray.Count(); index++ )
       
   127     	{
       
   128     	if(iFileArray[index].SubSessionHandle())
       
   129     		{
       
   130     		iFileArray[index].Close();
       
   131     		}
       
   132     	}
       
   133     	
       
   134     iFileArray.Close();
       
   135     
       
   136     if ( iPutBufObject )
       
   137         {        
       
   138         delete iPutBufObject;
       
   139         iPutBufObject = NULL;
       
   140         }
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CBTServiceClient::PutObjectL
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void CIRClient::PutObjectL( )
       
   148     {
       
   149     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL()"));
       
   150 
       
   151     if ( iPutBufObject )
       
   152         {        
       
   153         delete iPutBufObject;
       
   154         iPutBufObject = NULL;
       
   155         }
       
   156 
       
   157     // Create object
       
   158     //
       
   159     iPutBufObject = CObexBufObject::NewL(NULL);    
       
   160     
       
   161     RFile file;    
       
   162     file.Duplicate(iFileArray[iObjectIndex]);
       
   163     
       
   164     iBuffer = CBufFlat::NewL(1000);
       
   165     iBuffer ->ResizeL(1000);
       
   166     TObexRFileBackedBuffer bufferdetails(*iBuffer,file,CObexBufObject::ESingleBuffering);
       
   167     iPutBufObject->SetDataBufL(bufferdetails);
       
   168     
       
   169     
       
   170     TFileName filename;    
       
   171     file.Name(filename);
       
   172     
       
   173     TInt size;
       
   174     file.Size(size);
       
   175     iPutBufObject->SetLengthL(size);	
       
   176 		
       
   177     iPutBufObject->SetNameL(filename);
       
   178     TTime time;
       
   179 	if(file.Modified(time) == KErrNone)
       
   180 		iPutBufObject->SetTimeL(time);
       
   181 	
       
   182         
       
   183         
       
   184 
       
   185     // Send object
       
   186     //
       
   187     iClient->Put( *iPutBufObject, iStatus );    
       
   188 	SetActive();
       
   189     iClientState = EIRCliPutting;
       
   190 
       
   191     FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL() completed"));
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CBTServiceClient::CloseClientConnection
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CIRClient::CloseClientConnection()
       
   199     {    
       
   200 
       
   201     iClient->Disconnect( iStatus );
       
   202    	SetActive();
       
   203     iClientState = EIRCliDisconnected;
       
   204     
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CBTServiceClient::GetProgressStatus
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 TInt CIRClient::GetProgressStatus()
       
   212     {    
       
   213 
       
   214     TInt bytesSent = 0;
       
   215     if ( iPutBufObject )
       
   216         {
       
   217         bytesSent = iPutBufObject->BytesSent();
       
   218         }
       
   219         
       
   220     return iTotalBytesSent + bytesSent;
       
   221     }
       
   222 
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CBTServiceClient::DoCancel
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CIRClient::DoCancel()
       
   229     {
       
   230     FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel()"));
       
   231 
       
   232     // Sending an error to Obex is the only way to cancel active requests
       
   233     //    
       
   234     if(iClient)
       
   235         {
       
   236         delete iClient;
       
   237         iClient = NULL;        
       
   238         }    
       
   239     
       
   240 
       
   241     FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel() completed"));
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CBTServiceClient::RunL
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CIRClient::RunL()
       
   249     {
       
   250 
       
   251 
       
   252     switch ( iClientState )
       
   253         {
       
   254         case EIRCliConnecting:
       
   255             {
       
   256                
       
   257             if ( iPutBufObject )
       
   258 				{				
       
   259 				delete iPutBufObject;
       
   260 				iPutBufObject = NULL;
       
   261 				}				
       
   262 				iObjectIndex=0;		
       
   263 				iDialog->CancelWaitDialogL();		
       
   264 				iDialog->LaunchProgressDialogL( this, FileListsize(), 
       
   265             									R_IR_SENDING_DATA, KIRProgressInterval );             
       
   266 				TRAPD(error,SendL());
       
   267 				error=error;         	
       
   268 			
       
   269 			
       
   270             break;
       
   271             }
       
   272 
       
   273         case EIRCliPutting:
       
   274             {
       
   275                      
       
   276 			if ( iPutBufObject )
       
   277 				{				
       
   278 				delete iPutBufObject;
       
   279 				iPutBufObject = NULL;
       
   280 				}		
       
   281 		    if(iBuffer)
       
   282 		        {
       
   283 		        delete iBuffer;
       
   284 		        iBuffer=NULL;
       
   285 		        }
       
   286 			iObjectIndex++;
       
   287 			TRAPD(error,SendL());
       
   288 			error=error;         
       
   289           
       
   290             break;
       
   291             }
       
   292 
       
   293         case EIRCliDisconnected:
       
   294             {            
       
   295             break;
       
   296             }       
       
   297         default:
       
   298            {
       
   299               
       
   300            break;
       
   301            }
       
   302         }
       
   303 
       
   304     FLOG(_L("[BTSU]\t CBTServiceClient::RunL() completed"));
       
   305     }
       
   306     
       
   307  // -----------------------------------------------------------------------------
       
   308 // CBTServiceClient::DialogDismissed
       
   309 // -----------------------------------------------------------------------------
       
   310 //   
       
   311  void CIRClient::DialogDismissed( TInt aButtonId )
       
   312     {
       
   313     
       
   314     if( aButtonId == EAknSoftkeyCancel )
       
   315         {
       
   316         FLOG(_L("[BTSU]\t CBTServiceStarter::DialogDissmissed(), cancelled by user"));
       
   317         Cancel();
       
   318 
       
   319         // Cancelled by user, stop service
       
   320         //
       
   321         iClientState = EIRCliDisconnected;
       
   322         TRequestStatus* temp = &iStatus;
       
   323         User::RequestComplete( temp, KErrNone );        
       
   324         SetActive();
       
   325         }
       
   326     }
       
   327 
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CBTServiceClient::StartSendL
       
   331 // -----------------------------------------------------------------------------
       
   332 //     
       
   333  void CIRClient::StartSendL(const CAiwGenericParamList& aOutParamList )
       
   334     {
       
   335     
       
   336     if ( iDialog )
       
   337         {
       
   338         iDialog->CancelWaitDialogL();        
       
   339         }
       
   340     
       
   341     for( TInt index = 0; index < aOutParamList.Count(); index++ )
       
   342         {
       
   343         if ( aOutParamList[index].SemanticId() != EGenericParamFile )
       
   344             {
       
   345             FLOG(_L("[BTSS]\t CBTSSProvider::AddParametersL() wrong semantic Id: Leave"));
       
   346             User::Leave( KErrArgument );
       
   347             }
       
   348             // Try to add file as an image
       
   349             //          
       
   350        
       
   351     
       
   352         if(aOutParamList[index].Value().TypeId()== EVariantTypeFileHandle)
       
   353         	{        	        	
       
   354             AddFileHandleL( aOutParamList[index].Value().AsFileHandle() );           
       
   355         	
       
   356         	}
       
   357         else 
       
   358         	{            
       
   359             AddFileL( aOutParamList[index].Value().AsDes() );            
       
   360         	}
       
   361     
       
   362         }      
       
   363     
       
   364     }
       
   365     
       
   366 // -----------------------------------------------------------------------------
       
   367 // CBTServiceClient::SendL
       
   368 // -----------------------------------------------------------------------------
       
   369 //     
       
   370  void CIRClient::SendL()
       
   371     {
       
   372     if(iObjectIndex<iFileArray.Count())
       
   373         {        
       
   374         PutObjectL();
       
   375         }
       
   376      else
       
   377         {
       
   378         ShowNote();            
       
   379         iDialog->CancelProgressDialogL();
       
   380         iClient->Disconnect( iStatus );        
       
   381         SetActive();    
       
   382         iClientState=EIRCliDisconnected;        
       
   383         
       
   384         }
       
   385        
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CBTServiceClient::AddFileHandleL
       
   390 // -----------------------------------------------------------------------------
       
   391 //      
       
   392     
       
   393     
       
   394  void CIRClient::AddFileHandleL(RFile aFile)
       
   395     {
       
   396     if(!aFile.SubSessionHandle())
       
   397         {
       
   398         User::Leave( KErrArgument );
       
   399         }
       
   400     RFile file;
       
   401     
       
   402     file.Duplicate(aFile);
       
   403 
       
   404     iFileArray.AppendL( file );
       
   405     
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CBTServiceClient::AddFileL
       
   410 // -----------------------------------------------------------------------------
       
   411 //      
       
   412 	    
       
   413  void CIRClient::AddFileL(const TDesC& aFilePath)
       
   414     {
       
   415     if ( &aFilePath == NULL || 
       
   416          aFilePath.Length() == 0 ||
       
   417          aFilePath.Length() > KMaxFileName )
       
   418         {
       
   419         User::Leave( KErrArgument );
       
   420         } 
       
   421     RFile file;
       
   422     User::LeaveIfError( file.Open( iFileSession, aFilePath, EFileShareReadersOnly) );
       
   423     AddFileHandleL(file);
       
   424     
       
   425     }
       
   426     
       
   427 // -----------------------------------------------------------------------------
       
   428 // CBTServiceClient::FileListsize
       
   429 // -----------------------------------------------------------------------------
       
   430 //      
       
   431  TInt CIRClient::FileListsize()
       
   432     {
       
   433     
       
   434     TInt totalFileSize = 0; 	    
       
   435     for ( TInt index = 0; index < iFileArray.Count(); index++ )
       
   436         {                
       
   437         TInt fileSize = 0;
       
   438         iFileArray[index].Size( fileSize );
       
   439         totalFileSize += fileSize;        
       
   440         }
       
   441     return totalFileSize;
       
   442     
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CBTServiceClient::ShowNote
       
   447 // -----------------------------------------------------------------------------
       
   448 //      
       
   449 void CIRClient::ShowNote()
       
   450     {
       
   451     if(iObjectIndex==iFileArray.Count() && iStatus==KErrNone)
       
   452         {
       
   453         TRAPD(error,TObexUtilsUiLayer::ShowInformationNoteL(R_IR_DATA_SENT));
       
   454         error=error;
       
   455         }
       
   456      else
       
   457      	{
       
   458      	TRAPD(error,TObexUtilsUiLayer::ShowInformationNoteL(R_IR_SENDING_FAILED));
       
   459      	error=error;
       
   460      	}
       
   461     }
       
   462 //  End of File