layouts/cdl/CdlEngine/src/CdlChangeObserver.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 #include "CdlChangeObserver.h"
       
    18 #include "CCdlEngine.h"
       
    19 
       
    20 const TInt KCdlGranularity = 4;
       
    21 
       
    22 
       
    23 //
       
    24 // CCdlChangeMonitor
       
    25 //
       
    26 
       
    27 CCdlChangeMonitor::CCdlChangeMonitor(RCdlSession& aServer, CCdlEngine* aEngine)
       
    28 : CActive(EPriorityStandard), iServer(aServer), iEngine(aEngine)
       
    29 	{
       
    30 	CActiveScheduler::Add(this);
       
    31 	Queue();
       
    32 	}
       
    33 
       
    34 CCdlChangeMonitor::~CCdlChangeMonitor()
       
    35 	{
       
    36 	Cancel();
       
    37 	}
       
    38 
       
    39 void CCdlChangeMonitor::Queue()
       
    40 	{
       
    41 	iServer.NotifyChange(iStatus, iChange);
       
    42 	SetActive();
       
    43 	}
       
    44 
       
    45 void CCdlChangeMonitor::RunL()
       
    46 	{
       
    47 	if (iStatus == KErrNone)
       
    48 		Queue();
       
    49 
       
    50 	TRAP_IGNORE(ProcessL());
       
    51 	}
       
    52 
       
    53 void CCdlChangeMonitor::ProcessL()
       
    54 	{
       
    55 	switch (iChange().iType)
       
    56 		{
       
    57 		case SCdlServerChange::EGlobalCustomisation:
       
    58 			iEngine->HandleGlobalCustomisationChangeL(iChange().iSize);
       
    59 			break;
       
    60 
       
    61 		case SCdlServerChange::EAvailableRefs:
       
    62 			iEngine->HandleAvailableRefsChangeL();
       
    63 			break;
       
    64 
       
    65 		default:
       
    66 			Panic(ECdlEngPanic_BadChangeType);
       
    67 			break;
       
    68 		}
       
    69 	}
       
    70 
       
    71 void CCdlChangeMonitor::DoCancel()
       
    72 	{
       
    73 	iServer.CancelNotifyChange();
       
    74 	}
       
    75 
       
    76 
       
    77 //
       
    78 // CCdlChangeObserver
       
    79 //
       
    80 
       
    81 CCdlChangeObserver* CCdlChangeObserver::NewLC(MCdlChangeObserver* aObserver, TUid aUid)
       
    82 	{
       
    83 	CCdlChangeObserver* self = new(ELeave) CCdlChangeObserver;
       
    84 	CleanupStack::PushL(self);
       
    85 	self->iObserver = aObserver;
       
    86 	self->AddUidL(aUid);
       
    87 	return self;
       
    88 	}
       
    89 
       
    90 CCdlChangeObserver* CCdlChangeObserver::NewLC(MCdlChangeObserver* aObserver)
       
    91 	{
       
    92 	CCdlChangeObserver* self = new(ELeave) CCdlChangeObserver;
       
    93 	CleanupStack::PushL(self);
       
    94 	self->iObserver = aObserver;
       
    95 	return self;
       
    96 	}
       
    97 
       
    98 CCdlChangeObserver::~CCdlChangeObserver()
       
    99 	{
       
   100 	}
       
   101 
       
   102 MCdlChangeObserver* CCdlChangeObserver::Observer() const
       
   103 	{
       
   104 	return iObserver;
       
   105 	}
       
   106 
       
   107 void CCdlChangeObserver::AddUidL(TUid aUid)
       
   108 	{
       
   109 	iUids.AddL(aUid);
       
   110 	}
       
   111 
       
   112 void CCdlChangeObserver::HandleCustomisationChangeL(const CCdlUids& aUids)
       
   113 	{
       
   114 	CCdlUids* change = iUids.IntersectionLC(aUids);
       
   115 	if (change->Count())
       
   116 		iObserver->HandleCustomisationChangeL(*change);
       
   117 	CleanupStack::PopAndDestroy(change);
       
   118 	}
       
   119 
       
   120 void CCdlChangeObserver::HandleAvailableRefsChangeL()
       
   121 	{
       
   122 	iObserver->HandleAvailableRefsChangeL();
       
   123 	}
       
   124 
       
   125 
       
   126 //
       
   127 // CCdlChangeObservers
       
   128 //
       
   129 
       
   130 CCdlChangeObservers::CCdlChangeObservers()
       
   131 : iObservers(KCdlGranularity)
       
   132 	{
       
   133 	}
       
   134 
       
   135 CCdlChangeObservers::~CCdlChangeObservers()
       
   136 	{
       
   137 	iObservers.ResetAndDestroy();
       
   138 	}
       
   139 
       
   140 void CCdlChangeObservers::AddCustomisationChangeObserverL(MCdlChangeObserver* aObserver, TUid aUid)
       
   141 	{
       
   142 	TInt index = FindIndex(aObserver);
       
   143 	if (index == KErrNotFound)
       
   144 		{
       
   145 		iObservers.AppendL(CCdlChangeObserver::NewLC(aObserver, aUid));
       
   146 		CleanupStack::Pop();
       
   147 		}
       
   148 	else
       
   149 		{
       
   150 		iObservers[index]->AddUidL(aUid);
       
   151 		}
       
   152 	}
       
   153 
       
   154 void CCdlChangeObservers::AddGeneralChangeObserverL(MCdlChangeObserver* aObserver)
       
   155 	{
       
   156 	TInt index = FindIndex(aObserver);
       
   157 	if (index == KErrNotFound)
       
   158 		{
       
   159 		iObservers.AppendL(CCdlChangeObserver::NewLC(aObserver));
       
   160 		CleanupStack::Pop();
       
   161 		}
       
   162 	}
       
   163 
       
   164 void CCdlChangeObservers::RemoveChangeObserver(MCdlChangeObserver* aObserver)
       
   165 	{
       
   166 	TInt index = FindIndex(aObserver);
       
   167 	if (index != KErrNotFound)
       
   168 		{
       
   169 		delete iObservers[index];
       
   170 		iObservers.Delete(index);
       
   171 		}
       
   172 	}
       
   173 
       
   174 void CCdlChangeObservers::HandleCustomisationChangeL(const CCdlUids& aUids)
       
   175 	{
       
   176 	TInt count = iObservers.Count();
       
   177 	for (TInt ii=0; ii<count; ii++)
       
   178 		{
       
   179 		iObservers[ii]->HandleCustomisationChangeL(aUids);
       
   180 		}
       
   181 	}
       
   182 
       
   183 void CCdlChangeObservers::HandleAvailableRefsChangeL()
       
   184 	{
       
   185 	TInt count = iObservers.Count();
       
   186 	for (TInt ii=0; ii<count; ii++)
       
   187 		{
       
   188 		iObservers[ii]->HandleAvailableRefsChangeL();
       
   189 		}
       
   190 	}
       
   191 
       
   192 TInt CCdlChangeObservers::FindIndex(MCdlChangeObserver* aObserver) const
       
   193 	{
       
   194 	TInt count = iObservers.Count();
       
   195 	for (TInt ii=0; ii<count; ii++)
       
   196 		{
       
   197 		if (iObservers[ii]->Observer() == aObserver)
       
   198 			return ii;
       
   199 		}
       
   200 	return KErrNotFound;
       
   201 	}