upnpmediaserver/mediaserverengine/src/upnpiconmanager.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file
       
     2 * Copyright (c) 2008-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:  Media Server icon's manager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "upnpiconmanager.h"
       
    22 #include "upnpicon.h"
       
    23 #include "upnpmediaserverglobals.h"
       
    24 #include "upnpcons.h"
       
    25 #include "upnpdeviceimplementation.h"
       
    26 #include "upnpstring.h"
       
    27 #include "upnpmediaserverdescriptionprovider.h"
       
    28 #include <upnpdevicedescriptionstore.h>
       
    29 
       
    30 _LIT8( KIconDir, "/mediaserver1/icon/") ;
       
    31 
       
    32 _LIT( KPathC, "C:" );
       
    33 _LIT( KFileSpec,"*" );
       
    34 
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CUpnpIconManager::NewL
       
    38 // Two-phased constructor.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CUpnpIconManager* CUpnpIconManager::NewL( CUpnpDeviceDescriptionStore& aDescriptionStore,
       
    42                               CUpnpMediaServerDescriptionProvider& aDescriptionProvider )
       
    43     {
       
    44     CUpnpIconManager* self = CUpnpIconManager::NewLC( aDescriptionStore, 
       
    45                                                       aDescriptionProvider );
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CUpnpIconManager::NewLC
       
    52 // Two-phased constructor.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CUpnpIconManager* CUpnpIconManager::NewLC( CUpnpDeviceDescriptionStore& aDescriptionStore,
       
    56                                CUpnpMediaServerDescriptionProvider& aDescriptionProvider )
       
    57     {
       
    58     CUpnpIconManager* self= new (ELeave) CUpnpIconManager( aDescriptionStore, 
       
    59                                                            aDescriptionProvider );
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CUpnpIconManager::~CUpnpIconManager
       
    67 // Destructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CUpnpIconManager::~CUpnpIconManager()
       
    71     {
       
    72     iFileSession.Close();
       
    73     iIconList.ResetAndDestroy();
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CUpnpIconManager::AddIconL
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CUpnpIconManager::AddIconL( const TDesC8& aIconDes )
       
    81     {
       
    82     CUpnpIcon* icon = CUpnpIcon::NewL();
       
    83     CleanupStack::PushL( icon );
       
    84     
       
    85     RDesReadStream stream( aIconDes );
       
    86     CleanupClosePushL( stream );
       
    87     stream >> *icon;
       
    88     
       
    89     TPtrC8 iconUrl;
       
    90     TPtrC8 iconName8;
       
    91     TInt iconNameTemp;
       
    92     TInt iconNameInt(0);
       
    93     
       
    94     if ( iIconList.Count() != 0 )
       
    95         {
       
    96         for ( TInt i(0); i < iIconList.Count(); i++ )
       
    97             {
       
    98             iconUrl.Set( iIconList[ i ]->Url() );
       
    99             iconName8.Set( iconUrl.Mid( KIconDir().Length() ) );
       
   100 
       
   101             TLex8 lexer( iconName8 );
       
   102             lexer.Val( iconNameTemp );
       
   103 
       
   104             if ( iconNameInt < iconNameTemp )
       
   105                 iconNameInt = iconNameTemp;
       
   106             }
       
   107         iconNameInt++;
       
   108         }
       
   109     
       
   110     TBufC<UpnpString::KMaxTUintLength> iconNameBuff;
       
   111     iconNameBuff.Des().Num( iconNameInt );
       
   112     
       
   113     HBufC8* iconUrl8 = HBufC8::NewLC( KIconDir().Length() + iconNameBuff.Length() );
       
   114     iconUrl8->Des().Append( KIconDir() );
       
   115     iconUrl8->Des().Append( iconNameBuff );
       
   116 
       
   117     icon->SetUrlL( *iconUrl8 );
       
   118 
       
   119     HBufC* iconPath = GetIconPathLC();
       
   120 
       
   121     HBufC16* destinationPath = HBufC16::NewLC( iconPath->Length() + UpnpString::KMaxTUintLength );
       
   122     destinationPath->Des().Append( *iconPath );
       
   123     destinationPath->Des().Append( iconNameBuff );
       
   124  
       
   125     CopyIconToPrivateL( icon , *destinationPath );
       
   126     iDescriptionStore.AddIconL( *icon );
       
   127     iIconList.AppendL( icon );
       
   128 
       
   129     iDescriptionProvider.AddUriToFileMapL( *iconUrl8, *destinationPath );
       
   130         
       
   131     CleanupStack::PopAndDestroy( destinationPath );
       
   132     CleanupStack::PopAndDestroy( iconPath );
       
   133     CleanupStack::PopAndDestroy( iconUrl8 );
       
   134     CleanupStack::PopAndDestroy( &stream );
       
   135     CleanupStack::Pop( icon );
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpnpIconManager::UpdateIconL
       
   140 // -----------------------------------------------------------------------------
       
   141 //    
       
   142 void CUpnpIconManager::UpdateIconL( const TDesC8& aNewIconDes, const TDesC8& aUrl )
       
   143     {
       
   144     HBufC16* url = UpnpString::ToUnicodeL( aUrl );
       
   145     CleanupStack::PushL( url );
       
   146 
       
   147     HBufC16* iconName = GetIconNameLC( aUrl );
       
   148 
       
   149     HBufC8* iconUrl = HBufC8::NewLC( KIconDir().Length() + iconName->Length() );
       
   150     iconUrl->Des().Append( KIconDir() );
       
   151     iconUrl->Des().Append( *iconName );
       
   152 
       
   153     CUpnpIcon* icon = CUpnpIcon::NewL();
       
   154     CleanupStack::PushL( icon );
       
   155 
       
   156     RDesReadStream stream( aNewIconDes );
       
   157     CleanupClosePushL( stream );
       
   158     stream >> *icon;
       
   159     icon->SetUrlL( *iconUrl );
       
   160     
       
   161     User::LeaveIfError( FindIcon( *iconUrl ) );
       
   162 
       
   163     HBufC* iconPath = GetIconPathLC();
       
   164 
       
   165     HBufC16* destinationPath = HBufC16::NewLC( iconPath->Length() + 
       
   166                                                iconName->Length() );
       
   167     destinationPath->Des().Append( *iconPath );
       
   168     destinationPath->Des().Append( *iconName );
       
   169    
       
   170     HBufC* backupName = PrepareIconToRollbackLC( *destinationPath );
       
   171     
       
   172     TRAPD( err , DoUpdateIconL( icon , *destinationPath ) );
       
   173     if ( err )
       
   174         {
       
   175         TUint att;
       
   176         if( ! iFileSession.Att( *backupName , att ) )
       
   177             {
       
   178             iFileSession.Delete( *destinationPath );
       
   179             iFileSession.Rename( *backupName , *destinationPath );         
       
   180             }
       
   181         else
       
   182             {
       
   183             RestoreFromOrginalPathL( aUrl );
       
   184             }
       
   185         DeleteIconFile( *backupName );
       
   186         User::Leave( err );
       
   187         }
       
   188     
       
   189     DeleteIconFile( *backupName );
       
   190     
       
   191     CleanupStack::PopAndDestroy( backupName );
       
   192     CleanupStack::PopAndDestroy( destinationPath );
       
   193     CleanupStack::PopAndDestroy( iconPath );
       
   194     CleanupStack::PopAndDestroy( &stream );
       
   195     CleanupStack::Pop( icon );
       
   196     CleanupStack::PopAndDestroy( iconUrl );
       
   197     CleanupStack::PopAndDestroy( iconName );
       
   198     CleanupStack::PopAndDestroy( url );
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CUpnpIconManager::RemoveIconL
       
   203 // -----------------------------------------------------------------------------
       
   204 //  
       
   205 void CUpnpIconManager::RemoveIconL( const TDesC8& aUrl )
       
   206     {
       
   207     HBufC16* url = UpnpString::ToUnicodeL( aUrl );
       
   208     CleanupStack::PushL( url );
       
   209 
       
   210     HBufC16* iconName16 = GetIconNameLC( aUrl );    
       
   211 
       
   212     HBufC8* iconName8 = UpnpString::FromUnicodeL( *iconName16 );
       
   213     CleanupStack::PushL( iconName8 );
       
   214 
       
   215     HBufC8* iconUrl = HBufC8::NewLC( KIconDir().Length() 
       
   216                                     + iconName8->Length() );  
       
   217     iconUrl->Des().Append( KIconDir() );
       
   218     iconUrl->Des().Append( *iconName8 );
       
   219 
       
   220     HBufC* iconDirectory = GetIconPathLC();
       
   221     HBufC* iconFullPath = HBufC::NewLC( iconDirectory->Length() 
       
   222                                         + iconName16->Length() );
       
   223     iconFullPath->Des().Append( *iconDirectory );
       
   224     iconFullPath->Des().Append( *iconName16 );
       
   225       
       
   226     TInt iconIdx = FindIcon( *iconUrl );
       
   227     User::LeaveIfError( iconIdx );
       
   228     
       
   229     User::LeaveIfError( DeleteIconFile( *iconFullPath ) );
       
   230     
       
   231     TRAPD( error, iDescriptionStore.RemoveIconL( *iconUrl ) );
       
   232     if ( error != KErrNotFound )
       
   233         {
       
   234         User::LeaveIfError( error );  
       
   235         }
       
   236     delete iIconList[ iconIdx ];
       
   237     iIconList.Remove( iconIdx );
       
   238     
       
   239     iDescriptionProvider.RemoveUriToFileMapL( *iconUrl );
       
   240     
       
   241     CleanupStack::PopAndDestroy( iconFullPath );
       
   242     CleanupStack::PopAndDestroy( iconDirectory );
       
   243     CleanupStack::PopAndDestroy( iconUrl ); 
       
   244     CleanupStack::PopAndDestroy( iconName8 );
       
   245     CleanupStack::PopAndDestroy( iconName16 );    
       
   246     CleanupStack::PopAndDestroy( url );      
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CUpnpIconManager::RemoveIconsL
       
   251 // -----------------------------------------------------------------------------
       
   252 //      
       
   253 void CUpnpIconManager::RemoveIconsL()
       
   254     {
       
   255     for ( TInt i = 0; i < iIconList.Count(); i++ )
       
   256         {
       
   257         iDescriptionProvider.RemoveUriToFileMapL( iIconList[ i ]->Url() );
       
   258         }
       
   259 
       
   260     TRAPD( error, iDescriptionStore.RemoveIconsL() );
       
   261     if ( error != KErrNotFound )
       
   262         {
       
   263         User::LeaveIfError( error );  
       
   264         }
       
   265     iIconList.ResetAndDestroy();
       
   266 
       
   267     HBufC* iconPath = GetIconPathLC();
       
   268     
       
   269     HBufC* fileSpec = HBufC::NewLC( iconPath->Length() + KFileSpec().Length() );
       
   270     fileSpec->Des().Append( *iconPath );
       
   271     fileSpec->Des().Append( KFileSpec() );
       
   272 
       
   273     CDir* dirList;
       
   274     User::LeaveIfError( iFileSession.GetDir( *fileSpec , 
       
   275                                              KEntryAttMaskSupported, 
       
   276                                              ESortByName, dirList) );
       
   277     CleanupStack::PushL( dirList );    
       
   278 
       
   279     TBuf<UpnpString::KMaxTUintLength> fileName;
       
   280     for ( TInt i(0); i < dirList->Count(); i++)
       
   281         {
       
   282         fileName = (*dirList)[i].iName;
       
   283 
       
   284         HBufC* totalPath = HBufC::NewLC( iconPath->Length() + fileName.Length() );
       
   285         totalPath->Des().Append( *iconPath );
       
   286         totalPath->Des().Append( fileName );
       
   287 
       
   288         DeleteIconFile( *totalPath );
       
   289 
       
   290         CleanupStack::PopAndDestroy( totalPath );
       
   291         }
       
   292 
       
   293     CleanupStack::PopAndDestroy( dirList );
       
   294     CleanupStack::PopAndDestroy( fileSpec );
       
   295     CleanupStack::PopAndDestroy( iconPath );
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CUpnpIconManager::GetIconPathLC
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 HBufC* CUpnpIconManager::GetIconPathLC()
       
   303     {
       
   304     TFileName privatePath;
       
   305     iFileSession.PrivatePath( privatePath );
       
   306 
       
   307     HBufC* iconPath = HBufC::NewLC( KPathC().Length() + privatePath.Length() 
       
   308                                   + KIconDest().Length());
       
   309     iconPath->Des().Append( KPathC() );
       
   310     iconPath->Des().Append( privatePath );
       
   311     iconPath->Des().Append( KIconDest() );
       
   312 
       
   313     return iconPath;
       
   314     }
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CUpnpMediaServer::GetIconNameLC
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 HBufC* CUpnpIconManager::GetIconNameLC( const TDesC8& aPath )
       
   321     {
       
   322     _LIT16( KIcon, "icon" );
       
   323     const TInt KSlashesOffset( 1 );
       
   324     
       
   325     HBufC* iconPath = HBufC::NewLC( aPath.Length() );
       
   326     iconPath->Des().Copy( aPath );
       
   327     
       
   328     iconPath->Des() = iconPath->Mid( 
       
   329                    User::LeaveIfError( iconPath->FindF( KIcon() ) ) 
       
   330                                      + KIcon().Length()
       
   331                                      + KSlashesOffset );
       
   332     
       
   333     HBufC* iconName = HBufC::NewL( iconPath->Length() );
       
   334     iconName->Des().Append( *iconPath );
       
   335 
       
   336     CleanupStack::PopAndDestroy( iconPath );
       
   337     CleanupStack::PushL( iconName );
       
   338     
       
   339     TLex iconLex( *iconName );
       
   340     TInt iconNum;
       
   341     TInt err( iconLex.Val( iconNum ) );
       
   342     if ( err )
       
   343         {
       
   344         User::Leave( KErrNotFound );
       
   345         }
       
   346         
       
   347     return iconName;
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CUpnpIconManager::CopyIconToPrivateL
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 void CUpnpIconManager::CopyIconToPrivateL( const CUpnpIcon* aIcon , 
       
   355                                            const TDesC& aDestinationPath 
       
   356                                          )
       
   357     {   
       
   358     CFileMan* fM = CFileMan::NewL( iFileSession );
       
   359     CleanupStack::PushL( fM );
       
   360 
       
   361     TInt err( fM->Copy( aIcon->BitmapFilename(), aDestinationPath ) );
       
   362     if( err == KErrAccessDenied )
       
   363         {
       
   364         fM->Attribs( aDestinationPath ,KEntryAttNormal , KEntryAttReadOnly|
       
   365                                                          KEntryAttHidden|
       
   366                                                          KEntryAttSystem , 0 );
       
   367         User::LeaveIfError( fM->Copy( aIcon->BitmapFilename(), aDestinationPath ) );
       
   368         }
       
   369     else
       
   370         {
       
   371         User::LeaveIfError( err );
       
   372         }
       
   373     User::LeaveIfError( fM->Attribs( aDestinationPath , 
       
   374                                      KEntryAttNormal , KEntryAttReadOnly|
       
   375                                                        KEntryAttHidden|
       
   376                                                        KEntryAttSystem , 0 ) );
       
   377        
       
   378     CleanupStack::PopAndDestroy( fM );    
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CUpnpMediaServer::PrepareIconToRollbackLC
       
   383 // -----------------------------------------------------------------------------
       
   384 //   
       
   385 HBufC* CUpnpIconManager::PrepareIconToRollbackLC( const TDesC& aPath )
       
   386     {
       
   387     _LIT( KBeckupExt, ".beckup" );
       
   388     HBufC* beckupName = HBufC::NewLC( aPath.Length() + KBeckupExt().Length() );//on cleanupstack
       
   389     beckupName->Des().Append( aPath );
       
   390     beckupName->Des().Append( KBeckupExt() );
       
   391     
       
   392     iFileSession.Delete( *beckupName );
       
   393     
       
   394     iFileSession.Rename( aPath , *beckupName );
       
   395     
       
   396     return beckupName;
       
   397     }
       
   398       
       
   399 // -----------------------------------------------------------------------------
       
   400 // CUpnpMediaServer::DoUpdateIconL
       
   401 // -----------------------------------------------------------------------------
       
   402 //      
       
   403 void CUpnpIconManager::DoUpdateIconL( CUpnpIcon* aIcon, const TDesC& aDestinationPath )
       
   404     {
       
   405     CopyIconToPrivateL( aIcon , aDestinationPath );
       
   406 
       
   407     iDescriptionStore.UpdateIconL( *aIcon );
       
   408     TInt idx = FindIcon( aIcon->Url() );
       
   409     if ( idx > KErrNotFound )
       
   410         {
       
   411         delete iIconList[ idx ];
       
   412         iIconList.Remove( idx );
       
   413         iIconList.AppendL( aIcon );
       
   414         }
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // CUpnpMediaServer::DeleteIconFile
       
   419 // -----------------------------------------------------------------------------
       
   420 //  
       
   421 TInt CUpnpIconManager::DeleteIconFile( const TDesC& aPath )
       
   422     {
       
   423     TInt err( iFileSession.Delete( aPath ) );  
       
   424     if( err == KErrAccessDenied )
       
   425         {
       
   426         iFileSession.SetAtt( aPath ,KEntryAttNormal , KEntryAttReadOnly|
       
   427                                                      KEntryAttHidden|
       
   428                                                      KEntryAttSystem );
       
   429         err = iFileSession.Delete( aPath );
       
   430         }
       
   431     else if ( err == KErrNotFound )
       
   432         {
       
   433         err = KErrNone;
       
   434         }
       
   435     return err;
       
   436     }
       
   437 
       
   438 // -----------------------------------------------------------------------------
       
   439 // CUpnpMediaServer::RestoreFromOrginalPathL
       
   440 // -----------------------------------------------------------------------------
       
   441 //  
       
   442 void CUpnpIconManager::RestoreFromOrginalPathL( const TDesC8& aUrl )
       
   443     {
       
   444     for ( TInt i(0) ; i< iIconList.Count() ; i++ )
       
   445         {
       
   446         if ( !iIconList[ i ]->Url().CompareF( aUrl ) )
       
   447             {
       
   448             HBufC* iconPath = GetIconPathLC();
       
   449             HBufC* iconName = GetIconNameLC( aUrl );
       
   450             HBufC* destinationPath = HBufC16::NewLC( iconPath->Length() +
       
   451                                                      UpnpString::KMaxTUintLength );
       
   452             destinationPath->Des().Append( *iconPath );
       
   453             destinationPath->Des().Append( *iconName );
       
   454             
       
   455             CopyIconToPrivateL( iIconList[ i ] , *destinationPath );
       
   456             
       
   457             CleanupStack::PopAndDestroy( destinationPath );
       
   458             CleanupStack::PopAndDestroy( iconName );
       
   459             CleanupStack::PopAndDestroy( iconPath );
       
   460             return;
       
   461             }
       
   462         }
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // CUpnpMediaServer::FindIcon
       
   467 // -----------------------------------------------------------------------------
       
   468 // 
       
   469 TInt CUpnpIconManager::FindIcon( const TDesC8& aUrl )
       
   470     {
       
   471     for ( TInt i(0) ; i< iIconList.Count() ; i++ )
       
   472         {
       
   473         if ( !iIconList[i]->Url().CompareF( aUrl ) )
       
   474             {
       
   475             return i;
       
   476             }
       
   477         }
       
   478 
       
   479     return KErrNotFound;
       
   480     }
       
   481 
       
   482 // -----------------------------------------------------------------------------
       
   483 // CUpnpIconManager::AddIconsToDescriptionPropoertyMapL
       
   484 // -----------------------------------------------------------------------------
       
   485 //
       
   486 void CUpnpIconManager::AddIconsToDescriptionPropertyMapL()
       
   487     {
       
   488     HBufC* iconPath = GetIconPathLC();
       
   489     for ( TInt i = 0; i < iIconList.Count(); i++ )
       
   490         {
       
   491         TPtrC8 url = iIconList[ i ]->Url();
       
   492         HBufC* iconName = GetIconNameLC( url );
       
   493         HBufC* path = HBufC::NewLC( iconPath->Length() + iconName->Length() );
       
   494         
       
   495         path->Des().Copy( iconPath->Des() );
       
   496         path->Des().Append( iconName->Des() );
       
   497         
       
   498         iDescriptionProvider.AddUriToFileMapL( url, path->Des() );
       
   499         
       
   500         CleanupStack::PopAndDestroy( path );
       
   501         CleanupStack::PopAndDestroy( iconName );
       
   502         }
       
   503     CleanupStack::PopAndDestroy( iconPath );
       
   504     }
       
   505 
       
   506 // -----------------------------------------------------------------------------
       
   507 // CUpnpIconManager::ConstructL
       
   508 // Two-phase constructor
       
   509 // -----------------------------------------------------------------------------
       
   510 //
       
   511 void CUpnpIconManager::ConstructL()
       
   512     {
       
   513     User::LeaveIfError( iFileSession.Connect() );
       
   514     
       
   515     iIconList = iDescriptionStore.IconListL();
       
   516 
       
   517     AddIconsToDescriptionPropertyMapL();
       
   518     }
       
   519 
       
   520 // -----------------------------------------------------------------------------
       
   521 // CUpnpIconManager::CUpnpIconManager
       
   522 // Constructor
       
   523 // -----------------------------------------------------------------------------
       
   524 //    
       
   525 CUpnpIconManager::CUpnpIconManager( CUpnpDeviceDescriptionStore& aDescriptionStore,
       
   526                                     CUpnpMediaServerDescriptionProvider& aDescriptionProvider )
       
   527      : iDescriptionProvider( aDescriptionProvider ), iDescriptionStore( aDescriptionStore )
       
   528     {
       
   529     }
       
   530     
       
   531 // End of File