testexecfw/useremul/src/ImageCapture.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*------------------------------------------------------------------
       
     2  -
       
     3  * Software Name : UserEmulator
       
     4  * Version       : v4.2.1309
       
     5  * 
       
     6  * Copyright (c) 2009 France Telecom. All rights reserved.
       
     7  * This software is distributed under the License 
       
     8  * "Eclipse Public License - v 1.0" the text of which is available
       
     9  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10  *
       
    11  * Initial Contributors:
       
    12  * France Telecom 
       
    13  *
       
    14  * Contributors:
       
    15  *------------------------------------------------------------------
       
    16  -
       
    17  * File Name: ImageCapture.cpp
       
    18  * 
       
    19  * Created: 13/08/2009
       
    20  * Author(s): Marcell Kiss, Reshma Sandeep Das
       
    21  *   
       
    22  * Description:
       
    23  * Active object implementation for Image capture
       
    24  *------------------------------------------------------------------
       
    25  -
       
    26  *
       
    27  */
       
    28 
       
    29 //System Includes
       
    30 #include <icl\ImageData.h>
       
    31 #include <icl\ImageCodecData.h>
       
    32 #include <coemain.h>
       
    33 #include <PathInfo.h>
       
    34 #include <bautils.h>
       
    35 #include <apgwgnam.h>// CApaWindowGroupName
       
    36 
       
    37 //User Include
       
    38 #include "ImageCapture.h"
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CImageCapture::NewL
       
    42 // Creates the instance of class and returns it.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CImageCapture* CImageCapture::NewL(CSettings& aSettings ,MScreenshotObserver& aObserver, CEikonEnv* aEikonEnv)
       
    46 {
       
    47 	CImageCapture* self=CImageCapture::NewLC(aSettings, aObserver, aEikonEnv);
       
    48 	CleanupStack::Pop(); // self;
       
    49 	return self;
       
    50 }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CImageCapture::NewLC
       
    54 // Creates the instance of class and pushes it to the CleanupStack and return
       
    55 // it.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CImageCapture* CImageCapture::NewLC(CSettings& aSettings,MScreenshotObserver& aObserver, CEikonEnv* aEikonEnv)
       
    59 {
       
    60 	CImageCapture* self = new (ELeave)CImageCapture(aSettings, aObserver, aEikonEnv);
       
    61 	CleanupStack::PushL(self);
       
    62 	self->ConstructL();
       
    63 	return self;
       
    64 }
       
    65 // -----------------------------------------------------------------------------
       
    66 // CImageCapture::CImageCapture
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CImageCapture::CImageCapture(CSettings& aSettings,MScreenshotObserver& aObserver, CEikonEnv* aEikonEnv)
       
    70     : CActive(CActive::EPriorityStandard),
       
    71       iSettings(aSettings),iBitmap(NULL),
       
    72       iImageEncoder(NULL),
       
    73       iObserver(aObserver),
       
    74       iEEnv(aEikonEnv)
       
    75 {
       
    76 }
       
    77 // -----------------------------------------------------------------------------
       
    78 // CImageCapture::ConstructL
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CImageCapture::ConstructL()
       
    82 {
       
    83 	CActiveScheduler::Add(this);
       
    84 	User::LeaveIfError(iTimer.CreateLocal());
       
    85 }
       
    86 // -----------------------------------------------------------------------------
       
    87 // CImageCapture::~CImageCapture
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CImageCapture::~CImageCapture()
       
    91 {
       
    92     Cancel();
       
    93      
       
    94     if (iImageEncoder)
       
    95     {
       
    96        delete iImageEncoder;
       
    97        iImageEncoder = 0;
       
    98     }
       
    99     
       
   100     if(iBitmap)
       
   101     	delete iBitmap;
       
   102    
       
   103     iTimer.Close();
       
   104     iScreenShotStatus = EIdle;
       
   105 }
       
   106 // ------------------------------------------------------------------------------
       
   107 // CImageCapture::RunL
       
   108 // Issues request to save Images and notifies the observer after saving the image
       
   109 // ------------------------------------------------------------------------------
       
   110 //
       
   111 void CImageCapture::RunL()
       
   112 {
       
   113 	switch (iScreenShotStatus)
       
   114 	{
       
   115 		case EIdle:
       
   116 		break;
       
   117 			
       
   118 		case EWaiting:
       
   119 			{
       
   120 			RWsSession& wsSession = iEEnv->WsSession();
       
   121 			TInt id = wsSession.GetFocusWindowGroup();
       
   122 			CApaWindowGroupName* lApaWGName;
       
   123 			lApaWGName = CApaWindowGroupName::NewLC(wsSession,id);
       
   124 			
       
   125 			TUid uid = lApaWGName->AppUid();
       
   126 			
       
   127 			if(uid.operator ==(iAppUid) )
       
   128    			   Fire(ECapturing);
       
   129 			else
       
   130 				FireCapture(1000000);
       
   131 			
       
   132 			CleanupStack::PopAndDestroy(); // lApaWGName
       
   133 			
       
   134 			}
       
   135 			break;
       
   136 			
       
   137 		case ECapturing:
       
   138 			DoCaptureL();
       
   139 			break;
       
   140 			
       
   141 	
       
   142 		case ESaveComplete:
       
   143 		default:
       
   144 			iObserver.PerformNextAction(1);
       
   145 			delete this;		
       
   146 			
       
   147 	}          
       
   148 }
       
   149 // -----------------------------------------------------------------------------
       
   150 // CImageCapture::CaptureL
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CImageCapture::CaptureL(const TDesC& aName,const TDesC& aXmlFileName, TUid aAppUid)
       
   154 {
       
   155 	iFileName.Copy(aName);
       
   156 	iXmlFileName.Copy(aXmlFileName);
       
   157 	iAppUid = aAppUid;
       
   158 	
       
   159 	if(AknLayoutUtils::PenEnabled() && !(aAppUid.iUid ) )
       
   160 	{
       
   161 		User::After(KWait1);
       
   162 	}
       
   163 	
       
   164 	if(iAppUid.iUid)
       
   165 		Fire(EWaiting);
       
   166 	else
       
   167 		DoCaptureL();
       
   168 	
       
   169 }
       
   170 
       
   171 // --------------------------------------------------------------------------
       
   172 // Captures the screenshot and store it to a buffer.
       
   173 // --------------------------------------------------------------------------
       
   174 void CImageCapture::DoCaptureL()
       
   175 {
       
   176     const CWsScreenDevice* screenDevice = CCoeEnv::Static()->ScreenDevice();
       
   177 	
       
   178 	TPixelsTwipsAndRotation sizeAndRotation;
       
   179 	screenDevice->GetScreenModeSizeAndRotation(
       
   180 			screenDevice->CurrentScreenMode(), sizeAndRotation);
       
   181 
       
   182 	if(iBitmap)
       
   183 		delete iBitmap;
       
   184 	iBitmap = NULL;
       
   185 	iBitmap = new (ELeave) CFbsBitmap;
       
   186 	iBitmap->SetSizeInTwips( screenDevice);
       
   187 	User::LeaveIfError( iBitmap->Create( screenDevice->SizeInPixels(),screenDevice->DisplayMode() ) );	
       
   188 	User::LeaveIfError( screenDevice->CopyScreenToBitmap(iBitmap));
       
   189 
       
   190 	//save it in a file
       
   191 	DoSaveL(iFileName);	
       
   192 	iScreenShotStatus = ESaveComplete;
       
   193 	SetActive();
       
   194 }
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // CImageCapture::Fire
       
   198 // Fires an event. It means set this active object to active, but completes
       
   199 // the request immediately.
       
   200 // --------------------------------------------------------------------------
       
   201 void CImageCapture::Fire(TScreenShotStatus aStatus)
       
   202 {
       
   203     iScreenShotStatus = aStatus;
       
   204     SetActive();
       
   205     TRequestStatus* status = &iStatus;
       
   206     User::RequestComplete(status, KErrNone);
       
   207 }
       
   208 
       
   209 // --------------------------------------------------------------------------
       
   210 // CImageCapture::FireCapture
       
   211 // Fires an event after aTime microseconds.
       
   212 // --------------------------------------------------------------------------
       
   213 void CImageCapture::FireCapture(TTimeIntervalMicroSeconds32 aTime)
       
   214 {
       
   215     iScreenShotStatus = EWaiting;
       
   216     iTimer.After(iStatus, aTime);
       
   217     if(!IsActive())
       
   218     	SetActive();
       
   219 }
       
   220 
       
   221 // --------------------------------------------------------------------------
       
   222 // CImageCapture::DoSaveL
       
   223 // Saves captured screenshot to a file.
       
   224 // --------------------------------------------------------------------------
       
   225 void CImageCapture::DoSaveL(const TDesC& aName)
       
   226 {
       
   227     // Fetch the next available file name.
       
   228 
       
   229     HBufC* fileName = GetNextFileNameLC(aName);
       
   230     CCoeEnv::Static()->FsSession().MkDirAll(*fileName);
       
   231 
       
   232     // Set the MIME type
       
   233     _LIT8(KMimeTypepng,"image/png");
       
   234     TPtrC8 mimeType(KMimeTypepng);
       
   235 
       
   236     // Prepares the image data (if applied).
       
   237     CFrameImageData* frameImageData = CFrameImageData::NewL();
       
   238     CleanupStack::PushL(frameImageData);
       
   239     
       
   240     TPngEncodeData* encodeData = new (ELeave) TPngEncodeData();
       
   241     encodeData->iPaletted     = EFalse;
       
   242     encodeData->iColor        = ETrue;
       
   243     encodeData->iBitsPerPixel = 24;
       
   244     encodeData->iLevel = TPngEncodeData::EBestCompression;
       
   245     User::LeaveIfError(frameImageData->AppendFrameData(encodeData));
       
   246     
       
   247    // Creates a new encoder and then convert the bitmap.
       
   248     TRAPD(err, iImageEncoder = CImageEncoder::FileNewL(
       
   249     		CCoeEnv::Static()->FsSession(), *fileName, mimeType));
       
   250     if(err == KErrNone)
       
   251     	iImageEncoder->Convert(&iStatus, *iBitmap, frameImageData);
       
   252     
       
   253     CleanupStack::PopAndDestroy(frameImageData); // frameImageData
       
   254     CleanupStack::PopAndDestroy(fileName);           // fileName
       
   255     
       
   256 }
       
   257 // --------------------------------------------------------------------------
       
   258 // CImageCapture::GetNextFileNameLC
       
   259 // Get the file name
       
   260 // --------------------------------------------------------------------------
       
   261 HBufC* CImageCapture::GetNextFileNameLC(const TDesC& aName) const
       
   262 {
       
   263     const TInt KNumberLength = 4; // this is to indicate the file numbering,
       
   264                                   // e.g. 0001, 0002, etc.
       
   265     const TInt KMaxIndex = 10000;
       
   266     const TInt KTimeRecordSize = 512;
       
   267     
       
   268     TPtrC filePathPtr;
       
   269     _LIT(KExtensionpng, ".png");
       
   270     _LIT(KSlash,"\\");
       
   271     
       
   272     // Gets the file extension.
       
   273     TPtrC fileExtension;
       
   274     fileExtension.Set(KExtensionpng);
       
   275    
       
   276     filePathPtr.Set(iSettings.iLogPath);
       
   277     TInt result = filePathPtr.LocateReverse('\\');
       
   278     TPtrC string;
       
   279     if(result!=KErrNotFound)
       
   280     	string.Set(filePathPtr.Left(result+1));
       
   281     
       
   282     TBuf8<KTimeRecordSize> fileName;
       
   283     fileName.Copy(string);
       
   284     
       
   285     if(iXmlFileName.Length()>0)
       
   286     {
       
   287     	TInt pos=iXmlFileName.LocateReverse('.');
       
   288     	TPtrC8 ptr;
       
   289     	if(pos!=KErrNotFound)
       
   290     	{
       
   291     		ptr.Set(iXmlFileName.Left(pos));
       
   292     		fileName.Append(ptr);
       
   293     	}
       
   294     	
       
   295 		fileName.Append(KSlash);
       
   296     }
       
   297     
       
   298     fileName.Append(aName);
       
   299     HBufC* newFileName = HBufC::NewLC(fileName.Length()+ KNumberLength + fileExtension.Length() + 1);
       
   300     TPtr newFileNamePtr(newFileName->Des());
       
   301     newFileNamePtr.Copy(fileName);
       
   302 
       
   303     
       
   304     // Checks whether aNamexxxx.png already exists on the phone or not.
       
   305     // This is to prevent over-riding of any existing images with the same name
       
   306     TBool IsFileExist = ETrue;
       
   307     TInt index = 1;
       
   308     HBufC* buffer = HBufC::NewL(newFileNamePtr.MaxLength());
       
   309     TPtr bufferPtr(buffer->Des());
       
   310     while ((index < KMaxIndex) && (IsFileExist))
       
   311     {
       
   312         bufferPtr.Copy(newFileNamePtr);
       
   313         bufferPtr.AppendNumFixedWidth(index, EDecimal, KNumberLength);
       
   314         bufferPtr.Append(fileExtension);
       
   315         if (BaflUtils::FileExists(CCoeEnv::Static()->FsSession(), *buffer))
       
   316         {
       
   317         	index++;
       
   318         }
       
   319         else
       
   320         {
       
   321         	IsFileExist = EFalse;
       
   322         }
       
   323     }
       
   324     delete buffer;
       
   325 
       
   326     // If the index exceeds KMaxIndex, then we don't need to format the file name.
       
   327     if (index >= KMaxIndex)
       
   328     {
       
   329     	newFileNamePtr.AppendNum(index);
       
   330     }
       
   331     else
       
   332     {
       
   333     	newFileNamePtr.AppendNumFixedWidth(index, EDecimal, KNumberLength);
       
   334     }
       
   335     newFileNamePtr.Append(fileExtension);
       
   336 
       
   337     // If the index greated then KMaxIndex, then rollback to 1
       
   338     
       
   339     if (index >= KMaxIndex)
       
   340     {
       
   341     	index = 1;
       
   342     }
       
   343     
       
   344     return newFileName;
       
   345 }
       
   346 
       
   347 void CImageCapture::DoCancel()
       
   348 {
       
   349    if (iImageEncoder)
       
   350    {
       
   351 	   iImageEncoder->Cancel();
       
   352        delete iImageEncoder;
       
   353        iImageEncoder = 0;
       
   354    }
       
   355    iTimer.Cancel();
       
   356 }