upnpmediaserver/mediaserverengine/src/upnpmediaserver.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:  Media Server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpmediaserver.h"
       
    21 #include "upnpcontentdirectoryservice.h"
       
    22 #include "upnpmediaserversession.h"
       
    23 #include "upnpmediaserverglobals.h"
       
    24 #include "upnpcontentdirectoryglobals.h"
       
    25 #include "upnpmediaserverclient.h"
       
    26 #include "upnpsecuritymanager.h"
       
    27 #include "upnpfiletransferevent.h"
       
    28 #include <upnpdevicedescriptionstore.h>
       
    29 
       
    30 #include "upnpdeviceimplementation.h"
       
    31 #include "upnpcm.h"
       
    32 #include "upnpiconmanager.h"
       
    33 #include "upnpdiskremovedetector.h"
       
    34 #include <sysutil.h>
       
    35 #define KLogFile _L("upnpmediaserver.txt")
       
    36 #include "upnpcustomlog.h"
       
    37 #include <upnpconnectionmanagernetworkeventprovider.h>
       
    38 #include "upnpmediaserverdescriptionprovider.h"
       
    39 
       
    40 const TVersion KServerSupportedVersion(
       
    41     KMediaServerMajorVersionNumber,
       
    42     KMediaServerMinorVersionNumber,
       
    43     KMediaServerBuildVersionNumber );
       
    44 
       
    45 using namespace UpnpMediaServerSettings;
       
    46 
       
    47 _LIT8( KUriMediaServer, "/mediaserver1/mediaserver1.xml" );
       
    48 _LIT8( KUriContentDirectory, "/mediaserver1/contentdirectory1.xml" );
       
    49 _LIT8( KUriConnectionManager, "/mediaserver1/connectionmanager1.xml" );
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpMediaServer::CUpnpMediaServer
       
    55 // C++ default constructor can NOT contain any code, that
       
    56 // might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CUpnpMediaServer::CUpnpMediaServer():
       
    60     iMsStatus( RUpnpMediaServerClient::EStopped )
       
    61     {
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CUpnpMediaServer::ConstructL
       
    66 // Symbian 2nd phase constructor can leave.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CUpnpMediaServer::ConstructL()
       
    70     {
       
    71     BaseConstructL();
       
    72     iSettings = CUpnpMediaServerSettings::NewL();
       
    73     iDiskDetector = CUpnpDiskRemoveDetector::NewL( this );
       
    74     iStopAVMediaServerTimer = CUpnpNotifyTimer::NewL( this );
       
    75     iConnectionManagerNetworkEventProvider = 
       
    76         CUpnpConnectionManagerNetworkEventProvider::NewL( *this );
       
    77     iDescriptionProvider = CUpnpMediaServerDescriptionProvider::NewL();
       
    78     
       
    79     InitializeL( EFalse );
       
    80     
       
    81     iDescriptionStore = CUpnpDeviceDescriptionStore::NewL( *iMsName );
       
    82     iIconManager = CUpnpIconManager::NewL( *iDescriptionStore, *iDescriptionProvider );
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CUpnpMediaServer::NewL
       
    87 // Two-phased constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CUpnpSymbianServerBase* CUpnpMediaServer::NewLC()
       
    91     {
       
    92     CUpnpMediaServer* self = new( ELeave ) CUpnpMediaServer();
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL();
       
    95     return self;
       
    96     }
       
    97 
       
    98 // Destructor
       
    99 CUpnpMediaServer::~CUpnpMediaServer()
       
   100     {
       
   101     if ( iDescriptionStore )
       
   102         {
       
   103         TRAP_IGNORE( iDescriptionStore->SaveL() );
       
   104         }
       
   105 
       
   106     ASSERT( CanBeStopped() );//check if we are in correct state
       
   107     delete iStopAVMediaServerTimer;
       
   108     delete iMsName;
       
   109     delete iCdName;
       
   110     delete iCmName;
       
   111 
       
   112     delete iConnectionManagerNetworkEventProvider;
       
   113     
       
   114     iObserverList.ResetAndDestroy();
       
   115 
       
   116     delete iSettings;
       
   117     delete iManager;
       
   118     delete iIconManager;
       
   119 
       
   120     delete iDevice;
       
   121     delete iDescriptionStore;
       
   122     delete iContentDirecory;
       
   123     delete iCm;
       
   124     delete iDiskDetector;
       
   125     delete iDescriptionProvider;
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CUpnpMediaServer::StopAVMediaServer
       
   130 //
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CUpnpMediaServer::StopAVMediaServer( TBool aSilent )
       
   134     {
       
   135     LOGS( "CUpnpMediaServer::StopAVMediaServer begin" );
       
   136     iIsSilentShutdown = aSilent;
       
   137     //ms will be stopped in KStopAVMediaServerDelay unless he gets start request
       
   138     iStopAVMediaServerTimer->Start( KStopAVMediaServerDelay );
       
   139     }
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CUpnpMediaServer::StopAVMediaServerImmediately
       
   144 // Immediately stops media server ( no delay )
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void CUpnpMediaServer::StopAVMediaServerImmediately( TBool aSilent, TBool aWlanLost )
       
   148     {
       
   149     iStopAVMediaServerTimer->Cancel();
       
   150     iDiskDetector->Cancel();
       
   151 
       
   152     delete iManager;
       
   153     iManager = NULL;
       
   154 
       
   155     // we do not want any stop devices in case of WLAN lost
       
   156     if ( !aWlanLost )
       
   157         {
       
   158         if ( iDevice )
       
   159             {
       
   160             if ( !aSilent )
       
   161                 {
       
   162                 TRAP_IGNORE( iDevice->StopL() );
       
   163                 }
       
   164             else
       
   165                 {
       
   166                 TRAP_IGNORE( iDevice->StopL( CUpnpDeviceImplementation::EStopSilent ) );
       
   167                 }
       
   168             }
       
   169         }
       
   170 
       
   171     delete iContentDirecory;
       
   172     iContentDirecory = NULL;
       
   173 
       
   174     delete iCm;
       
   175     iCm = NULL;
       
   176 
       
   177     delete iDevice;
       
   178     iDevice = NULL;
       
   179 
       
   180     SetStatus( RUpnpMediaServerClient::EStopped );
       
   181 
       
   182     SuggestShutdown();
       
   183     LOGS( "CUpnpMediaServer::StopAVMediaServer end" );
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CUpnpMediaServer::NewSessionL
       
   188 // Create new session.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 CSession2* CUpnpMediaServer::NewSessionL( const RMessage2& /*aMessage*/ ) const
       
   192     {
       
   193     return CUpnpMediaServerSession::NewL( (CUpnpMediaServer*) this );
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CUpnpMediaServer::Status
       
   198 // Check status of the server.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 RUpnpMediaServerClient::TServerStatus CUpnpMediaServer::Status() const
       
   202     {
       
   203     if ( iStopAVMediaServerTimer && iStopAVMediaServerTimer->IsActive() )
       
   204         {
       
   205         return RUpnpMediaServerClient::EStopped;
       
   206         }
       
   207     return iMsStatus;
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CUpnpMediaServer::StartAVMediaServerL
       
   212 // Create and start the AV Media Server.
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CUpnpMediaServer::StartAVMediaServerL()
       
   216     {
       
   217     LOGS( "CUpnpMediaServer::StartAVMediaServerL begin" );
       
   218 
       
   219     // cleanup because previous start could fail
       
   220     StopAVMediaServerImmediately( EFalse, EFalse );
       
   221     
       
   222     // save description
       
   223     iDescriptionStore->SaveL();
       
   224 
       
   225     // create device
       
   226     iDevice = CUpnpDeviceImplementation::NewL( KUriMediaServer, 
       
   227                                                *iDescriptionStore, 
       
   228                                                *iDescriptionProvider );
       
   229 
       
   230     // create security manager
       
   231     iManager = CUpnpSecurityManager::NewInstanceFromUpnpSettingsL();
       
   232     //iDevice->SetSecurityManager( iManager );
       
   233 
       
   234     // create content direcory service
       
   235     HBufC8* uploadtmp = iSettings->GetL( EUploadDirectory );
       
   236     CleanupStack::PushL( uploadtmp );
       
   237     if ( uploadtmp->Length() == 0 )
       
   238         {
       
   239         User::Leave( KErrNotReady );
       
   240         }
       
   241     HBufC* uploaddir = HBufC::NewL( uploadtmp->Length() );
       
   242     CleanupStack::PushL( uploaddir );
       
   243     uploaddir->Des().Copy( uploadtmp->Des() );
       
   244     HBufC* thumbnaildir = HBufC::NewL( uploaddir->Length()
       
   245             + KThumbnailDirLen );
       
   246     CleanupStack::PushL( thumbnaildir );
       
   247     thumbnaildir->Des().Copy( uploaddir->Des() );
       
   248     thumbnaildir->Des().Append( KThumbnailSubDir );
       
   249 
       
   250     TInt createThumbnails;
       
   251     User::LeaveIfError( iSettings->Get( EUpnpMediaServerThumbnails,
       
   252         createThumbnails ) );
       
   253 
       
   254     TInt maxReqCount;
       
   255     User::LeaveIfError( iSettings->Get( KUpnpMediaServerBrowseLimit,
       
   256         maxReqCount ) );
       
   257 
       
   258     TUpnpCdSettings cdset =
       
   259         {
       
   260         iDevice, *uploaddir, *thumbnaildir, *iCdName, NULL, createThumbnails,
       
   261                 maxReqCount, this, KNullDesC(), NULL
       
   262         };
       
   263 
       
   264     iContentDirecory = CUpnpContentDirectoryService::NewL( cdset );
       
   265 
       
   266     // create connection manager service
       
   267     HBufC8* media = iSettings->GetL( ESupportedMedia );
       
   268     CleanupStack::PushL( media );
       
   269     if ( media->Length() == 0 )
       
   270         {
       
   271         User::Leave( KErrNotReady );
       
   272         }
       
   273     iCm = CUpnpCM::NewL( *iDevice, *media, *iCmName );
       
   274     CleanupStack::PopAndDestroy( media );
       
   275 
       
   276     CleanupStack::PopAndDestroy( thumbnaildir );
       
   277     CleanupStack::PopAndDestroy( uploaddir );
       
   278     CleanupStack::PopAndDestroy( uploadtmp );
       
   279 
       
   280     SetDescriptionSettingsL( iDevice );
       
   281 
       
   282     // start media server
       
   283     iDevice->StartL();
       
   284     iDiskDetector->StartL();
       
   285 
       
   286     SetStatus( RUpnpMediaServerClient::EStartedOnline );
       
   287     LOGS( "CUpnpMediaServer::StartAVMediaServerL end" );
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CUpnpMediaServer::StartAVMediaServerOfflineL
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 void CUpnpMediaServer::StartAVMediaServerOfflineL()
       
   295     {
       
   296     LOGS( "StartAVMediaServerOfflineL begin" );
       
   297 
       
   298     // cleanup because previous start could fail
       
   299     StopAVMediaServerImmediately( EFalse, EFalse );
       
   300 
       
   301     // create content directory
       
   302     iContentDirecory = CUpnpContentDirectoryService::NewL();
       
   303     SetStatus( RUpnpMediaServerClient::EStartedOffline );
       
   304     LOGS( "StartAVMediaServerOfflineL end" );
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CUpnpMediaServer::RestartAVMediaServerL
       
   309 // Start and stop the AV Media Server.
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CUpnpMediaServer::RestartAVMediaServerL()
       
   313     {
       
   314     if ( iDevice )
       
   315         {
       
   316         iDevice->StopL();
       
   317         SetDescriptionSettingsL( iDevice );
       
   318         iDevice->StartL();
       
   319         }
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CUpnpMediaServer::SetFriendlyNameL
       
   324 // Sets friendly name for device, changes are available after restarting
       
   325 // the server
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CUpnpMediaServer::SetDescriptionSettingsL(
       
   329     CUpnpDeviceImplementation* aDevice )
       
   330     {
       
   331     SetDescriptionRequiredPropertyL( aDevice, EFriendlyName, KFriendlyName );
       
   332     SetDescriptionRequiredPropertyL( aDevice, EModelName, KModelName );
       
   333     SetDescriptionRequiredPropertyL( aDevice, EManufacturer, KManufacturer );
       
   334 
       
   335     SetDescriptionPropertyL( aDevice, EModelUrl, KModelUrl );
       
   336     SetDescriptionPropertyL( aDevice, EModelDescription, KModelDescription );
       
   337     SetDescriptionPropertyL( aDevice, EModelNumber, KModelNumber );
       
   338     SetDescriptionPropertyL( aDevice, ESerialNumber, KSerialNumber );
       
   339     SetDescriptionPropertyL( aDevice, EPresentationUrl, KPresentationUrl );
       
   340     SetDescriptionPropertyL( aDevice, EManufacturerUrl, KManufacturerUrl );
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CUpnpMediaServer::RemoveIconL
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 void CUpnpMediaServer::RemoveIconL( const TDesC8& aUrl )
       
   348     {
       
   349     iIconManager->RemoveIconL( aUrl );
       
   350     }
       
   351 
       
   352 // -----------------------------------------------------------------------------
       
   353 // CUpnpMediaServer::RemoveIconsL
       
   354 // ------------------------------------------------------------------------------
       
   355 //
       
   356 void CUpnpMediaServer::RemoveIconsL()
       
   357     {
       
   358     iIconManager->RemoveIconsL();
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CUpnpMediaServer::AddIconL
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 void CUpnpMediaServer::AddIconL( const TDesC8& aProperty )
       
   366     {
       
   367     iIconManager->AddIconL( aProperty );
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CUpnpMediaServer::UpdateIconL
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CUpnpMediaServer::UpdateIconL( const TDesC8& aProperty,
       
   375     const TDesC8& aUrl )
       
   376     {
       
   377     iIconManager->UpdateIconL( aProperty, aUrl );
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CUpnpMediaServer::SetUploadDirToCDL
       
   382 // Set the download and thumbnail in CD according to the value
       
   383 // of the upload directory
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 void CUpnpMediaServer::SetUploadDirToCDL( const TDesC& aPath )
       
   387     {
       
   388     if ( iContentDirecory )
       
   389         {
       
   390         HBufC* uploaddir= NULL;
       
   391 
       
   392         if ( aPath != KNullDesC() )
       
   393             {
       
   394             uploaddir = aPath.AllocLC();
       
   395             }
       
   396         else
       
   397             {
       
   398             HBufC8* uploadtmp = iSettings->GetL( EUploadDirectory );
       
   399             CleanupStack::PushL( uploadtmp );
       
   400 
       
   401             uploaddir = HBufC::NewL( uploadtmp->Length() );
       
   402             uploaddir->Des().Copy( uploadtmp->Des() );
       
   403             CleanupStack::PopAndDestroy( uploadtmp );
       
   404             CleanupStack::PushL( uploaddir );
       
   405             }
       
   406 
       
   407         HBufC* thumbnaildir = HBufC::NewL( uploaddir->Length()
       
   408                 + KThumbnailDirLen );
       
   409         CleanupStack::PushL( thumbnaildir );
       
   410         thumbnaildir->Des().Copy( uploaddir->Des() );
       
   411         thumbnaildir->Des().Append( KThumbnailSubDir );
       
   412 
       
   413         iContentDirecory->SetDownloadDirectoryL( uploaddir->Des() );
       
   414         iContentDirecory->SetThumbnailDirectoryL( thumbnaildir->Des() );
       
   415 
       
   416         CleanupStack::PopAndDestroy( thumbnaildir );
       
   417         CleanupStack::PopAndDestroy( uploaddir );
       
   418         }
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // CUpnpMediaServer::SetUploadDirL
       
   423 // Set the upload and thumbnail path in CD and central repository.
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void CUpnpMediaServer::SetUploadDirL( const TDesC& aPath )
       
   427     {
       
   428     User::LeaveIfError( iSettings->Set(
       
   429         UpnpMediaServerSettings::EUploadDirectory, aPath ) ) ;
       
   430 
       
   431     SetUploadDirToCDL( KNullDesC( )); // empty descriptor -> it means, value will be taken directly from cenrep
       
   432     }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CUpnpMediaServer::SetDescriptionPropertyL
       
   436 // Sets description property for device
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 void CUpnpMediaServer::SetDescriptionPropertyL(
       
   440     CUpnpDeviceImplementation* aDevice, TInt aKey, const TDesC8& aName )
       
   441     {
       
   442     HBufC8* value = iSettings->GetL( aKey );
       
   443     CleanupStack::PushL( value );
       
   444     if ( value->Size() )
       
   445         {
       
   446         aDevice->SetDescriptionPropertyL( aName, *value );
       
   447         }
       
   448     else
       
   449         {
       
   450         aDevice->RemoveDescriptionPropertyL( aName );
       
   451         }
       
   452 
       
   453     CleanupStack::PopAndDestroy( value );
       
   454     }
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // CUpnpMediaServer::SetDescriptionRequiredPropertyL
       
   458 // Sets description required property for device
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 void CUpnpMediaServer::SetDescriptionRequiredPropertyL(
       
   462     CUpnpDeviceImplementation* aDevice, TInt aKey, const TDesC8& aName )
       
   463     {
       
   464     HBufC8* value = iSettings->GetL( aKey );
       
   465 
       
   466     if ( !value )
       
   467         {
       
   468         User::Leave( KErrNotFound );
       
   469         }
       
   470     else
       
   471         {
       
   472         CleanupStack::PushL( value );
       
   473 
       
   474         HBufC8* tmpBuf = (*value).AllocLC();
       
   475         TPtr8 tmpPtr(tmpBuf->Des() );
       
   476 
       
   477         RemoveWhiteSpaces( tmpPtr );
       
   478         if ( tmpPtr.Length() == 0 )
       
   479             {
       
   480             User::Leave( KErrNotFound );
       
   481             }
       
   482         CleanupStack::PopAndDestroy( tmpBuf );
       
   483         }
       
   484 
       
   485     TRAPD( error, aDevice->SetDescriptionPropertyL( aName, *value ) )
       
   486     if ( error != KErrNotFound )
       
   487         {
       
   488         User::LeaveIfError( error );
       
   489         }
       
   490 
       
   491     CleanupStack::PopAndDestroy( value );
       
   492     }
       
   493 
       
   494 // -----------------------------------------------------------------------------
       
   495 // CUpnpMediaServer::RemoveWhiteSpaces
       
   496 // Removes white characters from input descriptor
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 void CUpnpMediaServer::RemoveWhiteSpaces( TDes8& aString )
       
   500     {
       
   501     for ( TInt i=0; i<aString.Length(); i++ )
       
   502         {
       
   503         if ( aString[i] == ' ' || aString[i] == '\r' || aString[i] == '\n'
       
   504                 || aString[i] == '\t' )
       
   505             {
       
   506             aString.Replace( i, 1, KNullDesC8() );
       
   507             i--;
       
   508             }
       
   509         }
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CUpnpMediaServer::InitPrivateDir
       
   514 // Init Media Server directory structure in private directory
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 void CUpnpMediaServer::InitPrivateDirL( RFs &aFs )
       
   518     {
       
   519     TParse fp;
       
   520     TFileName path;
       
   521     aFs.PrivatePath( path );
       
   522     fp.Set( KDefaultMediaDrive(), &path, 0 );
       
   523     User::LeaveIfError( fp.AddDir( KMSPublicAccessedDir() ) );
       
   524     User::LeaveIfError( fp.AddDir( KMSDeviceXmlDescDir() ) );
       
   525     User::LeaveIfError( fp.AddDir( KIconDirectory() ) );
       
   526     path = fp.DriveAndPath();
       
   527 
       
   528     aFs.MkDirAll( path ) ;
       
   529     fp.PopDir();
       
   530     User::LeaveIfError( fp.AddDir( KMSMediaDir ) );
       
   531     path = fp.DriveAndPath();
       
   532     aFs.MkDirAll( path );
       
   533     }
       
   534 
       
   535 // -----------------------------------------------------------------------------
       
   536 // CUpnpMediaServer::InitDescFileL
       
   537 // Checks is description file exists. If not creates it from teplate stored on Z:
       
   538 // -----------------------------------------------------------------------------
       
   539 //
       
   540 HBufC* CUpnpMediaServer::InitDescFileL( RFs &aFs,
       
   541     const TDesC& aTempleteDescFile, TBool aOverwrite )
       
   542     {
       
   543     TUint temp;
       
   544     HBufC* descName = CreateFullNameL( aFs, KDefaultMediaDrive(),
       
   545         aTempleteDescFile );
       
   546     CleanupStack::PushL( descName );
       
   547 
       
   548     if ( aFs.Att( *descName, temp ) != KErrNone || aOverwrite )
       
   549         {
       
   550 
       
   551         HBufC* templName = CreateFullNameL( aFs, KDescTemplateDrive(),
       
   552             aTempleteDescFile );
       
   553         CleanupStack::PushL( templName );
       
   554         CFileMan* fileMan = CFileMan::NewL( aFs );
       
   555         CleanupStack::PushL( fileMan );
       
   556         User::LeaveIfError( fileMan->Copy( *templName, *descName ) );
       
   557         User::LeaveIfError( fileMan->Attribs( *descName, KEntryAttNormal,
       
   558             KEntryAttReadOnly, TTime( 0 ) ) );
       
   559         CleanupStack::PopAndDestroy( fileMan );
       
   560         CleanupStack::PopAndDestroy( templName );
       
   561         }
       
   562     CleanupStack::Pop( descName );
       
   563     return descName;
       
   564     }
       
   565 
       
   566 // -----------------------------------------------------------------------------
       
   567 // CUpnpMediaServer::InitializeL
       
   568 // Initialize Media Server using files from drive z:
       
   569 // -----------------------------------------------------------------------------
       
   570 //
       
   571 void CUpnpMediaServer::InitializeL( TBool aOverwrite )
       
   572     {
       
   573     RFs fs;
       
   574     User::LeaveIfError( fs.Connect() );
       
   575     CleanupClosePushL( fs );
       
   576 
       
   577     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KMSDescriptionMaxSize,
       
   578         EDriveC ) )
       
   579         {
       
   580         User::Leave( KErrDiskFull );
       
   581         }
       
   582 
       
   583     InitPrivateDirL( fs );
       
   584 
       
   585     delete iMsName;
       
   586     iMsName= NULL;
       
   587     iMsName = InitDescFileL( fs, KMSDeviceXmlDescFile(), aOverwrite );
       
   588     iDescriptionProvider->AddUriToFileMapL( KUriMediaServer(), iMsName->Des() );
       
   589     delete iCdName;
       
   590     iCdName = NULL;
       
   591     iCdName = InitDescFileL( fs, KCDXmlDescFile(), aOverwrite );
       
   592     iDescriptionProvider->AddUriToFileMapL( KUriContentDirectory(), iCdName->Des() );
       
   593     delete iCmName;
       
   594     iCmName = NULL;
       
   595     iCmName = InitDescFileL( fs, KCMXmlDescFile(), aOverwrite );
       
   596     iDescriptionProvider->AddUriToFileMapL( KUriConnectionManager(), iCmName->Des() );
       
   597 
       
   598     CleanupStack::PopAndDestroy( &fs );
       
   599     }
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // CUpnpMediaServer::ResetDbL
       
   603 // Reset database, renaming name of original DB
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 void CUpnpMediaServer::ResetDbL()
       
   607     {
       
   608     RFs fs;
       
   609     User::LeaveIfError( fs.Connect() );
       
   610     CleanupClosePushL( fs );
       
   611 
       
   612     TFileName orgPath;
       
   613 
       
   614     User::LeaveIfError( fs.PrivatePath( orgPath ) );
       
   615 
       
   616     TParse fp;
       
   617     fp.Set( KDatabaseFileName(), &orgPath, 0 );
       
   618     orgPath = fp.FullName();
       
   619 
       
   620     TFileName newPath;
       
   621     newPath = orgPath;
       
   622     newPath.Append( KBackupSuffix );
       
   623 
       
   624     CFileMan* fileMan = CFileMan::NewL( fs );
       
   625     CleanupStack::PushL( fileMan );
       
   626     User::LeaveIfError( fileMan->Rename( orgPath, newPath ) );
       
   627     CleanupStack::PopAndDestroy( fileMan );
       
   628 
       
   629     CleanupStack::PopAndDestroy( &fs );
       
   630     }
       
   631 
       
   632 // -----------------------------------------------------------------------------
       
   633 // CUpnpMediaServer::DeleteDbL
       
   634 // Delete database
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CUpnpMediaServer::DeleteDbL()
       
   638     {
       
   639     RFs fs;
       
   640     User::LeaveIfError( fs.Connect() );
       
   641     CleanupClosePushL( fs );
       
   642 
       
   643     TFileName orgPath;
       
   644 
       
   645     User::LeaveIfError( fs.PrivatePath( orgPath ) );
       
   646 
       
   647     TParse fp;
       
   648     fp.Set( KDatabaseFileName(), &orgPath, 0 );
       
   649     orgPath = fp.FullName();
       
   650 
       
   651     CFileMan* fileMan = CFileMan::NewL( fs );
       
   652     CleanupStack::PushL( fileMan );
       
   653     User::LeaveIfError( fileMan->Delete( orgPath ) );
       
   654     CleanupStack::PopAndDestroy( fileMan );
       
   655 
       
   656     CleanupStack::PopAndDestroy( &fs );
       
   657     }
       
   658 
       
   659 // -----------------------------------------------------------------------------
       
   660 // CUpnpMediaServer::SettingChangedL
       
   661 // Function notify when setting will change
       
   662 // -----------------------------------------------------------------------------
       
   663 //
       
   664 void CUpnpMediaServer::SettingChangedL()
       
   665     {
       
   666     //no actions needed
       
   667     }
       
   668 
       
   669 // -----------------------------------------------------------------------------
       
   670 // CUpnpMediaServer::CreateFullNameL
       
   671 // Create full name using drive MS directory structure and file name.
       
   672 // -----------------------------------------------------------------------------
       
   673 //
       
   674 HBufC* CUpnpMediaServer::CreateFullNameL( RFs &aFs, const TDesC& aDrive,
       
   675     const TDesC& aName )
       
   676     {
       
   677     TParse fp;
       
   678     TFileName path;
       
   679     User::LeaveIfError( aFs.PrivatePath( path ) );
       
   680     fp.Set( aDrive, &path, &aName );
       
   681     User::LeaveIfError( fp.AddDir( KMSPublicAccessedDir() ) );
       
   682     User::LeaveIfError( fp.AddDir( KMSDeviceXmlDescDir() ) );
       
   683     return fp.FullName().AllocL();
       
   684     }
       
   685 
       
   686 // -----------------------------------------------------------------------------
       
   687 // CUpnpMediaServer::ShareItemL
       
   688 // -----------------------------------------------------------------------------
       
   689 //
       
   690 void CUpnpMediaServer::ShareObjectL( CUpnpObject* aItem )
       
   691     {
       
   692     iContentDirecory->ShareObjectL( aItem );
       
   693     }
       
   694 
       
   695 // -----------------------------------------------------------------------------
       
   696 // CUpnpMediaServer::ShareReferenceL
       
   697 // -----------------------------------------------------------------------------
       
   698 //
       
   699 void CUpnpMediaServer::ShareReferenceL( CUpnpItem* aItem )
       
   700     {
       
   701     iContentDirecory->ShareReferenceL( aItem );
       
   702     }
       
   703 
       
   704 // -----------------------------------------------------------------------------
       
   705 // CUpnpMediaServer::ShareReferenceListL
       
   706 // -----------------------------------------------------------------------------
       
   707 //
       
   708 void CUpnpMediaServer::ShareReferenceListL( CUpnpItemList* aItemList,
       
   709     TInt* aStatus )
       
   710     {
       
   711     iContentDirecory->ShareReferenceListL( aItemList, aStatus );
       
   712     }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // CUpnpMediaServer::ShareItemListL
       
   716 // -----------------------------------------------------------------------------
       
   717 //
       
   718 void CUpnpMediaServer::ShareItemListL( CUpnpItemList* aItemList, TInt* aStatus )
       
   719     {
       
   720     iContentDirecory->ShareItemListL( aItemList, aStatus );
       
   721     }
       
   722 
       
   723 // -----------------------------------------------------------------------------
       
   724 // CUpnpMediaServer::UnshareRootL
       
   725 // -----------------------------------------------------------------------------
       
   726 //
       
   727 void CUpnpMediaServer::UnshareRootL()
       
   728     {
       
   729     TBool online = ETrue;
       
   730 
       
   731     if ( Status()==RUpnpMediaServerClient::EStartedOnline )
       
   732         {
       
   733         online = ETrue;
       
   734         }
       
   735     else if ( Status()==RUpnpMediaServerClient::EStartedOffline )
       
   736         {
       
   737         online = EFalse;
       
   738         }
       
   739 
       
   740     StopAVMediaServerImmediately( EFalse, EFalse );
       
   741 
       
   742     DeleteDbL();
       
   743 
       
   744     if ( online )
       
   745         {
       
   746         StartAVMediaServerL();
       
   747         }
       
   748     else
       
   749         {
       
   750         StartAVMediaServerOfflineL();
       
   751         }
       
   752     }
       
   753 
       
   754 // -----------------------------------------------------------------------------
       
   755 // CUpnpMediaServer::UnshareItemL
       
   756 // -----------------------------------------------------------------------------
       
   757 //
       
   758 void CUpnpMediaServer::UnshareItemL( TInt aId )
       
   759     {
       
   760     iContentDirecory->UnshareItemL( aId );
       
   761     }
       
   762 
       
   763 // -----------------------------------------------------------------------------
       
   764 // CUpnpMediaServer::UnshareContainerL
       
   765 // -----------------------------------------------------------------------------
       
   766 //
       
   767 void CUpnpMediaServer::UnshareContainerL( TInt aId )
       
   768     {
       
   769     iContentDirecory->UnshareContainerL( aId );
       
   770     }
       
   771 
       
   772 // -----------------------------------------------------------------------------
       
   773 // CUpnpMediaServer::GetSharedItemL
       
   774 // -----------------------------------------------------------------------------
       
   775 //
       
   776 CUpnpItem* CUpnpMediaServer::GetSharedItemL( TInt aItem )
       
   777     {
       
   778     return iContentDirecory->GetSharedItemL( aItem );
       
   779     }
       
   780 
       
   781 // -----------------------------------------------------------------------------
       
   782 // CUpnpMediaServer::GetSingleContainerL
       
   783 // -----------------------------------------------------------------------------
       
   784 //
       
   785 CUpnpContainer* CUpnpMediaServer::GetSingleContainerL( TInt aContainerId )
       
   786     {
       
   787     return iContentDirecory->GetSingleContainerL( aContainerId );
       
   788     }
       
   789 
       
   790 // -----------------------------------------------------------------------------
       
   791 // CUpnpMediaServer::UnshareItemListL
       
   792 // -----------------------------------------------------------------------------
       
   793 //
       
   794 void CUpnpMediaServer::UnshareItemListL( RArray<TInt>& aItemList,
       
   795     TInt* aExecutionStatus )
       
   796     {
       
   797     iContentDirecory->UnshareItemListL( aItemList, aExecutionStatus );
       
   798     }
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CUpnpMediaServer::GetSharedItemListL
       
   802 // -----------------------------------------------------------------------------
       
   803 //
       
   804 void CUpnpMediaServer::GetSharedItemListL( TInt aContId,
       
   805     CUpnpBrowseCriteria* aBrowseCriteria, TInt* TotalMatches,
       
   806     CUpnpItemList* aItemList )
       
   807     {
       
   808     iContentDirecory->GetItemListL( aContId, aBrowseCriteria, TotalMatches,
       
   809         aItemList );
       
   810     }
       
   811 
       
   812 // -----------------------------------------------------------------------------
       
   813 // CUpnpMediaServer::GetSharedContainerListL
       
   814 // -----------------------------------------------------------------------------
       
   815 //
       
   816 void CUpnpMediaServer::GetSharedContainerListL( TInt aContId,
       
   817     CUpnpBrowseCriteria* aBrowseCriteria, TInt* TotalMatches,
       
   818     CUpnpContainerList* aContainerList )
       
   819     {
       
   820     iContentDirecory->GetContainerListL( aContId, aBrowseCriteria,
       
   821         TotalMatches, aContainerList );
       
   822     }
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // CUpnpMediaServer::GetIpAndPortL
       
   826 // -----------------------------------------------------------------------------
       
   827 //
       
   828 TInetAddr CUpnpMediaServer::GetAddress()
       
   829     {
       
   830     return iContentDirecory->GetAddress();
       
   831     }
       
   832 
       
   833 // ============================= EVENT FUNCTIONS ===============================
       
   834 
       
   835 // -----------------------------------------------------------------------------
       
   836 // CUpnpMediaServer::SubscribeEvents
       
   837 // -----------------------------------------------------------------------------
       
   838 //
       
   839 void CUpnpMediaServer::SubscribeEvents(
       
   840     MUpnpContentDirectoryEventObserver* aObserver )
       
   841     {
       
   842     iObserverList.Append( aObserver );
       
   843     }
       
   844 
       
   845 // -----------------------------------------------------------------------------
       
   846 // CUpnpMediaServer::UnsubscribeEvents
       
   847 // -----------------------------------------------------------------------------
       
   848 //
       
   849 void CUpnpMediaServer::UnsubscribeEvents(
       
   850     MUpnpContentDirectoryEventObserver* aObserver )
       
   851     {
       
   852     TInt index = iObserverList.Find( aObserver );
       
   853     if ( index != KErrNotFound )
       
   854         {
       
   855         iObserverList.Remove( index );
       
   856         }
       
   857     }
       
   858 
       
   859 // -----------------------------------------------------------------------------
       
   860 // CUpnpMediaServer::FileTransferEvent
       
   861 // -----------------------------------------------------------------------------
       
   862 //
       
   863 void CUpnpMediaServer::FileTransferEvent( CUpnpFileTransferEvent* aMessage )
       
   864     {
       
   865     for ( TInt i=0; i<iObserverList.Count(); i++ )
       
   866         {
       
   867         iObserverList[i]->FileTransferEvent( aMessage );
       
   868         }
       
   869     delete aMessage;
       
   870     }
       
   871 
       
   872 // -----------------------------------------------------------------------------
       
   873 // CUpnpMediaServer::IsInterfaceUp
       
   874 // -----------------------------------------------------------------------------
       
   875 //
       
   876 TBool CUpnpMediaServer::IsInterfaceUp()
       
   877     {
       
   878     return ( iMsStatus == RUpnpMediaServerClient::EStartedOnline );
       
   879     }
       
   880 
       
   881 // -----------------------------------------------------------------------------
       
   882 // CUpnpMediaServer::CancelAVMediaServerStop
       
   883 // -----------------------------------------------------------------------------
       
   884 //
       
   885 void CUpnpMediaServer::CancelAVMediaServerStop()
       
   886     {
       
   887     iStopAVMediaServerTimer->Cancel();
       
   888     }
       
   889 
       
   890 // -----------------------------------------------------------------------------
       
   891 // CUpnpMediaServer::CanBeStopped
       
   892 // -----------------------------------------------------------------------------
       
   893 //
       
   894 TBool CUpnpMediaServer::CanBeStopped() const
       
   895     {
       
   896     return RUpnpMediaServerClient::EStopped == Status(); //note: Status(),not iMsStatus
       
   897     }
       
   898 
       
   899 // -----------------------------------------------------------------------------
       
   900 // CUpnpMediaServer::TimerEventL
       
   901 // -----------------------------------------------------------------------------
       
   902 //
       
   903 void CUpnpMediaServer::TimerEventL( CUpnpNotifyTimer* aTimer )
       
   904     {
       
   905     if ( aTimer == iStopAVMediaServerTimer )
       
   906         {
       
   907         StopAVMediaServerImmediately( iIsSilentShutdown, EFalse );
       
   908         }
       
   909     else
       
   910         {
       
   911         CUpnpSymbianServerBase::TimerEventL( aTimer );
       
   912         }
       
   913     }
       
   914 // -----------------------------------------------------------------------------
       
   915 // CUpnpMediaServer::SupportedVersion
       
   916 // -----------------------------------------------------------------------------
       
   917 //
       
   918 TVersion CUpnpMediaServer::SupportedVersion() const
       
   919     {
       
   920     return KServerSupportedVersion;
       
   921     }
       
   922 
       
   923 // -----------------------------------------------------------------------------
       
   924 // CUpnpMediaServer::ServerName
       
   925 // -----------------------------------------------------------------------------
       
   926 //
       
   927 const TDesC& CUpnpMediaServer::ServerName() const
       
   928     {
       
   929     return KMediaServerName;
       
   930     }
       
   931 
       
   932 // -----------------------------------------------------------------------------
       
   933 // CUpnpMediaServer::NetworkEvent
       
   934 // -----------------------------------------------------------------------------
       
   935 //
       
   936 void CUpnpMediaServer::NetworkEvent( CUpnpNetworkEventBase* aEvent )
       
   937     {
       
   938     LOG_FUNC_NAME;
       
   939 
       
   940     if ( aEvent->SubscriberError() < KErrNone && aEvent->Type() == EUnknownEvent )
       
   941         {
       
   942         LOGS( "CUpnpMediaServer::NetworkEvent error" );
       
   943         return;
       
   944         }
       
   945         
       
   946     switch ( aEvent->Type() )
       
   947         {
       
   948         case EWlanLostEvent:
       
   949             {
       
   950             if ( IsInterfaceUp() )
       
   951                 {
       
   952                 StopAVMediaServerImmediately( ETrue, ETrue );
       
   953                 NotifyStatusChanged();
       
   954                 }
       
   955             }
       
   956             break;
       
   957 
       
   958         case EAddressChangeEvent:
       
   959             {
       
   960             if ( iMsStatus == RUpnpMediaServerClient::EStartedOnline )
       
   961                 {
       
   962                 TRAPD( error, RestartAVMediaServerL() );
       
   963                 if ( error != KErrNone )
       
   964                     {
       
   965                     if ( iStopAVMediaServerTimer->IsActive() )
       
   966                         {
       
   967                         StopAVMediaServerImmediately( EFalse, EFalse );
       
   968                         }
       
   969                     TRAP( error, StartAVMediaServerL() );
       
   970                     if ( error != KErrNone )
       
   971                          {
       
   972                          StopAVMediaServerImmediately( EFalse, EFalse );
       
   973                          NotifyStatusChanged();
       
   974                          }
       
   975                      }
       
   976                 else if ( iContentDirecory )
       
   977                     {
       
   978                     TRAP( error, iContentDirecory->AddressChangeL() );
       
   979                     if ( error != KErrNone )
       
   980                          {
       
   981                          StopAVMediaServerImmediately( EFalse, EFalse );
       
   982                          NotifyStatusChanged();
       
   983                          } 
       
   984                     }                     
       
   985                 }
       
   986             }
       
   987             break; 
       
   988         
       
   989         case EConnectionClosedEvent:
       
   990             {
       
   991             ASSERT( iMsStatus != RUpnpMediaServerClient::EStartedOnline );
       
   992             }
       
   993             break;
       
   994         }
       
   995     }                
       
   996 
       
   997 // -----------------------------------------------------------------------------
       
   998 // CUpnpMediaServer::SetStatus
       
   999 // -----------------------------------------------------------------------------
       
  1000 //
       
  1001 void CUpnpMediaServer::SetStatus( RUpnpMediaServerClient::TServerStatus aStatus )
       
  1002     {
       
  1003     iMsStatus = aStatus;
       
  1004     }
       
  1005 
       
  1006 // -----------------------------------------------------------------------------
       
  1007 // CUpnpMediaServer::NotifyStatusChanged
       
  1008 // -----------------------------------------------------------------------------
       
  1009 //
       
  1010 void CUpnpMediaServer::NotifyStatusChanged()
       
  1011     {
       
  1012     CUpnpMediaServerSession* ses = NULL;
       
  1013     iSessionIter.SetToFirst();
       
  1014     CSession2* cses = iSessionIter++;
       
  1015     ses = reinterpret_cast<CUpnpMediaServerSession*>( cses );
       
  1016     while ( ses )
       
  1017         {
       
  1018         ses->StatusChanged();
       
  1019         ses = reinterpret_cast<CUpnpMediaServerSession*>( iSessionIter++ );    
       
  1020         }
       
  1021     }
       
  1022 
       
  1023 // -----------------------------------------------------------------------------
       
  1024 // CUpnpMediaServer::RunL
       
  1025 // -----------------------------------------------------------------------------
       
  1026 //
       
  1027 void CUpnpMediaServer::RunL()
       
  1028     {
       
  1029     const RUpnpMediaServerClient::TServerStatus beforeStatus = Status();
       
  1030     CUpnpSymbianServerBase::RunL();
       
  1031     if ( Status() != beforeStatus )
       
  1032         {
       
  1033         NotifyStatusChanged();
       
  1034         }
       
  1035     }
       
  1036 
       
  1037 // -----------------------------------------------------------------------------
       
  1038 // E32Main entry point.
       
  1039 // Returns: KErrNone
       
  1040 // -----------------------------------------------------------------------------
       
  1041 //
       
  1042 TInt E32Main()
       
  1043     {
       
  1044     return CUpnpMediaServer::StartServer(
       
  1045         KMediaServerThreadName, CUpnpMediaServer::NewLC );
       
  1046     }
       
  1047 
       
  1048 
       
  1049 // End of File