upnpmediaserver/contentdirectoryservice/src/upnpmspathutility.cpp
changeset 0 7f85d04be362
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 *
       
    15 * Description:  Utility class to get path related info for the media files
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <bautils.h>    
       
    20 #include <upnpstring.h>
       
    21 #include <upnpdlnaprotocolinfo.h>
       
    22 #include <pathinfo.h>
       
    23 #include <centralrepository.h>
       
    24 
       
    25 #include "upnpmspathutility.h"
       
    26 #include "upnpcontentdirectoryglobals.h"
       
    27 #include "upnpcdutils.h"
       
    28 #include "upnpdominterface.h"
       
    29 
       
    30 // CONSTANTS
       
    31 _LIT( KYearMonthDayFormat,        "%04d\\%02d\\%02d\\");
       
    32 _LIT( KArtistFormat,              "%S\\");
       
    33 _LIT( KAlbumFormat,               "%S\\");
       
    34 _LIT( KBackSlash,                 "\\");
       
    35 _LIT( KSlashData,                 "\\Data\\");
       
    36 _LIT( KUnknown,                   "Unknown");
       
    37 
       
    38 _LIT( KSeparator,                 ":" );
       
    39 _LIT( KNullTime,                  "000000" );
       
    40 
       
    41 _LIT8( KMimeAudio,                "audio" );
       
    42 _LIT8( KMimeVideo,                "video" );
       
    43 _LIT8( KMimeImage,                "image" );
       
    44 _LIT8( KMimePlaylist,             "audio/mpegurl" );
       
    45 
       
    46 
       
    47 const TUid KCRUidMediaServer   = { 0x101F978F };
       
    48 const TInt KDateStringLength        = 10;
       
    49 const TInt KDateTimeStringLength    = 19;
       
    50 const TInt KMaxDateStringLength     = 30;
       
    51 
       
    52 // ============================ MEMBER FUNCTIONS ============================
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CUPnPMSPathUtility::CUPnPMSPathUtility
       
    56 // C++ default constructor can NOT contain any code, that
       
    57 // might leave.
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CUPnPMSPathUtility::CUPnPMSPathUtility()
       
    61     {
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // CUPnPMSPathUtility::NewL
       
    66 // Two-phased constructor.
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CUPnPMSPathUtility* CUPnPMSPathUtility::NewL()
       
    70     {
       
    71     CUPnPMSPathUtility* self = CUPnPMSPathUtility::NewLC();
       
    72     CleanupStack::Pop();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CUPnPMSPathUtility::NewLC
       
    78 // Two-phased constructor.
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CUPnPMSPathUtility* CUPnPMSPathUtility::NewLC()
       
    82     {
       
    83     CUPnPMSPathUtility* self = new( ELeave ) CUPnPMSPathUtility;
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( KCRUidMediaServer );
       
    86        return self;
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CUPnPMSPathUtility::ConstructL
       
    91 // Symbian 2nd phase constructor can leave.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void CUPnPMSPathUtility::ConstructL(TUid aRepositoryUid)
       
    95     {
       
    96     iRepository = CRepository::NewL(aRepositoryUid);
       
    97     }
       
    98     
       
    99 // ---------------------------------------------------------------------------
       
   100 // CUPnPMSPathUtility::~CUPnPMSPathUtility()
       
   101 // Destructor
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 CUPnPMSPathUtility::~CUPnPMSPathUtility()
       
   105     {
       
   106     delete iRepository;
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CUPnPMSPathUtility::GetCopyPathDriveL
       
   111 // Gets the drive for the copy operation
       
   112 // (other items were commented in a header).
       
   113 // --------------------------------------------------------------------------
       
   114 //
       
   115 void CUPnPMSPathUtility::GetCopyPathDriveL( 
       
   116              TDriveNumber& aDrive ) const
       
   117     {
       
   118     HBufC* location = HBufC::NewL(KMaxFileName);
       
   119     CleanupStack::PushL( location );
       
   120 
       
   121     TPtr locPtr = location->Des();
       
   122     
       
   123     // Get the location setting from ServerSettings
       
   124     iRepository->Get( EUploadDirectory, locPtr );
       
   125 
       
   126     // Check to see if drive is ready.
       
   127     TVolumeInfo volInfo;
       
   128     TDriveUnit driveUnit = TDriveUnit( locPtr );
       
   129     
       
   130     RFs fileServer;
       
   131     User::LeaveIfError( fileServer.Connect() );
       
   132     CleanupClosePushL( fileServer );
       
   133     User::LeaveIfError( fileServer.Volume( volInfo, driveUnit ) );
       
   134     CleanupStack::PopAndDestroy( &fileServer );
       
   135 
       
   136     aDrive = (TDriveNumber)((TInt)driveUnit);
       
   137 
       
   138     CleanupStack::PopAndDestroy( location );
       
   139     }
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CUPnPMSPathUtility::GetCopyPathL
       
   143 // Returns the path of the upnp item to be copied
       
   144 // (other items were commented in a header).
       
   145 // --------------------------------------------------------------------------
       
   146 //
       
   147 HBufC* CUPnPMSPathUtility::GetCopyPathL(
       
   148                         const TXmlEngElement& aItem,
       
   149                         CUpnpDlnaProtocolInfo& aProInfo ) const
       
   150     {
       
   151     TDriveNumber drive;
       
   152     GetCopyPathDriveL( drive );   
       
   153     return GetCreateCopyPathL( aItem, 
       
   154                                aProInfo, 
       
   155                                EFalse,
       
   156                                drive );
       
   157     }   
       
   158 
       
   159 // --------------------------------------------------------------------------
       
   160 // CUPnPMSPathUtility::CreateCopyPathL
       
   161 // Returns the path of the upnp item to be copied
       
   162 // Creates the path if necessary and appends the filename and extension if
       
   163 // required
       
   164 // (other items were commented in a header).
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 HBufC* CUPnPMSPathUtility::CreateCopyPathL(
       
   168                 const TXmlEngElement& aItem,
       
   169                 CUpnpDlnaProtocolInfo& aProInfo ) const
       
   170     {
       
   171     TDriveNumber drive;
       
   172     GetCopyPathDriveL( drive );
       
   173     return GetCreateCopyPathL( aItem, 
       
   174                                aProInfo, 
       
   175                                ETrue,
       
   176                                drive );
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CUPnPMSPathUtility::GetCreateCopyPathL
       
   181 // Returns the path of the upnp item to be copied
       
   182 // Creates the path if necessary and appends the filename and extension if
       
   183 // required
       
   184 // (other items were commented in a header).
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 HBufC* CUPnPMSPathUtility::GetCreateCopyPathL(
       
   188                const TXmlEngElement& aItem,
       
   189                CUpnpDlnaProtocolInfo& aProInfo,
       
   190                TBool aCreatePath,
       
   191                TDriveNumber aDriveNumber ) const
       
   192     {
       
   193        
       
   194     HBufC* path = HBufC::NewLC( KMaxPath );
       
   195     TPtr refPath = path->Des();
       
   196     
       
   197     TDriveUnit driveUnit = TDriveUnit( aDriveNumber );
       
   198     AppendDataL( refPath, driveUnit.Name() );
       
   199     if ( EDriveC == driveUnit )
       
   200         {
       
   201         //C:\\Data\\(Images/Videos/Sounds)....
       
   202         AppendDataL( refPath, KSlashData );
       
   203         }
       
   204     else
       
   205         {
       
   206         //\\(Images/Videos/Sounds)....
       
   207         AppendDataL( refPath, KBackSlash );
       
   208         }
       
   209     
       
   210     // Get the protocolinfo-attribute
       
   211     TUPnPItemType fileType = FileTypeByMimeTypeL( 
       
   212             aProInfo.ThirdField() );
       
   213     
       
   214     switch( fileType )
       
   215         {
       
   216         case ETypeAudio:
       
   217             {
       
   218             AppendDataL( refPath, PathInfo::SoundsPath() );
       
   219             AppendArtistAlbumL( refPath, aItem );
       
   220             break;
       
   221             }
       
   222         case ETypeVideo:
       
   223             {
       
   224             AppendDataL( refPath, PathInfo::VideosPath() );
       
   225             AppendYearMonthDayL( refPath, aItem );
       
   226             break;
       
   227             }
       
   228         case ETypeImage:
       
   229             {
       
   230             AppendDataL( refPath, PathInfo::ImagesPath() );
       
   231             AppendYearMonthDayL( refPath, aItem );
       
   232             break;
       
   233             }
       
   234         case ETypePlaylist:
       
   235         case ETypeOther:
       
   236         default:
       
   237             {
       
   238             AppendDataL( refPath, PathInfo::OthersPath() );
       
   239             }                
       
   240         }
       
   241     if( aCreatePath )
       
   242         {
       
   243         RFs fs;
       
   244         User::LeaveIfError( fs.Connect() );
       
   245         CleanupClosePushL(fs);
       
   246         BaflUtils::EnsurePathExistsL( fs, refPath );
       
   247         CleanupStack::PopAndDestroy(&fs);
       
   248         }
       
   249     CleanupStack::Pop( path );
       
   250     
       
   251     return path;
       
   252     }   
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CUPnPMSPathUtility::FileTypeByMimeTypeL
       
   256 // Get the file type based on MIME type of the file. Leaves in error cases.
       
   257 // --------------------------------------------------------------------------
       
   258 CUPnPMSPathUtility::TUPnPItemType CUPnPMSPathUtility::FileTypeByMimeTypeL(
       
   259         const TDesC8& aMimeType ) const
       
   260     {
       
   261     TUPnPItemType returnValue = ETypeOther;
       
   262 
       
   263     if( aMimeType != KNullDesC8 )
       
   264         {
       
   265         if( aMimeType.Find( KMimeImage() ) >= 0 )
       
   266             {
       
   267             returnValue = ETypeImage;
       
   268             }
       
   269         else if( aMimeType.Find( KMimePlaylist() ) >= 0 )
       
   270             {
       
   271             returnValue = ETypePlaylist;
       
   272             }
       
   273         else if( aMimeType.Find( KMimeAudio() ) >= 0 )
       
   274             {
       
   275             returnValue = ETypeAudio;
       
   276             }
       
   277         else if( aMimeType.Find( KMimeVideo() ) >= 0 )
       
   278             {
       
   279             returnValue = ETypeVideo;
       
   280             }
       
   281         else
       
   282             {
       
   283             returnValue = ETypeOther;
       
   284             }
       
   285         }
       
   286     else
       
   287         {
       
   288         User::Leave( KErrArgument );
       
   289         }
       
   290 
       
   291     return returnValue;
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CUPnPMSPathUtility::AppendYearMonthDayL
       
   296 // Appends year, month and day to the path.
       
   297 // --------------------------------------------------------------------------
       
   298 //
       
   299 void CUPnPMSPathUtility::AppendYearMonthDayL( 
       
   300         TDes& aPath, const TXmlEngElement& aItem ) const
       
   301     {  
       
   302     // Get the date-element
       
   303     TXmlEngElement dateElem;
       
   304     UpnpDomInterface::GetElementL( aItem, dateElem, KDate8 );
       
   305     TTime date; date.HomeTime();
       
   306     if ( !dateElem.IsNull( ) )
       
   307         {
       
   308         UPnPDateAsTTimeL( dateElem.Value(), date );
       
   309         }     
       
   310     
       
   311     TDateTime ymd = date.DateTime();
       
   312     CheckBufferSpaceL( aPath, 11 ); //4(year)+2(month)+2(day)+3(\)
       
   313     
       
   314     aPath.AppendFormat( KYearMonthDayFormat(), 
       
   315             ymd.Year(), ymd.Month()+1, ymd.Day()+1 );
       
   316     
       
   317     }
       
   318 
       
   319 // --------------------------------------------------------------------------
       
   320 // CUPnPMSPathUtility::AppendArtistAlbumL
       
   321 // Appends artist and album to the path.
       
   322 // --------------------------------------------------------------------------
       
   323 //
       
   324 void CUPnPMSPathUtility::AppendArtistAlbumL( 
       
   325         TDes& aPath, const TXmlEngElement& aItem ) const
       
   326     {
       
   327     
       
   328     // Get the artist-element
       
   329     TXmlEngElement artistElem;
       
   330     UpnpDomInterface::GetElementL( aItem, artistElem, KArtist );
       
   331     if ( !artistElem.IsNull( )  )
       
   332         {
       
   333         HBufC* artist = UpnpString::ToUnicodeL( artistElem.Value() );
       
   334         CleanupStack::PushL( artist );
       
   335         
       
   336         TPtr ptrArtist = artist->Des();
       
   337         UpnpCdUtils::ValidateFilePath( ptrArtist );
       
   338         
       
   339         CheckBufferSpaceL( aPath, artist->Length()+1 );// 1 for '\'          
       
   340         aPath.AppendFormat( KArtistFormat(), artist );
       
   341         
       
   342         CleanupStack::PopAndDestroy( artist );
       
   343         }
       
   344     else
       
   345         {
       
   346         CheckBufferSpaceL( aPath, KUnknown().Length()+1 );  // 1 for '\'       
       
   347         aPath.AppendFormat( KArtistFormat(), &KUnknown() );
       
   348         }
       
   349     
       
   350     // Get the album-element
       
   351     TXmlEngElement albumElem;
       
   352     UpnpDomInterface::GetElementL( aItem, albumElem, KAlbum );
       
   353     if ( !albumElem.IsNull( ) )
       
   354         {
       
   355         HBufC* album = UpnpString::ToUnicodeL( albumElem.Value() );
       
   356         CleanupStack::PushL( album );
       
   357         
       
   358         TPtr ptrAlbum = album->Des();
       
   359         UpnpCdUtils::ValidateFilePath( ptrAlbum );
       
   360         
       
   361         CheckBufferSpaceL( aPath, album->Length()+1 );// 1 for '\'
       
   362         aPath.AppendFormat( KAlbumFormat(), album );
       
   363         CleanupStack::PopAndDestroy( album );
       
   364         }
       
   365     else
       
   366         {
       
   367         CheckBufferSpaceL( aPath, KUnknown().Length()+1 );  // 1 for '\'       
       
   368         aPath.AppendFormat( KAlbumFormat(), &KUnknown() );
       
   369         }
       
   370     
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CUPnPMSPathUtility::AppendDataL
       
   375 // Appends data to the path's buffer.
       
   376 // --------------------------------------------------------------------------
       
   377 //
       
   378 void CUPnPMSPathUtility::AppendDataL( 
       
   379         TDes& aPath, const TDesC& aData ) const
       
   380     {  
       
   381     CheckBufferSpaceL( aPath, aData );
       
   382     aPath.Append( aData );
       
   383     }
       
   384 
       
   385 // --------------------------------------------------------------------------
       
   386 // CUPnPMSPathUtility::CheckBufferSpaceL
       
   387 // Checks whether the data can be appended to buffer or not.
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 void CUPnPMSPathUtility::CheckBufferSpaceL( 
       
   391         const TDes& aPath, const TDesC& aData ) const
       
   392     {
       
   393     CheckBufferSpaceL( aPath, aData.Length() );
       
   394     }
       
   395 
       
   396 // --------------------------------------------------------------------------
       
   397 // CUPnPMSPathUtility::CheckBufferSpaceL
       
   398 // Checks whether the data of the specified length
       
   399 // can be appended to buffer or not.
       
   400 // --------------------------------------------------------------------------
       
   401 //
       
   402 void CUPnPMSPathUtility::CheckBufferSpaceL( 
       
   403         const TDes& aPath, const TInt& aLength ) const
       
   404     {  
       
   405     if ( (aPath.Length() + aLength) > aPath.MaxLength() )
       
   406         {
       
   407         User::Leave( KErrOverflow );
       
   408         }
       
   409     }
       
   410 
       
   411 // --------------------------------------------------------------------------
       
   412 // CUPnPMSPathUtility::UPnPDateAsTTimeL
       
   413 // Converts upnp date to TTime object.
       
   414 //---------------------------------------------------------------------------
       
   415 void CUPnPMSPathUtility::UPnPDateAsTTimeL( const TDesC8& aUpnpDate,
       
   416     TTime& aTime ) const
       
   417     {
       
   418     // This method is capable of handling the most common dc:date formats:
       
   419     // CCYY-MM-DD and CCYY-MM-DDThh:mm:ss
       
   420     // Rest of the dc:date formats are handled as well, but they might not
       
   421     // be converted precisely
       
   422     
       
   423     TBuf<KMaxDateStringLength> formatDateString;
       
   424     HBufC* dateString = HBufC::NewL( aUpnpDate.Length() );
       
   425     dateString->Des().Copy( aUpnpDate );
       
   426 
       
   427     if( aUpnpDate.Length() >= KDateStringLength )
       
   428         {
       
   429         // CCYY-MM-DD --> CCYYMMDD
       
   430         formatDateString.Copy( dateString->Des().Left( 4 ) ); // Year
       
   431         formatDateString.Append( dateString->Des().Mid( 5,2 ) ); // Month
       
   432         formatDateString.Append( dateString->Des().Mid( 8,2 ) ); // Day        
       
   433 
       
   434         if( aUpnpDate.Length() >= KDateTimeStringLength )
       
   435             {
       
   436             // hh:mm:ss --> hhmmss
       
   437             formatDateString.Append( KSeparator );
       
   438             // Hours
       
   439             formatDateString.Append( dateString->Des().Mid( 11, 2 ) ); 
       
   440             // Minutes
       
   441             formatDateString.Append( dateString->Des().Mid( 14, 2 ) );
       
   442             // Seconds 
       
   443             formatDateString.Append( dateString->Des().Mid( 17, 2 ) ); 
       
   444             }
       
   445         else
       
   446             {
       
   447             // hh:mm:ss --> 000000
       
   448             formatDateString.Append( KSeparator );
       
   449             formatDateString.Append( KNullTime );
       
   450             }
       
   451         }
       
   452     delete dateString;
       
   453     
       
   454     User::LeaveIfError( aTime.Set( formatDateString ) );
       
   455     }
       
   456 
       
   457 //  End of File