ImagePrint/ImagePrintUI/imageprintapp/src/cpropertiesboxmodel.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 <akntextsettingpage.h>
       
    20 #include <eiklbx.h>
       
    21 #include <StringLoader.h>
       
    22 #include <AknQueryValueText.h>
       
    23 #include <aknpopupsettingpage.h>
       
    24 #include <aknslidersettingpage.h> 
       
    25 #include <bautils.h>
       
    26 #include <pathinfo.h>
       
    27 #include <aknmessagequerydialog.h>
       
    28 #include <apgcli.h>                 // rapalssession
       
    29 #include <imageprintapp.rsg>
       
    30 #include <AknQueryDialog.h>
       
    31 #include <driveinfo.h>
       
    32 
       
    33 #include "imageprint.h"
       
    34 #include "cprintsettings.h"
       
    35 #include "cbasecapability.h"
       
    36 #include "cimageprintsettingscontainer.h"
       
    37 #include "cimageprintengine.h"
       
    38 #include "ciffactory.h"
       
    39 #include "mdiscoveryobserver.h"
       
    40 #include "mdiscovery.h"
       
    41 #include "clog.h"
       
    42 #include "clistcapability.h"
       
    43 #include "imageprintvariant.h"
       
    44 #include "cpropertiesboxmodel.h"
       
    45 
       
    46 const TInt KListItemsRequired( 2 );
       
    47 const TInt KDefaultPrinterIndex( 0 );
       
    48 const TInt KNumerOfCopiesIndex( 1 );
       
    49 
       
    50 // CONSTRUCTION
       
    51 CPropertiesBoxModel* CPropertiesBoxModel::NewL(
       
    52     CImagePrintEngine& aEngine,
       
    53     MPrintSettings* aSettings,
       
    54     CImagePrintSettingsContainer* aContainer )
       
    55     {
       
    56     CPropertiesBoxModel* self =
       
    57         CPropertiesBoxModel::NewLC( aEngine, aSettings, aContainer );
       
    58     CleanupStack::Pop( self );
       
    59     return self;
       
    60     }
       
    61 
       
    62 CPropertiesBoxModel* CPropertiesBoxModel::NewLC(
       
    63     CImagePrintEngine& aEngine,
       
    64     MPrintSettings* aSettings,
       
    65     CImagePrintSettingsContainer* aContainer )
       
    66     {
       
    67     CPropertiesBoxModel* self =
       
    68         new ( ELeave ) CPropertiesBoxModel( aEngine, aSettings, aContainer );
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL();
       
    71     return self;
       
    72     }
       
    73 
       
    74 // Constructor
       
    75 CPropertiesBoxModel::CPropertiesBoxModel(
       
    76     CImagePrintEngine& aEngine,
       
    77     MPrintSettings* aSettings,
       
    78     CImagePrintSettingsContainer* aContainer ) :
       
    79     iEngine( aEngine ),
       
    80     iSettingsIF( aSettings ),
       
    81     iContainer( aContainer )
       
    82     {
       
    83     }
       
    84 
       
    85 // Destructor
       
    86 CPropertiesBoxModel::~CPropertiesBoxModel()
       
    87     {
       
    88     delete iPrintSettings;
       
    89     delete iDefaultPrinter;
       
    90     delete iNumberOfCopiesText; 
       
    91     delete iMMC;
       
    92     delete iPictBridge;
       
    93     }
       
    94 
       
    95 // 2nd phase constructor
       
    96 void CPropertiesBoxModel::ConstructL()
       
    97     {
       
    98     InitSettingsL();
       
    99     iDefaultPrinter = StringLoader::LoadL( R_PRINT_SELECTED_PRINTER );
       
   100     iNumberOfCopiesText = StringLoader::LoadL( R_QTN_PRINT_SETTINGS_NOC );
       
   101     iMMC = StringLoader::LoadL( R_PRINT_MEMORY_CARD );
       
   102     iPictBridge = StringLoader::LoadL( R_QTN_PRINT_SETTINGS_DEFAULT_USB );
       
   103     iNumberOfCopiesValue = 1;
       
   104     }
       
   105 
       
   106 void CPropertiesBoxModel::InitSettingsL()
       
   107     {
       
   108     delete iPrintSettings;
       
   109     iPrintSettings = NULL;
       
   110     iPrintSettings = iSettingsIF->PrinterSettingsL();
       
   111     iCount = iPrintSettings->iCapabilities.Count();
       
   112     TInt i( 0 );
       
   113     TInt counter = iPrintSettings->iCapabilities.Count();
       
   114     // If current protocol is MMC remove one item, because there is no layouts for mmc. 
       
   115     if ( MDiscoveryObserver::EMMC == iSettingsIF->GetCurrentPrinterProtocol() )
       
   116     	{
       
   117     		iCount--;
       
   118     	}
       
   119       
       
   120     for ( i = 0; i < counter; i++ )
       
   121         {
       
   122         if ( iPrintSettings->iCapabilities[i]->iTitle.CompareF( KNullDesC ) == 0 )
       
   123             {
       
   124             // Remove non-title capability items as those won't be displayed
       
   125             iCount--;
       
   126             }
       
   127         }
       
   128     
       
   129     // Add always first two for default printer item and for Number of copies item
       
   130     iCount = iCount + KListItemsRequired;
       
   131     }
       
   132 
       
   133 // Returns the number of listbox items
       
   134 TInt CPropertiesBoxModel::MdcaCount() const
       
   135     {
       
   136     LOG1("CPropertiesBoxModel::MdcaCount: %d", iCount );
       
   137     return iCount;
       
   138     }
       
   139 
       
   140 // Creates the text string for the list box item and returns it
       
   141 TPtrC CPropertiesBoxModel::MdcaPoint( TInt aIndex ) const
       
   142     {
       
   143     LOG1("CPropertiesBoxModel::MdcaPoint START index: %d", aIndex );
       
   144 	
       
   145 	switch ( aIndex )
       
   146 		{
       
   147 		case KDefaultPrinterIndex:
       
   148 		    {
       
   149 		    SetDefaultPrinterItem();
       
   150 			break;
       
   151 		    }
       
   152 		case KNumerOfCopiesIndex:
       
   153 		    {
       
   154 			SetNumberOfCopiesItem();
       
   155 			break;
       
   156 		    }
       
   157 		default:
       
   158             {
       
   159             SetOtherCapability( aIndex );
       
   160 			break;
       
   161             }    
       
   162 		}
       
   163 	
       
   164 	LOG1("CPropertiesBoxModel::MdcaPoint END, iBuf: %S", &iBuf );        
       
   165     return iBuf;
       
   166     }
       
   167 
       
   168 void CPropertiesBoxModel::SetDefaultPrinterItem() const
       
   169     {
       
   170     LOG("CPropertiesBoxModel::SetDefaultPrinterItem START");
       
   171 
       
   172     iBuf.Copy( KTabChar );
       
   173     iBuf.Append( *iDefaultPrinter );
       
   174     iBuf.Append( KTabChar );
       
   175     iBuf.Append( KTabChar );
       
   176     if( iSettingsIF->GetDefaultPrinterProtocol() ==
       
   177         MDiscoveryObserver::EMMC ) // mmc
       
   178         {
       
   179         iBuf.Append( *iMMC );
       
   180         }
       
   181     else if( iSettingsIF->GetDefaultPrinterProtocol() ==
       
   182              MDiscoveryObserver::EUSB ) // pictbridge
       
   183         {
       
   184         iBuf.Append( *iPictBridge );
       
   185         }
       
   186     else
       
   187         {
       
   188         // No can do if this fails. Not leaving function.
       
   189         TRAP_IGNORE(
       
   190             HBufC* defaultPrinterName =
       
   191                 iSettingsIF->GetDefaultPrinterNameLC();
       
   192             iBuf.Append( *defaultPrinterName );
       
   193             CleanupStack::PopAndDestroy( defaultPrinterName );
       
   194             );
       
   195         }
       
   196     
       
   197     LOG("CPropertiesBoxModel::SetDefaultPrinterItem END");	
       
   198     }
       
   199 
       
   200 void CPropertiesBoxModel::SetNumberOfCopiesItem() const
       
   201     {
       
   202     LOG("CPropertiesBoxModel::SetNumberOfCopiesItem START");	
       
   203 
       
   204     iBuf.Copy( KTabChar );
       
   205     iBuf.Append( *iNumberOfCopiesText );
       
   206     iBuf.Append( KTabChar );
       
   207     iBuf.Append( KTabChar );
       
   208     iBuf.AppendNum( iNumberOfCopiesValue );
       
   209 
       
   210     LOG("CPropertiesBoxModel::SetNumberOfCopiesItem END");	
       
   211     }
       
   212 
       
   213 void CPropertiesBoxModel::SetOtherCapability( const TInt &aIndex ) const
       
   214     {
       
   215     LOG("CPropertiesBoxModel::SetOtherCapability START");	
       
   216     
       
   217     iBuf.Zero();
       
   218     HBufC* buf = NULL;
       
   219 
       
   220     // Get the text for the list box
       
   221     TInt position = CapabilityArrayIndex( aIndex - KListItemsRequired );
       
   222     TRAPD( err,
       
   223        buf = iPrintSettings->iCapabilities[position]->ListBoxTextL() );
       
   224 	if ( !err && buf )
       
   225         {
       
   226         iBuf = buf->Des();
       
   227         delete buf;
       
   228         }
       
   229 
       
   230     LOG("CPropertiesBoxModel::SetOtherCapability END");	
       
   231     }
       
   232 
       
   233 // Handles the list box (enter) events by launching the pop-up list
       
   234 void CPropertiesBoxModel::HandleListBoxEventL(
       
   235     CEikListBox* aListBox,
       
   236     TListBoxEvent aEventType )
       
   237     {
       
   238     LOG("CPropertiesBoxModel::HandleListBoxEventL START");
       
   239     //Change to EEventItemSingleClicked for Single click event
       
   240     if ( (aEventType == EEventEnterKeyPressed || aEventType == EEventItemSingleClicked ) &&
       
   241          aListBox->CurrentItemIndex() != KErrNotFound )
       
   242         {
       
   243         if( aListBox->CurrentItemIndex() == 0 )
       
   244             {
       
   245             // launch dialog query for selecting default printer this returns
       
   246             // protocol id as defined in DiscoveryObserver.h or -1 if failed.
       
   247             TInt protocol = LaunchDefaultPrinterListL();
       
   248             if( protocol >= 0 )
       
   249                 {
       
   250                 iContainer->DoDiscoveryL( protocol );
       
   251                 InitSettingsL();
       
   252                 iContainer->UpdateListBox();
       
   253                 iContainer->DrawNow();
       
   254                 }
       
   255             }
       
   256         
       
   257          if( aListBox->CurrentItemIndex() == 1 )
       
   258             {
       
   259             LaunchNumberOfCopiesDialogL();
       
   260            	iContainer->UpdateListBox();
       
   261             iContainer->DrawNow();
       
   262             }
       
   263  	
       
   264          if ( aListBox->CurrentItemIndex() > 1 )
       
   265             {
       
   266             TInt selIndex = CapabilityArrayIndex( aListBox->CurrentItemIndex() - KListItemsRequired );
       
   267             TBool valueChanged = iPrintSettings->iCapabilities[selIndex]->LaunchPopupListL();
       
   268 
       
   269             if( valueChanged )
       
   270                 {
       
   271                 SaveCurrentSelectionL( *(iPrintSettings->iCapabilities[ selIndex ]), aListBox );
       
   272                 InitSettingsL();
       
   273                 iContainer->UpdateListBox();
       
   274                 iContainer->DrawNow();                    
       
   275                 }
       
   276             }
       
   277         }
       
   278 
       
   279 	LOG("CPropertiesBoxModel::HandleListBoxEventL END");        
       
   280     }
       
   281 
       
   282 // Saves selection to engine
       
   283 void CPropertiesBoxModel::SaveCurrentSelectionL(
       
   284                           CBaseCapability &aCapab,
       
   285                           CEikListBox* aListBox )
       
   286     {
       
   287     LOG("CPropertiesBoxModel::SaveCurrentSelectionL START");    
       
   288     
       
   289     TInt initList = iSettingsIF->SetPrinterSettingL( aCapab );
       
   290     if( initList == 0 )
       
   291         {
       
   292         CPrintSettings* tempSettings = iSettingsIF->PrinterSettingsL();
       
   293         delete iPrintSettings;
       
   294         iPrintSettings = tempSettings;
       
   295 
       
   296         // Sets the active page to be the first one, if settings have truly
       
   297         // changed
       
   298         iSettingsIF->SetActivePageL( 1 );
       
   299 
       
   300         aListBox->HandleItemRemovalL();
       
   301         aListBox->HandleItemAdditionL();
       
   302         }
       
   303 	
       
   304 	LOG("CPropertiesBoxModel::SaveCurrentSelectionL END");            
       
   305     }
       
   306 
       
   307 // Returns capability array index based on the list box index
       
   308 TUint CPropertiesBoxModel::CapabilityArrayIndex(
       
   309     TInt aListBoxIndex ) const
       
   310     {
       
   311 	LOG("CPropertiesBoxModel::CapabilityArrayIndex START");    
       
   312     
       
   313     // Skip capabilities with empty title as they won't be placed to list box
       
   314     TInt found( EFalse );
       
   315     TInt i( 0 );
       
   316     
       
   317     while ( !found )
       
   318         {
       
   319         if ( iPrintSettings->iCapabilities[i]->
       
   320              iTitle.CompareF( KNullDesC ) != 0  )
       
   321             {
       
   322             aListBoxIndex--;
       
   323             }
       
   324 
       
   325         if ( aListBoxIndex >= 0 )
       
   326             {
       
   327             i++;
       
   328             }
       
   329         else
       
   330             {
       
   331             found = ETrue;
       
   332             }
       
   333         }
       
   334 	
       
   335 	LOG("CPropertiesBoxModel::CapabilityArrayIndex END");    
       
   336     return TUint( i );
       
   337     }
       
   338 
       
   339 
       
   340 void CPropertiesBoxModel::LaunchNumberOfCopiesDialogL()
       
   341     {
       
   342 	LOG("CPropertiesBoxModel::LaunchNumberOfCopiesDialogL START");       
       
   343 	CAknNumberQueryDialog* dlg = new( ELeave ) CAknNumberQueryDialog ( iNumberOfCopiesValue );
       
   344 	dlg->PrepareLC( R_NOC_QUERY);
       
   345 	dlg->RunLD();
       
   346     
       
   347     // After selecting copies, NaviText is updated.
       
   348     iContainer->ChangeStatusPaneTextL( iNumberOfCopiesValue ); 
       
   349 	                                                           
       
   350     // Set value for settings
       
   351     SetNumberOfCopiesValueL();	
       
   352 	
       
   353 	LOG("CPropertiesBoxModel::LaunchNumberOfCopiesDialogL END");       
       
   354     }
       
   355 
       
   356 
       
   357 void CPropertiesBoxModel::SetNumberOfCopiesValueL()
       
   358     {
       
   359     RArray<TInt> numberOfCopiesArray;
       
   360     CleanupClosePushL( numberOfCopiesArray );
       
   361     iSettingsIF->NumberOfCopies( numberOfCopiesArray );
       
   362    
       
   363     // Images selected in print job
       
   364     TInt imagesCount = iEngine.FileArray().Count(); 
       
   365     if(0 != imagesCount)
       
   366         {
       
   367         // Get number of copies for current image
       
   368         if( numberOfCopiesArray.Count() != imagesCount )
       
   369             {
       
   370             numberOfCopiesArray.Reset();
       
   371             for( TInt i=0; i < imagesCount; ++i )
       
   372                 {
       
   373                 numberOfCopiesArray.Append( 1 );
       
   374                 }
       
   375             }
       
   376     
       
   377         TInt oldNOC( numberOfCopiesArray[ 0 ] );
       
   378     
       
   379         // if new value was entered
       
   380         if (iNumberOfCopiesValue != oldNOC)
       
   381             {
       
   382             // Set Number of copies value for each image.
       
   383             // Value is same for all images in IP 6.2
       
   384             for ( TInt i=0; i<imagesCount; i++ )
       
   385                 {
       
   386                 numberOfCopiesArray[ i ] = iNumberOfCopiesValue;
       
   387                 }
       
   388             
       
   389             iSettingsIF->SetNumberOfCopiesL( numberOfCopiesArray );
       
   390             }
       
   391         }
       
   392     CleanupStack::PopAndDestroy( &numberOfCopiesArray ); 
       
   393     }
       
   394 
       
   395 TInt CPropertiesBoxModel::LaunchDefaultPrinterListL()
       
   396     {
       
   397     const TInt expectedNumberOfProtocols( 4 );
       
   398     
       
   399     // Array for list item descriptors.
       
   400     CDesCArrayFlat* protocolNameArray = 
       
   401         new( ELeave ) CDesCArrayFlat( expectedNumberOfProtocols );
       
   402     CleanupStack::PushL( protocolNameArray );
       
   403 
       
   404     // Map from list item index to protocol.
       
   405     RArray<TInt> indexToProtocolMap( expectedNumberOfProtocols );
       
   406     CleanupClosePushL( indexToProtocolMap );
       
   407 
       
   408     // Get supported protocols from the engine
       
   409     TInt supportedProtocols =
       
   410         iEngine.InterfaceFactory().DiscoveryIF()->SupportedProtocols();
       
   411 
       
   412     // Read list item descriptors, append them in array and update map.
       
   413 
       
   414     // Bluetooth printer
       
   415     if( supportedProtocols & KImagePrint_PrinterProtocol_BPP )
       
   416         {
       
   417         protocolNameArray->AppendL( CCoeEnv::Static()->AllocReadResourceLC(
       
   418             R_QTN_PRINT_SETTINGS_DEFAULT_BT )->Des() );
       
   419         indexToProtocolMap.AppendL( MDiscoveryObserver::EBPP );
       
   420         CleanupStack::PopAndDestroy();	// protocolNameArray
       
   421         }
       
   422 
       
   423     // Memory card printer
       
   424     if( supportedProtocols & KImagePrint_PrinterProtocol_DPOF )
       
   425         {
       
   426         RFs fs;
       
   427     	CleanupClosePushL( fs ); 
       
   428     	if( fs.Connect() == KErrNone )
       
   429         	{
       
   430 			TInt driveId (-1);
       
   431 			TFileName path;
       
   432 			User::LeaveIfError( DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRemovableMassStorage, driveId ) );
       
   433 			User::LeaveIfError( PathInfo::GetRootPath( path, driveId ) );
       
   434 
       
   435     		if ( BaflUtils::PathExists( fs, path ))
       
   436     			{
       
   437 		        protocolNameArray->AppendL( CCoeEnv::Static()->AllocReadResourceLC(
       
   438 	            R_QTN_PRINT_MEMORY_CARD )->Des() );
       
   439 		        indexToProtocolMap.AppendL( MDiscoveryObserver::EMMC );
       
   440 		        CleanupStack::PopAndDestroy();	// protocolNameArray
       
   441     			}
       
   442     		
       
   443         	CleanupStack::PopAndDestroy( &fs );
       
   444         	}
       
   445         }
       
   446 
       
   447     // USB printers
       
   448     if( supportedProtocols & KImagePrint_PrinterProtocol_PictBridge )
       
   449         {
       
   450         protocolNameArray->AppendL( CCoeEnv::Static()->AllocReadResourceLC(
       
   451             R_QTN_PRINT_SETTINGS_DEFAULT_USB )->Des() );
       
   452         indexToProtocolMap.AppendL( MDiscoveryObserver::EUSB );
       
   453         CleanupStack::PopAndDestroy();	// protocolNameArray
       
   454         }
       
   455 
       
   456     // WLAN printers 
       
   457     if ( supportedProtocols & KImagePrint_PrinterProtocol_UPnP )
       
   458         {
       
   459         protocolNameArray->AppendL( CCoeEnv::Static()->AllocReadResourceLC(
       
   460         R_QTN_PRINT_SETTINGS_DEFAULT_WLAN )->Des() );
       
   461         indexToProtocolMap.AppendL( MDiscoveryObserver::EWLAN );
       
   462         CleanupStack::PopAndDestroy();	// protocolNameArray
       
   463         }
       
   464     
       
   465     // Prepare arrays for dialog
       
   466     CAknQueryValueTextArray* queryTextArray = CAknQueryValueTextArray::NewL();
       
   467     CleanupStack::PushL( queryTextArray );
       
   468     queryTextArray->SetArray( *protocolNameArray );
       
   469 
       
   470     CAknQueryValueText* queryValue = CAknQueryValueText::NewL();
       
   471     CleanupStack::PushL( queryValue );
       
   472     queryValue->SetArrayL( queryTextArray );
       
   473 
       
   474     // Determine which is the default protocol using settings interface.
       
   475     // Here defaultProtocol is protocol id as defined in MDiscoveryObserver.h.
       
   476     TInt defaultProtocol = iSettingsIF->GetDefaultPrinterProtocol();
       
   477 
       
   478     // Then do lookup in indexToProtocolMap to find correct index for this
       
   479     // protocol id. Note that after this defaultProtocol is an index.
       
   480     defaultProtocol = indexToProtocolMap.Find( defaultProtocol );
       
   481     if ( defaultProtocol != KErrNotFound )
       
   482     	{
       
   483 	    queryValue->SetCurrentValueIndex( defaultProtocol );
       
   484     	}
       
   485     else
       
   486     	{
       
   487    	    // Default protocol not available, get current protocol
       
   488    	    // This is for example for the case where MMC is default,
       
   489    	    // but removed from phone.
       
   490    	    TInt currentProtocol = iSettingsIF->GetCurrentPrinterProtocol();
       
   491 	    currentProtocol = indexToProtocolMap.Find( currentProtocol );
       
   492 	    if ( currentProtocol != KErrNotFound )
       
   493 	    	{
       
   494 			queryValue->SetCurrentValueIndex( currentProtocol );
       
   495 	    	}
       
   496     	}
       
   497 
       
   498     CAknPopupSettingPage* page = new ( ELeave ) CAknPopupSettingPage(
       
   499                 iDefaultPrinter,
       
   500                 EAknSettingPageNoOrdinalDisplayed,
       
   501                 EAknCtPopupSettingList,
       
   502                 R_EMPTY_PROTOCOL_SETTING_LIST,
       
   503                 R_SETTING_GENERIC_PROTOCOL_PAGE,
       
   504                 *queryValue );
       
   505     
       
   506     CleanupStack::PushL( page );
       
   507     page->ConstructL();
       
   508     CleanupStack::Pop( page );
       
   509 
       
   510     // Execute dialog and check that it returns properly. If so get the selected
       
   511     // index and use it to protocol map to get name of the selected protocol.
       
   512     TInt selectedProtocolId( -1 );
       
   513     if( page->ExecuteLD() )
       
   514         {
       
   515         selectedProtocolId =
       
   516             indexToProtocolMap[ queryValue->CurrentValueIndex() ];
       
   517         }
       
   518 
       
   519     // CleanupStack contains 4 items:
       
   520     // protocolNameArray, indexToProtocolMap (close), queryTextArray and
       
   521     // queryArray.
       
   522     const TInt numberOfItemsInCleanupStack( 4 );
       
   523     CleanupStack::PopAndDestroy( numberOfItemsInCleanupStack, protocolNameArray );
       
   524     return selectedProtocolId;
       
   525     }
       
   526 
       
   527 TInt CPropertiesBoxModel::NumberOfCopies()
       
   528 	{
       
   529 	return iNumberOfCopiesValue;
       
   530 	}
       
   531 
       
   532 //  End of File