// ---------------------------------------------------------------------------
// 
// ---------------------------------------------------------------------------
//
TBool CAiwImagePrintIf::IsPrintingSupported( const CAiwGenericParamList& aInParamList )
    {  
    TInt index(0);
    FLOG(_L("[CAiwImagePrintIf]  IsPrintingSupported "));
    const TAiwGenericParam* param = aInParamList.FindFirst( index, EGenericParamMIMEType );
	TBool printable( EFalse );
	
    if ( index == KErrNotFound )	
        {    
        index = 0;
   	    const TAiwGenericParam* param = aInParamList.FindFirst(index, EGenericParamFile, EVariantTypeDesC);

        //Check if there is any printable images available. At least image must be
        //JPEG and it also must be unprotected to be printable.
        while ( index != KErrNotFound && !printable)
            {
            FLOG(_L("[CAiwImagePrintIf]  IsPrintingSupported 3"));
            TRAP_IGNORE( printable = IsPrintingSupportedL( param->Value().AsDes() ));
   	        param = aInParamList.FindNext(index, EGenericParamFile, EVariantTypeDesC);        
            }
        FTRACE(FPrint(_L("[CAiwImagePrintIf] IsPrintingSupported  printable is  %d"), printable ));     
        return printable;    
        }
	
	while ( index != KErrNotFound && !printable )
		{
	   	if ( param->Value().TypeId() == EVariantTypeDesC &&
			 param->Value().AsDes() == KJpegFileType16 )
	    	{
	    	// MIME-type parameter follows filename parameter in parameter list.
	    	// Because of that previous item in list is used.
	    	if (index > 0)
	    	    {   
	    	    FLOG(_L("[CAiwImagePrintIf]  IsPrintingSupported 5")); 	    
    	      	printable = !IsProtected( aInParamList[index-1].Value().AsDes() );
	    	    }
	    	}

	    if ( !printable )
	        {        
    	   	param = aInParamList.FindNext(index, EGenericParamMIMEType);
	        }
       	}    
	FTRACE(FPrint(_L("[CAiwImagePrintIf] IsPrintingSupported 2 printable is  %d"), printable ));     
    return printable;	 
    }
// -----------------------------------------------------------------------------
// Implements getting a AIW parameter as descriptor
// -----------------------------------------------------------------------------
//
TPtrC CAknsWallpaperPlugin::GetAiwParamAsDescriptor(
        const CAiwGenericParamList& aParamList,
        TGenericParamId aParamType)
    {
    TInt index = 0;
    const TAiwGenericParam* genericParam = NULL;
    genericParam = aParamList.FindFirst(
        index,
        aParamType,
        EVariantTypeDesC);

    if (index >= 0 && genericParam)
        {
        // Get the data
        return genericParam->Value().AsDes();
        }
    else
        {
        return KNullDesC();
        }
    }
// ---------------------------------------------------------------------------
// 
// ---------------------------------------------------------------------------
//
void CAiwPrintingProvider::DoHandleCmdL(TInt aMenuCmdId,
                            const CAiwGenericParamList& aInParamList,
                            CAiwGenericParamList& aOutParamList,
                            TUint /*aCmdOptions*/,
                            const MAiwNotifyCallback* aCallback)
    {
    if ( aMenuCmdId == KAiwCmdPrint || aMenuCmdId == KAiwCmdPrintPreview  )
        {
        FLOG(_L("[CAiwPrintingProvider]<<<  DoHandleCmdL"));
        
        CAiwGenericParamList* checkedParams = CAiwGenericParamList::NewL();
        
        iConsumerInParamList = &aInParamList;
        iConsumerOutParamList = &aOutParamList;
        iConsumerCallback = aCallback;
        
        TInt index( 0 );
	    const TAiwGenericParam* param = aInParamList.FindFirst(index,
	            EGenericParamFile,
	            EVariantTypeDesC);
	    while ( index != KErrNotFound )
			{
	        TFileName filename( param->Value().AsDes() );
 	        TInt err = KErrNone;
	        TBool result = EFalse;
	        TRAP( err, result = IsPrintingSupportedL( filename ) );
	        if ( err == KErrNone && result )
	        	{
	            FLOG(_L("[CAiwPrintingProvider] DoHandleCmdL; supported file"));
	        	checkedParams->AppendL(*param);
	        	}
	        else
	        	{
	        	FLOG(_L("[CAiwPrintingProvider] DoHandleCmdL; not supported"));
	        	++iNumberOfUnSuppFiles;
	        	iUnsupportedFiles = ETrue;
	        	}	
	        param = aInParamList.FindNext(index,
	            EGenericParamFile,
	            EVariantTypeDesC);
	        }
   
                
		FTRACE(FPrint(_L("[CAiwPrintingProvider] UnSuppFiles is %d"), iNumberOfUnSuppFiles )); 

		RFileWriteStream stream;
	  	CleanupClosePushL(stream);
		if((stream.Replace(iEikEnv.FsSession(), *iUnsuppFileName ,EFileWrite)) == KErrNone)
			{
			stream.WriteInt16L(iNumberOfUnSuppFiles);
			stream.CommitL();
			}
		CleanupStack::PopAndDestroy(&stream); 
		
        FLOG(_L("[IMAGEPRINTUI]<<< CAiwPrintingProvider;Save iUnsupportedFiles  is done"));
	
        
        RFileWriteStream writeStream;
        User::LeaveIfError( writeStream.Replace(iEikEnv.FsSession(),
        										*iPrintFileName , EFileWrite) );
        writeStream.PushL();
        checkedParams->ExternalizeL(writeStream);
        writeStream.CommitL();
        CleanupStack::PopAndDestroy( &writeStream );
        
        iNumberOfUnSuppFiles = 0;
        delete checkedParams;
        checkedParams = NULL;
        
        LaunchImagePrintApplicationL();
        FLOG(_L("[CAiwPrintingProvider]>>> DoHandleCmdL "));
        }
    }