phonebookengines/VirtualPhonebook/VPbkLdapStore/src/ContactView.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  CContactView implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "contactview.h"
       
    21 #include "contactlink.h"
       
    22 #include "contactstore.h"
       
    23 #include "storebookmark.h"
       
    24 #include "viewcontact.h"
       
    25 
       
    26 #include <cvpbksortorder.h>
       
    27 #include <vpbkcontactview.hrh>
       
    28 #include <mvpbkcontactviewobserver.h>
       
    29 #include <mvpbkcontactbookmark.h>
       
    30 
       
    31 // CONSTANTS
       
    32 static const TInt KObserverArrayGranularity = 8;
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // LDAPNotifyObservers
       
    36 // Contact view state notifier callback
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 static TInt LDAPNotifyObservers(TAny* aThis)
       
    40     {
       
    41     if (aThis)
       
    42         {
       
    43         return static_cast<LDAPStore::CContactView*>(aThis)->NotifyObservers();
       
    44         }
       
    45     return EFalse;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // LDAP Store namespace
       
    50 // -----------------------------------------------------------------------------
       
    51 namespace LDAPStore {
       
    52 
       
    53 /**
       
    54 *  TContactViewObserver - contact view observer class.
       
    55 *  
       
    56 */
       
    57 class TContactViewObserver
       
    58     {
       
    59     public:     // TContactViewObserver public constructor
       
    60         /**
       
    61         @function   TContactViewObserver
       
    62         @discussion TContactViewObserver contructor
       
    63         @param      aObserver View observer
       
    64         */
       
    65         inline TContactViewObserver(MVPbkContactViewObserver& aObserver)
       
    66             : iNotifiedState(ENotReady), iObserver(aObserver)
       
    67             {
       
    68             };
       
    69     public:     // TContactViewObserver public members
       
    70 
       
    71         /**
       
    72         @function   NotifiedState
       
    73         @discussion Returns notified state
       
    74         @return     Notified view state
       
    75         */
       
    76         inline TViewState NotifiedState() const {
       
    77             return iNotifiedState;
       
    78         }
       
    79         /**
       
    80         @function   SetNotifiedState
       
    81         @discussion Returns notified state
       
    82         @parame     aState Notified view state
       
    83         */
       
    84         inline void SetNotifiedState(TViewState aState) {
       
    85             iNotifiedState = aState;
       
    86         }
       
    87 
       
    88         /**
       
    89         @function   Observer
       
    90         @discussion Returns observer
       
    91         @return     Observer
       
    92         */
       
    93         inline MVPbkContactViewObserver& Observer() const {
       
    94             return iObserver;
       
    95         }
       
    96 
       
    97     private:    // TContactViewObserver private members
       
    98 
       
    99         // Notified state
       
   100         TViewState iNotifiedState;
       
   101 
       
   102         // Observer
       
   103         MVPbkContactViewObserver& iObserver;
       
   104     };
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CContactView::CContactView
       
   108 // C++ default constructor can NOT contain any code, that might leave.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 CContactView::CContactView(CContactStore& aStore,TVPbkContactViewType aType)
       
   112 :   iStore(aStore), iType(aType), iViewState(ENotReady)
       
   113     {
       
   114     // No implementation required
       
   115     }
       
   116 // -----------------------------------------------------------------------------
       
   117 // CContactView::ConstructL
       
   118 // CContactView constructor for performing 2nd stage construction
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CContactView::ConstructL(const MVPbkFieldTypeList& aSortOrder)
       
   122     {
       
   123     // Observers
       
   124     iObservers = new (ELeave)
       
   125         CArrayPtrFlat<TContactViewObserver>(KObserverArrayGranularity);
       
   126 
       
   127     // Copy the sort order
       
   128     iSortOrder = CVPbkSortOrder::NewL(aSortOrder);
       
   129 
       
   130     // Create contact
       
   131     iContact = CViewContact::NewL(*this,*iSortOrder);
       
   132 
       
   133     // Create view state notify handler
       
   134     iIdle = CIdle::NewL(CActive::EPriorityIdle);
       
   135 
       
   136     // Store initialized and folding view
       
   137     if (iStore.IsInitialized() && iType == EVPbkFoldingView)
       
   138         {
       
   139         // View is ready when store is
       
   140         iViewState = EReady;
       
   141         }
       
   142     else
       
   143         {
       
   144         // Add store observer
       
   145         iStore.AddObserverL(*this);
       
   146         }
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CContactView::NewLC
       
   151 // CContactView two-phased constructor.
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 CContactView* CContactView::NewLC(
       
   155     MVPbkContactViewObserver& aObserver,
       
   156     CContactStore&               aStore,
       
   157     const MVPbkFieldTypeList& aSortOrder,
       
   158     TVPbkContactViewType           aType
       
   159 )
       
   160     {
       
   161     CContactView* self = new (ELeave)CContactView(aStore,aType);
       
   162     CleanupStack::PushL(self);
       
   163     self->ConstructL(aSortOrder);
       
   164     self->AddObserverL(aObserver);
       
   165     return self;
       
   166     }
       
   167 // -----------------------------------------------------------------------------
       
   168 // CContactView::NewL
       
   169 // CContactView two-phased constructor.
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 CContactView* CContactView::NewL(
       
   173     MVPbkContactViewObserver& aObserver,
       
   174     CContactStore&               aStore,
       
   175     const MVPbkFieldTypeList& aSortOrder,
       
   176     TVPbkContactViewType           aType
       
   177 )
       
   178     {
       
   179     CContactView* self = CContactView::NewLC(aObserver,aStore,aSortOrder,aType);
       
   180     CleanupStack::Pop(self);
       
   181     return self;
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CContactView::~CContactView
       
   186 // CContactView Destructor
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 CContactView::~CContactView()
       
   190     {
       
   191     // Callback
       
   192     if (iIdle)
       
   193         {
       
   194         // Always cancel pending operations
       
   195         iIdle->Cancel();
       
   196         // Delete
       
   197         delete iIdle;
       
   198         iIdle = NULL;
       
   199         }
       
   200     // Store contact
       
   201     if (iContact)
       
   202         {
       
   203         delete iContact;
       
   204         iContact = NULL;
       
   205         }
       
   206     // Sort order
       
   207     if (iSortOrder)
       
   208         {
       
   209         delete iSortOrder;
       
   210         iSortOrder = NULL;
       
   211         }
       
   212     // Observers
       
   213     if (iObservers)
       
   214         {
       
   215         iObservers->ResetAndDestroy();
       
   216         delete iObservers;
       
   217         iObservers = NULL;
       
   218         }
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 //                      CContactView public methods
       
   223 // -----------------------------------------------------------------------------
       
   224 // CContactView::NotifyObservers
       
   225 // Notifies observers about view state changes
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 TInt CContactView::NotifyObservers()
       
   229     {
       
   230     TInt status = ETrue;
       
   231 
       
   232     // Observers
       
   233     const TInt count = (iObservers) ? iObservers->Count() : 0;
       
   234 
       
   235     // View state
       
   236     TViewState state = State();
       
   237     if (state != EReady)
       
   238         {
       
   239         // Store has contacts - view ready
       
   240         if (iStore.ContactCount() > 0 &&
       
   241             iStore.State() == CContactStore::ERetrieved)
       
   242             {
       
   243             // Set ready
       
   244             iViewState = EReady; state = EReady;
       
   245             }
       
   246         }
       
   247 
       
   248     // Notify all
       
   249     for (TInt loop = iObservers->Count() - 1; loop >= 0;loop--)
       
   250         {
       
   251         // This observer
       
   252         TContactViewObserver* op = iObservers->At(loop);
       
   253         if (op && op->NotifiedState() != state)
       
   254             {
       
   255             // View ready
       
   256             if (state == EReady)
       
   257                 {
       
   258                 op->Observer().ContactViewReady(*this);
       
   259                 }
       
   260             else
       
   261             // Not available
       
   262             if (state == ENotAvailable)
       
   263                 {
       
   264                 op->Observer().ContactViewUnavailable(*this);
       
   265                 }
       
   266             // Set state
       
   267             op->SetNotifiedState(state);            
       
   268             }
       
   269         }
       
   270 
       
   271     // Observers
       
   272     if (count == 0)
       
   273         {
       
   274         status = EFalse;
       
   275         }
       
   276     // True to continue notification or false when break
       
   277     return status;
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CContactView::State
       
   282 // Returns view state
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 TViewState CContactView::State() const
       
   286     {
       
   287     return iViewState;
       
   288     }
       
   289 // -----------------------------------------------------------------------------
       
   290 // CContactView::State
       
   291 // Returns view store
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 CContactStore& CContactView::Store()
       
   295     {
       
   296     return iStore;
       
   297     }
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 //                  MVPbkContactViewBase implementation
       
   301 // -----------------------------------------------------------------------------
       
   302 // CContactView::Type
       
   303 // Returns type of this contact view.
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 TVPbkContactViewType CContactView::Type() const
       
   307     {
       
   308     return iType;
       
   309     }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CContactView::ChangeSortOrderL
       
   313 // Changes sort order of the view. 
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CContactView::ChangeSortOrderL(const MVPbkFieldTypeList& aSortOrder)
       
   317     {
       
   318     // This is store view sort order does not apply, but
       
   319     CVPbkSortOrder* sp = CVPbkSortOrder::NewL(aSortOrder);
       
   320     delete iSortOrder;
       
   321     iSortOrder = sp;
       
   322     }
       
   323 // -----------------------------------------------------------------------------
       
   324 // CContactView::SortOrder
       
   325 // Returns the current sort order of the view.
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 const MVPbkFieldTypeList& CContactView::SortOrder() const
       
   329     {
       
   330     return *iSortOrder;
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CContactView::RefreshL
       
   335 // Refreshes the view contents.
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CContactView::RefreshL()
       
   339     {
       
   340     // Rebuild view
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CContactView::ContactCountL
       
   345 // Returns the number of contacts in this view
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 TInt CContactView::ContactCountL() const
       
   349     {
       
   350     // Contact count
       
   351     TInt count = 0;
       
   352     // Contacts view
       
   353     if (iType == EVPbkContactsView)
       
   354         {
       
   355         // Get from store
       
   356         count = iStore.ContactCount();
       
   357         }
       
   358     else
       
   359     // Folding view, one store
       
   360     if (iType == EVPbkFoldingView)
       
   361         {
       
   362         count = 1;
       
   363         }
       
   364     return count;
       
   365     }
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CContactView::ContactAtL
       
   369 // Returns a contact in this view.
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 const MVPbkViewContact& CContactView::ContactAtL(TInt aIndex) const
       
   373     {
       
   374     // Contacts view
       
   375     if (iType == EVPbkContactsView)
       
   376         {
       
   377         // Set contact
       
   378         iContact->SetContactViewContact(iStore.ContactAt(aIndex));
       
   379         }
       
   380     else
       
   381     // Folding view
       
   382     if (iType == EVPbkFoldingView)
       
   383         {
       
   384         // Store name as contact
       
   385         iContact->SetFoldingViewContact(iStore.Name());
       
   386         }
       
   387     return *iContact; 
       
   388     }
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CContactView::CreateLinkLC
       
   392 // Returns a contact link at index
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 MVPbkContactLink* CContactView::CreateLinkLC(TInt aIndex) const
       
   396     {
       
   397     // Create link to contact or store
       
   398     return CContactLink::NewLC(iStore,aIndex);
       
   399     }
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CContactView::IndexOfLinkL
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 TInt CContactView::IndexOfLinkL(const MVPbkContactLink& aContactLink) const
       
   406     {
       
   407     TInt index = KErrNotFound;
       
   408     // Same store
       
   409     if (&aContactLink.ContactStore() == &iStore)
       
   410         {
       
   411         // Contacts view
       
   412         if (iType == EVPbkContactsView)
       
   413             {
       
   414             // Get from link
       
   415             index = static_cast<const CContactLink&>(aContactLink).Index();
       
   416             }
       
   417         else
       
   418         // Folding view, one store
       
   419         if (iType == EVPbkFoldingView)
       
   420             {
       
   421             index = 0;
       
   422             }
       
   423         }
       
   424     return index;
       
   425     }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // CContactView::AddObserverL
       
   429 // -----------------------------------------------------------------------------
       
   430 //
       
   431 void CContactView::AddObserverL(MVPbkContactViewObserver& aObserver)
       
   432     {
       
   433     TInt index = FindObserver(aObserver);
       
   434     if (index == KErrNotFound && iObservers)
       
   435         {
       
   436         // New view observer
       
   437         TContactViewObserver* op = new (ELeave) TContactViewObserver(aObserver);
       
   438         CleanupStack::PushL(op);
       
   439         iObservers->AppendL(op);
       
   440         CleanupStack::Pop(op);
       
   441         }
       
   442     // Callback handler has not been started
       
   443     if (! iIdle->IsActive())
       
   444         {
       
   445         // Notify observer callback
       
   446         TCallBack callback(LDAPNotifyObservers,this);
       
   447         // Start callback
       
   448         iIdle->Start(callback);
       
   449         }
       
   450     }
       
   451 // -----------------------------------------------------------------------------
       
   452 // CContactView::RemoveObserver
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 void CContactView::RemoveObserver(MVPbkContactViewObserver& aObserver)
       
   456     {
       
   457     TInt index = FindObserver(aObserver);
       
   458     if (index != KErrNotFound)
       
   459         {
       
   460         // Observer
       
   461         TContactViewObserver* op = iObservers->At(index);
       
   462         // Remove from observers
       
   463         iObservers->Delete(index);
       
   464         // Delete observer
       
   465         if (op) delete op;
       
   466         }
       
   467     }
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CContactView::MatchContactStore
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 TBool CContactView::MatchContactStore(const TDesC& aContactStoreUri) const
       
   474     {
       
   475     return iStore.MatchContactStore(aContactStoreUri);
       
   476     }
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CContactView::MatchContactStoreDomain
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 TBool CContactView::MatchContactStoreDomain(const TDesC& aContactStoreDomain) const
       
   483     {
       
   484     return iStore.MatchContactStoreDomain(aContactStoreDomain);
       
   485     }
       
   486 
       
   487 // -----------------------------------------------------------------------------
       
   488 // CContactView::CreateBookmarkLC
       
   489 // -----------------------------------------------------------------------------
       
   490 //
       
   491 MVPbkContactBookmark* CContactView::CreateBookmarkLC(TInt aIndex) const
       
   492     {
       
   493     MVPbkContactBookmark* bp = NULL;
       
   494     // Contacts view
       
   495     if (iType == EVPbkContactsView)
       
   496         {
       
   497         // Index in range
       
   498         if (aIndex >= 0 && aIndex < iStore.ContactCount())
       
   499             {
       
   500             // Bookmark contact in store
       
   501             bp = CStoreBookmark::NewLC(iStore,aIndex);
       
   502             }
       
   503         else
       
   504             {
       
   505             // Bookmark store
       
   506             bp = CStoreBookmark::NewLC(iStore);
       
   507             }
       
   508         }
       
   509     else
       
   510     // Folding view, one store
       
   511     if (iType == EVPbkFoldingView)
       
   512         {
       
   513         // Bookmark store
       
   514         bp = CStoreBookmark::NewLC(iStore);
       
   515         }
       
   516     return bp;
       
   517     }
       
   518 
       
   519 // -----------------------------------------------------------------------------
       
   520 // CContactView::IndexOfBookmarkL
       
   521 // -----------------------------------------------------------------------------
       
   522 //
       
   523 TInt CContactView::IndexOfBookmarkL(const MVPbkContactBookmark& aBookmark) const
       
   524     {
       
   525     TInt index = KErrNotFound;
       
   526     // Contacts view
       
   527     if (iType == EVPbkContactsView)
       
   528         {
       
   529         // Cast to bookmark
       
   530         const CStoreBookmark* bp = dynamic_cast<const CStoreBookmark*>(&aBookmark);
       
   531         if ( bp && &bp->Store == &iStore )
       
   532             {
       
   533             // Bookmarked index
       
   534             TInt bookmark = (bp) ? bp->Index() : 0;
       
   535             if (bookmark >= 0 && bookmark < iStore.ContactCount())
       
   536                 {
       
   537                 index = bookmark;
       
   538                 }            
       
   539             }
       
   540         }
       
   541     else
       
   542     // Folding view, one store
       
   543     if (iType == EVPbkFoldingView)
       
   544         {
       
   545         // Store bookmark index is zero
       
   546         index = 0;
       
   547         }
       
   548     return index;
       
   549     }
       
   550 
       
   551 // -----------------------------------------------------------------------------
       
   552 // CContactView::ViewFiltering
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 MVPbkContactViewFiltering* CContactView::ViewFiltering()
       
   556     {
       
   557     // TODO
       
   558     return NULL;
       
   559     }
       
   560     
       
   561 // -----------------------------------------------------------------------------
       
   562 //                  MVPbkObjectHierarchy implementation
       
   563 // -----------------------------------------------------------------------------
       
   564 // CContactView::ParentObject
       
   565 // Returns the parent object of this view, contact store
       
   566 // -----------------------------------------------------------------------------
       
   567 //
       
   568 MVPbkObjectHierarchy& CContactView::ParentObject() const
       
   569     {
       
   570     return iStore;
       
   571     }
       
   572 
       
   573 // -----------------------------------------------------------------------------
       
   574 //                  MVPbkContactStoreObserver implementation
       
   575 // -----------------------------------------------------------------------------
       
   576 // CViewContact::StoreReady
       
   577 // -----------------------------------------------------------------------------
       
   578 //
       
   579 void CContactView::StoreReady(MVPbkContactStore& /* aContactStore */)
       
   580     {
       
   581     // Contacts view
       
   582     if (iType == EVPbkContactsView)
       
   583         {
       
   584         iViewState = EReady;
       
   585         }
       
   586     else
       
   587     // Folding view, one store
       
   588     if (iType == EVPbkFoldingView)
       
   589         {
       
   590         iViewState = EReady;
       
   591         }
       
   592     }
       
   593 // -----------------------------------------------------------------------------
       
   594 // CContactView::StoreUnavailable
       
   595 // -----------------------------------------------------------------------------
       
   596 //
       
   597 void CContactView::StoreUnavailable(MVPbkContactStore& /* aContactStore */,
       
   598                                                         TInt /* aReason */)
       
   599     {
       
   600     iViewState = ENotAvailable;
       
   601     }
       
   602 // -----------------------------------------------------------------------------
       
   603 // CContactView::HandleStoreEventL
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 void CContactView::HandleStoreEventL(MVPbkContactStore&   /* aContactStore */,
       
   607                                      TVPbkContactStoreEvent /* aStoreEvent */)
       
   608     {
       
   609     // No need to handle this here, events are received from the view
       
   610     }
       
   611 
       
   612 // -----------------------------------------------------------------------------
       
   613 //                      CContactView private methods
       
   614 // -----------------------------------------------------------------------------
       
   615 // CContactView::FindObserver
       
   616 // Find observer
       
   617 // -----------------------------------------------------------------------------
       
   618 //
       
   619 TInt CContactView::FindObserver(MVPbkContactViewObserver& aObserver)
       
   620     {
       
   621     TInt found = KErrNotFound;
       
   622     // Number of observers
       
   623     const TInt count = (iObservers) ? iObservers->Count() : 0;
       
   624     // Find one
       
   625     for (TInt loop = 0; loop < count;loop++)
       
   626         {
       
   627         // This observer
       
   628         TContactViewObserver* op = iObservers->At(loop);
       
   629         if (op && &op->Observer() == &aObserver)
       
   630             {
       
   631             found = loop;
       
   632             break;
       
   633             }
       
   634         }
       
   635     return found;
       
   636     }
       
   637 
       
   638 }  // End of namespace LDAPStore
       
   639 // -----------------------------------------------------------------------------
       
   640 //  End of File
       
   641 // -----------------------------------------------------------------------------