uifw/AknGlobalUI/AknDynamicSoftNote/eventmanagersrc/akndynamicsoftnoteeventmanager.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Handles dynamic soft notification events.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "akndynamicsoftnoteeventmanager.h"
       
    19 
       
    20 /// Observer array granularity
       
    21 const TInt KAknEventManagerGranularity = 3;
       
    22 
       
    23 /// Non-delivered event array reserved extra space.
       
    24 const TInt KAknEventManagerExtraSpace = 5;
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // CAknDynamicSoftNoteEventManager::CreateUniqueInstanceL
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 EXPORT_C CAknDynamicSoftNoteEventManager* CAknDynamicSoftNoteEventManager::CreateUniqueInstanceL()
       
    31     {
       
    32     if (Dll::Tls())
       
    33         {
       
    34         User::Panic(KAknPanicCategory, EAknEventMgrPanicEventManagerAlreadyExists);                
       
    35         }        
       
    36         
       
    37     CAknDynamicSoftNoteEventManager* self = new(ELeave) CAknDynamicSoftNoteEventManager;
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop(self);
       
    41     
       
    42     Dll::SetTls(self);
       
    43     
       
    44     return self;
       
    45     }
       
    46 
       
    47 //-----------------------------------------------------------------------------
       
    48 // CAknDynamicSoftNoteEventManager::CAknDynamicSoftNoteEventManager
       
    49 //-----------------------------------------------------------------------------
       
    50 //
       
    51 CAknDynamicSoftNoteEventManager::CAknDynamicSoftNoteEventManager()
       
    52     : iObservers(KAknEventManagerGranularity),
       
    53       iNonDeliveredEvents(KAknEventManagerGranularity)
       
    54     {
       
    55     }
       
    56 
       
    57 //-----------------------------------------------------------------------------
       
    58 // CAknDynamicSoftNoteEventManager::ConstructL
       
    59 //-----------------------------------------------------------------------------
       
    60 //
       
    61 void CAknDynamicSoftNoteEventManager::ConstructL()
       
    62     {
       
    63     iNonDeliveredEvents.ReserveL(KAknEventManagerExtraSpace);
       
    64     }
       
    65   
       
    66 //-----------------------------------------------------------------------------
       
    67 // CAknDynamicSoftNoteEventManager::~CAknDynamicSoftNoteEventManager
       
    68 //-----------------------------------------------------------------------------
       
    69 //    
       
    70 CAknDynamicSoftNoteEventManager::~CAknDynamicSoftNoteEventManager()
       
    71     {
       
    72     for (TInt i = iObservers.Count() - 1; i >= 0; i--)
       
    73         {
       
    74         if (!iObservers[i].iMessage.IsNull())
       
    75             {
       
    76             iObservers[i].iMessage.Complete(KErrCancel);
       
    77             }
       
    78         }
       
    79     iObservers.Close();
       
    80     iNonDeliveredEvents.Close();
       
    81     
       
    82     Dll::SetTls(NULL);
       
    83     }
       
    84     
       
    85 //-----------------------------------------------------------------------------
       
    86 // CAknDynamicSoftNoteEventManager::UniqueInstance
       
    87 //-----------------------------------------------------------------------------
       
    88 //    
       
    89 EXPORT_C CAknDynamicSoftNoteEventManager* CAknDynamicSoftNoteEventManager::UniqueInstance()
       
    90     {
       
    91     if (Dll::Tls())
       
    92         {        
       
    93         CAknDynamicSoftNoteEventManager* eventManager = 
       
    94             static_cast<CAknDynamicSoftNoteEventManager*>(Dll::Tls());
       
    95             
       
    96         return eventManager;
       
    97         }
       
    98     else
       
    99         {
       
   100         return NULL;
       
   101         }        
       
   102     }    
       
   103   
       
   104 //-----------------------------------------------------------------------------
       
   105 // CAknDynamicSoftNoteEventManager::ClientExit
       
   106 //-----------------------------------------------------------------------------
       
   107 //    
       
   108 EXPORT_C void CAknDynamicSoftNoteEventManager::ClientExit(TAny* aClient)
       
   109     {
       
   110     const TInt count = iObservers.Count();       
       
   111     for (TInt i = 0; i < count; i++)
       
   112         {
       
   113         if (iObservers[i].iClient == aClient)
       
   114             {
       
   115             if (!iObservers[i].iMessage.IsNull())
       
   116                 {
       
   117                 iObservers[i].iMessage.Complete(KErrCancel);                    
       
   118                 }
       
   119             iObservers.Remove(i);
       
   120             break;       
       
   121             }
       
   122         }
       
   123     }
       
   124 
       
   125 //-----------------------------------------------------------------------------
       
   126 // CAknDynamicSoftNoteEventManager::RegisterEventL
       
   127 //-----------------------------------------------------------------------------
       
   128 //    
       
   129 EXPORT_C void CAknDynamicSoftNoteEventManager::RegisterEventL(TAny* aClient, 
       
   130     const RMessage2& aMessage)
       
   131     {
       
   132     TInt pos = FindByClientFromObservers(aClient);
       
   133     if (pos != KErrNotFound)
       
   134         {
       
   135         aMessage.Panic(KAknPanicCategory, EAknEventMgrPanicAlreadyRegistered);
       
   136         return;        
       
   137         }
       
   138     
       
   139     TInt reserveCount = Max(
       
   140         iNonDeliveredEvents.Count(), 
       
   141         iObservers.Count() + KAknEventManagerExtraSpace);
       
   142     
       
   143     iNonDeliveredEvents.ReserveL(reserveCount);
       
   144     
       
   145     TObserverItem item;
       
   146     item.iClient = aClient;
       
   147     item.iNoteId = aMessage.Int0();
       
   148     
       
   149     iObservers.AppendL(item);
       
   150     aMessage.Complete(KErrNone);
       
   151     }
       
   152   
       
   153 //-----------------------------------------------------------------------------
       
   154 // CAknDynamicSoftNoteEventManager::NotifyEvent
       
   155 //-----------------------------------------------------------------------------
       
   156 //    
       
   157 EXPORT_C void CAknDynamicSoftNoteEventManager::NotifyEvent(TAny* aClient, 
       
   158     const RMessage2& aMessage)
       
   159     {
       
   160     TInt pos = FindByClientFromObservers(aClient);
       
   161     if (pos == KErrNotFound)
       
   162         {
       
   163         aMessage.Panic(KAknPanicCategory, EAknEventMgrPanicNotRegistered);
       
   164         return;
       
   165         }
       
   166 
       
   167     const TObserverItem& item = iObservers[pos];      
       
   168     if (!iObservers[pos].iMessage.IsNull())
       
   169         {
       
   170         aMessage.Panic(KAknPanicCategory, EAknEventMgrPanicDuplicateNotify);
       
   171         return;
       
   172         }
       
   173     
       
   174     TInt nonDeliveredPos = FindByNoteIdFromNonDeliveredEvents(item.iNoteId);
       
   175     
       
   176     if (nonDeliveredPos != KErrNotFound)
       
   177         {
       
   178         TPckgBuf<TInt> actionId(iNonDeliveredEvents[nonDeliveredPos].iActionId);
       
   179         if (aMessage.Write(0, actionId) != KErrNone)
       
   180             {
       
   181             aMessage.Panic(KAknPanicCategory, EAknEventMgrPanicBadMessage);
       
   182             return;
       
   183             }
       
   184         aMessage.Complete(KErrNone);
       
   185         iNonDeliveredEvents.Remove(nonDeliveredPos);
       
   186         }
       
   187     
       
   188     if (!aMessage.IsNull())
       
   189         {
       
   190         iObservers[pos].iMessage = aMessage;
       
   191         }
       
   192     }
       
   193   
       
   194 //-----------------------------------------------------------------------------
       
   195 // CAknDynamicSoftNoteEventManager::CancelEvent
       
   196 //-----------------------------------------------------------------------------
       
   197 //    
       
   198 EXPORT_C void CAknDynamicSoftNoteEventManager::CancelEvent(TAny* aClient, 
       
   199     const RMessage2& aMessage)
       
   200     {
       
   201     TInt pos = FindByClientFromObservers(aClient);
       
   202    
       
   203     if (pos != KErrNotFound)
       
   204         {
       
   205         if (!iObservers[pos].iMessage.IsNull())
       
   206             {
       
   207             iObservers[pos].iMessage.Complete(KErrCancel);
       
   208             }
       
   209         }
       
   210         
       
   211     aMessage.Complete(KErrNone);
       
   212     }
       
   213   
       
   214 //-----------------------------------------------------------------------------
       
   215 // CAknDynamicSoftNoteEventManager::IssueEvent
       
   216 //-----------------------------------------------------------------------------
       
   217 //    
       
   218 EXPORT_C void CAknDynamicSoftNoteEventManager::IssueEvent(TInt aNoteId, 
       
   219     TInt aActionId, TBool aMustDeliver)
       
   220     {
       
   221     TInt pos = FindByNoteIdFromObservers(aNoteId);    
       
   222     TBool completed = EFalse;
       
   223     
       
   224     if (pos != KErrNotFound)
       
   225         {
       
   226         if (!iObservers[pos].iMessage.IsNull())
       
   227             {
       
   228             completed = ETrue;
       
   229             TPckgBuf<TInt> actionId(aActionId);
       
   230             if (iObservers[pos].iMessage.Write(0, actionId) != KErrNone)
       
   231                 {
       
   232                 iObservers[pos].iMessage.Panic(
       
   233                     KAknPanicCategory, EAknEventMgrPanicBadMessage);
       
   234                 return;
       
   235                 }
       
   236             iObservers[pos].iMessage.Complete(KErrNone);            
       
   237             }
       
   238         }
       
   239    
       
   240     if (!completed)
       
   241         {
       
   242         TInt nonDeliveredPos = FindByNoteIdFromNonDeliveredEvents(aNoteId);
       
   243         
       
   244         if (nonDeliveredPos != KErrNotFound)
       
   245             {
       
   246             iNonDeliveredEvents[nonDeliveredPos].iActionId = aActionId;
       
   247             }
       
   248         else if (aMustDeliver)
       
   249             {
       
   250             TNonDeliveredEvent event;
       
   251             event.iNoteId = aNoteId;
       
   252             event.iActionId = aActionId;
       
   253             iNonDeliveredEvents.Append(event);
       
   254             }
       
   255         }
       
   256     }
       
   257   
       
   258 //-----------------------------------------------------------------------------
       
   259 // CAknDynamicSoftNoteEventManager::FindByNoteIdFromObservers
       
   260 //-----------------------------------------------------------------------------
       
   261 //    
       
   262 TInt CAknDynamicSoftNoteEventManager::FindByNoteIdFromObservers(TInt aNoteId) const
       
   263     {
       
   264     TInt result = KErrNotFound;
       
   265     
       
   266     const TInt count = iObservers.Count();
       
   267     for (TInt i = 0; i < count; i++)
       
   268         {
       
   269         if (iObservers[i].iNoteId == aNoteId)
       
   270             {
       
   271             result = i;
       
   272             break;
       
   273             }
       
   274         }
       
   275 
       
   276     return result;        
       
   277     }
       
   278   
       
   279 //-----------------------------------------------------------------------------
       
   280 // CAknDynamicSoftNoteEventManager::FindByClientFromObservers
       
   281 //-----------------------------------------------------------------------------
       
   282 //    
       
   283 TInt CAknDynamicSoftNoteEventManager::FindByClientFromObservers(TAny* aClient) const
       
   284     {
       
   285     TInt result = KErrNotFound;
       
   286     
       
   287     const TInt count = iObservers.Count();
       
   288     for (TInt i = 0; i < count; i++)
       
   289         {
       
   290         if (iObservers[i].iClient == aClient)
       
   291             {
       
   292             result = i;
       
   293             break;
       
   294             }
       
   295         }
       
   296 
       
   297     return result;        
       
   298     }    
       
   299 
       
   300 //-----------------------------------------------------------------------------
       
   301 // CAknDynamicSoftNoteEventManager::FindByNoteIdFromNonDeliveredEvents
       
   302 //-----------------------------------------------------------------------------
       
   303 //
       
   304 TInt CAknDynamicSoftNoteEventManager::FindByNoteIdFromNonDeliveredEvents(TInt aNoteId) const
       
   305     {
       
   306     TInt result = KErrNotFound;
       
   307     
       
   308     const TInt count = iNonDeliveredEvents.Count();
       
   309     for (TInt i = 0; i < count; i++)
       
   310         {
       
   311         if (iNonDeliveredEvents[i].iNoteId == aNoteId)
       
   312             {
       
   313             result = i;
       
   314             break;
       
   315             }
       
   316         }
       
   317 
       
   318     return result;    
       
   319     }
       
   320 
       
   321 // End of file