internetradio2.0/dataprovidersrc/irdataprovider.cpp
changeset 0 09774dfdd46b
child 12 608f67c22514
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2006-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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "irdataprovider.h"
       
    20 #include "irdataproviderobserver.h"
       
    21 #include "irdebug.h"
       
    22 #include "irhttpdataprovider.h"
       
    23 #include "irhttprequestdata.h"
       
    24 #include "irsettings.h"
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 //  CIRDataProvider::NewL(MIRDataProviderObserver& aObserver)
       
    28 //  Creates instance of CIRDataProvider.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C CIRDataProvider *CIRDataProvider::NewL(
       
    32     MIRDataProviderObserver &aObserver )
       
    33     {
       
    34     IRLOG_DEBUG( "CIRDataProvider::NewL - Entering" );
       
    35     CIRDataProvider *self = NewLC( aObserver );
       
    36     CleanupStack::Pop(self);
       
    37     IRLOG_DEBUG( "CIRDataProvider::NewL - Exiting" );
       
    38     return self;
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 //  CIRDataProvider::NewL(MIRDataProviderObserver& aObserver)
       
    43 //  Creates instance of CIRDataProvider.
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C CIRDataProvider *CIRDataProvider::NewL(
       
    47     MIRDataProviderObserver &aObserver, const TDesC &aFileName )
       
    48     {
       
    49     IRLOG_DEBUG( "CIRDataProvider::NewL(..., &aFileName) - Entering" );
       
    50     CIRDataProvider *self = NewLC( aObserver, aFileName );
       
    51     CleanupStack::Pop(self);
       
    52     IRLOG_DEBUG( "CIRDataProvider::NewL(..., &aFileName) - Exiting." );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 //  CIRDataProvider::~CIRDataProvider()
       
    58 //  Destructs an instance of CIRDataProvider.
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CIRDataProvider::~CIRDataProvider() // destruct - virtual, so no export
       
    62     {
       
    63     IRLOG_DEBUG( "CIRDataProvider::~CIRDataProvider() - Entering" );
       
    64 
       
    65     if (iHttpDataProvider)
       
    66         {
       
    67         // Cancel any active transactions
       
    68         iHttpDataProvider->CancelTransaction();
       
    69         // Destroy the data provider object
       
    70         }
       
    71 
       
    72     delete iHttpDataProvider;
       
    73 
       
    74     if( iIRSettings )
       
    75     	{
       
    76     	iIRSettings->Close();
       
    77 		}
       
    78 
       
    79     if (iDataProviderTimer)
       
    80         {
       
    81      	iDataProviderTimer->Cancel(); // Cancel the timer
       
    82         }
       
    83 
       
    84     delete iDataProviderTimer; // Destroy the timer object
       
    85 	if( iResponseHeaders )
       
    86 		{
       
    87         delete iResponseHeaders;
       
    88 		}
       
    89     iFile.Close();
       
    90     iFsSession.Close(); // Close the file server session
       
    91     IRLOG_DEBUG( "CIRDataProvider::~CIRDataProvider() - Exiting" );
       
    92     }
       
    93 
       
    94 // General functions exported ( These are the API`s exposed )( HTTP )
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 //  CIRDataProvider::IRHttpIssueRequest(TDesC8& aUri)
       
    98 //  Used to issue an Http request
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 EXPORT_C void CIRDataProvider::IRHttpIssueRequestL(
       
   102      CIRHttpRequestData &aRequestObject )
       
   103     {
       
   104     IRHttpCancelRequest();
       
   105     IRLOG_DEBUG( "CIRDataProvider::IRHttpIssueRequestL - Entering" );
       
   106 	IRRDEBUG2("CIRDATAPROVIDER::IRHTTPISSUEREQUESTL",KNullDesC);
       
   107     CIRHttpResponseData* newResponseHeaders = new ( ELeave ) CIRHttpResponseData;
       
   108     delete iResponseHeaders;
       
   109     iResponseHeaders = newResponseHeaders;
       
   110 
       
   111 
       
   112     // Create or replace the file used to store xml response from iSDS
       
   113     User::LeaveIfError(iFile.Replace( iFsSession, iXmlFilePath, EFileWrite ));
       
   114     iHttpDataProvider->CancelTransaction();
       
   115 
       
   116     TInt err = iHttpDataProvider->IssueHttpRequestL( aRequestObject );
       
   117     // Cancel the timer if active
       
   118     iDataProviderTimer->Cancel();
       
   119     // Start the timer for timeout
       
   120     iDataProviderTimer->After( iTimeOut );
       
   121     if ( err == KErrCouldNotConnect )
       
   122         {
       
   123         // If error in IssueHttpRequest then close the open file
       
   124 	    iFile.Close();
       
   125 	    iDataProviderTimer->Cancel();
       
   126 	    iDataProviderObserver.IRHttpGeneralError( err );
       
   127 	    // Cancel any possibly pending transactions
       
   128 	    iHttpDataProvider->CancelTransaction();
       
   129         }
       
   130 
       
   131     IRLOG_DEBUG( "CIRDataProvider::IRHttpIssueRequestL - Exiting." );
       
   132     }
       
   133 
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 //  CIRDataProvider::IRHttpCancelRequest()
       
   137 //  Used to cancel a request
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C void CIRDataProvider::IRHttpCancelRequest()
       
   141     {
       
   142     IRLOG_DEBUG( "CIRDataProvider::IRHttpCancelRequest - Entering" );
       
   143     // Cancel the timer if active
       
   144     iDataProviderTimer->Cancel();
       
   145     // Cancel any possibly pending transactions
       
   146     iHttpDataProvider->CancelTransaction();
       
   147     // Close the file handle used to store the xml response
       
   148     iFile.Close();
       
   149     IRLOG_DEBUG( "CIRDataProvider::IRHttpCancelRequest - Exiting." );
       
   150     }
       
   151 
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 //  CIRDataProvider::ReleaseResources()
       
   155 //  Used to release the resources held by the IRHttpDataProvider
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C void CIRDataProvider::ReleaseResources()
       
   159 	{
       
   160 	IRLOG_DEBUG( "CIRDataProvider::ReleaseResources - Entering" );
       
   161 	// Release the resources held by the IRHttpDataProvider
       
   162 	iHttpDataProvider->ReleaseResources();
       
   163 	IRLOG_DEBUG( "CIRDataProvider::ReleaseResources - Exiting" );
       
   164 	}
       
   165 
       
   166 
       
   167 //These are the callback functions used by CIRHttpDataProvider to
       
   168 //provide the CIRDataProvider with the data after processing the
       
   169 //HTTP request.
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 //  CIRDataProvider::HttpEventComplete()
       
   173 //  Used to Indicate to the observer that the request event has completed
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CIRDataProvider::HttpEventComplete()
       
   177     {
       
   178     IRLOG_DEBUG( "CIRDataProvider::HttpEventComplete - Entering" );
       
   179     iFile.Close();
       
   180     iDataProviderTimer->Cancel();
       
   181 
       
   182     // Need to take a member to a local variable, as the IRHttpDataReceived may initiate
       
   183     // an IRHttpIssueRequestL() call, causing the headers to be replaced with empty ones, and
       
   184     // causing crashes.
       
   185     CIRHttpResponseData* currentHeaders = iResponseHeaders;
       
   186     iResponseHeaders = NULL; // prevents the destructor to delete in case something streange happens.
       
   187     iDataProviderObserver.IRHttpDataReceived( iXmlFilePath,*currentHeaders );
       
   188 
       
   189     delete currentHeaders;
       
   190     IRLOG_DEBUG( "CIRDataProvider::HttpEventComplete - Exiting." );
       
   191     }
       
   192 
       
   193 
       
   194 void CIRDataProvider::ExtractHeaderValue(const TDesC8& aHeaderData,const
       
   195 	TDesC8& aHeaderName,const TDesC8& aDelimeter,TDes8& aHolder) const
       
   196 	{
       
   197 	IRLOG_DEBUG( "CIRDataProvider::ExtractHeaderValue - Entering" );
       
   198 	TInt position = aHeaderData.Find(aHeaderName);
       
   199 	if( position >= 0)
       
   200 		{
       
   201 		TPtrC8 headerValue = aHeaderData.Mid(position);
       
   202 		TInt delimeterPosition = headerValue.Find(aDelimeter);
       
   203 		if( delimeterPosition != KErrNotFound )
       
   204 			{
       
   205 			delimeterPosition++;
       
   206 			TPtrC8 value = headerValue.Mid(delimeterPosition);
       
   207 			aHolder.Copy(value);
       
   208 			aHolder.TrimAll();
       
   209 			}
       
   210 		}
       
   211 	IRLOG_DEBUG( "CIRDataProvider::ExtractHeaderValue - Exiting." );
       
   212 	}
       
   213 
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 //  CIRDataProvider::HttpHeaderReceived( const TDesC8& aHeaderData )
       
   217 //  Used by CIRHttpDataProvider to indicate that an HTTP header is received.
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 void CIRDataProvider::HttpHeaderReceived( const TDesC8& aHeaderData )
       
   221     {
       
   222     IRLOG_DEBUG( "CIRDataProvider::HttpHeaderReceived - Entering." );
       
   223 	_LIT8(KDelim,":");
       
   224 	_LIT8(KContentType,"Content-Type");
       
   225 	ExtractHeaderValue(aHeaderData,KContentType,KDelim,iResponseHeaders->
       
   226 		iContentType);
       
   227 	_LIT8(KMaxAge,"max-age");
       
   228 	_LIT8(KDelimEqual,"=");
       
   229 	ExtractHeaderValue(aHeaderData,KMaxAge,KDelimEqual,iResponseHeaders->
       
   230 		iMaxAge);
       
   231 	_LIT8(KContentLength,"Content-Length");
       
   232 	ExtractHeaderValue(aHeaderData,KContentLength,KDelim,iResponseHeaders->
       
   233 		iContentLength);
       
   234 	_LIT8(KExpires,"Expires");
       
   235 	ExtractHeaderValue(aHeaderData,KExpires,KDelim,iResponseHeaders->iExpires);
       
   236 	IRLOG_DEBUG( "CIRDataProvider::HttpHeaderReceived - Exiting." );
       
   237     }
       
   238 
       
   239 
       
   240 void CIRDataProvider::HttpDateHeaderReceived(const TDesC8 &aHeader,
       
   241 	const TTime& aTime )
       
   242 	{
       
   243 	IRLOG_DEBUG( "CIRDataProvider::HttpDateHeaderReceived - Entering." );
       
   244 	_LIT8(KDate,"Date");
       
   245 	_LIT8(KLastModified,"Last-Modified");
       
   246 	TInt position = aHeader.Find(KDate);
       
   247 	if( position != KErrNotFound )
       
   248 		{
       
   249 		iResponseHeaders->iDate = aTime;
       
   250 		//find the difference between device time and response time
       
   251 		//and storing the offset
       
   252 		SetOffsetSeconds( aTime );
       
   253 		return ;
       
   254 		}
       
   255 	position = aHeader.Find(KLastModified);
       
   256 	if( position != KErrNotFound )
       
   257 		{
       
   258 		iResponseHeaders->iLastModified = aTime;
       
   259 		}
       
   260 	IRLOG_DEBUG( "CIRDataProvider::HttpDateHeaderReceived - Exiting." );
       
   261 	}
       
   262 
       
   263 
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 //  CIRDataProvider::HttpBodyReceived( const TDesC8& aBodyData )
       
   267 //  Used by CIRHttpDataProvider to indicate that an HTTP response body
       
   268 //  is received.
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CIRDataProvider::HttpBodyReceived( const TDesC8 &aBodyData )
       
   272     {
       
   273     IRLOG_DEBUG( "CIRDataProvider::HttpBodyReceived - Entering" );
       
   274     TInt FileWritePos = 0;
       
   275     iFile.Seek( ESeekEnd, FileWritePos );
       
   276     iFile.Write( FileWritePos, aBodyData );
       
   277     IRLOG_DEBUG( "CIRDataProvider::HttpBodyReceived - Exiting." );
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 //  CIRDataProvider::HttpTransactionError(TInt aErrCode)
       
   282 //  Used by CIRHttpDataProvider to indicate that a HTTP Transaction error
       
   283 //  has occured.
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CIRDataProvider::HttpTransactionError( TInt aErrCode )
       
   287     {
       
   288     IRLOG_DEBUG( "CIRDataProvider::HttpTransactionError - Entering" );
       
   289     iFile.Close();
       
   290     iDataProviderTimer->Cancel();
       
   291     iDataProviderObserver.IRHttpGeneralError( aErrCode );
       
   292     // Cancel any possibly pending transactions
       
   293     iHttpDataProvider->CancelTransaction();
       
   294     IRLOG_DEBUG( "CIRDataProvider::HttpTransactionError - Exiting." );
       
   295     }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 //  CIRDataProvider::HttpResponseCodeRecieved(TInt aResponseCode)
       
   299 //  Used by CIRHttpDataProvider to indicate to the iSDS Client that a
       
   300 //  304 Not Changed response received
       
   301 //  Note: Implemented in version 0.2
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CIRDataProvider::HttpResponseCodeRecieved( TInt aResponseCode )
       
   305     {
       
   306     IRLOG_DEBUG( "CIRDataProvider::HttpResponseCodeRecieved - Entering" );
       
   307     iDataProviderObserver.IRHttpResponseCodeReceived( aResponseCode, *iResponseHeaders );
       
   308     IRLOG_DEBUG( "CIRDataProvider::HttpResponseCodeRecieved- Exiting" );
       
   309     }
       
   310 
       
   311 // constructor support
       
   312 // don't export these, because used only by functions in this DLL
       
   313 // ---------------------------------------------------------------------------
       
   314 //  CIRDataProvider::CIRDataProvider(MIRDataProviderObserver& aObserver):
       
   315 //  iDataProviderObserver(aObserver)
       
   316 //  Default Constructor
       
   317 // ---------------------------------------------------------------------------
       
   318 //
       
   319 void CIRDataProvider::TimerExpired()
       
   320     {
       
   321     IRLOG_INFO( "CIRDataProvider::TimerExpired - Entering" );
       
   322     IRHttpCancelRequest();
       
   323     HttpTransactionError( KDataProviderTimeout );
       
   324     IRLOG_INFO( "CIRDataProvider::TimerExpired - Exiting" );
       
   325     }
       
   326 
       
   327 
       
   328 
       
   329 CIRDataProvider::CIRDataProvider( MIRDataProviderObserver &aObserver ):
       
   330     iDataProviderObserver( aObserver ) // first-phase C++ constructor
       
   331     {
       
   332     IRLOG_INFO( "CIRDataProvider::CIRDataProvider" );
       
   333     // Definition not required
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 //  void CIRDataProvider::ConstructL()
       
   338 //  2nd Phase construction
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CIRDataProvider::ConstructL() // second-phase constructor
       
   342     {
       
   343     IRLOG_DEBUG( "CIRDataProvider::ConstructL - Entering" );
       
   344     _LIT( KXmlFile, "iSdsResponse.xml" );
       
   345     ConstructL(KXmlFile);
       
   346     iHttpDataProvider->iSetNonUAProfUserAgent = EFalse;
       
   347     IRLOG_DEBUG( "CIRDataProvider::ConstructL - Exiting" );
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 //  void CIRDataProvider::ConstructL(TDesC& aFilePath)
       
   352 //  2nd Phase construction
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 void CIRDataProvider::ConstructL( const TDesC &aFileName )
       
   356     {
       
   357     IRLOG_DEBUG( "CIRDataProvider::ConstructL(const TDesC &aFileName) - Entering" );
       
   358     iHttpDataProvider = CIRHttpDataProvider::NewL( *this );
       
   359     iDataProviderTimer = CIRDataProviderTimer::NewL( EPriorityHigh,  *this );
       
   360     User::LeaveIfError(iFsSession.Connect());
       
   361     iIRSettings = CIRSettings::OpenL();
       
   362     iXmlFilePath = iIRSettings->PrivatePath();
       
   363     iXmlFilePath.Append( aFileName );
       
   364     iTimeOut = iIRSettings->GetTimeOut();
       
   365     iHttpDataProvider->iSetNonUAProfUserAgent = ETrue;
       
   366     IRLOG_DEBUG( "CIRDataProvider::ConstructL(const TDesC &aFileName) - Exiting." );
       
   367     }
       
   368 
       
   369 
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 //  CIRDataProvider::NewLC(MIRDataProviderObserver& aObserver)
       
   373 //  Creates instance of CIRDataProvider.
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 CIRDataProvider *CIRDataProvider::NewLC( MIRDataProviderObserver &aObserver )
       
   377     {
       
   378     IRLOG_DEBUG( "CIRDataProvider::NewLC - Entering." );
       
   379     CIRDataProvider *self = new( ELeave )CIRDataProvider( aObserver );
       
   380     CleanupStack::PushL( self );
       
   381     self->ConstructL();
       
   382     IRLOG_DEBUG( "CIRDataProvider::NewLC - Exiting." );
       
   383     return self;
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 //  CIRDataProvider::NewLC(MIRDataProviderObserver& aObserver)
       
   388 //  Creates instance of CIRDataProvider.
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 CIRDataProvider *CIRDataProvider::NewLC( MIRDataProviderObserver &aObserver,
       
   392     const TDesC &aFileName )
       
   393     {
       
   394     IRLOG_DEBUG( "CIRDataProvider::NewLC - Entering." );
       
   395     CIRDataProvider *self = new( ELeave )CIRDataProvider( aObserver );
       
   396     CleanupStack::PushL( self );
       
   397     self->ConstructL( aFileName );
       
   398     IRLOG_DEBUG( "CIRDataProvider::NewLC - Exiting." );
       
   399     return self;
       
   400     }
       
   401 
       
   402 EXPORT_C CIRHttpDataProvider* CIRDataProvider::GetHttpDataProvider()
       
   403 {
       
   404 IRLOG_DEBUG( "CIRDataProvider::GetHttpDataProvider" );
       
   405 	return iHttpDataProvider;
       
   406 }
       
   407 
       
   408 // ---------------------------------------------------------------------------
       
   409 //  CIRDataProvider::SetOffsetSeconds( const TTime& aTime )
       
   410 //  Stores the offset between device time and response header in settings
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void CIRDataProvider::SetOffsetSeconds( const TTime& aTime )
       
   414 	{
       
   415 	IRLOG_DEBUG( "CIRDataProvider::SetOffsetSeconds - Entering" );
       
   416 	TTime currenttime;
       
   417 	//calculates the current time
       
   418 	currenttime.UniversalTime();
       
   419 	//finds offset from isds response
       
   420 	TTimeIntervalSeconds offsetseconds;
       
   421 	//Find offset from isds response
       
   422 	TInt err = currenttime.SecondsFrom(aTime,offsetseconds);
       
   423 	if( err )
       
   424 		{
       
   425 		//if error offsetseconds is set to zero
       
   426 		offsetseconds = 0;
       
   427 		}
       
   428 	//storing the offset value in setting
       
   429 	TRAP_IGNORE( iIRSettings->SetTimeCorrectionL(offsetseconds.Int()) )
       
   430 	IRLOG_DEBUG( "CIRDataProvider::SetOffsetSeconds - Exiting." );
       
   431 	}
       
   432 
       
   433