browserutilities/downloadmgr/DownloadMgrUiLib/Src/CDownloadUtils.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 the License "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:  Supports download related utility functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "CDownloadUtils.h" 
       
    22 #include    "UiLibLogger.h"
       
    23 #include    <DownloadMgrClient.h>
       
    24 #include    <DocumentHandler.h>
       
    25 #include    <Oma2Agent.h>
       
    26 #include    <DRMCommon.h>
       
    27 #include	<apgcli.h>
       
    28 #include    <caf/caf.h>
       
    29 #include    <utf.h>
       
    30 #include    <Browser_Platform_Variant.hrh>
       
    31 // LOCAL CONSTANTS AND MACROS
       
    32 
       
    33 #ifdef BRDO_WML_DISABLED_FF
       
    34 _LIT8( KWmlType1, "text/vnd.wap.wml");
       
    35 _LIT8( KWmlType2, "text/vnd.wap.wmlc");
       
    36 _LIT8( KWmlType3, "application/vnd.wap.wmlscriptc");
       
    37 _LIT8( KWmlType4, "application/vnd.wap.wmlc");
       
    38 #endif 
       
    39 
       
    40 
       
    41 _LIT8(KAudio, "audio/");
       
    42 _LIT8(KVideo, "video/");
       
    43 _LIT8(KImage, "image/");
       
    44 _LIT8(KFlash, "application/x-shockwave-flash");
       
    45 _LIT8(Ksdp, "application/sdp");
       
    46 _LIT8(Krng, "application/vnd.nokia.ringing-tone");
       
    47 _LIT8(Krn, "application/vnd.rn-realmedia");
       
    48 _LIT8(Kpn, "application/x-pn-realmedia");
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CDownloadUtils::CDownloadUtils
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CDownloadUtils::CDownloadUtils() 
       
    57     {
       
    58     }
       
    59     
       
    60 // -----------------------------------------------------------------------------
       
    61 // CDownloadUtils::ConstructL
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void CDownloadUtils::ConstructL()
       
    65     {
       
    66     CLOG_ENTERFN("CDownloadUtils::ConstructL()");
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CDownloadUtils::NewL
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CDownloadUtils* CDownloadUtils::NewL()
       
    74     {
       
    75     CDownloadUtils* self = new ( ELeave ) CDownloadUtils();
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop();
       
    79     return self;
       
    80     }
       
    81 
       
    82 // Destructor
       
    83 CDownloadUtils::~CDownloadUtils()
       
    84     {
       
    85     delete iMimeType;
       
    86     CLOG_WRITE(" iMimeType OK");
       
    87     delete iContentURI;
       
    88     CLOG_WRITE(" iContentURI OK");
       
    89     }
       
    90     
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CDownloadUtils::DrmDownloadL
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 TBool CDownloadUtils::DrmDownloadL( RHttpDownload& aDownload )
       
    97     {
       
    98     CLOG_ENTERFN("CDownloadUtils::DrmDownloadL");
       
    99 
       
   100     TBool isDrmDownload( EFalse );
       
   101     HBufC8* contentType = HBufC8::NewLC( KMaxContentTypeLength );
       
   102     TPtr8 temp( contentType->Des() );
       
   103     User::LeaveIfError( aDownload.GetStringAttribute( EDlAttrContentType, temp ) );
       
   104     CLOG_WRITE(" EDlAttrContentType got");
       
   105     if( !contentType->Compare(KOma1DrmMessageContentType) ||
       
   106         !contentType->Compare(KOma1DcfContentType) ||
       
   107         !contentType->Compare(KOma2DcfContentType) )
       
   108         {
       
   109         isDrmDownload = ETrue;
       
   110         }
       
   111     CleanupStack::PopAndDestroy( contentType );
       
   112     
       
   113     CLOG_WRITE_FORMAT(" ret: %d",isDrmDownload);
       
   114     CLOG_LEAVEFN("CDownloadUtils::DrmDownloadL");
       
   115     return isDrmDownload;
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CDownloadUtils::DrmRightsOnThePhoneL
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 TBool CDownloadUtils::DrmRightsOnThePhoneL
       
   123     ( RHttpDownload& aDownload, TBool& aPreviewRights ) const
       
   124     {
       
   125     CLOG_ENTERFN("CDownloadUtils::DrmRightsOnThePhoneL");
       
   126 
       
   127     TBool drmRightsOnThePhone( EFalse );
       
   128     
       
   129     DRMCommon* drmCommon = DRMCommon::NewL();
       
   130     CLOG_WRITE(" DRMCommon::NewL OK");
       
   131     CleanupStack::PushL( drmCommon );
       
   132     User::LeaveIfError( drmCommon->Connect() );
       
   133     CLOG_WRITE(" Connect OK");
       
   134 
       
   135     HBufC* fileName = HBufC::NewLC( KMaxPath );
       
   136     TPtr fileNamePtr = fileName->Des();
       
   137     User::LeaveIfError
       
   138         ( aDownload.GetStringAttribute( EDlAttrDestFilename, fileNamePtr ) );
       
   139     CLOG_WRITE_FORMAT(" EDlAttrDestFilename: %S",&fileNamePtr);
       
   140 
       
   141     RPointerArray<CDRMRights>* rightsList(0);
       
   142     drmCommon->GetDetailedFileRights( *fileName, rightsList ); // Return val. ignored.
       
   143     // We are interested in if there is at least one right:
       
   144     if ( rightsList )
       
   145         {
       
   146         // BEGIN LEAVE safety: No LEAVE allowed!
       
   147         TInt rightsCnt = rightsList->Count();
       
   148         CLOG_WRITE_FORMAT(" rightsCnt: %d",rightsCnt);
       
   149         for ( TInt i=0; i<rightsCnt; ++i )
       
   150             {
       
   151             // Check: at least one right must be valid
       
   152             CDRMRights* r = (*rightsList)[i];
       
   153             TUint32 constraintSpec( 0 );
       
   154             CDRMRights::TRestriction restriction;
       
   155             CDRMRights::TExpiration expiration;
       
   156             TUint32 constType;
       
   157             TInt infoPri = r->GetRightsInfo( constraintSpec, restriction, expiration, constType );
       
   158             CLOG_WRITE_FORMAT(" infoPri: %d",infoPri);
       
   159             if ( infoPri != CDRMRights::ENoRights )
       
   160                 {
       
   161                 // Right must be valid
       
   162                 if ( expiration == CDRMRights::EValidRights )
       
   163                     {
       
   164                     drmRightsOnThePhone = ETrue;
       
   165                     // Set output parameter
       
   166                     aPreviewRights = ( restriction == CDRMRights::EPreviewRights );
       
   167                     // We found the first, we are not interested in others
       
   168                     break;
       
   169                     }
       
   170                 }
       
   171             }
       
   172         // END LEAVE safety: No LEAVE allowed!
       
   173             
       
   174         rightsList->Close();
       
   175         delete rightsList;
       
   176         rightsList = 0;
       
   177         }
       
   178     
       
   179     CleanupStack::PopAndDestroy( fileName );
       
   180     CleanupStack::PopAndDestroy( drmCommon );
       
   181     
       
   182     CLOG_WRITE_FORMAT(" ret: %d",drmRightsOnThePhone);
       
   183     CLOG_WRITE_FORMAT(" aPreviewRights: %d",aPreviewRights);
       
   184     CLOG_LEAVEFN("CDownloadUtils::DrmRightsOnThePhoneL");
       
   185     return drmRightsOnThePhone;
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CDownloadUtils::IsContentTypeSupportedL
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CDownloadUtils::IsContentTypeSupportedL( RHttpDownload& aDownload )
       
   193     {
       
   194     TBool supported = EFalse;
       
   195     
       
   196     HBufC8* contentType = ContentTypeL( aDownload, ETrue );
       
   197     CleanupStack::PushL( contentType );
       
   198 
       
   199     // if we get content-type that appears to be SIS (not SISx) type, may be due to
       
   200     // misconfigured server: change content-type to octet-stream to allow
       
   201     // app installer to make decision if supported or not
       
   202     if( contentType->Find( KSisFileMimeType ) != KErrNotFound )
       
   203         {
       
   204         contentType->Des().Copy( KOctetStreamMimeType );
       
   205         }
       
   206 
       
   207     supported = IsContentTypeSupportedL( aDownload, *contentType );
       
   208 
       
   209     CleanupStack::PopAndDestroy( contentType );
       
   210 
       
   211     return supported;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CDownloadUtils::IsContentTypeSupportedL
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TBool CDownloadUtils::IsContentTypeSupportedL( const TDesC8& aContentType )
       
   219     {
       
   220     if ( aContentType.Length() == 0 )
       
   221         {
       
   222         return EFalse;
       
   223         }
       
   224 #ifdef BRDO_WML_DISABLED_FF
       
   225     else if ( !aContentType.Compare( KWmlType1() ) || !aContentType.Compare( KWmlType2() )
       
   226            || aContentType.Compare( KWmlType3() ) || !aContentType.Compare( KWmlType4() ) )
       
   227         {
       
   228     	return EFalse; 
       
   229         }
       
   230 #endif 
       
   231     else
       
   232         {
       
   233         TBool canOpen( EFalse );
       
   234         CDocumentHandler* docHandler = CDocumentHandler::NewLC();
       
   235         TRAPD( err, canOpen = docHandler->CanOpenL( TDataType( aContentType ) ) );
       
   236         CleanupStack::PopAndDestroy( docHandler ); // docHandler
       
   237 
       
   238         if ( !(aContentType.Compare(KOma2RoContentType)) || !(aContentType.Compare(KOma2ProtectedRoType))
       
   239             || !(aContentType.Compare(KOma2TriggerContentType)) )
       
   240             {
       
   241             canOpen = EFalse;
       
   242             }
       
   243             
       
   244         if ( err != KErrNone ) 
       
   245             {
       
   246             if ( err == KMimeNotSupported ) 
       
   247                 {
       
   248                 canOpen = EFalse; 
       
   249                 }
       
   250             else 
       
   251                 {
       
   252                 User::Leave( err ); 
       
   253                 }
       
   254             }
       
   255         return canOpen;
       
   256         }
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CDownloadUtils::IsContentTypeSupportedL
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 TBool CDownloadUtils::IsContentTypeSupportedL( RHttpDownload& aDownload, const TDesC8& aContentType )
       
   264     {
       
   265     if ( aContentType.Length() == 0 )
       
   266         {
       
   267         return EFalse;
       
   268         }
       
   269 #ifdef BRDO_WML_DISABLED_FF
       
   270     else if ( !aContentType.Compare(KWmlType1()) || !aContentType.Compare(KWmlType2())
       
   271            || !aContentType.Compare(KWmlType3()) || !aContentType.Compare(KWmlType4()) )
       
   272         {
       
   273     	return EFalse; 
       
   274         }
       
   275 #endif         
       
   276     else
       
   277         {
       
   278         TBool canOpen( EFalse );
       
   279         CDocumentHandler* docHandler = CDocumentHandler::NewLC();
       
   280         TRAPD( err, canOpen = docHandler->CanOpenL( TDataType( aContentType ) ) );
       
   281         if ( err == KMimeNotSupported )
       
   282             {
       
   283 			      TUint8* contentTypeString = NULL;
       
   284 			      FindContentTypeFromFileL(aDownload, contentTypeString);
       
   285 			      if (contentTypeString != NULL)
       
   286 				        {
       
   287                 TRAPD( err1, canOpen = docHandler->CanOpenL( TDataType( TPtrC8(contentTypeString) ) ) );
       
   288 
       
   289 				        if ( err1 == KMimeNotSupported )
       
   290                     {
       
   291 					          canOpen = EFalse;
       
   292                     delete contentTypeString;
       
   293 					          }
       
   294 				        else if (err1 == KErrNone)
       
   295                     {
       
   296                     // Setting Download Content type to a recognized one
       
   297                     aDownload.SetStringAttribute( EDlAttrContentType, TPtrC8(contentTypeString) );
       
   298                     delete contentTypeString;
       
   299 					          canOpen = ETrue;
       
   300 					          }
       
   301 				       else
       
   302 					         {
       
   303                    delete contentTypeString;
       
   304 					         User::Leave( err );
       
   305 					          }
       
   306 				       }
       
   307             }
       
   308         else
       
   309             {
       
   310             if (err != KErrNone )
       
   311                 {
       
   312                 User::Leave( err ); 
       
   313                 }
       
   314             }
       
   315 
       
   316 		    CleanupStack::PopAndDestroy(docHandler ); // docHandler
       
   317         return canOpen;
       
   318         }
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CDownloadUtils::ContentTypeL
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 HBufC8* CDownloadUtils::ContentTypeL
       
   326     ( RHttpDownload& aDownload, TBool aDrmResolve, TInt mediaObjectIndex )
       
   327     {
       
   328     CLOG_ENTERFN("CDownloadUtils::ContentTypeL");
       
   329     
       
   330     HBufC8* retContentType = NULL;
       
   331     TInt32 numMediaObjects = 0;
       
   332     User::LeaveIfError( aDownload.GetIntAttribute( EDlAttrNumMediaObjects, numMediaObjects ) );
       
   333     
       
   334      if ( aDrmResolve && DrmDownloadL( aDownload ) )
       
   335         {
       
   336         HBufC* fileName = HBufC::NewLC( KMaxPath );
       
   337         TPtr fileNamePtr = fileName->Des();
       
   338         // Check if this is album
       
   339         if ((numMediaObjects > KFirstMoIndex) && mediaObjectIndex)
       
   340             {
       
   341             User::LeaveIfError
       
   342                 ( aDownload.GetStringAttribute( EDlAttrDestFilename, mediaObjectIndex, fileNamePtr ) );
       
   343             }
       
   344            else
       
   345             {
       
   346             User::LeaveIfError
       
   347                 ( aDownload.GetStringAttribute( EDlAttrDestFilename, fileNamePtr ) );
       
   348             }
       
   349         CLOG_WRITE_FORMAT(" EDlAttrDestFilename: %S",&fileNamePtr);
       
   350 
       
   351 		if(fileNamePtr.Compare(KNullDesC))
       
   352             {
       
   353 		    using namespace ContentAccess;
       
   354 
       
   355 		    CContent* content(NULL);
       
   356 		    TRAPD( err, content = CContent::NewL(fileNamePtr,EContentShareReadWrite) );
       
   357             CleanupStack::PushL(content);
       
   358 
       
   359             /*
       
   360             If file not present CContent::NewL() leaves with KErrNotFound.
       
   361             Need to ignore this error because in case of DRM paused downloads the partial downloaded file will be deleted.
       
   362             */
       
   363 
       
   364 		    if(err != KErrNotFound)
       
   365     		    {
       
   366                 User::LeaveIfError( err );
       
   367 
       
   368 				HBufC* mimeType = HBufC::NewL(256);
       
   369                 TPtr ptr = mimeType->Des();
       
   370 				TInt fileInfoErr = content->GetStringAttribute(EMimeType,ptr);
       
   371 				TBuf8<256> buf8;
       
   372 				CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,ptr);
       
   373 				iMimeType = buf8.Alloc();
       
   374 				
       
   375 				if ( !fileInfoErr )
       
   376 					{
       
   377 					retContentType = iMimeType;
       
   378 					iMimeType = NULL;
       
   379 					}
       
   380                 
       
   381 				// Free temp storage
       
   382 				delete iMimeType;
       
   383 				iMimeType = NULL;
       
   384 				delete iContentURI;
       
   385 				iContentURI = NULL;
       
   386     		    }
       
   387             CleanupStack::PopAndDestroy(content);
       
   388 		    }
       
   389 		CleanupStack::PopAndDestroy( fileName );    
       
   390         }        
       
   391 
       
   392     // If it's not DRM or it is, but the content type could not been got
       
   393     if ( retContentType == NULL )
       
   394         {
       
   395         HBufC8* contentType = HBufC8::NewLC( KMaxContentTypeLength );
       
   396         TPtr8 temp( contentType->Des() );
       
   397 
       
   398         TInt err = 0;
       
   399         // Check if this is album
       
   400         if ((numMediaObjects > KFirstMoIndex) && mediaObjectIndex)
       
   401             {
       
   402             err = aDownload.GetStringAttribute( EDlAttrContentType, mediaObjectIndex, temp );
       
   403             }
       
   404         else
       
   405             {
       
   406             err = aDownload.GetStringAttribute( EDlAttrContentType, temp );
       
   407             }
       
   408         CLOG_WRITE_FORMAT(" err: %d",err);
       
   409         if ( err != KErrNone && err != KErrNotFound )
       
   410             {
       
   411             User::LeaveIfError( err );
       
   412             }
       
   413         if ( err == KErrNotFound )
       
   414             {
       
   415             contentType->Des().Copy( KNullDesC );
       
   416             }
       
   417 
       
   418         CleanupStack::Pop( contentType );
       
   419         retContentType = contentType;
       
   420         contentType = NULL;
       
   421         }
       
   422     
       
   423     CLOG_LEAVEFN("CDownloadUtils::ContentTypeL");
       
   424     return retContentType;
       
   425     }
       
   426     
       
   427     
       
   428 // -----------------------------------------------------------------------------
       
   429 // CDownloadUtils::FindContentTypeFromFileL
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 void CDownloadUtils::FindContentTypeFromFileL( RHttpDownload& aDownload, TUint8*& aContentTypeString)
       
   433 {
       
   434     TDataRecognitionResult dataType;
       
   435     RApaLsSession apaSession;
       
   436     TInt ret;
       
   437 
       
   438     User::LeaveIfError(apaSession.Connect());
       
   439 
       
   440     // Create a buffer to hold data from the file
       
   441 	  TInt bufferSize = 0;
       
   442     TInt seekPosition = 0;
       
   443 	  apaSession.GetMaxDataBufSize(bufferSize);
       
   444 	  HBufC8* buffer = HBufC8::NewLC(bufferSize);
       
   445 	  TPtr8 buf = buffer->Des();
       
   446 
       
   447 	  RFile file;
       
   448     HBufC* fileName = HBufC::NewLC( KMaxPath );
       
   449     TPtr fileNamePtr = fileName->Des();
       
   450     User::LeaveIfError
       
   451         ( aDownload.GetStringAttribute( EDlAttrDestFilename, fileNamePtr ) );
       
   452 
       
   453     RFs fs;
       
   454     User::LeaveIfError( fs.Connect() );
       
   455     CleanupClosePushL( fs );
       
   456 
       
   457     User::LeaveIfError( file.Open( fs, fileNamePtr, 
       
   458 										EFileShareAny |
       
   459 										EFileRead ) );
       
   460 
       
   461 
       
   462 	  // Find current file pointer position
       
   463     file.Seek(ESeekStart, seekPosition);
       
   464 	  // Read from file
       
   465 	  file.Read(buf);
       
   466 	  // return file pointer to original position
       
   467     file.Seek(ESeekStart, seekPosition);
       
   468     // Ask the application architecture to find the file type
       
   469     ret = apaSession.RecognizeData(fileNamePtr, buf, dataType);
       
   470     apaSession.Close();
       
   471 
       
   472 	  CleanupStack::PopAndDestroy(3); //fs, fileName, buffer
       
   473     
       
   474     if (ret == KErrNone &&
       
   475         (dataType.iConfidence == CApaDataRecognizerType::ECertain) ||
       
   476         (dataType.iConfidence == CApaDataRecognizerType::EProbable))
       
   477         {
       
   478         // If the file type was found, try to match it to a known file type
       
   479         TPtrC8 mimeTypePtr = dataType.iDataType.Des8();
       
   480         TInt len = mimeTypePtr.Length() + 1;
       
   481         aContentTypeString = new(ELeave) TUint8 [len];
       
   482 
       
   483         TPtr8 contentTypeStringPtr(aContentTypeString, len);
       
   484         contentTypeStringPtr.Copy(mimeTypePtr);
       
   485         contentTypeStringPtr.ZeroTerminate();
       
   486         return;
       
   487         }
       
   488 }
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CDownloadUtils::IsGallerySupported
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 
       
   495 TBool  CDownloadUtils::IsGallerySupported(const TDesC8& aContentType)
       
   496 {
       
   497 	
       
   498 	TBool found (aContentType.Find(KAudio)==0 || aContentType.Find(KVideo)==0 || aContentType.Find(KImage)==0 || aContentType.Find(KFlash)==0 ||
       
   499 	            aContentType.Find(Ksdp)==0 || aContentType.Find(Krng)==0 || aContentType.Find(Krn)==0 || aContentType.Find(Kpn)==0);
       
   500 
       
   501 	return found;
       
   502 }