calendarui/controller/src/calendbchangenotifier.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 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:   Notifies observers of external changes to the calendar database
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //debug
       
    21 #include "calendarui_debug.h"
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include "calendbchangenotifier.h"    // CCalenDbChangeNotifier
       
    25 #include "calenglobaldata.h"            // Calendar global data
       
    26 #include <calsession.h>                 // CalSession
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // KTimerResolution limits the number of notifications sent to registered
       
    30 // MCalenDBChangeObserver instances.  Notifications may come from 
       
    31 // MCalChangeCallBack2 at a very high rate which could impact performance, 
       
    32 // for example by causing constant view refreshes.
       
    33 // CCalenDbChangeNotifier notifies observers when KTimerResolution has elapsed
       
    34 // since the last notification was received from MCalChangeCallBack2
       
    35 // -----------------------------------------------------------------------------
       
    36 const TInt KTimerResolution = 1000000;  // 1 Second
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CCalenDbChangeNotifier::CCalenDbChangeNotifier
       
    42 // C++ default constructor can NOT contain any code, that might leave.
       
    43 // (other items were commented in a header).
       
    44 // -----------------------------------------------------------------------------
       
    45 CCalenDbChangeNotifier::CCalenDbChangeNotifier( CCalenGlobalData& aGlobalData ) 
       
    46     : CActive( EPriorityNormal ),
       
    47       iGlobalData( aGlobalData )
       
    48     {
       
    49     TRACE_ENTRY_POINT;
       
    50 
       
    51     iRestartTimer = EFalse;
       
    52 
       
    53     TRACE_EXIT_POINT;
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CCalenDbChangeNotifier::NewL
       
    58 // Two-phased constructor.
       
    59 // (other items were commented in a header).
       
    60 // -----------------------------------------------------------------------------
       
    61 CCalenDbChangeNotifier* CCalenDbChangeNotifier::NewL( CCalenGlobalData& aGlobalData )
       
    62     {
       
    63     TRACE_ENTRY_POINT;
       
    64 
       
    65     CCalenDbChangeNotifier* self = new( ELeave ) CCalenDbChangeNotifier( aGlobalData );
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop( self );
       
    69 
       
    70     TRACE_EXIT_POINT;
       
    71     return self;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CCalenDbChangeNotifier::ConstructL
       
    76 // Symbian 2nd phase constructor can leave.
       
    77 // (other items were commented in a header).
       
    78 // -----------------------------------------------------------------------------
       
    79 void CCalenDbChangeNotifier::ConstructL()
       
    80     {
       
    81     TRACE_ENTRY_POINT;
       
    82 
       
    83     // We want to receive notifications for the full agenda date range
       
    84     TCalTime start, end;
       
    85     start.SetTimeUtcL( TCalTime::MinTime() );
       
    86     end.SetTimeUtcL( TCalTime::MaxTime() );
       
    87     CalCommon::TCalTimeRange range( start, end ); 
       
    88 
       
    89     // Create a notification filter
       
    90     iCalChangeFilter = CCalChangeNotificationFilter::NewL(
       
    91                                                         MCalChangeCallBack2::EChangeEntryAll, 
       
    92                                                         ETrue, 
       
    93                                                         range );
       
    94 
       
    95     // Enable database change notifications on current global data session
       
    96     iGlobalData.CalSessionL().StartChangeNotification( *this, *iCalChangeFilter );
       
    97 
       
    98     // Create a timer to limit the number of notifications broadcast
       
    99     iNotificationTimer.CreateLocal();
       
   100 
       
   101     // Active object, add to active scheduler
       
   102     CActiveScheduler::Add( this );
       
   103 
       
   104     TRACE_EXIT_POINT;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CCalenDbChangeNotifier::~CCalenDbChangeNotifier
       
   109 // Destructor
       
   110 // (other items were commented in a header).
       
   111 // -----------------------------------------------------------------------------
       
   112 CCalenDbChangeNotifier::~CCalenDbChangeNotifier()
       
   113     {
       
   114     TRACE_ENTRY_POINT;
       
   115 
       
   116     Cancel();
       
   117     // Reset the observer array.  Array contents are NOT owned by this class
       
   118     iDBObservers.Reset();
       
   119 
       
   120     iNotificationTimer.Close();
       
   121 
       
   122     // Disable database change notifications on current global data session   
       
   123     PIM_TRAPD_HANDLE( iGlobalData.CalSessionL().StopChangeNotification() );
       
   124 
       
   125     // Destroy the notification filter
       
   126     delete iCalChangeFilter;
       
   127     
       
   128     TRACE_EXIT_POINT;    
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CCalenDbChangeNotifier::LastDBModificationTime
       
   133 // Returns the time of the last database change notification.  This may not be
       
   134 // the time of the last notification sent to MCalenDBChangeObservers.
       
   135 // (other items were commented in a header).
       
   136 // -----------------------------------------------------------------------------
       
   137 TTime CCalenDbChangeNotifier::LastDBModificationTime() const
       
   138     {
       
   139     TRACE_ENTRY_POINT;
       
   140 
       
   141     TRACE_EXIT_POINT;
       
   142     return iLastDbChangeNotification;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CCalenDbChangeNotifier::CalChangeNotification
       
   147 // Called when a change to the agenda database occurs from a different session
       
   148 // to the one we are currently using.
       
   149 // (other items were commented in a header).
       
   150 // -----------------------------------------------------------------------------
       
   151 void CCalenDbChangeNotifier::CalChangeNotification( RArray<TCalChangeEntry>& /*aChangeItems*/ )
       
   152     {
       
   153     TRACE_ENTRY_POINT;
       
   154 
       
   155     // Always update the last notification time, even if we don't notify 
       
   156     // our observers
       
   157     iLastDbChangeNotification.UniversalTime();
       
   158     if( !IsActive() )
       
   159         {
       
   160         iNotificationTimer.After( iStatus, KTimerResolution );
       
   161         SetActive();
       
   162         }
       
   163     else
       
   164         {
       
   165         iRestartTimer = ETrue;
       
   166         iNotificationTimer.Cancel();
       
   167         }
       
   168 
       
   169     TRACE_EXIT_POINT;
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CCalenDbChangeNotifier::RegisterObserverL
       
   174 // Adds the passed observer to the observer array.  All observers in the array 
       
   175 // will be notified of changes to the agenda database.
       
   176 // (other items were commented in a header).
       
   177 // -----------------------------------------------------------------------------    
       
   178  void CCalenDbChangeNotifier::RegisterObserverL( MCalenDBChangeObserver& aDBObserver )
       
   179     {
       
   180     TRACE_ENTRY_POINT;
       
   181 
       
   182     iDBObservers.Append( &aDBObserver );
       
   183 
       
   184     TRACE_EXIT_POINT;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CCalenDbChangeNotifier::DeRegisterObserverL
       
   189 // Removes the passed observer to the observer array.  All observers in the array 
       
   190 // will be notified of changes to the agenda database.
       
   191 // -----------------------------------------------------------------------------       
       
   192 void CCalenDbChangeNotifier::DeRegisterObserverL( MCalenDBChangeObserver& aDBObserver )
       
   193     {
       
   194     TRACE_ENTRY_POINT;
       
   195 
       
   196     for( TInt x = 0; x < iDBObservers.Count(); ++x )
       
   197         {
       
   198         if( iDBObservers[x] == &aDBObserver )
       
   199             {
       
   200             iDBObservers.Remove( x );
       
   201             return;
       
   202             }
       
   203         }
       
   204     User::Leave( KErrNotFound );
       
   205 
       
   206     TRACE_EXIT_POINT;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------       
       
   210 // CCalenDbChangeNotifier::RunL
       
   211 // From CActive::RunL
       
   212 // Called when notification timer expires
       
   213 // (other items were commented in a header).
       
   214 // -----------------------------------------------------------------------------       
       
   215 void CCalenDbChangeNotifier::RunL()
       
   216     {
       
   217     TRACE_ENTRY_POINT;
       
   218 
       
   219     switch( iStatus.Int() )
       
   220         {
       
   221         case KErrCancel:
       
   222             {
       
   223             // The normal reason for the timer being cancelled is another
       
   224             // database change.  Restart the timer.
       
   225             if( iRestartTimer )
       
   226                 {
       
   227                 iRestartTimer = EFalse;
       
   228                 iNotificationTimer.After( iStatus, KTimerResolution );
       
   229                 SetActive();
       
   230                 }        
       
   231             }
       
   232         break;
       
   233 
       
   234         case KErrNone:
       
   235             {
       
   236             //Timer completion, notify observers
       
   237             for( TInt x = 0; x < iDBObservers.Count(); ++x )
       
   238                 {
       
   239                 iDBObservers[x]->HandleDBChangeL();
       
   240                 }
       
   241             }
       
   242         break;
       
   243 
       
   244         default:
       
   245             {
       
   246             User::Leave( KErrArgument );
       
   247             }
       
   248         break;
       
   249         }
       
   250 
       
   251     TRACE_EXIT_POINT;
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------       
       
   255 // CCalenDbChangeNotifier::RunError
       
   256 // From CActive::RunError
       
   257 // (other items were commented in a header).
       
   258 // -----------------------------------------------------------------------------
       
   259 TInt CCalenDbChangeNotifier::RunError( TInt aError )
       
   260     {
       
   261     TRACE_ENTRY_POINT;
       
   262 
       
   263     //RunL leaving means that the view could not be refreshed.
       
   264     //Theres not much we can do except be ready for the next database event.
       
   265     iRestartTimer = EFalse;
       
   266 
       
   267     TRACE_EXIT_POINT;
       
   268     return aError;
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------       
       
   272 // CCalenDbChangeNotifier::DoCancel
       
   273 // From CActive::DoCancel
       
   274 // (other items were commented in a header).
       
   275 // -----------------------------------------------------------------------------
       
   276 void CCalenDbChangeNotifier::DoCancel()
       
   277     {
       
   278     TRACE_ENTRY_POINT;
       
   279 
       
   280     // Stop the notification timer
       
   281     iRestartTimer = EFalse;
       
   282     iNotificationTimer.Cancel();
       
   283 
       
   284     TRACE_EXIT_POINT;
       
   285     }
       
   286 
       
   287 // End of File