phonebookui/Phonebook/View/src/CPbkSingleItemFetchDlg.cpp
changeset 0 e686773b3f54
child 68 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 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 *       Provides methods for Phonebook Single Item Fetch API.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbkSingleItemFetchDlg.h"  // This class
       
    22 
       
    23 #include <avkon.rsg>
       
    24 #include <StringLoader.h>
       
    25 
       
    26 #include <PbkView.rsg>
       
    27 #include "CPbkFetchDlg.h"
       
    28 #include "MPbkFetchCallbacks.h"
       
    29 #include "CPbkSelectFieldDlg.h"
       
    30 
       
    31 #include <CPbkContactEngine.h>
       
    32 #include <CPbkContactItem.h>
       
    33 #include <CPbkFieldInfo.h>
       
    34 
       
    35 // Unnamed namespace for local definitions
       
    36 namespace {
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 #ifdef _DEBUG
       
    40 enum TPanicCode
       
    41     {
       
    42     EPanicPostCond_Constructor = 1,
       
    43     EPanicPreCond_ConstructL,
       
    44     EPanicPreCond_ExecuteLD
       
    45     };
       
    46 
       
    47 void Panic(TPanicCode aReason)
       
    48     {
       
    49     _LIT(KPanicText, "CPbkSingleItemFetchDlg");
       
    50     User::Panic(KPanicText, aReason);
       
    51     }
       
    52 #endif // _DEBUG
       
    53 
       
    54 CPbkFieldArray* CreateFieldArrayLC
       
    55         (const CPbkContactItem& aItem, const CPbkFieldIdArray* aFieldIds)
       
    56     {
       
    57     CPbkFieldArray* fieldArray = new(ELeave) CPbkFieldArray;
       
    58     CleanupStack::PushL(fieldArray);
       
    59     const TInt count = aItem.CardFields().Count();
       
    60     for (TInt i=0; i < count; ++i)
       
    61         {
       
    62         const TPbkContactItemField& field = aItem.CardFields()[i];
       
    63         // Add only nonempty fields
       
    64         if (!field.IsEmptyOrAllSpaces())
       
    65             {
       
    66             if (!aFieldIds)
       
    67                 {
       
    68                 // add all nonempty fields if no field type filter is given
       
    69                 fieldArray->AppendL(field);
       
    70                 }
       
    71             else
       
    72                 {
       
    73                 // Check that field's type matches with selectable types
       
    74                 if (aFieldIds->Contains(field.FieldInfo().FieldId()))
       
    75                     {
       
    76                     fieldArray->AppendL(field);
       
    77                     }
       
    78                 }
       
    79             }
       
    80         }
       
    81     return fieldArray;
       
    82     }
       
    83 
       
    84 }  // namespace
       
    85 
       
    86 
       
    87 // MODULE DATA STRUCTURES
       
    88 
       
    89 /**
       
    90  * MPbkFetchDlgAccept implementation for CPbkSingleItemFetchDlg.
       
    91  */
       
    92 NONSHARABLE_CLASS(CPbkSingleItemFetchDlg::TFetchCallback) : 
       
    93         public MPbkFetchDlgAccept
       
    94     {
       
    95     public: // constructor
       
    96         inline TFetchCallback(CPbkSingleItemFetchDlg& aFetchDlg) 
       
    97             : iFetchDlg(aFetchDlg)
       
    98             {
       
    99             };
       
   100 
       
   101     public: // from MPbkFetchEntryDlgAccept
       
   102         /**
       
   103          * called by CPbkFetchEntryDlg when selection is made. 
       
   104          * @param aId   The focused contact entry
       
   105          * @param aMarkedEntries    The marked contact entries
       
   106          * @return          ETrue to close CPbkFetchEntryDlg, 
       
   107          *                  EFalse to keep open.
       
   108          */
       
   109         TPbkFetchAccepted PbkFetchAcceptedL
       
   110             (TContactItemId aId, CContactIdArray* aMarkedEntries);
       
   111 
       
   112     private:
       
   113         /// Ref: to parent
       
   114         CPbkSingleItemFetchDlg& iFetchDlg;
       
   115     };
       
   116 
       
   117 
       
   118 // ================= MEMBER FUNCTIONS =======================
       
   119 
       
   120 // CPbkSingleItemFetchDlg::TFetchCallback
       
   121 MPbkFetchDlgAccept::TPbkFetchAccepted CPbkSingleItemFetchDlg::TFetchCallback::PbkFetchAcceptedL
       
   122         (TContactItemId aId, CContactIdArray* /*aMarkedEntries*/)
       
   123     {
       
   124     if (iFetchDlg.EntryFetchAcceptedL(aId))
       
   125         {
       
   126         return KFetchYes;
       
   127         }
       
   128     return KFetchNo;
       
   129     }
       
   130 
       
   131 // CPbkSingleItemFetchDlg::TParams
       
   132 EXPORT_C CPbkSingleItemFetchDlg::TParams::TParams() :
       
   133     iContactView(NULL), 
       
   134     iFieldIdArray(NULL), 
       
   135     iContactItem(NULL),
       
   136     iContactItemField(NULL),
       
   137     iCbaId(0)
       
   138     {
       
   139     }
       
   140 
       
   141 EXPORT_C CPbkSingleItemFetchDlg::TParams::operator TCleanupItem()
       
   142     {
       
   143     return TCleanupItem(Cleanup,this);
       
   144     }
       
   145 
       
   146 void CPbkSingleItemFetchDlg::TParams::Cleanup(TAny* aSelf)
       
   147     {
       
   148     TParams* self = static_cast<TParams*>(aSelf);
       
   149     delete self->iContactItem;
       
   150     self->iContactItem = NULL;
       
   151     self->iContactItemField = NULL;
       
   152     }
       
   153 
       
   154 // CPbkSingleItemFetchDlg
       
   155 inline CPbkSingleItemFetchDlg::CPbkSingleItemFetchDlg
       
   156         (TParams& aParams, 
       
   157         CPbkContactEngine& aEngine) : 
       
   158     iParams(aParams),
       
   159     iContactView(aParams.iContactView),
       
   160     iPbkEngine(aEngine)
       
   161     {
       
   162     // PostCond
       
   163     __ASSERT_DEBUG(!iFetchDlg && !iDestroyedPtr, Panic(EPanicPostCond_Constructor));
       
   164     }
       
   165 
       
   166 inline void CPbkSingleItemFetchDlg::ConstructL()
       
   167     {
       
   168     // PreCond
       
   169     __ASSERT_DEBUG(!iFetchDlg && !iDestroyedPtr, Panic(EPanicPreCond_ConstructL));
       
   170 
       
   171     // Set up contact array if needed
       
   172     if (!iContactView)
       
   173         {
       
   174         iContactView = &iPbkEngine.AllContactsView();
       
   175         }
       
   176     }
       
   177 
       
   178 EXPORT_C CPbkSingleItemFetchDlg* CPbkSingleItemFetchDlg::NewL
       
   179         (TParams& aParams, CPbkContactEngine& aEngine)
       
   180     {
       
   181     CPbkSingleItemFetchDlg* self = new(ELeave) CPbkSingleItemFetchDlg(aParams, aEngine);
       
   182     CleanupStack::PushL(self);
       
   183     self->ConstructL();
       
   184     CleanupStack::Pop();  // self
       
   185     return self;
       
   186     }
       
   187 
       
   188 EXPORT_C void CPbkSingleItemFetchDlg::SetMopParent(MObjectProvider* aParent)
       
   189     {
       
   190     iObjectProvider = aParent;
       
   191     }
       
   192 
       
   193 EXPORT_C TInt CPbkSingleItemFetchDlg::ExecuteLD()
       
   194     {
       
   195     // PreCond
       
   196     __ASSERT_DEBUG(iContactView, Panic(EPanicPreCond_ExecuteLD));
       
   197 
       
   198     // "D" function semantics
       
   199     CleanupStack::PushL(this);
       
   200     TBool thisDestroyed = EFalse;
       
   201     // Destructor sets thisDestroyed to ETrue
       
   202     iDestroyedPtr = &thisDestroyed;
       
   203 
       
   204     // Convert aParams for CPbkFetchDlg
       
   205     CPbkFetchDlg::TParams params;
       
   206     params.iResId = R_PBK_SINGLE_ENTRY_FETCH_DLG;
       
   207     params.iFlags = CPbkFetchDlg::FETCH_FOCUSED;
       
   208     params.iContactView = iContactView;
       
   209     params.iMarkedEntries = NULL;
       
   210     TFetchCallback callback(*this);
       
   211     params.iAcceptCallback = &callback;
       
   212     params.iCbaId = iParams.iCbaId;
       
   213 
       
   214     iFetchDlg = CPbkFetchDlg::NewL(params, iPbkEngine);
       
   215     iFetchDlg->ResetWhenDestroyed(&iFetchDlg);
       
   216     iFetchDlg->SetMopParent(iObjectProvider);
       
   217     TInt result = iFetchDlg->ExecuteLD();
       
   218     
       
   219     if (thisDestroyed)
       
   220         {
       
   221         // this object was destroyed
       
   222         result = 0;
       
   223         CleanupStack::Pop(this);
       
   224         }
       
   225     else
       
   226         {
       
   227         if (result)
       
   228             {
       
   229             // Switch ownership of result objects to iParams
       
   230             iParams.iContactItem = iContactItem;
       
   231             iContactItem = NULL;
       
   232             iParams.iContactItemField = iContactItemField;
       
   233             iContactItemField = NULL;
       
   234             }
       
   235         CleanupStack::PopAndDestroy(this);
       
   236         }
       
   237 
       
   238     return result;
       
   239     }
       
   240 
       
   241 TBool CPbkSingleItemFetchDlg::EntryFetchAcceptedL
       
   242         (TContactItemId aCid)
       
   243     {
       
   244     TBool thisDestroyed = EFalse;
       
   245     TBool* prevDestroyedPtr = iDestroyedPtr;
       
   246     iDestroyedPtr = &thisDestroyed;
       
   247 
       
   248     CPbkContactItem* item = iPbkEngine.ReadContactLC(aCid);
       
   249 
       
   250     // create array of fields    
       
   251     CPbkFieldArray* fieldArray = 
       
   252         CreateFieldArrayLC(*item, iParams.iFieldIdArray);
       
   253 
       
   254     TPbkContactItemField* field = NULL;
       
   255 
       
   256     if ( fieldArray->Count() != 1 )
       
   257         {
       
   258         HBufC* heading = StringLoader::LoadLC(R_QTN_PHOB_QTL_SELECT_ONE_ITEM);
       
   259 
       
   260         iFieldDlg = new(ELeave) CPbkSelectFieldDlg;
       
   261         iFieldDlg->ResetWhenDestroyed(&iFieldDlg);
       
   262         TInt cbaResourceId( R_AVKON_SOFTKEYS_SELECT_CANCEL__SELECT );
       
   263         if ( fieldArray->Count() == 0 )
       
   264             {
       
   265             // If field selection is empty, only back soft button
       
   266             // is available
       
   267             cbaResourceId = R_AVKON_SOFTKEYS_BACK;
       
   268             }
       
   269         field = iFieldDlg->ExecuteLD(*fieldArray, 
       
   270                 cbaResourceId, *heading);
       
   271         CleanupStack::PopAndDestroy(heading);
       
   272         }
       
   273     else
       
   274         {
       
   275         // Only one field in the array, grab it!
       
   276         field = &fieldArray->At(0);
       
   277         }
       
   278     
       
   279     if (thisDestroyed)
       
   280         {
       
   281         // This must be enough to ensure that rest of the code in this 
       
   282         // function doesn't access member variables, this object has been 
       
   283         // destroyed, remember!
       
   284         if (prevDestroyedPtr) *prevDestroyedPtr = ETrue;
       
   285         field = NULL;
       
   286         }
       
   287     else
       
   288         {
       
   289         iDestroyedPtr = prevDestroyedPtr;
       
   290         }
       
   291 
       
   292     if (field)
       
   293         {
       
   294         const TInt index = item->FindFieldIndex(*field);
       
   295         TPbkContactItemField& itemField = item->CardFields()[index];
       
   296         CleanupStack::PopAndDestroy(fieldArray);
       
   297         CleanupStack::Pop(item);
       
   298         // Take ownership of item
       
   299         iContactItem = item;
       
   300         iContactItemField = &itemField;
       
   301         }
       
   302     else
       
   303         {
       
   304         CleanupStack::PopAndDestroy(2,item);
       
   305         }
       
   306 
       
   307     return (field!=NULL); 
       
   308     }
       
   309 
       
   310 CPbkSingleItemFetchDlg::~CPbkSingleItemFetchDlg()
       
   311     {
       
   312     if (iDestroyedPtr) 
       
   313         {
       
   314         *iDestroyedPtr = ETrue;
       
   315         }
       
   316 
       
   317     // Close the dialogs
       
   318     delete iFieldDlg;
       
   319     delete iFetchDlg;
       
   320 
       
   321     // delete return values in case of cancel
       
   322     delete iContactItem;
       
   323     }
       
   324 
       
   325 
       
   326 //  End of File