profilesapplication/Profiles/ProfileApp/SettingsViewSrc/CProfileToneHandlerDrmFull.cpp
branchRCL_3
changeset 19 cd54903d48da
equal deleted inserted replaced
18:b7fa36b488f8 19:cd54903d48da
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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:  A helper interface to be used by S60 applications for
       
    15 *                setting ringing and alert tones for different profiles.
       
    16 *                Registers DRM protected tones as automated content
       
    17 *                and removes old content from automated content list.
       
    18 *                Shows a content activation query if necessary.
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 #include    "cprofiletonehandler.h"
       
    26 
       
    27 #include    <DRMHelper.h>
       
    28 #include    <DRMCommon.h>
       
    29 #include    <profilesettingsview.rsg>
       
    30 
       
    31 #ifdef RD_DRM_COMMON_INTERFACE_FOR_OMA_AND_WMDRM
       
    32 #include	<drmutility.h>
       
    33 #include	<drmagents.h>
       
    34 #endif
       
    35 
       
    36 #include <caf/caf.h>
       
    37 
       
    38 
       
    39 // ============================ LOCAL FUNCTIONS ================================
       
    40 
       
    41 CDRMHelper::TDRMHelperAutomatedType ToneSettingToAutomatedType(
       
    42         TProfileTones aToneSetting )
       
    43     {
       
    44     switch( aToneSetting )
       
    45         {
       
    46         case EProfileRingingToneSetting:
       
    47             {
       
    48             return CDRMHelper::EAutomatedTypeRingingTone;
       
    49             }
       
    50         case EProfileMessageAlertToneSetting:
       
    51             {
       
    52             return CDRMHelper::EAutomatedTypeMessageAlert;
       
    53             }
       
    54         case EProfileEmailAlertToneSetting:
       
    55             {
       
    56             return CDRMHelper::EAutomatedTypeEmailAlert;
       
    57             }
       
    58         case EProfileInstantMessageAlertToneSetting:
       
    59             {
       
    60             return CDRMHelper::EAutomatedTypeIMAlert;
       
    61             }
       
    62         default:
       
    63             {
       
    64             return CDRMHelper::EAutomatedTypeOther;
       
    65             }
       
    66         }
       
    67     }
       
    68 
       
    69 // ============================ MEMBER FUNCTIONS ===============================
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CProfileToneHandler::DrmConstructL
       
    73 // 2nd phase construct for DRM objects
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CProfileToneHandler::DrmConstructL()
       
    77     {
       
    78     iDrmCommon = DRMCommon::NewL();
       
    79     iDrmHelper = CDRMHelper::NewL();
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CProfileToneHandler::ReleaseDrm
       
    84 // Destructor for DRM objects
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CProfileToneHandler::ReleaseDrm()
       
    88     {
       
    89     delete iDrmHelper;
       
    90     iDrmHelper = NULL;
       
    91     delete iDrmCommon;
       
    92     iDrmCommon = NULL;
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CProfileToneHandler::CheckProtectedFileL
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 TInt CProfileToneHandler::CheckProtectedFileL( const TDesC& aFileName )
       
   100     {
       
   101     DRMCommon::TContentProtection protection;
       
   102     HBufC8* mimeType = NULL; // ignored
       
   103     HBufC8* contentUri = NULL;
       
   104     TUint dataLength; // ignored
       
   105     TInt err = iDrmCommon->GetFileInfo( aFileName, protection, mimeType,
       
   106                                         contentUri, dataLength );
       
   107     delete mimeType;
       
   108     if( err )
       
   109         {
       
   110         delete contentUri;
       
   111         User::Leave( err );
       
   112         }
       
   113     CleanupStack::PushL( contentUri );
       
   114 
       
   115     TInt result( KErrGeneral );
       
   116     CDRMRights* rights = NULL;
       
   117     err = iDrmCommon->GetActiveRights( *contentUri, DRMCommon::EPlay, rights );
       
   118 
       
   119     CleanupStack::PopAndDestroy( contentUri );
       
   120 
       
   121     if( rights == NULL )
       
   122         {
       
   123         if( err == DRMCommon::ENoRights )
       
   124             {
       
   125             // Handle "No rights" situation here
       
   126             iDrmHelper->HandleErrorL( DRMCommon::ENoRights, aFileName );
       
   127             return result;
       
   128             }
       
   129         else
       
   130             {
       
   131             User::Leave( KErrGeneral );
       
   132             }
       
   133         }
       
   134 
       
   135     CleanupStack::PushL( rights );
       
   136 
       
   137     switch ( err )
       
   138 		{
       
   139 		case CDRMRights::EFullRights: // Fall through
       
   140 		case CDRMRights::ERestrictedRights:
       
   141 			{
       
   142 			CDRMRights::TRestriction restriction; // ignore
       
   143 			CDRMRights::TExpiration expiration;
       
   144 			TUint32 constType;
       
   145 			
       
   146 			rights->GetRightsInfo( DRMCommon::EPlay, restriction, // ignore return
       
   147 								expiration, constType );       // value
       
   148 		
       
   149 			// This is CFM protected file, ringingtone is set to "no"
       
   150 			if ( ( rights->GetPermission().iInfoBits & ENoRingingTone )
       
   151 				// This is normal DRM protected count-based tone
       
   152 				|| ( constType & CDRMRights::ECountBased ) )
       
   153 				{
       
   154 				ShowErrorNoteL( R_PROFILE_TEXT_DRM_PREV_RIGHTS_SET );
       
   155 				}		
       
   156 			else
       
   157 				{
       
   158 				switch ( expiration )
       
   159 					{
       
   160 					case CDRMRights::EValidRights:
       
   161 						{
       
   162 						result = KErrNone;
       
   163 						break;
       
   164 						}
       
   165 
       
   166 					case CDRMRights::EFutureRights:
       
   167 						{
       
   168 						iDrmHelper->HandleErrorL( DRMCommon::ENoRights, aFileName );
       
   169 						break;
       
   170 						}
       
   171 
       
   172 					case CDRMRights::EExpiredRights: 
       
   173 						{
       
   174 						iDrmHelper->HandleErrorL( DRMCommon::ERightsExpired, aFileName );
       
   175 						break;
       
   176 						}
       
   177 
       
   178 					default:
       
   179 						{
       
   180 						break;
       
   181 						}
       
   182 					}	
       
   183 
       
   184 				}
       
   185 				
       
   186 
       
   187 			break;
       
   188 			}
       
   189 
       
   190 		case CDRMRights::EPreviewRights:
       
   191 			{
       
   192 			ShowErrorNoteL( R_PROFILE_TEXT_DRM_PREV_RIGHTS_SET );
       
   193 			break;
       
   194 			}
       
   195 
       
   196 		case DRMCommon::ENoRights:
       
   197 			{
       
   198 			iDrmHelper->HandleErrorL( DRMCommon::ENoRights, aFileName );
       
   199 			break;
       
   200 			}
       
   201 
       
   202 		default:
       
   203 			{
       
   204 			break;
       
   205 			}
       
   206 
       
   207 		}
       
   208    
       
   209     CleanupStack::PopAndDestroy( rights );
       
   210 
       
   211     return result;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CProfileToneHandler::CanSetAutomated
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TBool CProfileToneHandler::CanSetAutomated( const TDesC& aFileName )
       
   219     {
       
   220     TBool canSetAutomated( EFalse );
       
   221     return
       
   222         iDrmHelper->CanSetAutomated( aFileName, canSetAutomated ) == KErrNone &&
       
   223         canSetAutomated;
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CProfileToneHandler::AskAutomated
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 TInt CProfileToneHandler::AskAutomated( const TDesC& aFileName )
       
   231     {
       
   232     return iDrmHelper->ShowAutomatedNote( aFileName );
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CProfileToneHandler::SetAutomated
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 TInt CProfileToneHandler::SetAutomated( TProfileTones aToneSetting,
       
   240                                         const TDesC& aFileName )
       
   241     {
       
   242     TInt err = KErrNone;
       
   243     
       
   244     TRAP_IGNORE( err = SetAutomatedL( aToneSetting, aFileName ) );
       
   245 
       
   246     return err;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CProfileToneHandler::SetAutomatedL
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 TInt CProfileToneHandler::SetAutomatedL( TProfileTones aToneSetting,
       
   254                                         const TDesC& aFileName )
       
   255     {
       
   256     iDrmHelper->SetAutomatedType( ToneSettingToAutomatedType( aToneSetting ) );
       
   257     TInt err( iDrmHelper->SetAutomatedSilent( aFileName, EFalse ) );
       
   258     if( err == KErrNone )
       
   259         {
       
   260         using namespace ContentAccess;
       
   261        	TVirtualPathPtr path( aFileName, KDefaultContentObject );
       
   262         CData* data = CData::NewLC( path, EContentShareReadWrite );
       
   263         err = data->ExecuteIntent( EPlay );
       
   264         CleanupStack::PopAndDestroy( data );
       
   265         // According to Risto Vilkman from DRM, KErrAccessDenied
       
   266         // can be ignored at this point, since if
       
   267         // CanSetAutomated says the tone is OK, it's OK.
       
   268         if( err == KErrAccessDenied )
       
   269         	{
       
   270         	err = KErrNone;
       
   271         	}
       
   272         }
       
   273     return err;
       
   274     }
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CProfileToneHandler::RemoveAutomated
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CProfileToneHandler::RemoveAutomated( const TDesC& aFileName )
       
   281     {
       
   282     iDrmHelper->RemoveAutomatedPassive( aFileName ); // ignore return value
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CProfileToneHandler::IsProtected
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TBool CProfileToneHandler::IsProtected( const TDesC& aFileName ) const
       
   290     {
       
   291     TBool result = EFalse;
       
   292     TRAP_IGNORE( result = IsProtectedL( aFileName ) );
       
   293     return result;
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CProfileToneHandler::IsProtectedL
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 TBool CProfileToneHandler::IsProtectedL( const TDesC& aFileName ) const
       
   301     {
       
   302     TInt isProtected( EFalse );
       
   303    	ContentAccess::TVirtualPathPtr path( aFileName,
       
   304    						ContentAccess::KDefaultContentObject );
       
   305    	CData* data = CData::NewLC( path, EContentShareReadWrite );
       
   306    	TInt result = data->GetAttribute( EIsProtected, isProtected );
       
   307    	CleanupStack::PopAndDestroy(); // data
       
   308 
       
   309     return( ( result == DRMCommon::EOk ) && isProtected );
       
   310     }
       
   311 
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 // Functions related to WMDRM protection check
       
   316 //
       
   317 // -----------------------------------------------------------------------------
       
   318 
       
   319 #ifndef RD_DRM_COMMON_INTERFACE_FOR_OMA_AND_WMDRM
       
   320 
       
   321 // Some magic constants
       
   322 static const TInt KMinContentLength( 16 );
       
   323 //_LIT8( KContentProtectionType, "DRM" );
       
   324 _LIT8( KASFHeaderObject, "75B22630668E11CFA6D900AA0062CE6C" );
       
   325 _LIT8( KWrmHeader, "W\0R\0M\0H\0E\0A\0D\0E\0R\0" );
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // FormatGUID
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 LOCAL_C void FormatGUID( TDes8 &aGUID )
       
   332     {
       
   333     TBuf8<16> copyGUID( aGUID );
       
   334     TInt i;
       
   335     for( i = 0; i < 4; i++ )
       
   336         {
       
   337         copyGUID[i] = aGUID[3-i];
       
   338         }
       
   339     for( i = 4; i < 6; i++ )
       
   340         {
       
   341         copyGUID[i] = aGUID[9 - i];
       
   342         }
       
   343     for( i = 6; i < 8; i++ )
       
   344         {
       
   345         copyGUID[i] = aGUID[13 - i];
       
   346         }
       
   347     for( i = 8; i < 16 ; i++ )
       
   348         {
       
   349         copyGUID[i] = aGUID[i];
       
   350         }
       
   351     aGUID.Delete( 0, 32 );
       
   352     for( i = 0; i <16; i++ )
       
   353         {
       
   354         aGUID.AppendNumFixedWidthUC( copyGUID[i], EHex, 2 );
       
   355         }
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // ConvertToInt64
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 LOCAL_C TInt64 ConvertToInt64( TDesC8& aDes )
       
   363     {
       
   364     TInt64 num = 0;
       
   365     TInt i;
       
   366     for( i = 7 ; i >= 0; i-- )
       
   367         {
       
   368         num <<= 8;
       
   369         num |= aDes[i];
       
   370         }
       
   371     return num;
       
   372     }
       
   373 
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // IsProtectedWmDrmL
       
   377 // returns ETrue, if file is protected WMDRM file
       
   378 //         EFalse if file is not protected WMDRM file
       
   379 // Leaves with KErrUnderflow if file has too little data to decide
       
   380 //         whether WmDrm or not
       
   381 //         may also leave with other system wide error code
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 LOCAL_C TBool IsProtectedWmDrmL( RFile& aFileHandle )
       
   385     {
       
   386     TInt r( KErrNone );
       
   387     HBufC8* buffer( NULL );
       
   388     TInt pos( 0 );
       
   389     RFile file;
       
   390     TBuf8< 32 > header;
       
   391 
       
   392     TInt64 headerSize( 0 );
       
   393     TBool isProtectedWmDrm( EFalse );
       
   394     TPtr8 headerPtr( NULL, 0 );
       
   395 
       
   396     // Leave if given handle is invalid
       
   397     if( !aFileHandle.SubSessionHandle() )
       
   398         {
       
   399         User::Leave( KErrBadHandle );
       
   400         }
       
   401 
       
   402     User::LeaveIfError( file.Duplicate( aFileHandle ) );
       
   403     CleanupClosePushL( file );
       
   404 
       
   405     User::LeaveIfError( file.Seek( ESeekStart, pos ) );
       
   406 
       
   407     // Check if the file is an ASF file
       
   408     // Check on runtime wether WM DRM is supporeted or not
       
   409 
       
   410     User::LeaveIfError( file.Read( 0, header, KMinContentLength ) );
       
   411     if( header.Length() < KMinContentLength )
       
   412         {
       
   413         User::Leave( KErrUnderflow );
       
   414         }
       
   415 
       
   416     FormatGUID( header );
       
   417 
       
   418     if( header == KASFHeaderObject )
       
   419         {
       
   420         // It's ASF, check still whether it's WM DRM protected or not
       
   421         file.Read( header,8 );
       
   422         headerSize = ConvertToInt64( header );
       
   423         if( headerSize <= 30 )
       
   424             {
       
   425             User::Leave( KErrUnderflow );
       
   426             }
       
   427         if ( headerSize > ( ( KMaxTInt32 / 2 ) - 1 ) )
       
   428             {
       
   429             User::Leave( KErrOverflow );
       
   430             }
       
   431         buffer = HBufC8::NewLC( headerSize );
       
   432 
       
   433         headerPtr.Set( buffer->Des() );
       
   434         User::LeaveIfError( file.Read( headerPtr, headerSize - 24 ) );
       
   435 
       
   436         r = headerPtr.Find( KWrmHeader );
       
   437         if ( KErrNotFound != r )
       
   438             {
       
   439             isProtectedWmDrm = ETrue;
       
   440             }
       
   441         CleanupStack::PopAndDestroy( buffer ); // buffer
       
   442         }
       
   443         CleanupStack::PopAndDestroy(); // file
       
   444 
       
   445     return isProtectedWmDrm;
       
   446     }
       
   447 
       
   448 #endif // RD_DRM_COMMON_INTERFACE_FOR_OMA_AND_WMDRM
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 // CProfileToneHandler::IsFileWMDRMProtectedL
       
   453 //
       
   454 // -----------------------------------------------------------------------------
       
   455 //
       
   456 TBool CProfileToneHandler::IsFileWMDRMProtectedL( const TDesC& aFileName )
       
   457 	{
       
   458 	TBool res = EFalse;
       
   459 	RFile hFile;
       
   460 
       
   461 	TInt err = hFile.Open( iFs, aFileName, 
       
   462 						EFileRead | EFileStream | EFileShareReadersOnly );
       
   463 	if( err == KErrInUse )
       
   464 		{
       
   465 		err = hFile.Open( iFs, aFileName, 
       
   466 						EFileRead | EFileStream | EFileShareAny );
       
   467 		}
       
   468 	if( err != KErrNone )
       
   469 		{
       
   470 		User::Leave( err );
       
   471 		}
       
   472 	CleanupClosePushL( hFile );
       
   473 
       
   474 #ifdef RD_DRM_COMMON_INTERFACE_FOR_OMA_AND_WMDRM
       
   475 	TPtrC agent( KNullDesC );
       
   476 	DRM::CDrmUtility* drmUtil( DRM::CDrmUtility::NewLC() );
       
   477 	drmUtil->GetAgentL( hFile, agent );
       
   478 	if( agent.Compare( DRM::KDrmWMAgentName ) == 0 )
       
   479 		{
       
   480 		res = ETrue;
       
   481 		}
       
   482 	CleanupStack::PopAndDestroy( drmUtil );
       
   483 #else
       
   484 	res = IsProtectedWmDrmL( hFile );
       
   485 #endif
       
   486 
       
   487 	CleanupStack::PopAndDestroy( &hFile );
       
   488 	return res;
       
   489 	}
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 // End of Functions related to WMDRM protection check
       
   494 //
       
   495 // -----------------------------------------------------------------------------
       
   496 
       
   497 //  End of File