widgets/widgetapp/src/WidgetUiObserver.cpp
branchRCL_3
changeset 47 e1bea15f9a39
parent 34 220a17280356
child 48 79859ed3eea9
--- a/widgets/widgetapp/src/WidgetUiObserver.cpp	Thu Jul 15 19:53:20 2010 +0300
+++ b/widgets/widgetapp/src/WidgetUiObserver.cpp	Thu Aug 19 10:58:56 2010 +0300
@@ -52,6 +52,16 @@
 _LIT( KPathChar, "\\" );
 _LIT( KLprojExt, ".lproj" );
 
+
+_LIT( KJavaExtension, ".js");
+_LIT( KJavaMimeType, "application/x-javascript");
+_LIT( KCssExtension, ".css");
+_LIT( KCssMimeType,"text/css");
+_LIT( KPngExtension, ".png");
+_LIT( KPngMimeType, "image/png");
+_LIT( KGifExtension, ".gif");
+_LIT( KGifMimeType,"image/gif");
+
 // MACROS
 
 // LOCAL CONSTANTS AND MACROS
@@ -249,7 +259,7 @@
             { // For files in Public areas, don't use localized subdirectory
             TranslateURLToFilenameL( aEmbeddedUrl, KNullDesC); 
             } 
-        
+
         HBufC8* buf = ReadFileL( *iFileName);
         
         if ( !buf && isSandboxed )
@@ -266,7 +276,56 @@
         CleanupStack::PushL( buf );
         HBufC* contentType = NULL;
         TPtrC p( NULL, 0 );
-        contentType = RecognizeLC( *iFileName );
+        
+        // file extension check for .js, .css, .png & .gif        
+        TInt err(0);
+        //.js 
+        err = iFileName->FindF(KJavaExtension);
+        if(err != KErrNotFound)
+        {
+             //.js
+             contentType = HBufC::NewLC(KJavaMimeType().Length());
+             *contentType = KJavaMimeType;
+        }
+        else 
+        {
+            //.css
+            err = iFileName->FindF(KCssExtension) ;
+            if(err != KErrNotFound) 
+            {
+                contentType = HBufC::NewLC(KCssMimeType().Length());
+                *contentType = KCssMimeType;                         
+            }
+            else if (err == KErrNotFound)
+            {
+                    //.png 
+                    err = iFileName->FindF(KPngExtension);
+                    if(err != KErrNotFound)
+                         {
+                         //.png
+                         contentType = HBufC::NewLC(KPngMimeType().Length());
+                         *contentType = KPngMimeType;
+                         }
+                    else
+                        {    
+                        //.gif      
+                        err = iFileName->FindF(KGifExtension) ;
+                        if(err!=KErrNotFound)
+                        {
+                            contentType = HBufC::NewLC(KGifMimeType().Length());
+                            *contentType = KGifMimeType;                         
+                        }
+                    }
+            }
+        }
+        if(err == KErrNotFound)
+        {
+             //Let's use existing iFile the handle to the opened file (no need to re-open it)
+             contentType = RecognizeLC( *iFileName);
+                   
+         }
+         iFile.Close(); 
+  
         aEmbeddedLinkContent.HandleResolveComplete( *contentType, p, buf );
         CleanupStack::PopAndDestroy( 2, buf ); // contentType, buf
         return ETrue;
@@ -413,54 +472,45 @@
     return EFalse;
     }
 
+//
 // -----------------------------------------------------------------------------
-// CWidgetUiObserver::ReadFile
+// CWidgetUiObserver::ReadFileL
 // -----------------------------------------------------------------------------
 //
 HBufC8* CWidgetUiObserver::ReadFileL( const TDesC& aFileName )
     {
-    RFile file;
-
-    if (KErrNone != file.Open( CCoeEnv::Static()->FsSession(), aFileName, EFileRead ) )
+    if (KErrNone != iFile.Open(CCoeEnv::Static()->FsSession(), aFileName, EFileRead|EFileShareAny) )
         {
         return NULL;
         }
 
-    CleanupClosePushL( file );
-
     TInt size;
-    User::LeaveIfError( file.Size( size ) );
+    User::LeaveIfError( iFile.Size( size ) );
     HBufC8* buf = HBufC8::NewLC( size );
     TPtr8 bufPtr( buf->Des() );
-    User::LeaveIfError( file.Read( bufPtr ) );
+    User::LeaveIfError( iFile.Read( bufPtr ) );
     CleanupStack::Pop( buf );
-    CleanupStack::PopAndDestroy( &file );
     return buf;
     }
-
+//
 // -----------------------------------------------------------------------------
-// CWidgetUiObserver::RecognizeL
+// CWidgetUiObserver::RecognizeLC
 // -----------------------------------------------------------------------------
 //
-HBufC* CWidgetUiObserver::RecognizeLC( const TDesC& aFileName )
+HBufC* CWidgetUiObserver::RecognizeLC( const TDesC& aFileName)
     {
     TDataRecognitionResult dataType;
     RApaLsSession apaSession;
     TInt ret;
     
-    RFile file;
-    User::LeaveIfError( file.Open(CCoeEnv::Static()->FsSession(), aFileName, EFileRead|EFileShareAny) );
-    CleanupClosePushL( file );
-
     CleanupClosePushL(apaSession);
     User::LeaveIfError( apaSession.Connect() );
 
     // Ask the application architecture to find the file type
-    ret = apaSession.RecognizeData( file, dataType );
+    ret = apaSession.RecognizeData( iFile, dataType );
     apaSession.Close();
     
     CleanupStack::PopAndDestroy(1, &apaSession);
-    CleanupStack::PopAndDestroy( &file );
     
     TPtrC8 mimeTypePtr = dataType.iDataType.Des8();
     TInt len = mimeTypePtr.Length() + 1;