mediator/src/Server/MediatorServerObjectHandler.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Mediator server object handler.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <e32base.h>
       
    21 
       
    22 #include    "MediatorServerObjectHandler.h"
       
    23 #include    "Debug.h"
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CMediatorServerObjectHandler::CMediatorServerObjectHandler
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CMediatorServerObjectHandler::CMediatorServerObjectHandler()
       
    33     {
       
    34     }
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CMediatorServerObjectHandler::ConstructL
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 void CMediatorServerObjectHandler::ConstructL()
       
    41     {
       
    42     iSearchDomain = CDomain::NewL( TUid() );
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CMediatorServerObjectHandler::NewL
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CMediatorServerObjectHandler* CMediatorServerObjectHandler::NewL()
       
    50     {
       
    51     LOG(_L("[Mediator Server]\t CMediatorServerObjectHandler::NewL"));
       
    52     CMediatorServerObjectHandler* self = new( ELeave ) CMediatorServerObjectHandler;
       
    53     
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop( self );
       
    57 
       
    58     return self;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CMediatorServerObjectHandler::~CMediatorServerObjectHandler
       
    63 // -----------------------------------------------------------------------------
       
    64 //    
       
    65 CMediatorServerObjectHandler::~CMediatorServerObjectHandler()
       
    66     {
       
    67     // Clear domain list
       
    68     iDomainList.ResetAndDestroy();
       
    69     delete iSearchDomain;
       
    70     }
       
    71 
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CMediatorServerObjectHandler::AddObserverL
       
    75 // Adds notification observer
       
    76 // (other items were commented in a header).
       
    77 // -----------------------------------------------------------------------------
       
    78 // 
       
    79 void CMediatorServerObjectHandler::AddObserverL( 
       
    80                     MMediatorServerNotificationObserver* aObserver )
       
    81     {
       
    82     TBool found = EFalse;
       
    83     for ( TInt index = 0; index < iObserverList.Count() && !found; index ++ )
       
    84         {
       
    85         if ( iObserverList[index] == aObserver )
       
    86             {
       
    87             found = ETrue;
       
    88             }
       
    89         }
       
    90     if ( !found )
       
    91         {
       
    92         iObserverList.AppendL( aObserver );
       
    93         } 
       
    94     else
       
    95         {
       
    96         User::Leave( KErrAlreadyExists );
       
    97         }
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CMediatorServerObjectHandler::RemoveObserverL
       
   102 // Removes notification observer
       
   103 // (other items were commented in a header).
       
   104 // -----------------------------------------------------------------------------
       
   105 // 
       
   106 void CMediatorServerObjectHandler::RemoveObserverL( 
       
   107                     MMediatorServerNotificationObserver* aObserver )
       
   108     {
       
   109     TBool found = EFalse;
       
   110     for (TInt index = 0; index < iObserverList.Count() && !found; index ++ )
       
   111         {
       
   112         if ( iObserverList[index] == aObserver )
       
   113             {
       
   114             iObserverList.Remove( index );
       
   115             found = ETrue;
       
   116             }
       
   117         }
       
   118     if ( !found )
       
   119         {
       
   120         User::Leave( KErrNotFound );
       
   121         }
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CMediatorServerObjectHandler::FindDomain
       
   126 // Returns pointer to found domain
       
   127 // (other items were commented in a header).
       
   128 // -----------------------------------------------------------------------------
       
   129 // 
       
   130 CDomain* CMediatorServerObjectHandler::FindDomain( const TUid aDomain )
       
   131     {
       
   132      // Create utilities to be used in find
       
   133     TIdentityRelation<CDomain> relation( CompareDomains );
       
   134 	
       
   135 	iSearchDomain->SetDomainUid( aDomain );
       
   136 	
       
   137 	// Find the item from array
       
   138 	TInt index = iDomainList.Find( iSearchDomain, relation );	
       
   139 	
       
   140 	// Then return domain, if found. 
       
   141     CDomain* domain = NULL;
       
   142     if ( index != KErrNotFound )
       
   143         {
       
   144         domain = iDomainList[index];
       
   145         }
       
   146 	return domain;    
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CMediatorServerObjectHandler::AddDomainL
       
   151 // Returns pointer to new domain
       
   152 // (other items were commented in a header).
       
   153 // -----------------------------------------------------------------------------
       
   154 // 
       
   155 CDomain* CMediatorServerObjectHandler::AddDomainL( const TUid aDomain )
       
   156     {
       
   157     CDomain* newDomain = CDomain::NewL( aDomain );
       
   158     CleanupStack::PushL( newDomain );
       
   159     iDomainList.AppendL( newDomain );
       
   160     CleanupStack::Pop( newDomain );
       
   161     return newDomain;
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CMediatorServerObjectHandler::CategoryL
       
   166 // Returns pointer to found category
       
   167 // (other items were commented in a header).
       
   168 // -----------------------------------------------------------------------------
       
   169 //     
       
   170 CCategory* CMediatorServerObjectHandler::CategoryL( TMediatorCategory aCategory )
       
   171     {
       
   172     // Check that domain exists 
       
   173     CDomain* domain = FindDomain( aCategory.iDomain );
       
   174     if ( !domain )
       
   175         {
       
   176         ERROR_TRACE(Print(_L("[Mediator] CMediatorServerObjectHandler::CategoryL: domain %d not found\n"), aCategory.iDomain ) );
       
   177         User::Leave( KErrMediatorDomainNotFound );
       
   178         }
       
   179     // Check that category exists
       
   180     TInt index = 0; // not used
       
   181     CCategory* category = domain->FindCategory( aCategory.iCategory, index );
       
   182     if ( !category )
       
   183         {
       
   184         ERROR_TRACE(Print(_L("[Mediator] CMediatorServerObjectHandler::CategoryL: category %d not found\n"), aCategory.iCategory ) );
       
   185         User::Leave( KErrMediatorCategoryNotFound );
       
   186         }
       
   187     return category;
       
   188     }
       
   189 // -----------------------------------------------------------------------------
       
   190 // CMediatorServerObjectHandler::CompareDomains
       
   191 // Compares domains and returns boolean according to comparison
       
   192 // (other items were commented in a header).
       
   193 // -----------------------------------------------------------------------------
       
   194 //    
       
   195 TBool CMediatorServerObjectHandler::CompareDomains( const CDomain& aLeftDomain, 
       
   196                                                     const CDomain& aRightDomain )
       
   197     {
       
   198     return aLeftDomain.DomainUid() == aRightDomain.DomainUid();
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CMediatorServerObjectHandler::GetDomainsL
       
   203 //  
       
   204 // (other items were commented in a header).
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CMediatorServerObjectHandler::GetDomainsL( RDomainList& aDomains )
       
   208     {
       
   209     // Loop through array and append all domains
       
   210     for ( TInt index = 0; index < iDomainList.Count(); index++ )
       
   211         {
       
   212         aDomains.AppendL( iDomainList[index]->DomainUid() );
       
   213         }
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CMediatorServerObjectHandler::GetCategoriesL
       
   218 //  
       
   219 // (other items were commented in a header).
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CMediatorServerObjectHandler::GetCategoriesL( TMediatorCategory aCategory, 
       
   223                                                    RCategoryList& aCategories )
       
   224     {
       
   225     // Check that domain exists 
       
   226     CDomain* domain = FindDomain( aCategory.iDomain );
       
   227     if ( !domain )
       
   228         {
       
   229         ERROR_TRACE(Print(_L("[Mediator] CMediatorServerObjectHandler::GetCategoriesL: domain %d not found\n"), aCategory.iDomain ) );
       
   230         User::Leave( KErrMediatorDomainNotFound );
       
   231         }
       
   232     else
       
   233         {
       
   234         // Ok, get categories from domain.
       
   235         domain->GetCategoriesL( aCategories );
       
   236         }
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CMediatorServerObjectHandler::GetCommandsL
       
   241 //  
       
   242 // (other items were commented in a header).
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CMediatorServerObjectHandler::GetCommandsL( TMediatorCategory aCategory, 
       
   246                                                  RCommandList& aCommands )
       
   247     {
       
   248     // Find domain & category
       
   249     CCategory* category = CategoryL( aCategory ); 
       
   250     if ( category )
       
   251         {
       
   252         category->GetCommandsL( aCommands );
       
   253         }
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CMediatorServerObjectHandler::GetEventsL
       
   258 //  
       
   259 // (other items were commented in a header).
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 void CMediatorServerObjectHandler::GetEventsL( TMediatorCategory aCategory, 
       
   263                                                REventList& aEvents )
       
   264     {
       
   265     // Find domain & category
       
   266     CCategory* category = CategoryL( aCategory ); 
       
   267     if ( category )
       
   268         {
       
   269         category->GetEventsL( aEvents );
       
   270         }
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CMediatorServerObjectHandler::EventsAdded
       
   275 //  
       
   276 // (other items were commented in a header).
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CMediatorServerObjectHandler::EventsAdded( TUid aDomain, 
       
   280                                                 TUid aCategory, 
       
   281                                                 const REventList& aEvents )
       
   282     {
       
   283     for ( TInt index = 0; index < iObserverList.Count(); index++ )
       
   284         {
       
   285         MMediatorServerNotificationObserver* observer = iObserverList[index];
       
   286         if ( observer )
       
   287             {
       
   288             // In case of leave, ignore the leave value and continue notifications
       
   289             TRAP_IGNORE( observer->MediatorEventsAddedL( aDomain, 
       
   290                                                          aCategory, 
       
   291                                                          aEvents ) );
       
   292             }
       
   293         }
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CMediatorServerObjectHandler::CommandsAdded
       
   298 //  
       
   299 // (other items were commented in a header).
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void CMediatorServerObjectHandler::CommandsAdded( TUid aDomain, 
       
   303                      TUid aCategory,
       
   304                      const RCommandList& aCommands )
       
   305     {
       
   306     for ( TInt index = 0; index < iObserverList.Count(); index++ )
       
   307         {
       
   308         MMediatorServerNotificationObserver* observer = iObserverList[index];
       
   309         if ( observer )
       
   310             {
       
   311             // In case of leave, ignore the leave value and continue notifications
       
   312             TRAP_IGNORE( observer->MediatorCommandsAddedL( aDomain, 
       
   313                                                            aCategory, 
       
   314                                                            aCommands ) );
       
   315             }
       
   316         }
       
   317     }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CMediatorServerObjectHandler::EventsRemoved
       
   321 //  
       
   322 // (other items were commented in a header).
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CMediatorServerObjectHandler::EventsRemoved( TUid aDomain, 
       
   326                      TUid aCategory,
       
   327                      const REventList& aEvents )
       
   328     {
       
   329     // If category is empty, remove it
       
   330     TBool categoryRemoved = EFalse;
       
   331     TRAP_IGNORE( categoryRemoved = RemoveCategoryIfEmptyL( aDomain, aCategory ));
       
   332     for ( TInt index = 0; index < iObserverList.Count(); index++ )
       
   333         {
       
   334         MMediatorServerNotificationObserver* observer = iObserverList[index];
       
   335         if ( observer )
       
   336             {
       
   337             if ( categoryRemoved )  // different callback if category was removed
       
   338                 {
       
   339                 // In case of leave, ignore the leave value and continue 
       
   340                 // notifications
       
   341                 TRAP_IGNORE( observer->MediatorCategoryRemovedL( aDomain, 
       
   342                                                                  aCategory ) );
       
   343                 }
       
   344             else
       
   345                 {
       
   346                 // In case of leave, ignore the leave value and continue notifications
       
   347                 TRAP_IGNORE( observer->MediatorEventsRemovedL( aDomain, 
       
   348                                                                aCategory, 
       
   349                                                                aEvents ) );
       
   350                 }
       
   351             }
       
   352         }
       
   353     }
       
   354                                      
       
   355 // -----------------------------------------------------------------------------
       
   356 // CMediatorServerObjectHandler::CommandsRemoved
       
   357 //  
       
   358 // (other items were commented in a header).
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 void CMediatorServerObjectHandler::CommandsRemoved( TUid aDomain, 
       
   362                                                TUid aCategory,
       
   363                                                const RCommandList& aCommands )
       
   364     {
       
   365     // If category is empty, remove it
       
   366     TBool categoryRemoved = EFalse;
       
   367     
       
   368     TRAP_IGNORE( categoryRemoved = RemoveCategoryIfEmptyL( aDomain, aCategory ));
       
   369     
       
   370     for ( TInt index = 0; index < iObserverList.Count(); index++ )
       
   371         {
       
   372         MMediatorServerNotificationObserver* observer = iObserverList[index];
       
   373         if ( observer )
       
   374             {
       
   375             if ( categoryRemoved )  // different callback if category was removed
       
   376                 {
       
   377                 // In case of leave, ignore the leave value and continue 
       
   378                 // notifications
       
   379                 TRAP_IGNORE( observer->MediatorCategoryRemovedL( aDomain, 
       
   380                                                                  aCategory ) );
       
   381                 }
       
   382             else
       
   383                 {
       
   384                 // In case of leave, ignore the leave value and continue 
       
   385                 // notifications
       
   386                 TRAP_IGNORE( observer->MediatorCommandsRemovedL( aDomain, 
       
   387                                                                  aCategory, 
       
   388                                                                  aCommands ) );
       
   389                 }
       
   390             
       
   391             }
       
   392         }
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CMediatorServerObjectHandler::IsCategoryEmptyL
       
   397 //  
       
   398 // (other items were commented in a header).
       
   399 // -----------------------------------------------------------------------------
       
   400 //     
       
   401 TBool CMediatorServerObjectHandler::IsCategoryEmptyL( TUid aDomain, TUid aCategory)
       
   402     {
       
   403     TBool empty = EFalse;
       
   404     TMediatorCategory category;
       
   405     category.iDomain = aDomain;
       
   406     category.iCategory = aCategory;
       
   407     CCategory* categoryPtr = CategoryL( category );
       
   408     if ( categoryPtr )
       
   409         {
       
   410         TInt eventCount = categoryPtr->EventCount();
       
   411         TInt commandCount = categoryPtr->CommandCount();
       
   412         if ( !eventCount && !commandCount )
       
   413             {
       
   414             empty = ETrue;
       
   415             }
       
   416         categoryPtr = NULL; // Not owned
       
   417         }
       
   418     return empty;
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // CMediatorServerObjectHandler::RemoveCategoryIfEmptyL
       
   423 //  
       
   424 // (other items were commented in a header).
       
   425 // -----------------------------------------------------------------------------
       
   426 //      
       
   427 TBool CMediatorServerObjectHandler::RemoveCategoryIfEmptyL( TUid aDomain, TUid aCategory)
       
   428     {
       
   429     TBool removed = EFalse;
       
   430     // Check if category is empty
       
   431     if ( IsCategoryEmptyL( aDomain, aCategory ))
       
   432         {
       
   433         CDomain* domainPtr = FindDomain( aDomain );
       
   434         if ( domainPtr )
       
   435             {
       
   436             domainPtr->RemoveCategory( aCategory );
       
   437             removed = ETrue;
       
   438             domainPtr = NULL; // Not owned
       
   439             }
       
   440         }
       
   441     return removed;
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CMediatorServerObjectHandler::ClearRegistrations
       
   446 //  
       
   447 // (other items were commented in a header).
       
   448 // -----------------------------------------------------------------------------
       
   449 //  
       
   450 void CMediatorServerObjectHandler::ClearRegistrations( 
       
   451                           MMediatorServerEventObserver* aEventObserver, 
       
   452                           MMediatorCommandObserver* aCommandObserver,
       
   453                           MMediatorServerNotificationObserver* aNotifObserver )
       
   454     {
       
   455     LOG(_L("[Mediator Server]\t CMediatorServerObjectHandler::ClearRegistrationsL"));
       
   456     
       
   457     // Clear all pending subscriptions -- loop through domains
       
   458     for ( TInt index = iDomainList.Count()-1; index >= 0; index-- )
       
   459         {
       
   460         CDomain* domainPtr = iDomainList[index];
       
   461         if ( domainPtr )
       
   462             {
       
   463             // Loop through categories
       
   464             for( TInt index2 = domainPtr->CategoryCount()-1; index2 >= 0; index2-- )
       
   465                 {
       
   466                 CCategory* categoryPtr = domainPtr->GetCategory( index2 );
       
   467                 if ( categoryPtr )
       
   468                     {
       
   469                     // Loop through events within category
       
   470                     for ( TInt index3 = categoryPtr->EventCount()-1; index3 >= 0; index3-- )
       
   471                         {
       
   472                         CEvent* eventPtr = categoryPtr->GetEvent( index3 );
       
   473                         if ( eventPtr )
       
   474                             {
       
   475                             // Unregister if found --> ignore the return value
       
   476                             eventPtr->RemoveObserver( aEventObserver );
       
   477                             }
       
   478                         eventPtr = NULL;
       
   479                         }
       
   480                     RCommandList removedCommands;
       
   481                     // Loop through commands within category
       
   482                     for ( TInt index4 = categoryPtr->CommandCount()-1; index4 >= 0; index4-- )
       
   483                         {
       
   484                         CCommand* commandPtr = categoryPtr->GetCommand( index4 );
       
   485                         if ( commandPtr )
       
   486                             {
       
   487                             if ( commandPtr->Observer() == aCommandObserver )
       
   488                                 {
       
   489                                 // Found the command --> remove it and append
       
   490                                 // to notification list
       
   491                                 TCommand removeCommand;
       
   492                                 removeCommand.iCommandId = commandPtr->Id();
       
   493                                 removeCommand.iVersion = commandPtr->Version();
       
   494                                 removedCommands.Append( removeCommand );
       
   495                                 categoryPtr->RemoveCommand( index4 );
       
   496                                 delete commandPtr;
       
   497                                 commandPtr = NULL;
       
   498                                 }
       
   499                             }
       
   500                         commandPtr = NULL;
       
   501                         }
       
   502                     // Notify command removal
       
   503                     if ( removedCommands.Count() > 0 )
       
   504                         {
       
   505                         CommandsRemoved( domainPtr->DomainUid(),
       
   506                                          categoryPtr->CategoryUid(),
       
   507                                          removedCommands );
       
   508                         }
       
   509                     removedCommands.Reset();
       
   510                     }
       
   511                 categoryPtr = NULL;
       
   512                 }
       
   513             }
       
   514         domainPtr = NULL;
       
   515         }
       
   516     
       
   517     // Loop through the notification subscriber array still
       
   518     TBool found = EFalse;
       
   519     for ( TInt index = 0; index < iObserverList.Count() && !found; index++ )
       
   520         {
       
   521         if ( iObserverList[index] == aNotifObserver )
       
   522             {
       
   523             iObserverList.Remove( index );
       
   524             found = ETrue;
       
   525             }
       
   526         }
       
   527     }
       
   528 
       
   529 // -----------------------------------------------------------------------------
       
   530 // CMediatorServerObjectHandler::GetStatistics
       
   531 // Get count of domains/categories/observers.
       
   532 // (other items were commented in a header).
       
   533 // -----------------------------------------------------------------------------
       
   534 //    
       
   535 void CMediatorServerObjectHandler::GetStatistics(TInt& aDomainCount, 
       
   536                                                  TInt& aCategoryCount,
       
   537                                                  TInt& aEventCount,
       
   538                                                  TInt& aCommandCount,
       
   539                                                   TInt& aObserverCount)
       
   540     {
       
   541     aDomainCount = iDomainList.Count();
       
   542     aCategoryCount = 0;
       
   543     aEventCount = 0;
       
   544     aCommandCount = 0;
       
   545     
       
   546     for (TInt i = 0; i < aDomainCount; i++)
       
   547         {
       
   548         CDomain* domain = iDomainList[i];
       
   549         aCategoryCount += domain->CategoryCount();
       
   550         
       
   551         for ( TInt j = 0; j < domain->CategoryCount(); j++ )
       
   552             {
       
   553             CCategory* categoryPtr = domain->GetCategory( j );
       
   554             aEventCount += categoryPtr->EventCount();
       
   555             aCommandCount += categoryPtr->CommandCount();
       
   556             }
       
   557         }
       
   558     aObserverCount = iObserverList.Count();
       
   559     }
       
   560 
       
   561 //  End of File