|
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: Flow controller for messages |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <coemain.h> |
|
21 |
|
22 #include "CCAMessageFlowHandlerPC.h" |
|
23 #include "ChatDebugAssert.h" |
|
24 #include "MCAMessageContainer.h" |
|
25 #include "MCAMessagesReadInterface.h" |
|
26 #include "MCASettings.h" |
|
27 #include "ChatDebugPrint.h" |
|
28 #include "imutils.h" |
|
29 #include "imnginternalcrkeys.h" |
|
30 #include "imprivatecrkeys.h" |
|
31 #include "imvariant.hrh" |
|
32 #include "CCARecordedChatsPC.h" |
|
33 #include "CCAProcessManagerFactory.h" |
|
34 #include "CCAConversationMessage.h" |
|
35 #include "CCAEngine.h" |
|
36 #include "MCAProcessManager.h" |
|
37 #include "MCARecordedChatsPC.h" |
|
38 |
|
39 |
|
40 // CONSTANTS |
|
41 const TInt KMilliToMicro = 1000; // multiplier for converting milliseconds to microseconds |
|
42 |
|
43 const TInt KTimeIntervalSlow = 2000; // slowest message speed (in milliseconds) |
|
44 const TInt KTimeIntervalFast = 200; // fastest message speed (in milliseconds) |
|
45 |
|
46 const TInt KTimeIntervalOpen = 0; // message fetching speed when opening the view |
|
47 const TInt KInitialMessages = 3; // fetch n messages at once when opening the view |
|
48 |
|
49 const TInt KSettingValueMin = 1; // minimum value for flow control setting |
|
50 const TInt KSettingValueMax = 3; // maximum value for flow control setting |
|
51 |
|
52 |
|
53 // ============================ MEMBER FUNCTIONS =============================== |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // CCAMessageFlowHandlerPC::CCAMessageFlowHandlerPC |
|
57 // C++ default constructor can NOT contain any code, that |
|
58 // might leave. |
|
59 // ----------------------------------------------------------------------------- |
|
60 // |
|
61 CCAMessageFlowHandlerPC::CCAMessageFlowHandlerPC( MCAMessageContainer& aMessageContainer, |
|
62 MCAMessagesReadInterface& aReadInterface, |
|
63 MCASettings& aSettings ) |
|
64 : CTimer( EPriorityStandard ), |
|
65 iMessages( aMessageContainer ), |
|
66 iReadInterface( aReadInterface ), |
|
67 iSettings( aSettings ), |
|
68 iFetchMessages( ETrue ) |
|
69 |
|
70 { |
|
71 CActiveScheduler::Add( this ); |
|
72 } |
|
73 |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // CCAMessageFlowHandler::NewL |
|
77 // Two-phased constructor. |
|
78 // ----------------------------------------------------------------------------- |
|
79 // |
|
80 CCAMessageFlowHandlerPC* CCAMessageFlowHandlerPC::NewL( |
|
81 MCAMessageContainer& aMessageContainer, |
|
82 MCAMessagesReadInterface& aReadInterface, |
|
83 MCASettings& aSettings, |
|
84 TBool aRecordedChatHandler ) |
|
85 { |
|
86 CCAMessageFlowHandlerPC* self = new( ELeave ) CCAMessageFlowHandlerPC( |
|
87 aMessageContainer, |
|
88 aReadInterface, |
|
89 aSettings ); |
|
90 |
|
91 CleanupStack::PushL( self ); |
|
92 self->ConstructL( aRecordedChatHandler ); |
|
93 CleanupStack::Pop( self ); |
|
94 |
|
95 return self; |
|
96 } |
|
97 // ----------------------------------------------------------------------------- |
|
98 // CCAMessageFlowHandlerPC::ConstructL |
|
99 // Symbian 2nd phase constructor can leave. |
|
100 // ----------------------------------------------------------------------------- |
|
101 // |
|
102 void CCAMessageFlowHandlerPC::ConstructL( TBool aRecordedChatHandler ) |
|
103 { |
|
104 // construct base class |
|
105 CTimer::ConstructL(); |
|
106 |
|
107 // and observe changes |
|
108 iReadInterface.SetObserver( this ); |
|
109 |
|
110 // fetch flow control value from settings |
|
111 UpdateTimeIntervalL(); |
|
112 |
|
113 // and observe |
|
114 iSettings.AddObserverL( this ); |
|
115 |
|
116 // Check CR variation |
|
117 if ( aRecordedChatHandler ) |
|
118 { |
|
119 iBgOpeningMode = EFalse; |
|
120 } |
|
121 else |
|
122 { |
|
123 iBgOpeningMode = IMUtils::CRKeyL( KCRUidIMVariation, KIMVariationKey ) |
|
124 & EIMFeatBackgroundGroupOpening; |
|
125 } |
|
126 |
|
127 // start the timer if there are messages |
|
128 iInitialMsgCount = iReadInterface.MessageCount(); |
|
129 if ( iInitialMsgCount + iReadInterface.UnreadCount() > 0 ) |
|
130 { |
|
131 // lock the buffer |
|
132 iReadInterface.Lock( ETrue ); |
|
133 |
|
134 // use faster timer when constructing |
|
135 iTimeInterval = KTimeIntervalOpen * KMilliToMicro; |
|
136 After( iTimeInterval ); |
|
137 } |
|
138 |
|
139 |
|
140 } |
|
141 |
|
142 |
|
143 // Destructor |
|
144 CCAMessageFlowHandlerPC::~CCAMessageFlowHandlerPC() |
|
145 { |
|
146 |
|
147 if ( !iChatDeleted ) |
|
148 { |
|
149 iReadInterface.SetObserver( NULL ); |
|
150 iReadInterface.Lock( EFalse ); |
|
151 } |
|
152 iSettings.RemoveObserver( this ); |
|
153 Cancel(); |
|
154 |
|
155 } |
|
156 |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // CCAMessageFlowHandlerPC::FetchMessages |
|
160 // (other items were commented in a header). |
|
161 // ----------------------------------------------------------------------------- |
|
162 // |
|
163 void CCAMessageFlowHandlerPC::FetchMessages( TBool aFetch ) |
|
164 { |
|
165 TBool reallyInBg = |
|
166 CCoeEnv::Static()->RootWin().OrdinalPosition() == 0 ? EFalse : ETrue; |
|
167 |
|
168 if ( !reallyInBg && !aFetch ) |
|
169 { |
|
170 // Application is not really in background, this can happen |
|
171 // if key lock is activated while application is in foreground |
|
172 aFetch = ETrue; |
|
173 } |
|
174 |
|
175 iFetchMessages = aFetch; |
|
176 if ( iFetchMessages && !iChatDeleted ) |
|
177 { |
|
178 // we're allowed to fetch messages again |
|
179 if ( iReadInterface.UnreadCount() > 0 && !IsActive() ) |
|
180 { |
|
181 // there are some unread messages |
|
182 // => start the timer (if not active already) |
|
183 After( iTimeInterval ); |
|
184 } |
|
185 } |
|
186 else if ( iMsgIndex < iInitialMsgCount ) |
|
187 { |
|
188 Cancel(); |
|
189 After( iTimeInterval ); |
|
190 return; |
|
191 } |
|
192 else |
|
193 { |
|
194 // we're not allowed to fetch new messages any more, so cancel the timer |
|
195 Cancel(); |
|
196 } |
|
197 } |
|
198 |
|
199 // ----------------------------------------------------------------------------- |
|
200 // CCAMessageFlowHandlerPC::HandleMessageEvent |
|
201 // (other items were commented in a header). |
|
202 // ----------------------------------------------------------------------------- |
|
203 // |
|
204 void CCAMessageFlowHandlerPC::HandleMessageEvent( TMessageEventType aEvent, |
|
205 TInt aIndex ) |
|
206 { |
|
207 CHAT_DP( D_CHAT_LIT( "CCAMessageFlowHandlerPC::HandleMessageEvent event %d, \ |
|
208 index %d" ), aEvent, aIndex ); |
|
209 switch ( aEvent ) |
|
210 { |
|
211 case ENewMessage: |
|
212 { |
|
213 if ( !IsActive() && iFetchMessages ) |
|
214 { |
|
215 // if not active, start timer |
|
216 After( iTimeInterval ); |
|
217 } |
|
218 |
|
219 if ( IsOpening() && iBgOpeningMode ) |
|
220 { |
|
221 // New message during opening phase |
|
222 iNewMsgWhileOpening = ETrue; |
|
223 } |
|
224 |
|
225 // otherwise do nothing as timer fetches the messages |
|
226 break; |
|
227 } |
|
228 case EMessageDeleted: |
|
229 { |
|
230 TInt corIndex = MessageIndexCorrection( aIndex ); |
|
231 iMessages.RemoveMessage( corIndex ); |
|
232 break; |
|
233 } |
|
234 case EMessageChanged: |
|
235 { |
|
236 TInt corIndex = MessageIndexCorrection( aIndex ); |
|
237 TRAPD( err, iMessages.MessageChangedL( corIndex ) ); |
|
238 if ( err != KErrNone ) |
|
239 { |
|
240 CActiveScheduler::Current()->Error( err ); |
|
241 } |
|
242 break; |
|
243 } |
|
244 case EChatDeleted: |
|
245 { |
|
246 iChatDeleted = ETrue; |
|
247 iMessages.MarkDeleted(); |
|
248 break; |
|
249 } |
|
250 default: |
|
251 { |
|
252 // not supported |
|
253 __CHAT_ASSERT_DEBUG( EFalse ); |
|
254 break; |
|
255 } |
|
256 } |
|
257 } |
|
258 |
|
259 // ----------------------------------------------------------------------------- |
|
260 // CCAMessageFlowHandlerPC::RunL |
|
261 // (other items were commented in a header). |
|
262 // ----------------------------------------------------------------------------- |
|
263 // |
|
264 void CCAMessageFlowHandlerPC::RunL() |
|
265 { |
|
266 TBool messages( ETrue ); |
|
267 TBool opening = IsOpening(); |
|
268 |
|
269 // If we're opening the view, fetch KInitialMessages at a time, otherwise just one |
|
270 TInt count( opening ? KInitialMessages : 1 ); |
|
271 |
|
272 while ( count-- > 0 && messages ) |
|
273 { |
|
274 if ( iBgOpeningMode && opening ) |
|
275 { |
|
276 // Opening the chat with background message fetching. |
|
277 // Perform steps in following order: |
|
278 // 1. Add possible new messages that are received during |
|
279 // the opening phase to the end of chat normally |
|
280 // 2. Insert unread messages to the beginning of chat |
|
281 // 3. Insert read messages to the beginning |
|
282 if ( iNewMsgWhileOpening && iFetchMessages ) |
|
283 { |
|
284 // Add the new message now |
|
285 CCAConversationMessage* msg = CCAConversationMessage::NewL( |
|
286 iReadInterface.ReadNextUnread() ); |
|
287 |
|
288 CleanupStack::PushL( msg ); |
|
289 iMessages.AddMessageL( *msg ); |
|
290 CleanupStack::Pop( msg ); |
|
291 |
|
292 iNewMsgWhileOpening = EFalse; |
|
293 } |
|
294 else if ( iReadInterface.UnreadCount() > 0 && iFetchMessages ) |
|
295 { |
|
296 // Insert unread messages, insert in last-to-first order |
|
297 iAddedUnreadMsgs++; |
|
298 |
|
299 CCAConversationMessage* msg = CCAConversationMessage::NewL( |
|
300 iReadInterface.ReadUnreadFromIndex( |
|
301 iReadInterface.MessageCount() |
|
302 + iReadInterface.UnreadCount() |
|
303 - iAddedUnreadMsgs ) ); |
|
304 |
|
305 CleanupStack::PushL( msg ); |
|
306 iMessages.InsertMessageL( *msg ); |
|
307 CleanupStack::Pop( msg ); |
|
308 |
|
309 } |
|
310 else if ( iMsgIndex < iInitialMsgCount ) |
|
311 { |
|
312 // Insert unread messages, in last-to-first order |
|
313 iAddedUnreadMsgs = 0; // Init to zero |
|
314 TInt index = iInitialMsgCount - 1 - iMsgIndex; |
|
315 CCAConversationMessage* msg = CCAConversationMessage::NewL( |
|
316 iReadInterface.Message( index ) ); |
|
317 |
|
318 CleanupStack::PushL( msg ); |
|
319 iMessages.InsertMessageL( *msg ); |
|
320 CleanupStack::Pop( msg ); |
|
321 |
|
322 iMsgIndex++; |
|
323 } |
|
324 else |
|
325 { |
|
326 // Check if the initial speed was active |
|
327 if ( opening ) |
|
328 { |
|
329 UpdateTimeIntervalL(); |
|
330 } |
|
331 messages = EFalse; |
|
332 iReadInterface.Lock( EFalse ); |
|
333 } |
|
334 } |
|
335 else |
|
336 { |
|
337 // Functionality in opening in releases 3.1 and earlier |
|
338 // and normal functionality when the chat is already fully opened |
|
339 // and new messages are received. |
|
340 // 1. Add read messages in first-to-last order |
|
341 // 2. Add unread messages in first-to-last order |
|
342 if ( iMsgIndex < iInitialMsgCount ) |
|
343 { |
|
344 // Add read messages |
|
345 CCAConversationMessage* msg = CCAConversationMessage::NewL( |
|
346 iReadInterface.Message( iMsgIndex++ ) ); |
|
347 CleanupStack::PushL( msg ); |
|
348 iMessages.AddMessageL( *msg ); |
|
349 CleanupStack::Pop( msg ); |
|
350 |
|
351 } |
|
352 else if ( iReadInterface.UnreadCount() > 0 ) |
|
353 { |
|
354 // Add unread messages |
|
355 CCAConversationMessage* msg = CCAConversationMessage::NewL( |
|
356 iReadInterface.ReadNextUnread() ); |
|
357 CleanupStack::PushL( msg ); |
|
358 iMessages.AddMessageL( *msg ); |
|
359 CleanupStack::Pop( msg ); |
|
360 } |
|
361 else |
|
362 { |
|
363 // Check if the initial speed was active |
|
364 if ( opening ) |
|
365 { |
|
366 UpdateTimeIntervalL(); |
|
367 } |
|
368 messages = EFalse; |
|
369 iReadInterface.Lock( EFalse ); |
|
370 } |
|
371 } |
|
372 } |
|
373 |
|
374 // And restart timer if needed |
|
375 if ( messages ) |
|
376 { |
|
377 Cancel(); |
|
378 After( iTimeInterval ); |
|
379 } |
|
380 } |
|
381 |
|
382 // ----------------------------------------------------------------------------- |
|
383 // CCAMessageFlowHandlerPC::RunError |
|
384 // (other items were commented in a header). |
|
385 // ----------------------------------------------------------------------------- |
|
386 // |
|
387 TInt CCAMessageFlowHandlerPC::RunError( TInt aError ) |
|
388 { |
|
389 CHAT_DP( D_CHAT_LIT( "CCAMessageFlowHandlerPC::RunError (%d)" ), aError ); |
|
390 |
|
391 // Something leaved in RunL |
|
392 if ( aError == KErrNoMemory ) |
|
393 { |
|
394 // inform user about low memory |
|
395 CActiveScheduler::Current()->Error( aError ); |
|
396 } |
|
397 |
|
398 if ( IsActive() ) |
|
399 { |
|
400 // stop processing messages |
|
401 Cancel(); |
|
402 } |
|
403 |
|
404 return KErrNone; |
|
405 } |
|
406 |
|
407 // ----------------------------------------------------------------------------- |
|
408 // CCAMessageFlowHandlerPC::HandleSettingsChangeL |
|
409 // (other items were commented in a header). |
|
410 // ----------------------------------------------------------------------------- |
|
411 // |
|
412 void CCAMessageFlowHandlerPC::HandleSettingsChangeL( TInt aChangedSettingEnum ) |
|
413 { |
|
414 // code scanner warning can be ignored |
|
415 // non-leaving function in leavining function |
|
416 switch ( aChangedSettingEnum ) |
|
417 { |
|
418 case MCASettings::EMessageFlowSettingLevel: |
|
419 { |
|
420 UpdateTimeIntervalL(); |
|
421 // no need to restart the timer even if we're running, |
|
422 // because it will get updated after next message |
|
423 break; |
|
424 } |
|
425 default: |
|
426 { |
|
427 // no need to react to other changes |
|
428 break; |
|
429 } |
|
430 } |
|
431 } |
|
432 |
|
433 // ----------------------------------------------------------------------------- |
|
434 // CCAMessageFlowHandlerPC::UpdateTimeIntervalL |
|
435 // (other items were commented in a header). |
|
436 // ----------------------------------------------------------------------------- |
|
437 // |
|
438 void CCAMessageFlowHandlerPC::UpdateTimeIntervalL() |
|
439 { |
|
440 // codescanner warning can be ignored |
|
441 TInt flowSetting( iSettings.Value( |
|
442 MCASettings::EMessageFlowSettingLevel ) ); |
|
443 |
|
444 // flowSetting is from KSettingValueMin to KSettingValueMax |
|
445 TInt range( KSettingValueMax - KSettingValueMin ); |
|
446 TInt newRange( KTimeIntervalFast - KTimeIntervalSlow ); |
|
447 |
|
448 // convert it to a range from KTimeIntervalSlow to KTimeIntervalFast |
|
449 TInt flowSpeed( ( flowSetting - KSettingValueMin )*newRange / range ); |
|
450 |
|
451 // update the end point |
|
452 flowSpeed += KTimeIntervalSlow; |
|
453 |
|
454 // validate the result |
|
455 if ( flowSpeed > KTimeIntervalSlow ) |
|
456 { |
|
457 flowSpeed = KTimeIntervalSlow; |
|
458 } |
|
459 |
|
460 if ( flowSpeed < KTimeIntervalFast ) |
|
461 { |
|
462 flowSpeed = KTimeIntervalFast; |
|
463 } |
|
464 |
|
465 // and convert from milliseconds to microseconds |
|
466 iTimeInterval = flowSpeed * KMilliToMicro; |
|
467 } |
|
468 |
|
469 // ----------------------------------------------------------------------------- |
|
470 // CCAMessageFlowHandlerPC::IsOpening |
|
471 // (other items were commented in a header). |
|
472 // ----------------------------------------------------------------------------- |
|
473 // |
|
474 TBool CCAMessageFlowHandlerPC::IsOpening() const |
|
475 { |
|
476 return iTimeInterval == |
|
477 TTimeIntervalMicroSeconds32( KTimeIntervalOpen * KMilliToMicro ); |
|
478 } |
|
479 // ----------------------------------------------------------------------------- |
|
480 // CCAMessageFlowHandler::MessageIndexCorrection |
|
481 // ----------------------------------------------------------------------------- |
|
482 // |
|
483 TInt CCAMessageFlowHandlerPC::MessageIndexCorrection( TInt aIndex ) const |
|
484 { |
|
485 if ( !IsOpening() || !iBgOpeningMode ) |
|
486 { |
|
487 // if not opening chat or message insertion is not used |
|
488 // => then index is already correct |
|
489 return aIndex; |
|
490 } |
|
491 |
|
492 // We need to calculate correct index |
|
493 TInt index = -1; |
|
494 if ( iAddedUnreadMsgs > 0 ) |
|
495 { |
|
496 // we have only added some unread messages |
|
497 TInt totalCount = iReadInterface.MessageCount() + |
|
498 iReadInterface.UnreadCount(); |
|
499 index = aIndex - ( totalCount - iAddedUnreadMsgs ); |
|
500 } |
|
501 else |
|
502 { |
|
503 // added some read messages |
|
504 index = aIndex - ( iInitialMsgCount - 1 - iMsgIndex ); |
|
505 } |
|
506 |
|
507 CHAT_DP( D_CHAT_LIT( "CCAMessageFlowHandler::MessageIndexCorrection \ |
|
508 old index: %d, corrected index: %d" ), aIndex, index ); |
|
509 return index; |
|
510 } |
|
511 |
|
512 // End of File |