svgtviewer/SvgtViewerPlugin/UIControlSrc/SVGTDrmHelper.cpp
branchRCL_3
changeset 20 5fd161fa28b6
parent 0 632761c941a7
equal deleted inserted replaced
17:e52958d06c29 20:5fd161fa28b6
       
     1 /*
       
     2 * Copyright (c) 2004,2005 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:  DRM helper for svgt files.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <DRMHelper.h>
       
    22 #include    <bamdesca.h>
       
    23 #include    <bautils.h>
       
    24 #include    <coemain.h>
       
    25 #include    <StringLoader.h>
       
    26 #include    <aknnotewrappers.h>
       
    27 #include    <caf/content.h>
       
    28 #include    <caf/attribute.h>
       
    29 #include    <caf/caftypes.h>
       
    30 #include    <caf/caferr.h>
       
    31 #include    <Oma2Agent.h>
       
    32 
       
    33 #include    "SVGTDrmHelper.h"
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 const TUint32 KSVGTCountConstraint = 3;
       
    38 const TInt KSVGTTimeConstraint = 7;
       
    39 const TInt KSVGTSecondsInDay = 86400;
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CSVGTDrmHelper::CSVGTDrmHelper
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CSVGTDrmHelper::CSVGTDrmHelper() :      
       
    51     iEnv(CCoeEnv::Static())
       
    52     {
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CSVGTDrmHelper::ConstructL
       
    57 // Symbian 2nd phase constructor can leave.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 void CSVGTDrmHelper::ConstructL()
       
    61     {
       
    62     iDrmHelper = CDRMHelper::NewL( *iEnv );
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSVGTDrmHelper::NewL
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CSVGTDrmHelper* CSVGTDrmHelper::NewL()
       
    71     {
       
    72     CSVGTDrmHelper* self = CSVGTDrmHelper::NewLC();    
       
    73     CleanupStack::Pop( self );
       
    74     return self;
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CSVGTDrmHelper::NewLC
       
    79 // Two-phased constructor.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CSVGTDrmHelper* CSVGTDrmHelper::NewLC()
       
    83     {
       
    84     CSVGTDrmHelper* self = new(ELeave) CSVGTDrmHelper();    
       
    85     CleanupStack::PushL(self);
       
    86     self->ConstructL();
       
    87     return self;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CSVGTDrmHelper::~CSVGTDrmHelper
       
    92 // Destructor
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CSVGTDrmHelper::~CSVGTDrmHelper()
       
    96     {
       
    97     delete iRightsConstraints;  
       
    98 
       
    99     // Delete the DRM Helper
       
   100     delete iDrmHelper;
       
   101     // Reset the environment pointer
       
   102     iEnv = NULL;
       
   103     }
       
   104     
       
   105 // -----------------------------------------------------------------------------
       
   106 // CSVGTDrmHelper::IsPreviewL
       
   107 // -----------------------------------------------------------------------------
       
   108 //        
       
   109 TBool CSVGTDrmHelper::IsPreviewL( RFile& aFileHandle )    
       
   110     {
       
   111     LoadUsageRigthsInfoL( aFileHandle );        
       
   112     
       
   113     TSVGTRightsStatus rightsStatus;
       
   114     GetUsageRightsStatus( rightsStatus );         
       
   115     return ( rightsStatus == ESVGTPreview );  
       
   116 
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CSVGTDrmHelper::CheckRightsAmountL
       
   121 // -----------------------------------------------------------------------------
       
   122 //     
       
   123 TInt CSVGTDrmHelper::CheckRightsAmountL( RFile& aFileHandle )
       
   124     {
       
   125     LoadUsageRigthsInfoL( aFileHandle );
       
   126     if ( iProtected )
       
   127         {
       
   128         return iDrmHelper->CheckRightsAmountL( aFileHandle );   
       
   129         }
       
   130     else
       
   131         {
       
   132         return 0;
       
   133         }
       
   134     }
       
   135 // -----------------------------------------------------------------------------
       
   136 // CSVGTDrmHelper::GetDrmDetailsL
       
   137 // -----------------------------------------------------------------------------
       
   138 // 
       
   139 void CSVGTDrmHelper::GetDrmDetailsL( RFile& aFileHandle,
       
   140                          TBool& aRightsValid, TBool& aIsSeparateDeliveryNS, 
       
   141                          TBool& aIsProtected )
       
   142     {
       
   143 
       
   144 
       
   145     LoadUsageRigthsInfoL( aFileHandle );     
       
   146     
       
   147     aIsProtected = IsProtected();    
       
   148     
       
   149     if( !aIsProtected )
       
   150         {       
       
   151         return;
       
   152         }             
       
   153    
       
   154     TSVGTRightsStatus rightsStatus;
       
   155     GetUsageRightsStatus( rightsStatus );       
       
   156     aRightsValid = ( rightsStatus == ESVGTFull ) ||
       
   157                    ( rightsStatus == ESVGTRestricted ) ||
       
   158                    ( rightsStatus == ESVGTPreview ); 
       
   159                    
       
   160                    
       
   161     // Creates a handle to content handled via CAF
       
   162     CContent *content = CContent::NewLC( aFileHandle ); 
       
   163     content->GetAttribute( EIsForwardable, aIsSeparateDeliveryNS );
       
   164     CleanupStack::PopAndDestroy( content );     
       
   165     return;   
       
   166     
       
   167     }
       
   168     
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CSVGTDrmHelper::LoadUsageRigthsInfoL
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CSVGTDrmHelper::LoadUsageRigthsInfoL( RFile& aFileHandle )
       
   175     {
       
   176     
       
   177     delete iRightsConstraints;
       
   178     iRightsConstraints = NULL;
       
   179     //flag to check for SD protection
       
   180     iIsSeparateDelivery=0;
       
   181     // Creates a handle to content handled via CAF
       
   182     CContent *content = CContent::NewLC( aFileHandle ); 
       
   183     content->GetAttribute( EIsProtected, iProtected );
       
   184     content->GetAttribute( EDeliveryMethod, iIsSeparateDelivery );
       
   185     CleanupStack::PopAndDestroy( content );
       
   186 
       
   187     if ( iProtected )
       
   188         {
       
   189 
       
   190         CDRMHelperRightsConstraints* ignore1 = NULL;
       
   191         CDRMHelperRightsConstraints* ignore2 = NULL;
       
   192         CDRMHelperRightsConstraints* ignore3 = NULL;
       
   193         
       
   194 
       
   195         TRAPD(error, iDrmHelper->GetRightsDetailsL(aFileHandle,
       
   196                                                    ContentAccess::EView,
       
   197                                                    iExpired,
       
   198                                                    iSendingAllowed,
       
   199                                                    ignore1,
       
   200                                                    iRightsConstraints,
       
   201                                                    ignore2,
       
   202                                                    ignore3));  
       
   203        
       
   204         // Delete ignored constraints
       
   205         delete ignore1;
       
   206         ignore1 = NULL;
       
   207         delete ignore2;
       
   208         ignore2 = NULL;
       
   209         delete ignore3;
       
   210         ignore3 = NULL;
       
   211 
       
   212         if ( error == KErrCANoRights )
       
   213             {
       
   214             delete iRightsConstraints;
       
   215             iRightsConstraints = NULL;
       
   216             }
       
   217         else
       
   218             {
       
   219             User::LeaveIfError(error);
       
   220             }
       
   221         }
       
   222     else
       
   223         {
       
   224         iExpired = EFalse;
       
   225         iSendingAllowed = ETrue;
       
   226         }     
       
   227 
       
   228     iDrmHelper->CanSetAutomated(aFileHandle, iCanSetAutomated);
       
   229     
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CSVGTDrmHelper::GetUsageRightsStatus
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CSVGTDrmHelper::GetUsageRightsStatus(TSVGTRightsStatus& aStatus)
       
   237     {
       
   238     if (!iProtected)
       
   239         {
       
   240         aStatus = ESVGTFull;
       
   241         }
       
   242     else if (iRightsConstraints)
       
   243         {
       
   244         if (iExpired)
       
   245             {
       
   246             aStatus = ESVGTExpired;
       
   247             }
       
   248         else if (iRightsConstraints->IsPreview())
       
   249             {
       
   250             	if(!(iIsSeparateDelivery==EOmaDrm2||iIsSeparateDelivery==EOmaDrm1SeparateDelivery))
       
   251             	{
       
   252             	aStatus = ESVGTPreview;	
       
   253             	}
       
   254             }
       
   255         else if (iRightsConstraints->FullRights())
       
   256             {
       
   257             aStatus = ESVGTFull;
       
   258             }
       
   259         else
       
   260             {
       
   261             TTime time;
       
   262             TTime startTime;
       
   263 
       
   264             time.HomeTime();
       
   265             TInt error = GetStartTime(startTime);
       
   266 
       
   267             // Check that usage time has already started
       
   268             if (!error && time < startTime)
       
   269                 {
       
   270                 aStatus = ESVGTExpired;
       
   271                 }
       
   272             else
       
   273                 {
       
   274                 aStatus = ESVGTRestricted;
       
   275                 }
       
   276             }
       
   277         }
       
   278     else
       
   279         {
       
   280         aStatus = ESVGTMissing;
       
   281         }
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CSVGTDrmHelper::GetExpireStatus
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 void CSVGTDrmHelper::GetExpireStatus(TBool& aAboutToExpire,
       
   289                                             TSVGTExpireConstraint& aConstraint,
       
   290                                             TInt& aCountLeft,
       
   291                                             TInt& aDaysLeft)
       
   292     {
       
   293     TSVGTRightsStatus status;
       
   294     GetUsageRightsStatus(status);
       
   295 
       
   296     if (status == ESVGTRestricted)
       
   297         {
       
   298         TUint32 count = 0;
       
   299         TInt countError = GetCount(count);
       
   300         
       
   301         TTime endTime;
       
   302         TInt timeError = GetEndTime(endTime);
       
   303         TTime currentTime;
       
   304         currentTime.HomeTime();
       
   305         TInt diff = endTime.DaysFrom(currentTime).Int();
       
   306 
       
   307         TTimeIntervalSeconds interval;
       
   308         TInt intervalError = GetInterval(interval);
       
   309         TInt intervalDiff = 0;
       
   310         if (!intervalError)
       
   311             {
       
   312             TTime intervalStart;
       
   313             TTimeIntervalSeconds secondsFromStart;
       
   314 
       
   315             if (!GetIntervalStart(intervalStart))
       
   316                 {
       
   317                 // No error => interval has already started
       
   318                 currentTime.SecondsFrom(intervalStart, secondsFromStart);
       
   319                 intervalDiff = 
       
   320                     (interval.Int() - secondsFromStart.Int()) / KSVGTSecondsInDay;
       
   321                 }
       
   322             else
       
   323                 {
       
   324                 // Error occurred => interval not started yet
       
   325                 intervalDiff = interval.Int() / KSVGTSecondsInDay;
       
   326                 }
       
   327             }
       
   328 
       
   329         if (!countError && count <= KSVGTCountConstraint &&
       
   330             ((!timeError && diff <= KSVGTTimeConstraint) ||
       
   331             (!intervalError && intervalDiff <= KSVGTTimeConstraint)))
       
   332             {
       
   333             // Count and time or interval contraints about to expire
       
   334             aConstraint = ESVGTCountAndTime;
       
   335             aCountLeft = count;
       
   336             aAboutToExpire = ETrue;
       
   337 
       
   338             if (!timeError && !intervalError)
       
   339                 {
       
   340                 // Both constraints exist => use smaller one
       
   341                 aDaysLeft = Min(diff, intervalDiff);
       
   342                 }
       
   343             else if (!timeError)
       
   344                 {
       
   345                 // Only time constraint exists => use it
       
   346                 aDaysLeft = diff;
       
   347                 }
       
   348             else
       
   349                 {
       
   350                 // Only interval constraint exists => use it
       
   351                 aDaysLeft = intervalDiff;
       
   352                 }
       
   353             }
       
   354         else if (!countError && count <= KSVGTCountConstraint)
       
   355             {
       
   356             // Count constraint about to expire
       
   357             aConstraint = ESVGTCount;
       
   358             aCountLeft = count;
       
   359             aAboutToExpire = ETrue;
       
   360             }
       
   361         else if ((!timeError && diff <= KSVGTTimeConstraint) ||
       
   362                  (!intervalError && intervalDiff <= KSVGTTimeConstraint))
       
   363             {
       
   364             // Time or interval constraint about to expire
       
   365             aConstraint = ESVGTTime;
       
   366             aAboutToExpire = ETrue;
       
   367 
       
   368             if (!timeError && !intervalError)
       
   369                 {
       
   370                 // Both constraints exist => use smaller one
       
   371                 aDaysLeft = Min(diff, intervalDiff);
       
   372                 }
       
   373             else if (!timeError)
       
   374                 {
       
   375                 // Only time constraint exists => use it
       
   376                 aDaysLeft = diff;
       
   377                 }
       
   378             else
       
   379                 {
       
   380                 // Only Interval constraint exists => use it
       
   381                 aDaysLeft = intervalDiff;
       
   382                 }
       
   383             }
       
   384         else
       
   385             {
       
   386             // No constraint about to expire
       
   387             aAboutToExpire = EFalse;
       
   388             aConstraint = ESVGTNone;
       
   389             }
       
   390         }
       
   391     else
       
   392         {
       
   393         // Non-restricted object cannot expire
       
   394         aAboutToExpire = EFalse;
       
   395         }
       
   396     }
       
   397 
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CSVGTDrmHelper::SendingAllowed
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 TBool CSVGTDrmHelper::SendingAllowed() const
       
   404     {
       
   405     return iSendingAllowed;
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CSVGTDrmHelper::GetCount
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TInt CSVGTDrmHelper::GetCount(TUint32& aCount)
       
   413     {
       
   414     TInt error = KErrNone;
       
   415 
       
   416     if (!iProtected)
       
   417         {
       
   418         error = KErrNotFound;
       
   419         }
       
   420     else if (iRightsConstraints)
       
   421         {
       
   422         TUint32 ignore;
       
   423         TRAP(error, iRightsConstraints->GetCountersL(aCount, ignore));
       
   424         }
       
   425     else
       
   426         {
       
   427         error = KErrNotReady;
       
   428         }
       
   429 
       
   430     return error;
       
   431     }
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CSVGTDrmHelper::GetStartTime
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 TInt CSVGTDrmHelper::GetStartTime(TTime& aStartTime)
       
   438     {
       
   439     TInt error = KErrNone;
       
   440 
       
   441     if (!iProtected)
       
   442         {
       
   443         error = KErrNotFound;
       
   444         }
       
   445     else if (iRightsConstraints)
       
   446         {
       
   447         TRAP(error, iRightsConstraints->GetStartTimeL(aStartTime));
       
   448         }
       
   449     else
       
   450         {
       
   451         error = KErrNotReady;
       
   452         }
       
   453 
       
   454     return error;
       
   455     }
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CSVGTDrmHelper::GetEndTime
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 TInt CSVGTDrmHelper::GetEndTime(TTime& aEndTime)
       
   462     {
       
   463     TInt error = KErrNone;
       
   464 
       
   465     if (!iProtected)
       
   466         {
       
   467         error = KErrNotFound;
       
   468         }
       
   469     else if (iRightsConstraints)
       
   470         {
       
   471         TRAP(error, iRightsConstraints->GetEndTimeL(aEndTime));
       
   472         }
       
   473     else
       
   474         {
       
   475         error = KErrNotReady;
       
   476         }
       
   477 
       
   478     return error;
       
   479     }
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // CSVGTDrmHelper::GetInterval
       
   483 // -----------------------------------------------------------------------------
       
   484 //
       
   485 TInt CSVGTDrmHelper::GetInterval(TTimeIntervalSeconds& aInterval)
       
   486     {
       
   487     TInt error = KErrNone;
       
   488 
       
   489     if (!iProtected)
       
   490         {
       
   491         error = KErrNotFound;
       
   492         }
       
   493     else if (iRightsConstraints)
       
   494         {
       
   495         TRAP(error, iRightsConstraints->GetIntervalL(aInterval));
       
   496         }
       
   497     else
       
   498         {
       
   499         error = KErrNotReady;
       
   500         }
       
   501 
       
   502     return error;
       
   503     }
       
   504 
       
   505 // -----------------------------------------------------------------------------
       
   506 // CSVGTDrmHelper::GetIntervalStart
       
   507 // -----------------------------------------------------------------------------
       
   508 //
       
   509 TInt CSVGTDrmHelper::GetIntervalStart(TTime& aStartTime)
       
   510     {
       
   511     TInt error = KErrNone;
       
   512 
       
   513     if (!iProtected)
       
   514         {
       
   515         error = KErrNotFound;
       
   516         }
       
   517     else if (iRightsConstraints)
       
   518         {
       
   519         TRAP(error, iRightsConstraints->GetIntervalStartL(aStartTime));
       
   520         }
       
   521     else
       
   522         {
       
   523         error = KErrNotReady;
       
   524         }
       
   525 
       
   526     return error;
       
   527     }
       
   528     
       
   529 // -----------------------------------------------------------------------------
       
   530 // CSVGTDrmHelper::IsProtected
       
   531 // -----------------------------------------------------------------------------
       
   532 //
       
   533 TBool CSVGTDrmHelper::IsProtected() const
       
   534     {
       
   535     return iProtected;
       
   536     }
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CSVGTDrmHelper::CanSetAutomated
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 TBool CSVGTDrmHelper::CanSetAutomated() const
       
   543     {
       
   544     return iCanSetAutomated;
       
   545     }
       
   546     
       
   547 //  End of File