|
1 /* |
|
2 * Copyright (c) 2007-2007 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: CCFScriptSubscription class implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDES |
|
21 #include "cfscriptsubscription.h" |
|
22 #include "cfscriptlistener.h" |
|
23 #include "CFContextSubscriptionImpl.h" |
|
24 #include "cftrace.h" |
|
25 |
|
26 #include <cfcontextinterface.h> |
|
27 #include <cfcontextsubscriptionlistener.h> |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // CCFScriptSubscription::CCFScriptSubscription |
|
33 // C++ default constructor can NOT contain any code, that might leave. |
|
34 // --------------------------------------------------------------------------- |
|
35 // |
|
36 CCFScriptSubscription::CCFScriptSubscription( MCFContextInterface& aCF, |
|
37 MCFContextSubscriptionListener& aCFListener ) |
|
38 : iCF( aCF ), |
|
39 iCFListener( aCFListener ), |
|
40 iSubscribed( EFalse ) |
|
41 { |
|
42 FUNC_LOG; |
|
43 } |
|
44 |
|
45 // --------------------------------------------------------------------------- |
|
46 // CCFScriptSubscription::ConstructL |
|
47 // Symbian 2nd phase constructor can leave. |
|
48 // --------------------------------------------------------------------------- |
|
49 // |
|
50 void CCFScriptSubscription::ConstructL( MCFScriptListener* aListener ) |
|
51 { |
|
52 FUNC_LOG; |
|
53 |
|
54 if ( !aListener ) |
|
55 { |
|
56 ERROR( KErrBadHandle, "CCFScriptSubscription::ConstructL - MCFScriptListener is NULL!" ); |
|
57 User::Leave( KErrBadHandle ); |
|
58 } |
|
59 |
|
60 iSubscription = CCFContextSubscriptionImpl::NewL(); |
|
61 iSubscription->SetContextSourceL( aListener->Source() ); |
|
62 iSubscription->SetContextTypeL( aListener->Type() ); |
|
63 iSubscription->SetSubscriptionListener( iCFListener ); |
|
64 |
|
65 iListeners.AppendL( aListener ); |
|
66 DoSubscribeL( aListener ); |
|
67 } |
|
68 |
|
69 // --------------------------------------------------------------------------- |
|
70 // CCFScriptSubscription::NewL |
|
71 // Two-phased constructor. |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 CCFScriptSubscription* CCFScriptSubscription::NewL( MCFContextInterface& aCF, |
|
75 MCFContextSubscriptionListener& aCFListener, |
|
76 MCFScriptListener* aListener ) |
|
77 { |
|
78 FUNC_LOG; |
|
79 |
|
80 CCFScriptSubscription* self = NewLC( aCF, aCFListener, aListener ); |
|
81 CleanupStack::Pop( self ); |
|
82 return self; |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // CCFScriptSubscription::NewLC |
|
87 // Two-phased constructor. |
|
88 // --------------------------------------------------------------------------- |
|
89 // |
|
90 CCFScriptSubscription* CCFScriptSubscription::NewLC( MCFContextInterface& aCF, |
|
91 MCFContextSubscriptionListener& aCFListener, |
|
92 MCFScriptListener* aListener ) |
|
93 { |
|
94 FUNC_LOG; |
|
95 |
|
96 CCFScriptSubscription* self |
|
97 = new( ELeave ) CCFScriptSubscription( aCF, aCFListener ); |
|
98 CleanupStack::PushL( self ); |
|
99 self->ConstructL( aListener ); |
|
100 return self; |
|
101 } |
|
102 |
|
103 |
|
104 // Destructor |
|
105 CCFScriptSubscription::~CCFScriptSubscription() |
|
106 { |
|
107 FUNC_LOG; |
|
108 |
|
109 if ( !iSubscribed ) |
|
110 { |
|
111 delete iSubscription; |
|
112 } |
|
113 iListeners.Close(); |
|
114 } |
|
115 |
|
116 |
|
117 // ----------------------------------------------------------------------------- |
|
118 // CCFScriptSubscription::AddListenerL |
|
119 // ----------------------------------------------------------------------------- |
|
120 // |
|
121 void CCFScriptSubscription::AddListenerL( MCFScriptListener* aListener ) |
|
122 { |
|
123 FUNC_LOG; |
|
124 |
|
125 iListeners.AppendL( aListener ); |
|
126 NotifyListenerOfCurrentContextValueL( aListener ); |
|
127 } |
|
128 |
|
129 // ----------------------------------------------------------------------------- |
|
130 // CCFScriptSubscription::RemoveListener |
|
131 // ----------------------------------------------------------------------------- |
|
132 // |
|
133 TBool CCFScriptSubscription::RemoveListener( MCFScriptListener* aListener ) |
|
134 { |
|
135 FUNC_LOG; |
|
136 |
|
137 TInt index = iListeners.Find( aListener ); |
|
138 if ( index != KErrNotFound ) |
|
139 { |
|
140 iListeners.Remove( index ); |
|
141 } |
|
142 |
|
143 if ( iListeners.Count() == 0 ) |
|
144 { |
|
145 if ( iSubscribed ) |
|
146 { |
|
147 iCF.UnsubscribeContext( *iSubscription, iCFListener ); |
|
148 iSubscription = NULL; // Was not owned anymore, and deleted by CF. |
|
149 iSubscribed = EFalse; |
|
150 } |
|
151 return ETrue; |
|
152 } |
|
153 |
|
154 return EFalse; |
|
155 } |
|
156 |
|
157 // ----------------------------------------------------------------------------- |
|
158 // CCFScriptSubscription::Match |
|
159 // ----------------------------------------------------------------------------- |
|
160 // |
|
161 TBool CCFScriptSubscription::Match( const MCFScriptListener& aListener ) const |
|
162 { |
|
163 FUNC_LOG; |
|
164 |
|
165 if ( iSubscription->ContextSource() == aListener.Source() |
|
166 && iSubscription->ContextType() == aListener.Type() ) |
|
167 { |
|
168 return ETrue; |
|
169 } |
|
170 |
|
171 return EFalse; |
|
172 } |
|
173 |
|
174 // ----------------------------------------------------------------------------- |
|
175 // CCFScriptSubscription::Match |
|
176 // ----------------------------------------------------------------------------- |
|
177 // |
|
178 TBool CCFScriptSubscription::Match( const CCFContextObject& aContext ) const |
|
179 { |
|
180 FUNC_LOG; |
|
181 |
|
182 if ( iSubscription->ContextSource() == aContext.Source() |
|
183 && iSubscription->ContextType() == aContext.Type() ) |
|
184 { |
|
185 return ETrue; |
|
186 } |
|
187 |
|
188 return EFalse; |
|
189 } |
|
190 |
|
191 // ----------------------------------------------------------------------------- |
|
192 // CCFScriptSubscription::NotifyListeners |
|
193 // ----------------------------------------------------------------------------- |
|
194 // |
|
195 TBool CCFScriptSubscription::NotifyListeners( const CCFContextObject& aContext, |
|
196 TInt& aContextLevelDelay ) const |
|
197 { |
|
198 FUNC_LOG; |
|
199 |
|
200 TBool evaluated( EFalse ); |
|
201 for ( TInt i = 0; i < iListeners.Count(); ++i ) |
|
202 { |
|
203 if ( !iListeners[ i ]->IsAllRequired() ) |
|
204 { |
|
205 TInt delay( 0 ); |
|
206 if ( iListeners[ i ]->Evaluate( aContext, delay ) ) |
|
207 { |
|
208 // Evaluation flag may not change back to false. |
|
209 evaluated = ETrue; |
|
210 } |
|
211 |
|
212 if ( delay > aContextLevelDelay ) |
|
213 { |
|
214 aContextLevelDelay = delay; |
|
215 } |
|
216 } |
|
217 } |
|
218 |
|
219 return evaluated; |
|
220 } |
|
221 |
|
222 // ----------------------------------------------------------------------------- |
|
223 // CCFScriptSubscription::DoSubscribeL |
|
224 // Do subscribe and notify listener about current context value. |
|
225 // ----------------------------------------------------------------------------- |
|
226 // |
|
227 TInt CCFScriptSubscription::DoSubscribeL( MCFScriptListener* aListener ) |
|
228 { |
|
229 FUNC_LOG; |
|
230 |
|
231 RThread thread; |
|
232 CleanupClosePushL( thread ); // CLEANUP<< thread |
|
233 TInt err = iCF.SubscribeContext( iSubscription, &iCFListener, thread ); |
|
234 if( err != KErrNone ) |
|
235 { |
|
236 TPtrC source( aListener->Source() ); |
|
237 TPtrC type( aListener->Type() ); |
|
238 ERROR( err, "CCFScriptSubscription::DoSubscribeL - Subscribing context failed!" ); |
|
239 ERROR_2( err, "- context source [%S], type [%S]", &source, &type ); |
|
240 } |
|
241 else |
|
242 { |
|
243 iSubscribed = ETrue; // Mark successful subscription. |
|
244 NotifyListenerOfCurrentContextValueL( aListener ); |
|
245 } |
|
246 CleanupStack::PopAndDestroy( &thread ); // CLEANUP>> thread |
|
247 |
|
248 return err; |
|
249 } |
|
250 |
|
251 // ----------------------------------------------------------------------------- |
|
252 // CCFScriptSubscription::NotifyListenerOfCurrentContextValue |
|
253 // ----------------------------------------------------------------------------- |
|
254 // |
|
255 void CCFScriptSubscription::NotifyListenerOfCurrentContextValueL( |
|
256 MCFScriptListener* aListener ) const |
|
257 { |
|
258 FUNC_LOG; |
|
259 |
|
260 RThread thread; |
|
261 CleanupClosePushL( thread ); // CLEANUP<< thread |
|
262 // Request current context value for the new subscription. |
|
263 CCFContextQuery* query = CCFContextQuery::NewLC(); // CLEANUP<< query |
|
264 RContextObjectArray results; // Pointers not owned. |
|
265 CleanupClosePushL( results ); // CLEANUP<< results |
|
266 query->SetSourceL( iSubscription->ContextSource() ); |
|
267 query->SetTypeL( iSubscription->ContextType() ); |
|
268 |
|
269 iCF.RequestContext( results, *query, thread ); |
|
270 if ( results.Count() ) |
|
271 { |
|
272 CCFContextObject* context = results[ 0 ]; |
|
273 TInt delay( 0 ); // Required by the call but not used. |
|
274 aListener->Evaluate( *context, delay ); |
|
275 } |
|
276 |
|
277 CleanupStack::PopAndDestroy( &results ); // CLEANUP>> results |
|
278 CleanupStack::PopAndDestroy( query ); // CLEANUP>> query |
|
279 CleanupStack::PopAndDestroy( &thread ); // CLEANUP>> thread |
|
280 } |