landmarksui/engine/src/CLmkDbEventListener.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     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:    LandmarksUi Content File -
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CLmkDbEventListener.h"
       
    23 #include "MLmkDbObserver.h"
       
    24 #include <EPos_CPosLandmarkDatabase.h>
       
    25 #include <lmkerrors.h>
       
    26 
       
    27 
       
    28 // CONSTANTS
       
    29 /// Unnamed namespace for local definitions
       
    30 namespace {
       
    31 
       
    32 const TInt KLmkEventObserversGranularity( 5 );
       
    33 
       
    34 #if defined(_DEBUG)
       
    35 _LIT( KPanicMsg, "CLmkDbEventListener" );
       
    36 void Panic( TPanicCode aReason )
       
    37     {
       
    38     User::Panic( KPanicMsg, aReason );
       
    39     }
       
    40 #endif
       
    41 }  // namespace
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CLmkDbEventListener::CLmkDbEventListener
       
    46 // C++ constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CLmkDbEventListener::CLmkDbEventListener()
       
    51     : CActive( EPriorityStandard ),
       
    52       iObservers( KLmkEventObserversGranularity )
       
    53     {
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CLmkDbEventListener::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CLmkDbEventListener::ConstructL()
       
    62     {
       
    63     CActiveScheduler::Add( this );
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CLmkDbEventListener::NewL
       
    68 // Two-phased constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CLmkDbEventListener* CLmkDbEventListener::NewL()
       
    72     {
       
    73     CLmkDbEventListener* self =
       
    74         new ( ELeave ) CLmkDbEventListener();
       
    75 
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop();
       
    79 
       
    80     return self;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CLmkDbEventListener::~CLmkDbEventListener
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CLmkDbEventListener::~CLmkDbEventListener()
       
    88     {
       
    89     Cancel();
       
    90     iObservers.Close();
       
    91     delete iDbURI;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CLmkDbEventListener::AddObserverL
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CLmkDbEventListener::AddObserverL(
       
    99     MLmkDbObserver& aObserver,
       
   100     CPosLandmarkDatabase& aDb )
       
   101     {
       
   102     HBufC* paramURI = aDb.DatabaseUriLC();
       
   103     if ( iDb )
       
   104         { // Only one database currently supported
       
   105         if ( iDbURI->Compare( *paramURI ) != 0 )
       
   106             {
       
   107             User::Leave( KErrNotSupported );
       
   108             }
       
   109         }
       
   110 
       
   111     TInt index = iObservers.Find( &aObserver );
       
   112     if ( index >= 0 )
       
   113         {
       
   114         User::Leave( KErrAlreadyExists );
       
   115         }
       
   116 
       
   117     User::LeaveIfError( iObservers.Append( &aObserver ) );
       
   118     if ( iObservers.Count() == 1 )
       
   119         {
       
   120         iDb = &aDb;
       
   121         StartListening();
       
   122         }
       
   123 
       
   124     // For simplicity we always assign new value to iDbURI although often
       
   125     // the content remains the same as it was.
       
   126     delete iDbURI;
       
   127     iDbURI = paramURI;
       
   128     CleanupStack::Pop(); // paramURI, ownership was transferred
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CLmkDbEventListener::RemoveObserver
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CLmkDbEventListener::RemoveObserver( MLmkDbObserver& aObserver )
       
   136     {
       
   137     TInt index = iObservers.Find( &aObserver );
       
   138     if ( index >= 0 )
       
   139         {
       
   140         iObservers.Remove( index );
       
   141         }
       
   142     if ( iObservers.Count() == 0 )
       
   143         {
       
   144         Cancel();
       
   145         }
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CLmkDbEventListener::StartListening
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CLmkDbEventListener::StartListening()
       
   153     {
       
   154     iEvent.iEventType = EPosLmEventUnknownChanges;
       
   155     iEvent.iLandmarkItemId = KPosLmNullItemId;
       
   156     __ASSERT_DEBUG( !IsActive(), Panic( KLmkPanicAlreadyActive ) );
       
   157     __ASSERT_DEBUG( iDb, Panic( KLmkPanicNullMember ) );
       
   158     iDb->NotifyDatabaseEvent( iEvent, iStatus );
       
   159     SetActive();
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CLmkDbEventListener::RunL
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CLmkDbEventListener::RunL()
       
   167     {
       
   168     // No need for implementing RunError() as long as RunL() is non-leaving.
       
   169     for ( TInt i( 0 ); i < iObservers.Count(); ++i )
       
   170         {
       
   171         iObservers[i]->HandleDatabaseEvent( iEvent );
       
   172         }
       
   173     StartListening();
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CLmkDbEventListener::DoCancel
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CLmkDbEventListener::DoCancel()
       
   181     {
       
   182     if ( iDb )
       
   183         {
       
   184         iDb->CancelNotifyDatabaseEvent();
       
   185         // The policy is that when database observation is stopped we
       
   186         // release the database association. It may become invalid after that.
       
   187         iDb = NULL;
       
   188         delete iDbURI;
       
   189         iDbURI = NULL;
       
   190         }
       
   191     }
       
   192 
       
   193 //  End of File