|
1 /* |
|
2 * Copyright (c) 2010 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 |
|
18 // INCLUDES |
|
19 #include "DelayedCallback.h" |
|
20 |
|
21 |
|
22 // --------------------------------------------------------------------------- |
|
23 // CDelayedCallback::NewL |
|
24 // Creates new instance of CDelayedCallback |
|
25 // --------------------------------------------------------------------------- |
|
26 EXPORT_C CDelayedCallback* CDelayedCallback::NewL(TInt aPriority) |
|
27 { |
|
28 CDelayedCallback* self = new(ELeave) CDelayedCallback(aPriority); |
|
29 CleanupStack::PushL( self ); |
|
30 self->ConstructL(); |
|
31 CleanupStack::Pop( self ); |
|
32 return self; |
|
33 } |
|
34 |
|
35 // --------------------------------------------------------------------------- |
|
36 // CDelayedCallback::~CDelayedCallback |
|
37 // Destructor. |
|
38 // --------------------------------------------------------------------------- |
|
39 EXPORT_C CDelayedCallback::~CDelayedCallback() |
|
40 { |
|
41 if ( iTimer ) |
|
42 { |
|
43 iTimer->Cancel(); |
|
44 delete iTimer; |
|
45 iTimer = NULL; |
|
46 } |
|
47 iObserver = NULL; |
|
48 Cancel(); |
|
49 } |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // CDelayedCallback::RunL |
|
53 // From CActive, RunL |
|
54 // --------------------------------------------------------------------------- |
|
55 void CDelayedCallback::RunL() |
|
56 { |
|
57 User::LeaveIfError( iStatus.Int() ); |
|
58 if ( iObserver ) |
|
59 { |
|
60 iObserver->DelayedCallbackL(iCode); |
|
61 } |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // CDelayedCallback::DoCancel |
|
66 // From CActive, DoCancel |
|
67 // --------------------------------------------------------------------------- |
|
68 void CDelayedCallback::DoCancel() |
|
69 { |
|
70 if ( iTimer ) |
|
71 { |
|
72 if ( iTimer->IsActive() ) |
|
73 { |
|
74 iTimer->Cancel(); |
|
75 } |
|
76 delete iTimer; |
|
77 iTimer = NULL; |
|
78 } |
|
79 } |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // CDelayedCallback::RunError |
|
83 // From CActive, RunError |
|
84 // --------------------------------------------------------------------------- |
|
85 TInt CDelayedCallback::RunError(TInt aError) |
|
86 { |
|
87 if (iObserver) |
|
88 iObserver->DelayedError(aError); |
|
89 return KErrNone; |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // CDelayedCallback::Start |
|
94 // Activates this callback object. |
|
95 // --------------------------------------------------------------------------- |
|
96 EXPORT_C void CDelayedCallback::Start(TInt aCode, MDelayedCallbackObserver* aObserver, |
|
97 TInt aDelay) |
|
98 { |
|
99 iCode = aCode; |
|
100 iObserver = aObserver; |
|
101 if ( CallbackPending() ) |
|
102 { |
|
103 return; |
|
104 } |
|
105 |
|
106 if ( aDelay <= 0 ) |
|
107 { |
|
108 if ( !IsAdded() ) |
|
109 { |
|
110 CActiveScheduler::Add( this ); |
|
111 } |
|
112 SetActive(); |
|
113 TRequestStatus* status = &iStatus; |
|
114 User::RequestComplete( status, KErrNone ); |
|
115 } |
|
116 else |
|
117 { |
|
118 if ( iTimer ) |
|
119 { |
|
120 iTimer->Start( aDelay, aDelay, TCallBack(StaticTimerCallback, this) ); |
|
121 } |
|
122 } |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // CDelayedCallback::CancelCallback |
|
127 // Cancels the pending callback request. |
|
128 // --------------------------------------------------------------------------- |
|
129 EXPORT_C void CDelayedCallback::CancelCallback() |
|
130 { |
|
131 Cancel(); |
|
132 if ( iTimer ) |
|
133 { |
|
134 iTimer->Cancel(); |
|
135 } |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------------------------- |
|
139 // CDelayedCallback::CallbackPending |
|
140 // Tells if delayed callback request is pending. |
|
141 // --------------------------------------------------------------------------- |
|
142 EXPORT_C TBool CDelayedCallback::CallbackPending() const |
|
143 { |
|
144 TBool pending = IsActive(); |
|
145 if ( iTimer ) |
|
146 { |
|
147 pending |= iTimer->IsActive(); |
|
148 } |
|
149 return pending; |
|
150 } |
|
151 |
|
152 // --------------------------------------------------------------------------- |
|
153 // CDelayedCallback::StaticTimerCallback |
|
154 // The actual callback function of CPeriodic |
|
155 // --------------------------------------------------------------------------- |
|
156 TInt CDelayedCallback::StaticTimerCallback(TAny* aObject) |
|
157 { |
|
158 reinterpret_cast<CDelayedCallback*>(aObject)->TimerCallback(); |
|
159 return 1; |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // CDelayedCallback::TimerCallback |
|
164 // The CPeriodic callback method that just calls the observer. |
|
165 // --------------------------------------------------------------------------- |
|
166 void CDelayedCallback::TimerCallback() |
|
167 { |
|
168 if ( iTimer ) |
|
169 { |
|
170 iTimer->Cancel(); |
|
171 } |
|
172 |
|
173 if ( iObserver ) |
|
174 { |
|
175 TInt err = KErrNone; |
|
176 TRAP(err, iObserver->DelayedCallbackL(iCode)); |
|
177 if (err != KErrNone) |
|
178 iObserver->DelayedError(err); |
|
179 } |
|
180 } |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // CDelayedCallback::CDelayedCallback |
|
184 // Constructor. |
|
185 // --------------------------------------------------------------------------- |
|
186 CDelayedCallback::CDelayedCallback(TInt aPriority) : |
|
187 CActive( aPriority ) |
|
188 { |
|
189 } |
|
190 |
|
191 // --------------------------------------------------------------------------- |
|
192 // CDelayedCallback::ConstructL |
|
193 // 2nd phase constructor. |
|
194 // --------------------------------------------------------------------------- |
|
195 void CDelayedCallback::ConstructL() |
|
196 { |
|
197 iTimer = CPeriodic::NewL( Priority() ); |
|
198 } |
|
199 |
|
200 // End of file |