mobilemessaging/postcard/postcardsrc/PostcardRecipientWrapper.cpp
changeset 0 72b543305e3a
child 2 0bf1d54f37d9
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005 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 *       Helper class for recipient/phonebook handling
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <badesca.h>
       
    24 #include <e32std.h>
       
    25 
       
    26 #include <aknlists.h> // PopupQuery stuff
       
    27 #include <aknPopup.h> 
       
    28 #include <aknpopuplayout.h>
       
    29 
       
    30 #include <StringLoader.h>
       
    31 
       
    32 #include <Postcard.rsg>
       
    33 #include "PostcardAppUi.h"
       
    34 #include "PostcardContact.h"
       
    35 #include "PostcardDocument.h"
       
    36 #include "PostcardRecipientWrapper.h"
       
    37 
       
    38 // ========== EXTERNAL DATA STRUCTURES =====================
       
    39 
       
    40 // ========== EXTERNAL FUNCTION PROTOTYPES =================
       
    41 
       
    42 // ========== CONSTANTS ====================================
       
    43 
       
    44 // ========== MACROS =======================================
       
    45 
       
    46 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    47 
       
    48 // ========== MODULE DATA STRUCTURES =======================
       
    49 
       
    50 // ========== LOCAL FUNCTION PROTOTYPES ====================
       
    51 
       
    52 // ========== LOCAL FUNCTIONS ==============================
       
    53 
       
    54 // ========== MEMBER FUNCTIONS =============================
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // CPostcardRecipientWrapper::CPostcardRecipientWrapper
       
    58 // ---------------------------------------------------------
       
    59 CPostcardRecipientWrapper::CPostcardRecipientWrapper( 
       
    60     CPostcardDocument& aDocument, 
       
    61     CPostcardAppUi& aAppUi,
       
    62     CPostcardContact& aContact
       
    63     ) :
       
    64     iDocument( aDocument ),
       
    65     iAppUi( aAppUi ),
       
    66     iContact( aContact )
       
    67 
       
    68     {    
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CPostcardRecipientWrapper::~CPostcardRecipientWrapper
       
    73 // ---------------------------------------------------------
       
    74 CPostcardRecipientWrapper::~CPostcardRecipientWrapper( )
       
    75     {
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CPostcardRecipientWrapper::ConstructL
       
    80 // ---------------------------------------------------------
       
    81 void CPostcardRecipientWrapper::ConstructL( )
       
    82     {
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CPostcardRecipientWrapper::NewL
       
    87 // ---------------------------------------------------------
       
    88 CPostcardRecipientWrapper* CPostcardRecipientWrapper::NewL( 
       
    89     CPostcardDocument& aDocument,
       
    90     CPostcardAppUi& aAppUi,
       
    91     CPostcardContact& aContact
       
    92     )
       
    93     {
       
    94     CPostcardRecipientWrapper* self = 
       
    95         new ( ELeave ) CPostcardRecipientWrapper(
       
    96             aDocument,
       
    97             aAppUi,
       
    98             aContact
       
    99             );
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL( );
       
   102     CleanupStack::Pop( self );
       
   103     return self;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // CPostcardRecipientWrapper::AskLocationL
       
   108 // ---------------------------------------------------------
       
   109 TBool CPostcardRecipientWrapper::AskLocationL(
       
   110     CPostcardContact::TLocation& aLocation )
       
   111     {
       
   112     static const CPostcardContact::TLocation locations[] =
       
   113         {
       
   114         CPostcardContact::ELocationPreferred,
       
   115         CPostcardContact::ELocationHome,
       
   116         CPostcardContact::ELocationWork
       
   117         };
       
   118     const TInt KNumLocations = sizeof( locations ) / sizeof( locations[ 0 ] );
       
   119 
       
   120     CPostcardContact::TLocation foundLocations[ KNumLocations ];
       
   121     TInt numLocations = 0;
       
   122 
       
   123     // Check if contact has address for possible locations
       
   124     for( TInt i = 0; i < KNumLocations; i++ )
       
   125         {
       
   126         if ( iContact.HasLocationL(locations[i]) )
       
   127             {
       
   128             foundLocations[ numLocations++ ] = locations[ i ];
       
   129             }
       
   130         }
       
   131 
       
   132     CPostcardContact::TLocation location =
       
   133         CPostcardContact::ELocationPreferred;
       
   134 
       
   135     if ( numLocations == 0 )
       
   136         {
       
   137         // Import only the name fields
       
   138         }
       
   139     else if ( numLocations == 1 )
       
   140         { // Only one location used -> use that
       
   141         location = foundLocations[ 0 ];
       
   142         }
       
   143     else
       
   144         {
       
   145         CAknSingleHeadingPopupMenuStyleListBox* listBox =
       
   146             new(ELeave) CAknSingleHeadingPopupMenuStyleListBox();
       
   147         CleanupStack::PushL( listBox );
       
   148         CAknPopupList* popupList = CAknPopupList::NewL( listBox,
       
   149             R_POSTCARD_SOFTKEYS_OK_BACK__OK,
       
   150             AknPopupLayouts::EMenuGraphicHeadingWindow );
       
   151         CleanupStack::PushL( popupList );
       
   152         listBox->ConstructL( popupList, CEikListBox::ELeftDownInViewRect );
       
   153         listBox->CreateScrollBarFrameL( ETrue );
       
   154         listBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   155             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
       
   156 
       
   157         TInt resId = R_POSTCARD_ADDRESS_SELECTION_TITLE;
       
   158         HBufC* titleText = iAppUi.ReadFromResourceLC( resId );
       
   159         popupList->SetTitleL( *titleText );
       
   160         CleanupStack::PopAndDestroy( titleText ); // titleText
       
   161 
       
   162         CDesCArrayFlat* strings = new (ELeave) CDesCArrayFlat( KNumLocations );
       
   163         CleanupStack::PushL( strings );
       
   164         
       
   165         for( TInt i = 0; i < numLocations; i++ )
       
   166             {
       
   167             HBufC* buffer = GetFormattedStringL( foundLocations[ i ] );
       
   168             CleanupStack::PushL( buffer );
       
   169             strings->AppendL( *buffer );
       
   170             CleanupStack::PopAndDestroy( buffer );
       
   171             }
       
   172 
       
   173         // For each list item replace the %N in the item format with the number 
       
   174         // of the icon, the %0N with the label of the address (Default, Home or
       
   175         // Business) and the %1N with the actual combined address string.        
       
   176         
       
   177         CTextListBoxModel* model = listBox->Model();
       
   178         model->SetItemTextArray( strings );
       
   179         model->SetOwnershipType( ELbmOwnsItemArray );
       
   180         CleanupStack::Pop( strings );
       
   181                 
       
   182         // And execute the dialog
       
   183         CleanupStack::Pop( popupList );
       
   184         TInt popupOk = popupList->ExecuteLD();
       
   185         TInt itemIndex = listBox->CurrentItemIndex();
       
   186         CleanupStack::PopAndDestroy( listBox ); // listBox;
       
   187 
       
   188         if ( popupOk )
       
   189 	        {
       
   190 	        location = foundLocations[ itemIndex ];
       
   191 	        }
       
   192         else
       
   193             {
       
   194             // Nothing was selected so just return..
       
   195             return EFalse;
       
   196             }
       
   197         }
       
   198     // Set location as out parameter
       
   199     aLocation = location;
       
   200     return ETrue;
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // CPostcardRecipientWrapper::GetFormattedStringL
       
   205 // ---------------------------------------------------------
       
   206 HBufC* CPostcardRecipientWrapper::GetFormattedStringL(
       
   207     CPostcardContact::TLocation aAddressLocation )
       
   208     {
       
   209     _LIT(KPostcardSelectItemFormat, "%0U\t%1U");
       
   210     TPtrC itemFormat(KPostcardSelectItemFormat);
       
   211 
       
   212     TBuf<64> addressBuffer;
       
   213     addressBuffer.Zero();
       
   214 
       
   215     static const TInt resIds[] =
       
   216         {
       
   217         R_POSTCARD_ADDRESS_SELECTION_NONE,
       
   218         R_POSTCARD_ADDRESS_SELECTION_HOME,
       
   219         R_POSTCARD_ADDRESS_SELECTION_WORK
       
   220         };
       
   221     TInt resId = resIds[ aAddressLocation ];
       
   222     HBufC* itemText = iAppUi.ReadFromResourceLC( resId );
       
   223     iContact.GetContactStringL( addressBuffer, aAddressLocation );
       
   224     
       
   225     TInt formatBufferLength = itemFormat.Length() + itemText->Length() + addressBuffer.Length();
       
   226             
       
   227     HBufC* sourceBuffer = HBufC::NewLC(formatBufferLength);
       
   228     HBufC* destinationBuffer = HBufC::NewLC(formatBufferLength);
       
   229     TPtr sourceBufferPtr = sourceBuffer->Des();
       
   230     TPtr destinationBufferPtr = destinationBuffer->Des();
       
   231             
       
   232     sourceBufferPtr.Copy(itemFormat);
       
   233 
       
   234     // List item label -> location 0, value itemText
       
   235     StringLoader::Format(destinationBufferPtr, sourceBufferPtr, 0, itemText->Des( ) );
       
   236     sourceBufferPtr.Copy(destinationBufferPtr);
       
   237 
       
   238     // List item text (address string) -> location 1, value addressBuffer
       
   239     StringLoader::Format(destinationBufferPtr, sourceBufferPtr, 1, addressBuffer);
       
   240 
       
   241     CleanupStack::Pop( destinationBuffer ); 
       
   242     CleanupStack::PopAndDestroy( 2, itemText ); // itemText, sourceBuffer
       
   243     return destinationBuffer;
       
   244     }
       
   245     
       
   246 // EOF