upnpavcontroller/upnpavcontrollerserver/src/upnpdeviceicondownloader.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     1 /*
       
     2 * Copyright (c) 2009 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include <httpdownloader.h>
       
    20 #include <e32property.h>
       
    21 #include <sysutil.h>
       
    22 #include "upnpdeviceicondownloader.h"
       
    23 #include "upnpconstantdefs.h"
       
    24 #include "upnpaverrorhandler.h"
       
    25 
       
    26 _LIT( KComponentLogfile, "upnpavcontrollerserver.txt");
       
    27 #include "upnplog.h"
       
    28 
       
    29 const TUint KBufferSize = 0x4000; // 16K
       
    30 const TInt KParallerTransfers = 1;
       
    31 _LIT( KIconFolder, "icons\\" );
       
    32 _LIT( KIconTemp, "temp" );
       
    33 _LIT( KIconExt, ".dat" );
       
    34 const TInt KCleanupNeededKey = KMaxTInt;
       
    35 const TInt KDefaultDiskSpaceNeeded = 0x8000; // 32K
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // FolderNameLC
       
    39 // --------------------------------------------------------------------------
       
    40 static HBufC* FolderNameLC( RFs& aFs )
       
    41     {
       
    42     HBufC* ret = HBufC::NewLC( KMaxPath );
       
    43     TPtr ptr( ret->Des() );
       
    44     aFs.PrivatePath( ptr );
       
    45     ptr.Insert( 0, TDriveUnit( RFs::GetSystemDrive() ).Name() );
       
    46     ptr.Append( KIconFolder );
       
    47     return ret;
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // FileNameLC
       
    52 // --------------------------------------------------------------------------
       
    53 static HBufC* FileNameLC( RFs& aFs, const TDesC8& aDeviceUuid )
       
    54     {
       
    55     HBufC* ret = FolderNameLC( aFs );
       
    56     HBufC* uuid = HBufC::NewLC( aDeviceUuid.Length() );
       
    57     uuid->Des().Copy( aDeviceUuid );
       
    58     TPtr ptr( ret->Des() );
       
    59     ptr.Append( *uuid );
       
    60     ptr.Append( KIconExt );
       
    61     CleanupStack::PopAndDestroy( uuid );
       
    62     return ret;
       
    63     }
       
    64 
       
    65 // --------------------------------------------------------------------------
       
    66 // TempFileNameLC
       
    67 // --------------------------------------------------------------------------
       
    68 static HBufC* TempFileNameLC( RFs& aFs )
       
    69     {
       
    70     HBufC* ret = FolderNameLC( aFs );
       
    71     TPtr ptr( ret->Des() );
       
    72     ptr.Append( KIconTemp );
       
    73     ptr.Append( KIconExt );
       
    74     return ret;
       
    75     }
       
    76 
       
    77 // --------------------------------------------------------------------------
       
    78 // CompleteFileL
       
    79 // --------------------------------------------------------------------------
       
    80 static void CompleteFileL( RFs& aFs, const TDesC8& aDeviceUuid )
       
    81     {
       
    82     HBufC* src = TempFileNameLC( aFs );
       
    83     HBufC* tgt = FileNameLC( aFs, aDeviceUuid );
       
    84     User::LeaveIfError( aFs.Replace( *src, *tgt ) );
       
    85     CleanupStack::PopAndDestroy( tgt );
       
    86     CleanupStack::PopAndDestroy( src );
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // PropertyCategory
       
    91 // --------------------------------------------------------------------------
       
    92 static TUid PropertyCategory()
       
    93     {
       
    94     RProcess process;
       
    95     TUid ret( TUid::Uid( process.SecureId().iId ) );
       
    96     process.Close();
       
    97     return ret;
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------
       
   101 // GetIntPropertyL
       
   102 // --------------------------------------------------------------------------
       
   103 static void GetIntPropertyL( const TUid& aCategory, TInt aKey, TInt& aValue )
       
   104     {
       
   105     TInt initValue( aValue );
       
   106     TInt err( RProperty::Get( aCategory, aKey, aValue ) );
       
   107     if ( err == KErrNotFound )
       
   108         {
       
   109         err = RProperty::Define( aCategory, aKey, RProperty::EInt );
       
   110         aValue = initValue;
       
   111         }
       
   112     __LOG3( "CUpnpDeviceIconDownloader - GetIntPropertyL - err: %d key: %d value: %d",
       
   113             err, aKey, aValue );
       
   114     User::LeaveIfError( err );
       
   115     }
       
   116 
       
   117 // ============================ MEMBER FUNCTIONS ============================
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CUpnpDeviceIconDownloader::NewL
       
   121 // --------------------------------------------------------------------------
       
   122 CUpnpDeviceIconDownloader* CUpnpDeviceIconDownloader::NewL(
       
   123         MUpnpDeviceIconDownloadObserver& aObserver, TUint aIap )
       
   124     {
       
   125     CUpnpDeviceIconDownloader* self = new ( ELeave ) CUpnpDeviceIconDownloader( 
       
   126             aObserver, aIap );
       
   127     CleanupStack::PushL( self );           
       
   128     self->ConstructL();
       
   129     CleanupStack::Pop();
       
   130     return self;
       
   131     }
       
   132 
       
   133 // --------------------------------------------------------------------------
       
   134 // CUpnpDeviceIconDownloader::~CUpnpDeviceIconDownloader
       
   135 // --------------------------------------------------------------------------
       
   136 CUpnpDeviceIconDownloader::~CUpnpDeviceIconDownloader()
       
   137     {
       
   138     iQueue.ResetAndDestroy();
       
   139     delete iDownloader;
       
   140     delete iCurrent;
       
   141     if ( iCleanupOnShutdown )
       
   142         {
       
   143         TRAP_IGNORE( CleanupFilesL() );
       
   144         }
       
   145     iFs.Close();
       
   146     }
       
   147 
       
   148 // --------------------------------------------------------------------------
       
   149 // CUpnpDeviceIconDownloader::TransferProgress
       
   150 // --------------------------------------------------------------------------
       
   151 void CUpnpDeviceIconDownloader::TransferProgress( TAny* /*aKey*/, TInt /*aBytes*/,
       
   152         TInt /*aTotalBytes*/ )
       
   153     {
       
   154     __LOG( "CUpnpDeviceIconDownloader::TransferProgress" );
       
   155     }
       
   156 
       
   157 // --------------------------------------------------------------------------
       
   158 // CUpnpDeviceIconDownloader::ReadyForTransferL
       
   159 // --------------------------------------------------------------------------
       
   160 void CUpnpDeviceIconDownloader::ReadyForTransferL( TAny* /*aKey*/ )
       
   161     {
       
   162     __LOG( "CUpnpDeviceIconDownloader::ReadyForTransferL" );
       
   163     if ( iCurrent )
       
   164         {
       
   165         iDownloader->StartTransferL( iCurrent );
       
   166         }
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CUpnpDeviceIconDownloader::TransferCompleted
       
   171 // --------------------------------------------------------------------------
       
   172 void CUpnpDeviceIconDownloader::TransferCompleted( TAny* /*aKey*/, TInt aStatus )
       
   173     {
       
   174     __LOG( "CUpnpDeviceIconDownloader::TransferCompleted" );
       
   175     if ( iCurrent )
       
   176         {
       
   177         TInt err( UPnPAVErrorHandler::ConvertToSymbianErrorCode( aStatus, EUPnPHTTPError ) );
       
   178         __LOG2( "CUpnpDeviceIconDownloader::TransferCompleted - Http: %d Err: %d",
       
   179                 aStatus, err );
       
   180         TPtrC8 uuid( iCurrent->DeviceUuid() );
       
   181         if ( err == KErrNone )
       
   182             {
       
   183             TRAP_IGNORE( CompleteFileL( iFs, uuid ) );
       
   184             }
       
   185         TRAP_IGNORE( iObserver.DeviceIconDownloadedL( uuid, err ) );
       
   186         delete iCurrent;
       
   187         iCurrent = NULL;
       
   188         }
       
   189     DownloadNext();
       
   190     }
       
   191 
       
   192 // --------------------------------------------------------------------------
       
   193 // CUpnpDeviceIconDownloader::StartDownloadL
       
   194 // --------------------------------------------------------------------------
       
   195 void CUpnpDeviceIconDownloader::StartDownloadL(
       
   196         const TDesC8& aDeviceUuid, const TDesC8& aDeviceIconUrl )
       
   197     {
       
   198     __LOG( "CUpnpDeviceIconDownloader::StartDownloadL" );
       
   199     CleanupFilesIfNeededL();
       
   200     if( !FileExistsL( aDeviceUuid ) && !InProgress( aDeviceUuid ) )
       
   201         {
       
   202         if ( !iDownloader )
       
   203             {
       
   204             iDownloader = CHttpDownloader::NewL( *this, iIap, KBufferSize,
       
   205                 KParallerTransfers );
       
   206             }
       
   207         CEntry* entry = CEntry::NewLC( aDeviceUuid, aDeviceIconUrl );
       
   208         iQueue.AppendL( entry );
       
   209         CleanupStack::Pop( entry );
       
   210         DownloadNext();
       
   211         }
       
   212     else
       
   213         {
       
   214         __LOG( "CUpnpDeviceIconDownloader::StartDownloadL - Skipped" );
       
   215         }
       
   216     }
       
   217 
       
   218 // --------------------------------------------------------------------------
       
   219 // CUpnpDeviceIconDownloader::CancelDownload
       
   220 // --------------------------------------------------------------------------
       
   221 void CUpnpDeviceIconDownloader::CancelDownload( const TDesC8& aDeviceUuid )
       
   222     {
       
   223     __LOG( "CUpnpDeviceIconDownloader::CancelDownload" );
       
   224     if ( iCurrent && !iCurrent->DeviceUuid().Compare( aDeviceUuid ) )
       
   225         {
       
   226         iDownloader->CancelTransfer( iCurrent );
       
   227         delete iCurrent;
       
   228         iCurrent = NULL;
       
   229         }
       
   230     else
       
   231         {
       
   232         TInt pos( iQueue.Find( aDeviceUuid, CEntry::Compare ) );
       
   233         if ( pos >= 0 && pos < iQueue.Count() )
       
   234             {
       
   235             delete iQueue[ pos ];
       
   236             iQueue.Remove( pos );
       
   237             }
       
   238         }
       
   239     DownloadNext();
       
   240     }
       
   241 
       
   242 // --------------------------------------------------------------------------
       
   243 // CUpnpDeviceIconDownloader::TransferFileToClientL
       
   244 // --------------------------------------------------------------------------
       
   245 void CUpnpDeviceIconDownloader::TransferFileToClientL( const RMessage2& aMessage,
       
   246         TInt aSlot, const TDesC8& aDeviceUuid )
       
   247     {
       
   248     if ( InProgress( aDeviceUuid ) )
       
   249         {
       
   250         User::Leave( KErrNotReady );
       
   251         }
       
   252     HBufC* filename = FileNameLC( iFs, aDeviceUuid );
       
   253     __LOG1( "CUpnpDeviceIconDownloader::TransferFileToClientL - %S", filename );
       
   254     RFile file;
       
   255     CleanupClosePushL( file );
       
   256     User::LeaveIfError( file.Open( iFs, *filename, EFileShareReadersOnly | EFileRead ) );
       
   257     User::LeaveIfError( file.TransferToClient( aMessage, aSlot ) );
       
   258     CleanupStack::PopAndDestroy( &file );
       
   259     CleanupStack::PopAndDestroy( filename );
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // CUpnpDeviceIconDownloader::CEntry::NewLC
       
   264 // --------------------------------------------------------------------------
       
   265 CUpnpDeviceIconDownloader::CEntry* CUpnpDeviceIconDownloader::CEntry::NewLC(
       
   266         const TDesC8& aDeviceUuid, const TDesC8& aDeviceIconUrl )
       
   267     {
       
   268     CEntry* self = new ( ELeave ) CEntry();
       
   269     CleanupStack::PushL( self );
       
   270     self->ConstructL( aDeviceUuid, aDeviceIconUrl );
       
   271     return self;
       
   272     }
       
   273 
       
   274 // --------------------------------------------------------------------------
       
   275 // CUpnpDeviceIconDownloader::CEntry::~CEntry
       
   276 // --------------------------------------------------------------------------
       
   277 CUpnpDeviceIconDownloader::CEntry::~CEntry()
       
   278     {
       
   279     delete iDeviceUuid;
       
   280     delete iDeviceIconUrl;
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CUpnpDeviceIconDownloader::CEntry::Compare
       
   285 // --------------------------------------------------------------------------
       
   286 TInt CUpnpDeviceIconDownloader::CEntry::Compare( const TDesC8* aDeviceUuid,
       
   287         const CUpnpDeviceIconDownloader::CEntry& aEntry )
       
   288     {
       
   289     return !aEntry.iDeviceUuid->Compare( *aDeviceUuid );
       
   290     }
       
   291 
       
   292 // --------------------------------------------------------------------------
       
   293 // CUpnpDeviceIconDownloader::CEntry::ConstructL
       
   294 // --------------------------------------------------------------------------
       
   295 void CUpnpDeviceIconDownloader::CEntry::ConstructL( const TDesC8& aDeviceUuid,
       
   296         const TDesC8& aDeviceIconUrl )
       
   297     {
       
   298     iDeviceUuid = aDeviceUuid.AllocL();
       
   299     iDeviceIconUrl = aDeviceIconUrl.AllocL();
       
   300     }
       
   301 
       
   302 // --------------------------------------------------------------------------
       
   303 // CUpnpDeviceIconDownloader::CUpnpDeviceIconDownloader
       
   304 // --------------------------------------------------------------------------
       
   305 CUpnpDeviceIconDownloader::CUpnpDeviceIconDownloader( 
       
   306         MUpnpDeviceIconDownloadObserver& aObserver, TUint aIap ) :
       
   307     iObserver( aObserver ), iIap( aIap ) 
       
   308     {
       
   309     }
       
   310 
       
   311 // --------------------------------------------------------------------------
       
   312 // CUpnpDeviceIconDownloader::ConstructL
       
   313 // --------------------------------------------------------------------------
       
   314 void CUpnpDeviceIconDownloader::ConstructL()
       
   315     {
       
   316     __LOG1( "CUpnpDeviceIconDownloader::ConstructL - IAP %d", iIap );
       
   317     User::LeaveIfError( iFs.Connect() );
       
   318     User::LeaveIfError( iFs.ShareProtected() );
       
   319     }
       
   320 
       
   321 // --------------------------------------------------------------------------
       
   322 // CUpnpDeviceIconDownloader::DownloadNext
       
   323 // --------------------------------------------------------------------------
       
   324 void CUpnpDeviceIconDownloader::DownloadNext()
       
   325     {
       
   326     while( !iCurrent && iQueue.Count() > 0  )
       
   327         {
       
   328         iCurrent = iQueue[ 0 ];
       
   329         iQueue.Remove( 0 );
       
   330         TRAPD( err, DownloadNextL( *iCurrent ) );
       
   331         if ( err != KErrNone )
       
   332             {
       
   333             __LOG1( "CUpnpDeviceIconDownloader::DownloadNext - Error: %d", err );
       
   334             TRAP_IGNORE( iObserver.DeviceIconDownloadedL( iCurrent->DeviceUuid(), err ) );
       
   335             delete iCurrent;
       
   336             iCurrent = NULL;
       
   337             }
       
   338         }
       
   339     __LOG1( "CUpnpDeviceIconDownloader::DownloadNext - Waiting: %d", iQueue.Count() );
       
   340     }
       
   341 
       
   342 // --------------------------------------------------------------------------
       
   343 // CUpnpDeviceIconDownloader::DownloadNextL
       
   344 // --------------------------------------------------------------------------
       
   345 void CUpnpDeviceIconDownloader::DownloadNextL( CEntry& aNext )
       
   346     {
       
   347     CheckDiskSpaceL();
       
   348     HBufC* fileName = TempFileNameLC( iFs );
       
   349     iFs.Delete( *fileName ); // Delete previous, ignore error
       
   350     iFs.MkDirAll( *fileName ); // Ensure folder exists, ignore error
       
   351     iDownloader->DownloadFileL( aNext.DeviceIconUrl(), *fileName, &aNext );
       
   352     CleanupStack::PopAndDestroy( fileName );
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // CUpnpDeviceIconDownloader::CleanupFilesL
       
   357 // --------------------------------------------------------------------------
       
   358 
       
   359 void CUpnpDeviceIconDownloader::CleanupFilesL()
       
   360     {
       
   361     HBufC* folderName = FolderNameLC( iFs );    
       
   362     __LOG1( "CUpnpDeviceIconDownloader::CleanupFilesL - %S", folderName );
       
   363     CFileMan* fileMan = CFileMan::NewL( iFs );
       
   364     CleanupStack::PushL( fileMan );
       
   365     fileMan->RmDir( *folderName ); // Ignore error
       
   366     CleanupStack::PopAndDestroy( fileMan );
       
   367     CleanupStack::PopAndDestroy( folderName );
       
   368     }
       
   369 
       
   370 // --------------------------------------------------------------------------
       
   371 // CUpnpDeviceIconDownloader::FileExistsL
       
   372 // --------------------------------------------------------------------------
       
   373 TBool CUpnpDeviceIconDownloader::FileExistsL( const TDesC8& aDeviceUuid )
       
   374     {
       
   375     HBufC* filename = FileNameLC( iFs, aDeviceUuid );
       
   376     TEntry entry;
       
   377     TBool ret( iFs.Entry( *filename, entry ) == KErrNone );
       
   378     __LOG2( "CUpnpDeviceIconDownloader::FileExistsL - %S %d", filename, ret );
       
   379     CleanupStack::PopAndDestroy( filename );
       
   380     return ret;
       
   381     }
       
   382 
       
   383 // --------------------------------------------------------------------------
       
   384 // CUpnpDeviceIconDownloader::CleanupFilesIfNeededL
       
   385 // --------------------------------------------------------------------------
       
   386 void CUpnpDeviceIconDownloader::CleanupFilesIfNeededL()
       
   387     {
       
   388     TInt cleanupNeeded( ETrue );
       
   389     TUid category( PropertyCategory() );
       
   390     GetIntPropertyL( category, KCleanupNeededKey, cleanupNeeded );
       
   391     if ( cleanupNeeded )
       
   392         {
       
   393         CleanupFilesL();
       
   394         User::LeaveIfError( RProperty::Set( category, KCleanupNeededKey, EFalse ) );
       
   395         }
       
   396     }
       
   397 
       
   398 // --------------------------------------------------------------------------
       
   399 // CUpnpDeviceIconDownloader::CheckDiskSpaceL
       
   400 // --------------------------------------------------------------------------
       
   401 void CUpnpDeviceIconDownloader::CheckDiskSpaceL()
       
   402     {
       
   403     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, KDefaultDiskSpaceNeeded,
       
   404             RFs::GetSystemDrive() ) )
       
   405         {
       
   406         iCleanupOnShutdown = ETrue;
       
   407         User::Leave( KErrDiskFull );
       
   408         }
       
   409     }
       
   410 
       
   411 // --------------------------------------------------------------------------
       
   412 // CUpnpDeviceIconDownloader::InProgress
       
   413 // --------------------------------------------------------------------------
       
   414 TBool CUpnpDeviceIconDownloader::InProgress( const TDesC8& aDeviceUuid )
       
   415     {
       
   416     return ( ( iCurrent && !iCurrent->DeviceUuid().Compare( aDeviceUuid ) ) ||
       
   417              iQueue.Find( aDeviceUuid, CEntry::Compare ) != KErrNotFound );
       
   418     }
       
   419 
       
   420 // end of file