ImagePrint/ImagePrintUI/imageprinteng/src/cprintjobmanager.cpp
branchGCC_SURGE
changeset 25 59ea2209bb67
parent 23 08cc4cc059d4
parent 15 a92d00fca574
equal deleted inserted replaced
23:08cc4cc059d4 25:59ea2209bb67
     1 /*
       
     2 * Copyright (c) 2004-2007 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <imageprintapp.rsg>
       
    20 #include <StringLoader.h>
       
    21 #include <aknnotewrappers.h>
       
    22 
       
    23 #include "cprintjobmanager.h"
       
    24 #include "cprinteventcatcher.h"
       
    25 #include "csettingsmanager.h"
       
    26 #include "mprintjobobserver.h"
       
    27 #include "imageprint.h"
       
    28 #include "cimageprintengine.h"
       
    29 #include "crealfactory.h"
       
    30 #include "clog.h"
       
    31 #include "printcapabilitycodes.h"
       
    32 #include "printmessagecodes.h"
       
    33 #include "mprintsettings.h"
       
    34 #include "cimageprint.h"
       
    35 #include "mdiscoveryobserver.h"
       
    36 
       
    37 const TInt KCancelRetries( 2 );
       
    38 // CONSTRUCTION
       
    39 CPrintJobManager* CPrintJobManager::NewL(
       
    40     CRealFactory* aFactory,
       
    41     CImagePrintEngine* aDLLEngine )
       
    42     {
       
    43     CPrintJobManager* self = CPrintJobManager::NewLC(
       
    44         aFactory, aDLLEngine );
       
    45     CleanupStack::Pop();    // self
       
    46 
       
    47     return self;
       
    48     }
       
    49 
       
    50 CPrintJobManager* CPrintJobManager::NewLC(
       
    51     CRealFactory* aFactory,
       
    52     CImagePrintEngine* aDLLEngine )
       
    53     {
       
    54     CPrintJobManager* self = new ( ELeave ) CPrintJobManager(
       
    55         aFactory, aDLLEngine );
       
    56     CleanupStack::PushL( self );
       
    57     self->ConstructL();
       
    58 
       
    59     return self;
       
    60     }
       
    61 
       
    62 // Constructor
       
    63 CPrintJobManager::CPrintJobManager(
       
    64     CRealFactory* aFactory,
       
    65     CImagePrintEngine* aDLLEngine ) :
       
    66       iFactory( aFactory ),
       
    67       iDLLEngine( aDLLEngine )
       
    68     {
       
    69     }
       
    70 
       
    71 // Destructor
       
    72 CPrintJobManager::~CPrintJobManager()
       
    73     {
       
    74     LOG("CPrintJobManager::~CPrintJobManager BEGIN");
       
    75 
       
    76     iImages.ResetAndDestroy();
       
    77     iImages.Close();
       
    78     
       
    79     // Cancel current print job at exit
       
    80     if(IsPrinting())
       
    81     {		
       
    82     	TRAP_IGNORE( CancelL() );    	
       
    83     }
       
    84 
       
    85     LOG("CPrintJobManager::~CPrintJobManager END");
       
    86     }
       
    87 
       
    88 // Second phase constructor
       
    89 void CPrintJobManager::ConstructL()
       
    90     {
       
    91     }
       
    92 
       
    93 // Creates a new print job
       
    94 TInt CPrintJobManager::CreatePrintJobL(
       
    95     TInt aPrinterId )
       
    96     {
       
    97 	LOG("CPrintJobManager::CreatePrintJobL BEGIN");
       
    98 	iPrintingOnGoing = EFalse;
       
    99     // Creates a print job
       
   100     MPrintEventObserver* notifier = iFactory->PrintEventObserverIF();
       
   101     iPrinterUid = aPrinterId;
       
   102 
       
   103     TInt printJobError = iFactory->Engine()->CreatePrintJobL(
       
   104         aPrinterId, iDLLEngine->FileArray(), *notifier );
       
   105         
       
   106    iImages.ResetAndDestroy();
       
   107    
       
   108    for ( TInt i(0) ; i < iDLLEngine->FileArray().Count() ; i++ )
       
   109         {
       
   110         HBufC* image = iDLLEngine->FileArray()[i]->AllocLC();
       
   111         iImages.AppendL( image );
       
   112         CleanupStack::Pop( image );
       
   113         }
       
   114 
       
   115 	LOG1( "CPrintJobManager::CreatePrintJobL END, err: %d", printJobError );
       
   116     return printJobError;
       
   117     }
       
   118 
       
   119 // Submits the created print job
       
   120 void CPrintJobManager::PrintL(
       
   121     MPrintJobObserver* aObserver )
       
   122     {
       
   123 	LOG("CPrintJobManager::PrintL BEGIN");
       
   124     TInt getSettings;
       
   125     iPrintingOnGoing = ETrue;
       
   126 
       
   127     //Get current template UID from settings manager
       
   128     TInt uid = iFactory->SettingsIF()->TemplateUid();
       
   129 
       
   130     LOG1("Print done with id: %d", uid);
       
   131 
       
   132     //Set the UID to Image Print Engine.
       
   133     TInt err = iFactory->Engine()->SetJobSettingL(
       
   134                EPrintCapabLayout, uid, getSettings );
       
   135 	LOG1("CPrintJobManager::PrintL SetJobSettingL err: %d", err);
       
   136 
       
   137 	if ( err == KErrNotFound )
       
   138 		{
       
   139 		HBufC* buf = StringLoader::LoadLC( R_QTN_PRINT_NOT_FOUND_ERROR );
       
   140 		CAknErrorNote* errornote = new (ELeave) CAknErrorNote( ETrue );
       
   141 		CleanupStack::PushL( errornote );
       
   142 		errornote->ExecuteLD( *buf );
       
   143 		CleanupStack::PopAndDestroy( errornote );
       
   144 		CleanupStack::PopAndDestroy( buf );
       
   145 		}
       
   146 	if ( err != KErrNone )
       
   147 		{
       
   148 		iPrintingOnGoing = EFalse;
       
   149 	   	User::Leave( err );
       
   150 		}
       
   151     err = iFactory->Engine()->SubmitPrintJobL();
       
   152 	LOG1("CPrintJobManager::PrintL SubmitPrintJobL err: %d", err);
       
   153 	if ( err == KErrNotFound )
       
   154 		{
       
   155 		CAknErrorNote* errornote = new (ELeave) CAknErrorNote( ETrue );
       
   156 		HBufC* buf = StringLoader::LoadLC( R_QTN_PRINT_NOT_FOUND_ERROR );
       
   157 		errornote->ExecuteLD( *buf );
       
   158 		CleanupStack::PopAndDestroy( buf );
       
   159 		}
       
   160 	if ( err != KErrNone )
       
   161 		{
       
   162 		iPrintingOnGoing = EFalse;
       
   163 	   	User::Leave( err );
       
   164 		}
       
   165     iObserver = aObserver;
       
   166 	LOG("CPrintJobManager::PrintL END");
       
   167     }
       
   168 
       
   169 // Cancels printing
       
   170 void CPrintJobManager::CancelL()
       
   171     {
       
   172     LOG("CPrintJobManager::CancelL BEGIN");
       
   173     if ( IsPrinting() )
       
   174     	{
       
   175 	    iCancelling = ETrue;
       
   176 	    TInt err = iFactory->Engine()->CancelPrintJob();
       
   177 	    if ( err )
       
   178 	        {
       
   179 	        LOG1("CPrintJobManager::CancelL failed err: %d", err);
       
   180 	        if ( err == EPbStatusErrorReasonHardwarePrinterUnavailable )
       
   181 	        	{
       
   182 	        	//Printer was not ready, retry after a while a couple of times
       
   183 	        	for( TInt i=0; i<KCancelRetries; i++ )
       
   184 	        		{
       
   185 	        		// If cancellation doesn't work as expected. Add here Periodic timer.
       
   186 	        		err = iFactory->Engine()->CancelPrintJob();
       
   187 	        		if ( err == KErrNone )
       
   188 	        			{
       
   189 	        			break;
       
   190 	        			}
       
   191 	        		else
       
   192 	        			{
       
   193 	        			LOG1("CPrintJobManager::CancelL failed err: %d", err);
       
   194 	        			}
       
   195 	        		}
       
   196 	        	}
       
   197 	        else
       
   198 	        	{
       
   199 		        iCancelling = EFalse;
       
   200 		        User::LeaveIfError( err );	        	
       
   201 	        	}
       
   202 	        }
       
   203 		// Cancellation is returned to UI after PrintJobProgress returns completion status
       
   204 	    iPrintingOnGoing = EFalse;
       
   205 	    }
       
   206 	   else
       
   207 	   	{
       
   208 	   	if(iObserver)
       
   209 	   		{
       
   210 	   		iObserver->JobFinished(); 
       
   211 	   		}
       
   212 	   	}
       
   213 	    
       
   214 	LOG("CPrintJobManager::CancelL END");
       
   215     }
       
   216     
       
   217 // Returns the images from current print job.
       
   218 void CPrintJobManager::GetPrintJobL( RPointerArray<TDesC>& aImages )
       
   219     {
       
   220     for ( TInt i(0); i < iImages.Count(); i++ )
       
   221         {
       
   222         aImages.AppendL( iImages[i] );
       
   223         }
       
   224     }
       
   225 
       
   226 // Returns the status of the printing
       
   227 TBool CPrintJobManager::IsPrinting() const
       
   228     {
       
   229     return iPrintingOnGoing;
       
   230     }
       
   231 
       
   232 // Called by engine for progress notifications
       
   233 void CPrintJobManager::PrintJobProgress(
       
   234     TInt aStatus,
       
   235     TInt aPercentCompletion,
       
   236     TInt aJobStateCode )
       
   237     {
       
   238     if ( aStatus == EActive )
       
   239         {
       
   240         iPrintingOnGoing = ETrue;  
       
   241         iObserver->PrintProgress( aPercentCompletion );
       
   242         }
       
   243     else
       
   244         {
       
   245         if ( aJobStateCode == ECancelling && aStatus == EDone )
       
   246         	{
       
   247         	iCancelling = EFalse;
       
   248         	iPrintingOnGoing = EFalse;
       
   249         	iObserver->JobError( aJobStateCode, KErrNone );
       
   250         	}
       
   251         else if ( iCancelling && aStatus == EDone)
       
   252 			{
       
   253 	        iCancelling = EFalse;
       
   254 			iObserver->JobFinished();
       
   255 	        }
       
   256         else if ( aJobStateCode == ECancellingNoMessage && aStatus == EDone )
       
   257 			{
       
   258 	  	    iObserver->JobError( aJobStateCode, KErrNone );
       
   259 	        }
       
   260 	    else
       
   261 	        {
       
   262 	        iObserver->JobFinished();
       
   263 	        iPrintingOnGoing = EFalse;
       
   264 	        }
       
   265         }
       
   266     }
       
   267 
       
   268 // Called by engine to notify print errors
       
   269 void CPrintJobManager::PrintJobError(
       
   270     TInt aError, TInt aErrorStringCode )
       
   271 	{
       
   272 	LOG2("[CPrintJobManager::PrintJobError] PrintJobError: %d, errorstring: %d", aError, aErrorStringCode);
       
   273 	if( iPrintingOnGoing )
       
   274 		{
       
   275 		if ( iObserver )
       
   276 			{        
       
   277 			LOG("CPrintJobManager::PrintJobError Printing is going, send error to UI.");
       
   278 			iObserver->JobError( aError, aErrorStringCode );
       
   279 			}    	
       
   280 		}
       
   281 	// ERKZ-7JDFZ8 - Canceling Job hangs for ever
       
   282 	// If WLAN printer is turned off while printing UI receives KErrHostUnreach. 
       
   283 	// If error is not handled, printing continues. 
       
   284 	if ( aError == KErrHostUnreach )
       
   285 		{
       
   286 		if ( iObserver )
       
   287 			{ 
       
   288 			LOG("CPrintJobManager::PrintJobError KErrHostUnreach");
       
   289 			iObserver->JobError( KErrHostUnreach, aErrorStringCode );
       
   290 			}
       
   291 		}
       
   292 	iPrintingOnGoing = EFalse;
       
   293 	// show Pictbridge fatal errors and warnings always
       
   294 	if ( iFactory )
       
   295 		{
       
   296 		if( iFactory->SettingsIF()->GetCurrentPrinterProtocol() == MDiscoveryObserver::EUSB )
       
   297 			{
       
   298 			if ( iObserver )
       
   299 				{ 
       
   300 				LOG("CPrintJobManager::PrintJobError sending PictBridge error to UI...");
       
   301 				iObserver->JobError( aError, aErrorStringCode );
       
   302 				}
       
   303 			}
       
   304 		}
       
   305 	}
       
   306 
       
   307 // Called by engine to notify printer status (errors)
       
   308 void CPrintJobManager::PrinterStatus(
       
   309     TInt aError,
       
   310     TInt aErrorStringCode )
       
   311     {
       
   312 	iObserver->JobStatusEvent( aError, aErrorStringCode );
       
   313     }
       
   314 
       
   315 //  End of File