upnpmediaserver/contentdirectoryservice/src/upnpcontentdirectoryservice.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file
       
     2 * Copyright (c) 2005-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:  Content Directory factory class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpcontentdirectoryservice.h"
       
    21 #include "upnpcontentdirectory.h"   
       
    22 #include "upnpcddbfactory.h"
       
    23 #include "upnpcontentdirectoryglobals.h"
       
    24 #include "upnplocalstorage.h"
       
    25 #include "upnpmetadatastorage.h"
       
    26 #include "upnpitem.h"
       
    27 #include "upnpelement.h"
       
    28 #include "upnpattribute.h"
       
    29 #include "upnpcontentdirectoryeventobserver.h"
       
    30 #include "upnpsender.h"
       
    31 #include "upnpfiletransfer.h"
       
    32 #include "upnptransferhandler.h"
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CContentDirectoryService::CContentDirectoryService
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CUpnpContentDirectoryService::CUpnpContentDirectoryService()
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CUpnpContentDirectoryService::ConstructL
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CUpnpContentDirectoryService::ConstructL( TUpnpCdSettings aSettings )
       
    52     {
       
    53     // create offline part
       
    54     ConstructL( );
       
    55 
       
    56     if ( !aSettings.iTransfer ) //create default transfer handler
       
    57         {
       
    58         iTransferHandler = CUpnpTransferHandler::NewL( );
       
    59 
       
    60         // create content directory 
       
    61         iCd = CUpnpContentDirectory::NewL( aSettings, iMetadataStorage,
       
    62             iTransferHandler );
       
    63         iTransferHandler->SetObserver( iCd );
       
    64         }
       
    65     else //use external transfer handler  
       
    66         {
       
    67         iCd = CUpnpContentDirectory::NewL( aSettings, iMetadataStorage,
       
    68             aSettings.iTransfer );
       
    69         }
       
    70 
       
    71     iLocalStorage->SetStateHandler( iCd->StateHandler( ) );
       
    72     // sender    
       
    73     iSender = CUpnpCdsSender::NewL( iCd );
       
    74     iCd->SetSender( iSender );
       
    75     }
       
    76 // -----------------------------------------------------------------------------
       
    77 // CUpnpContentDirectoryService::ConstructL
       
    78 // Symbian 2nd phase constructor can leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CUpnpContentDirectoryService::ConstructL()
       
    82     {
       
    83     CUpnpCdDbFactory* cdf = CUpnpCdDbFactory::NewLC( );
       
    84 
       
    85     // database file name
       
    86     RFs fs;
       
    87     CleanupClosePushL( fs );
       
    88     User::LeaveIfError( fs.Connect( ) );
       
    89 
       
    90     TFileName path;
       
    91     User::LeaveIfError( fs.PrivatePath( path ) );
       
    92     TParse fp;
       
    93     fp.Set( KDatabaseFileName( ), &path, 0 );
       
    94     path = fp.FullName( );
       
    95 
       
    96     // create metastorage
       
    97     iMetadataStorage = cdf->CreateMetadataStorageL( path );
       
    98 
       
    99     // create localstorage
       
   100     iLocalStorage = CUpnpLocalStorage::NewL( iMetadataStorage );
       
   101 
       
   102     CleanupStack::PopAndDestroy( &fs );
       
   103     CleanupStack::PopAndDestroy( cdf );
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CUpnpContentDirectoryService::NewL
       
   108 // Two-phased constructor.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CUpnpContentDirectoryService*
       
   112 CUpnpContentDirectoryService::NewL( TUpnpCdSettings aSettings )
       
   113     {
       
   114     CUpnpContentDirectoryService* self =
       
   115     CUpnpContentDirectoryService::NewLC( aSettings );
       
   116 
       
   117     CleanupStack::Pop(self);
       
   118 
       
   119     return self;
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CUpnpContentDirectoryService::NewLC
       
   124 // Two-phased constructor.
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C CUpnpContentDirectoryService*
       
   128 CUpnpContentDirectoryService::NewLC( TUpnpCdSettings aSettings )
       
   129     {
       
   130     CUpnpContentDirectoryService* self = new( ELeave ) CUpnpContentDirectoryService();
       
   131 
       
   132     CleanupStack::PushL( self );
       
   133     self->ConstructL(aSettings);
       
   134 
       
   135     return self;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpnpContentDirectoryService::NewL
       
   140 // Two-phased constructor.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 EXPORT_C CUpnpContentDirectoryService* CUpnpContentDirectoryService::NewL()
       
   144     {
       
   145     CUpnpContentDirectoryService* self = CUpnpContentDirectoryService::NewLC();
       
   146     CleanupStack::Pop(self);
       
   147     return self;
       
   148     }
       
   149 // -----------------------------------------------------------------------------
       
   150 // CUpnpContentDirectoryService::NewLC
       
   151 // Two-phased constructor.
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C CUpnpContentDirectoryService* CUpnpContentDirectoryService::NewLC()
       
   155     {
       
   156     CUpnpContentDirectoryService* self =
       
   157     new( ELeave ) CUpnpContentDirectoryService();
       
   158     CleanupStack::PushL( self );
       
   159     self->ConstructL();
       
   160 
       
   161     return self;
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CUpnpContentDirectoryService::~CUpnpContentDirectoryService
       
   166 // Destructor
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 CUpnpContentDirectoryService::~CUpnpContentDirectoryService()
       
   170     {
       
   171     delete iTransferHandler;
       
   172     delete iLocalStorage;
       
   173     delete iCd;
       
   174     delete iMetadataStorage;
       
   175     delete iSender;
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CUpnpContentDirectoryService::SetDownloadDirectoryL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 EXPORT_C void CUpnpContentDirectoryService::SetDownloadDirectoryL(
       
   183     const TDesC& aDownloadDir )
       
   184     {
       
   185     if ( IsOnlineMode() )
       
   186         {
       
   187         iCd->SetDownloadDirectoryL( aDownloadDir );
       
   188         }
       
   189     else
       
   190         {
       
   191         User::Leave( KErrNotReady );
       
   192         }
       
   193 
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void CUpnpContentDirectoryService::SetThumbnailDirectoryL(
       
   201     const TDesC& aDownloadDir )
       
   202     {
       
   203     if ( IsOnlineMode() )
       
   204         {
       
   205         iCd->SetThumbnailDirectoryL( aDownloadDir );
       
   206         }
       
   207     else
       
   208         {
       
   209         User::Leave( KErrNotReady );
       
   210         }
       
   211 
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 EXPORT_C void CUpnpContentDirectoryService::ShareObjectL( CUpnpObject* aItem )
       
   219     {
       
   220     CheckDbL( );
       
   221     TRAPD( error, iLocalStorage->ShareObjectL( aItem ) );
       
   222     HandleDbErrorL( error );
       
   223     if ( error )
       
   224         {
       
   225         TRAP( error, iLocalStorage->ShareObjectL( aItem ) );
       
   226         RecreateDbL( error );
       
   227         }
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CUpnpContentDirectoryService::ShareReferenceL
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 EXPORT_C void CUpnpContentDirectoryService::ShareReferenceL( CUpnpItem* aItem )
       
   235     {
       
   236     CheckDbL( );
       
   237     TRAPD( error, iLocalStorage->ShareReferenceL( aItem ) );
       
   238     HandleDbErrorL( error );
       
   239     if ( error )
       
   240         {
       
   241         TRAP( error, iLocalStorage->ShareReferenceL( aItem ) );
       
   242         RecreateDbL( error );
       
   243         }
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CUpnpContentDirectoryService::ShareReferenceListL
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 EXPORT_C void CUpnpContentDirectoryService::ShareReferenceListL(
       
   251     CUpnpItemList* aItemList, TInt* aExecutionStatus )
       
   252     {
       
   253     CheckDbL( );
       
   254     TRAPD( error, iLocalStorage->ShareReferenceListL( aItemList,
       
   255         aExecutionStatus ) );
       
   256     HandleDbErrorL( error );
       
   257     if ( error )
       
   258         {
       
   259         TRAP( error, iLocalStorage->ShareReferenceListL( aItemList,
       
   260             aExecutionStatus ) );
       
   261         RecreateDbL( error );
       
   262         }
       
   263     }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CUpnpContentDirectoryService::UnshareItemL
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 EXPORT_C void CUpnpContentDirectoryService::UnshareItemL( TInt aItemId )
       
   270     {
       
   271     CheckDbL( );
       
   272     TRAPD( error, iLocalStorage->UnshareItemL( aItemId ) );
       
   273     HandleDbErrorL( error );
       
   274     if ( error )
       
   275         {
       
   276         TRAP( error, iLocalStorage->UnshareItemL( aItemId ) );
       
   277         RecreateDbL( error );
       
   278         }
       
   279     }
       
   280 // -----------------------------------------------------------------------------
       
   281 // CUpnpContentDirectoryService::UnshareContainerL
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C void CUpnpContentDirectoryService::UnshareContainerL(
       
   285     TInt aContainerId )
       
   286     {
       
   287     CheckDbL( );
       
   288     TRAPD( error, iLocalStorage->UnshareContainerL( aContainerId ) );
       
   289     HandleDbErrorL( error );
       
   290     if ( error )
       
   291         {
       
   292         TRAP( error, iLocalStorage->UnshareContainerL( aContainerId ) );
       
   293         RecreateDbL( error );
       
   294         }
       
   295     }
       
   296 // -----------------------------------------------------------------------------
       
   297 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 EXPORT_C CUpnpItem* CUpnpContentDirectoryService::GetSharedItemL( TInt aItemId )
       
   301     {
       
   302     CheckDbL();
       
   303     CUpnpItem* item = NULL;
       
   304     TRAPD( error, item = iLocalStorage->GetSharedItemL( aItemId ) );
       
   305     HandleDbErrorL( error );
       
   306     if ( error )
       
   307         {
       
   308         TRAP( error, item = iLocalStorage->GetSharedItemL( aItemId ) );
       
   309         RecreateDbL( error );
       
   310         }
       
   311     return item;
       
   312     }
       
   313 // -----------------------------------------------------------------------------
       
   314 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 EXPORT_C void CUpnpContentDirectoryService::ShareItemListL(
       
   318     CUpnpItemList* aItemList, TInt* aExecutionStatus )
       
   319     {
       
   320     CheckDbL( );
       
   321     TRAPD( error, iLocalStorage->ShareItemListL( aItemList, aExecutionStatus ) );
       
   322     HandleDbErrorL( error );
       
   323     if ( error )
       
   324         {
       
   325         TRAP( error, iLocalStorage->ShareItemListL( aItemList,
       
   326             aExecutionStatus ) );
       
   327         RecreateDbL( error );
       
   328         }
       
   329     }
       
   330 // -----------------------------------------------------------------------------
       
   331 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 EXPORT_C void CUpnpContentDirectoryService::UnshareItemListL(
       
   335     RArray<TInt>& aItemList, TInt* aExecutionStatus )
       
   336     {
       
   337     CheckDbL( );
       
   338     TRAPD( error, iLocalStorage->UnshareItemListL( aItemList,
       
   339         aExecutionStatus ) );
       
   340     HandleDbErrorL( error );
       
   341     if ( error )
       
   342         {
       
   343         TRAP( error, iLocalStorage->UnshareItemListL( aItemList,
       
   344             aExecutionStatus ) );
       
   345         RecreateDbL( error );
       
   346         }
       
   347     }
       
   348 // -----------------------------------------------------------------------------
       
   349 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 EXPORT_C void CUpnpContentDirectoryService::GetItemListL( TInt aContainerId,
       
   353     CUpnpBrowseCriteria* aBrowseCriteria, TInt* aTotalMatches,
       
   354     CUpnpItemList* aItemList )
       
   355     {
       
   356     CheckDbL( );
       
   357     TRAPD( error, iLocalStorage->GetItemListL( aContainerId, aBrowseCriteria,
       
   358         aTotalMatches, aItemList ) );
       
   359     HandleDbErrorL( error );
       
   360     if ( error )
       
   361         {
       
   362         TRAP( error, iLocalStorage->GetItemListL( aContainerId,
       
   363             aBrowseCriteria, aTotalMatches, aItemList ) );
       
   364         RecreateDbL( error );
       
   365         }
       
   366     }
       
   367 // -----------------------------------------------------------------------------
       
   368 // CUpnpContentDirectoryService::GetContainerListL
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 EXPORT_C void CUpnpContentDirectoryService::GetContainerListL(
       
   372     TInt aContainerId, CUpnpBrowseCriteria* aBrowseCriteria,
       
   373     TInt* aTotalMatches, CUpnpContainerList* aContainerList )
       
   374     {
       
   375     CheckDbL( );
       
   376     TRAPD( error, iLocalStorage->GetContainerListL( aContainerId,
       
   377         aBrowseCriteria, aTotalMatches, aContainerList ) );
       
   378     HandleDbErrorL( error );
       
   379     if ( error )
       
   380         {
       
   381         TRAP( error, iLocalStorage->GetContainerListL( aContainerId,
       
   382             aBrowseCriteria, aTotalMatches, aContainerList ) );
       
   383         RecreateDbL( error );
       
   384         }
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CUpnpContentDirectoryService::SetThumbnailDirectoryL
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 EXPORT_C CUpnpContainer* CUpnpContentDirectoryService::GetSingleContainerL(
       
   392         TInt aContainerId)
       
   393     {
       
   394     CheckDbL();
       
   395     CUpnpContainer* container = NULL;
       
   396     TRAPD( error, container = iLocalStorage->GetSingleContainerL( aContainerId ) );
       
   397     HandleDbErrorL( error );
       
   398     if ( error )
       
   399         {
       
   400         TRAP( error, container = iLocalStorage->GetSingleContainerL( aContainerId ) );
       
   401         RecreateDbL( error );
       
   402         }
       
   403     return container;
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CUpnpContentDirectoryService::GetAddress
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 EXPORT_C TInetAddr CUpnpContentDirectoryService::GetAddress()
       
   411     {
       
   412     if ( IsOnlineMode() )
       
   413         {
       
   414         return iCd->GetAddress();
       
   415         }
       
   416     else
       
   417         {
       
   418         // no address available in offline mode
       
   419         return TInetAddr( INET_ADDR(0,0,0,0) );
       
   420         }
       
   421     }    
       
   422     
       
   423 // -----------------------------------------------------------------------------
       
   424 // CUpnpContentDirectoryService::GetContentDirectory
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 EXPORT_C CUpnpContentDirectory* CUpnpContentDirectoryService::GetContentDirectory()
       
   428     {
       
   429     return iCd;
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CUpnpContentDirectoryService::HandleDbErrorL
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 void CUpnpContentDirectoryService::HandleDbErrorL( TInt aError )
       
   437     {
       
   438     TInt error = iMetadataStorage->HandleDbError( aError );
       
   439     if ( error != KErrNone )
       
   440         {
       
   441         User::Leave( error );
       
   442         }
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CUpnpContentDirectoryService::RecreateDbL
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CUpnpContentDirectoryService::RecreateDbL( TInt aError )
       
   450     {
       
   451     if ( aError == KErrCorrupt )
       
   452         {
       
   453         iMetadataStorage->RecreateDatabaseFileL( );
       
   454         }
       
   455     }
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CUpnpContentDirectoryService::CheckDbL
       
   459 // -----------------------------------------------------------------------------
       
   460 //            
       
   461 void CUpnpContentDirectoryService::CheckDbL()
       
   462     {
       
   463     if ( !iMetadataStorage->IsDbCreated( ) )
       
   464         {
       
   465         HandleDbErrorL( KErrCorrupt );
       
   466         }
       
   467     }
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CUpnpContentDirectoryService::IsOnlineMode
       
   471 // -----------------------------------------------------------------------------
       
   472 //            
       
   473 TBool CUpnpContentDirectoryService::IsOnlineMode()
       
   474     {
       
   475     return reinterpret_cast<TBool>( iCd );
       
   476     }
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CUpnpContentDirectoryService::AddressChangeL
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 EXPORT_C void CUpnpContentDirectoryService::AddressChangeL()
       
   483     {
       
   484     if ( IsOnlineMode() )
       
   485         {
       
   486         iCd->AddressChangeL();
       
   487         }
       
   488     }
       
   489 //  End of File