phonebookui/Phonebook2/UIControls/src/CPbk2StoreListQuery.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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:  A list query for selecting a contact store.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbk2StoreListQuery.h"
       
    22 
       
    23 #include <aknlistquerydialog.h>
       
    24 
       
    25 #include <Pbk2UIControls.rsg>
       
    26 #include <MPbk2ApplicationServices.h>
       
    27 #include <MPbk2AppUi.h>
       
    28 #include <CPbk2StoreProperty.h>
       
    29 #include <CPbk2StorePropertyArray.h>
       
    30 #include <CPbk2StorePropertyContentStoreName.h>
       
    31 #include <CPbk2ApplicationServices.h>
       
    32 
       
    33 #include <CVPbkContactStoreUriArray.h>
       
    34 #include <badesca.h>
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CPbk2StoreListQuery::CPbk2StoreListQuery
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CPbk2StoreListQuery::CPbk2StoreListQuery()
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CPbk2StoreListQuery::NewL
       
    48 // Two-phased constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 EXPORT_C CPbk2StoreListQuery* CPbk2StoreListQuery::NewL()
       
    52     {
       
    53     CPbk2StoreListQuery* self = new( ELeave ) CPbk2StoreListQuery();
       
    54     return self;
       
    55     }
       
    56 
       
    57 // Destructor
       
    58 CPbk2StoreListQuery::~CPbk2StoreListQuery()
       
    59     {
       
    60     delete iAllItemText;
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CPbk2StoreListQuery::ExecuteLD
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C TInt CPbk2StoreListQuery::ExecuteLD(const TDesC& aTitle,
       
    68         CVPbkContactStoreUriArray& aUriArray)
       
    69     {
       
    70     CleanupStack::PushL(this);
       
    71 
       
    72     TInt selection = KErrNotFound;
       
    73     CAknListQueryDialog* dlg = new(ELeave) CAknListQueryDialog(&selection);
       
    74     CleanupStack::PushL( dlg );
       
    75     dlg->PrepareLC(R_PBK2_STORE_LIST_QUERY);
       
    76     dlg->QueryHeading()->SetTextL(aTitle);
       
    77     dlg->SetItemTextArray(CreateListItemsL(aUriArray));
       
    78     dlg->SetOwnershipType(ELbmOwnsItemArray);
       
    79     CleanupStack::Pop( dlg );
       
    80     TInt ret = dlg->RunLD();
       
    81 
       
    82     if (ret == EAknSoftkeyOk &&
       
    83         (selection != AllItemIndex(aUriArray)))
       
    84         {
       
    85         SetSelection(aUriArray, selection);
       
    86         }
       
    87     CleanupStack::PopAndDestroy(this);
       
    88     return ret;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CPbk2StoreListQuery::SetAllItemTextL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C void CPbk2StoreListQuery::SetAllItemTextL(const TDesC& aAllItemText,
       
    96             TBool aUseTopPosition)
       
    97     {
       
    98     delete iAllItemText;
       
    99     iAllItemText = NULL;
       
   100     iAllItemText = aAllItemText.AllocL();
       
   101     iAllItemUsesTopPosition = aUseTopPosition;
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CPbk2StoreListQuery::CreateListItemsL
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 MDesCArray* CPbk2StoreListQuery::CreateListItemsL(
       
   109         CVPbkContactStoreUriArray& aUriArray)
       
   110     {
       
   111     const TInt granularity = 4;
       
   112     const TInt firstPosition = 0;
       
   113     CDesCArrayFlat* itemArray = new(ELeave) CDesCArrayFlat(granularity);
       
   114     CleanupStack::PushL(itemArray);
       
   115 
       
   116     CPbk2ApplicationServices* appServices = CPbk2ApplicationServices::InstanceLC();
       
   117     CPbk2StorePropertyArray& props = appServices->StoreProperties();
       
   118     CleanupStack::PopAndDestroy(); // appServices
       
   119 
       
   120     const TInt count = aUriArray.Count();
       
   121     TPbk2ContentId contentId = EPbk2CopyQueryItem;
       
   122     if ( iAllItemText )
       
   123         {
       
   124         // All item text is used only in Memory info query.
       
   125         // If this class is used different way this contentId should be
       
   126         // determined more accurated way.
       
   127         contentId = EPbk2MemInfoPhone;
       
   128         }
       
   129 
       
   130     for (TInt i = count - 1; i >= 0; --i)
       
   131         {
       
   132         const CPbk2StoreProperty* prop = props.FindProperty(aUriArray[i]);
       
   133         if (prop)
       
   134             {
       
   135             CPbk2Content* content = prop->RetrieveContentLC( contentId );
       
   136 
       
   137             CPbk2StorePropertyContentStoreName* propContent =
       
   138                 dynamic_cast<CPbk2StorePropertyContentStoreName*>( content );
       
   139 
       
   140             if (propContent && propContent->StoreName() != KNullDesC())
       
   141                 {
       
   142                 // Keep the same order as in the URI array
       
   143                 itemArray->InsertL(firstPosition, propContent->StoreName());
       
   144                 }
       
   145             else
       
   146                 {
       
   147                 // Backup plan -> use store name
       
   148                 itemArray->InsertL(firstPosition, prop->StoreName());
       
   149                 }
       
   150 
       
   151             CleanupStack::PopAndDestroy( content );
       
   152             }
       
   153         else
       
   154             {
       
   155             aUriArray.Remove(aUriArray[i]);
       
   156             }
       
   157         }
       
   158 
       
   159     if (iAllItemText)
       
   160         {
       
   161         if (iAllItemUsesTopPosition)
       
   162             {
       
   163             itemArray->InsertL(firstPosition, *iAllItemText);
       
   164             }
       
   165         else
       
   166             {
       
   167             itemArray->AppendL(*iAllItemText);
       
   168             }
       
   169         }
       
   170 
       
   171     CleanupStack::Pop(itemArray);
       
   172     return itemArray;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CPbk2StoreListQuery::SetSelection
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CPbk2StoreListQuery::SetSelection(CVPbkContactStoreUriArray& aUriArray,
       
   180         TInt aSelection)
       
   181     {
       
   182     TInt selection = aSelection;
       
   183     if (iAllItemText && iAllItemUsesTopPosition)
       
   184         {
       
   185         --selection;
       
   186         }
       
   187 
       
   188     const TInt count = aUriArray.Count();
       
   189     for (TInt i = count - 1; i >= 0; --i)
       
   190         {
       
   191         if (i != selection)
       
   192             {
       
   193             aUriArray.Remove(aUriArray[i]);
       
   194             }
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CPbk2StoreListQuery::AllItemIndex
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TInt CPbk2StoreListQuery::AllItemIndex(CVPbkContactStoreUriArray& aUriArray)
       
   203     {
       
   204     if (iAllItemText)
       
   205         {
       
   206         TInt itemIndex = aUriArray.Count() - 1;
       
   207         if (iAllItemUsesTopPosition)
       
   208             {
       
   209             itemIndex = 0;
       
   210             }
       
   211         return itemIndex;
       
   212         }
       
   213     else
       
   214         {
       
   215         return KErrNotFound;
       
   216         }
       
   217     }
       
   218 //  End of File