svgtviewer/SvgtViewerPlugin/SvgtFileViewDetailsSrc/SVGTDrmHelper.cpp
branchRCL_3
changeset 20 5fd161fa28b6
parent 0 632761c941a7
equal deleted inserted replaced
17:e52958d06c29 20:5fd161fa28b6
       
     1 /*
       
     2 * Copyright (c) 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:  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 
       
   180     // Creates a handle to content handled via CAF
       
   181     CContent *content = CContent::NewLC( aFileHandle ); 
       
   182     content->GetAttribute( EIsProtected, iProtected );
       
   183     CleanupStack::PopAndDestroy( content );
       
   184 
       
   185     if ( iProtected )
       
   186         {
       
   187 
       
   188         CDRMHelperRightsConstraints* ignore1 = NULL;
       
   189         CDRMHelperRightsConstraints* ignore2 = NULL;
       
   190         CDRMHelperRightsConstraints* ignore3 = NULL;
       
   191         
       
   192 
       
   193         TRAPD(error, iDrmHelper->GetRightsDetailsL(aFileHandle,
       
   194                                                    ContentAccess::EView,
       
   195                                                    iExpired,
       
   196                                                    iSendingAllowed,
       
   197                                                    ignore1,
       
   198                                                    iRightsConstraints,
       
   199                                                    ignore2,
       
   200                                                    ignore3));  
       
   201        
       
   202         // Delete ignored constraints
       
   203         delete ignore1;
       
   204         ignore1 = NULL;
       
   205         delete ignore2;
       
   206         ignore2 = NULL;
       
   207         delete ignore3;
       
   208         ignore3 = NULL;
       
   209 
       
   210         if ( error == KErrCANoRights )
       
   211             {
       
   212             delete iRightsConstraints;
       
   213             iRightsConstraints = NULL;
       
   214             }
       
   215         else
       
   216             {
       
   217             User::LeaveIfError(error);
       
   218             }
       
   219         }
       
   220     else
       
   221         {
       
   222         iExpired = EFalse;
       
   223         iSendingAllowed = ETrue;
       
   224         }     
       
   225 
       
   226     iDrmHelper->CanSetAutomated(aFileHandle, iCanSetAutomated);
       
   227     
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CSVGTDrmHelper::GetUsageRightsStatus
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CSVGTDrmHelper::GetUsageRightsStatus(TSVGTRightsStatus& aStatus)
       
   235     {
       
   236     if (!iProtected)
       
   237         {
       
   238         aStatus = ESVGTFull;
       
   239         }
       
   240     else if (iRightsConstraints)
       
   241         {
       
   242         if (iExpired)
       
   243             {
       
   244             aStatus = ESVGTExpired;
       
   245             }
       
   246         else if (iRightsConstraints->IsPreview())
       
   247             {
       
   248             aStatus = ESVGTPreview;
       
   249             }
       
   250         else if (iRightsConstraints->FullRights())
       
   251             {
       
   252             aStatus = ESVGTFull;
       
   253             }
       
   254         else
       
   255             {
       
   256             TTime time;
       
   257             TTime startTime;
       
   258 
       
   259             time.HomeTime();
       
   260             TInt error = GetStartTime(startTime);
       
   261 
       
   262             // Check that usage time has already started
       
   263             if (!error && time < startTime)
       
   264                 {
       
   265                 aStatus = ESVGTExpired;
       
   266                 }
       
   267             else
       
   268                 {
       
   269                 aStatus = ESVGTRestricted;
       
   270                 }
       
   271             }
       
   272         }
       
   273     else
       
   274         {
       
   275         aStatus = ESVGTMissing;
       
   276         }
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CSVGTDrmHelper::GetExpireStatus
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void CSVGTDrmHelper::GetExpireStatus(TBool& aAboutToExpire,
       
   284                                             TSVGTExpireConstraint& aConstraint,
       
   285                                             TInt& aCountLeft,
       
   286                                             TInt& aDaysLeft)
       
   287     {
       
   288     TSVGTRightsStatus status;
       
   289     GetUsageRightsStatus(status);
       
   290 
       
   291     if (status == ESVGTRestricted)
       
   292         {
       
   293         TUint32 count = 0;
       
   294         TInt countError = GetCount(count);
       
   295         
       
   296         TTime endTime;
       
   297         TInt timeError = GetEndTime(endTime);
       
   298         TTime currentTime;
       
   299         currentTime.HomeTime();
       
   300         TInt diff = endTime.DaysFrom(currentTime).Int();
       
   301 
       
   302         TTimeIntervalSeconds interval;
       
   303         TInt intervalError = GetInterval(interval);
       
   304         TInt intervalDiff = 0;
       
   305         if (!intervalError)
       
   306             {
       
   307             TTime intervalStart;
       
   308             TTimeIntervalSeconds secondsFromStart;
       
   309 
       
   310             if (!GetIntervalStart(intervalStart))
       
   311                 {
       
   312                 // No error => interval has already started
       
   313                 currentTime.SecondsFrom(intervalStart, secondsFromStart);
       
   314                 intervalDiff = 
       
   315                     (interval.Int() - secondsFromStart.Int()) / KSVGTSecondsInDay;
       
   316                 }
       
   317             else
       
   318                 {
       
   319                 // Error occurred => interval not started yet
       
   320                 intervalDiff = interval.Int() / KSVGTSecondsInDay;
       
   321                 }
       
   322             }
       
   323 
       
   324         if (!countError && count <= KSVGTCountConstraint &&
       
   325             ((!timeError && diff <= KSVGTTimeConstraint) ||
       
   326             (!intervalError && intervalDiff <= KSVGTTimeConstraint)))
       
   327             {
       
   328             // Count and time or interval contraints about to expire
       
   329             aConstraint = ESVGTCountAndTime;
       
   330             aCountLeft = count;
       
   331             aAboutToExpire = ETrue;
       
   332 
       
   333             if (!timeError && !intervalError)
       
   334                 {
       
   335                 // Both constraints exist => use smaller one
       
   336                 aDaysLeft = Min(diff, intervalDiff);
       
   337                 }
       
   338             else if (!timeError)
       
   339                 {
       
   340                 // Only time constraint exists => use it
       
   341                 aDaysLeft = diff;
       
   342                 }
       
   343             else
       
   344                 {
       
   345                 // Only interval constraint exists => use it
       
   346                 aDaysLeft = intervalDiff;
       
   347                 }
       
   348             }
       
   349         else if (!countError && count <= KSVGTCountConstraint)
       
   350             {
       
   351             // Count constraint about to expire
       
   352             aConstraint = ESVGTCount;
       
   353             aCountLeft = count;
       
   354             aAboutToExpire = ETrue;
       
   355             }
       
   356         else if ((!timeError && diff <= KSVGTTimeConstraint) ||
       
   357                  (!intervalError && intervalDiff <= KSVGTTimeConstraint))
       
   358             {
       
   359             // Time or interval constraint about to expire
       
   360             aConstraint = ESVGTTime;
       
   361             aAboutToExpire = ETrue;
       
   362 
       
   363             if (!timeError && !intervalError)
       
   364                 {
       
   365                 // Both constraints exist => use smaller one
       
   366                 aDaysLeft = Min(diff, intervalDiff);
       
   367                 }
       
   368             else if (!timeError)
       
   369                 {
       
   370                 // Only time constraint exists => use it
       
   371                 aDaysLeft = diff;
       
   372                 }
       
   373             else
       
   374                 {
       
   375                 // Only Interval constraint exists => use it
       
   376                 aDaysLeft = intervalDiff;
       
   377                 }
       
   378             }
       
   379         else
       
   380             {
       
   381             // No constraint about to expire
       
   382             aAboutToExpire = EFalse;
       
   383             aConstraint = ESVGTNone;
       
   384             }
       
   385         }
       
   386     else
       
   387         {
       
   388         // Non-restricted object cannot expire
       
   389         aAboutToExpire = EFalse;
       
   390         }
       
   391     }
       
   392 
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CSVGTDrmHelper::SendingAllowed
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 TBool CSVGTDrmHelper::SendingAllowed() const
       
   399     {
       
   400     return iSendingAllowed;
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // CSVGTDrmHelper::GetCount
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 TInt CSVGTDrmHelper::GetCount(TUint32& aCount)
       
   408     {
       
   409     TInt error = KErrNone;
       
   410 
       
   411     if (!iProtected)
       
   412         {
       
   413         error = KErrNotFound;
       
   414         }
       
   415     else if (iRightsConstraints)
       
   416         {
       
   417         TUint32 ignore;
       
   418         TRAP(error, iRightsConstraints->GetCountersL(aCount, ignore));
       
   419         }
       
   420     else
       
   421         {
       
   422         error = KErrNotReady;
       
   423         }
       
   424 
       
   425     return error;
       
   426     }
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // CSVGTDrmHelper::GetStartTime
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 TInt CSVGTDrmHelper::GetStartTime(TTime& aStartTime)
       
   433     {
       
   434     TInt error = KErrNone;
       
   435 
       
   436     if (!iProtected)
       
   437         {
       
   438         error = KErrNotFound;
       
   439         }
       
   440     else if (iRightsConstraints)
       
   441         {
       
   442         TRAP(error, iRightsConstraints->GetStartTimeL(aStartTime));
       
   443         }
       
   444     else
       
   445         {
       
   446         error = KErrNotReady;
       
   447         }
       
   448 
       
   449     return error;
       
   450     }
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // CSVGTDrmHelper::GetEndTime
       
   454 // -----------------------------------------------------------------------------
       
   455 //
       
   456 TInt CSVGTDrmHelper::GetEndTime(TTime& aEndTime)
       
   457     {
       
   458     TInt error = KErrNone;
       
   459 
       
   460     if (!iProtected)
       
   461         {
       
   462         error = KErrNotFound;
       
   463         }
       
   464     else if (iRightsConstraints)
       
   465         {
       
   466         TRAP(error, iRightsConstraints->GetEndTimeL(aEndTime));
       
   467         }
       
   468     else
       
   469         {
       
   470         error = KErrNotReady;
       
   471         }
       
   472 
       
   473     return error;
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // CSVGTDrmHelper::GetInterval
       
   478 // -----------------------------------------------------------------------------
       
   479 //
       
   480 TInt CSVGTDrmHelper::GetInterval(TTimeIntervalSeconds& aInterval)
       
   481     {
       
   482     TInt error = KErrNone;
       
   483 
       
   484     if (!iProtected)
       
   485         {
       
   486         error = KErrNotFound;
       
   487         }
       
   488     else if (iRightsConstraints)
       
   489         {
       
   490         TRAP(error, iRightsConstraints->GetIntervalL(aInterval));
       
   491         }
       
   492     else
       
   493         {
       
   494         error = KErrNotReady;
       
   495         }
       
   496 
       
   497     return error;
       
   498     }
       
   499 
       
   500 // -----------------------------------------------------------------------------
       
   501 // CSVGTDrmHelper::GetIntervalStart
       
   502 // -----------------------------------------------------------------------------
       
   503 //
       
   504 TInt CSVGTDrmHelper::GetIntervalStart(TTime& aStartTime)
       
   505     {
       
   506     TInt error = KErrNone;
       
   507 
       
   508     if (!iProtected)
       
   509         {
       
   510         error = KErrNotFound;
       
   511         }
       
   512     else if (iRightsConstraints)
       
   513         {
       
   514         TRAP(error, iRightsConstraints->GetIntervalStartL(aStartTime));
       
   515         }
       
   516     else
       
   517         {
       
   518         error = KErrNotReady;
       
   519         }
       
   520 
       
   521     return error;
       
   522     }
       
   523     
       
   524 // -----------------------------------------------------------------------------
       
   525 // CSVGTDrmHelper::IsProtected
       
   526 // -----------------------------------------------------------------------------
       
   527 //
       
   528 TBool CSVGTDrmHelper::IsProtected() const
       
   529     {
       
   530     return iProtected;
       
   531     }
       
   532 
       
   533 // -----------------------------------------------------------------------------
       
   534 // CSVGTDrmHelper::CanSetAutomated
       
   535 // -----------------------------------------------------------------------------
       
   536 //
       
   537 TBool CSVGTDrmHelper::CanSetAutomated() const
       
   538     {
       
   539     return iCanSetAutomated;
       
   540     }
       
   541     
       
   542 //  End of File