79
|
1 |
/*
|
|
2 |
* Copyright (c) 2002 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 |
* CMsgSentItemsObserver implementation file
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
// INCLUDE FILES
|
|
22 |
#include <watcher.h>
|
|
23 |
#include <msvids.h>
|
|
24 |
|
|
25 |
#include <centralrepository.h> // link against centralrepository.lib
|
|
26 |
#include <cenrepnotifyhandler.h> // link against CenRepNotifHandler.lib
|
|
27 |
#include <messaginginternalcrkeys.h> // for Central Repository keys
|
|
28 |
#include <msvipc.h>
|
|
29 |
|
|
30 |
#include "MsgSentItemsObserver.h"
|
|
31 |
|
|
32 |
#ifdef USE_LOGGER
|
|
33 |
#include "MsgErrorWatcherLogging.h"
|
|
34 |
#endif
|
|
35 |
|
|
36 |
// CONSTANTS
|
|
37 |
//const TUid KUidMceApp = { 0x100058C5 }; //needed to request shared data notifications
|
|
38 |
const TMsvId KWatcherSentFolderId = KMsvSentEntryIdValue;
|
|
39 |
|
|
40 |
// Entry deletion could fail with KErrInUse if it is being operated by MW while deleting
|
|
41 |
const TUint KMaxRetries = 3;
|
|
42 |
const TInt KMaxNumOfItems = 20; //Deafult value in case of error reading CommsDb
|
|
43 |
//const TInt KMsgErrorDiskSpaceForDelete = ( 5 * 1024 ); // minimum disk space needed when deleting messages
|
|
44 |
//_LIT( KSentItemsInUseOff, "0" );
|
|
45 |
|
|
46 |
|
|
47 |
// ================= MEMBER FUNCTIONS =======================
|
|
48 |
|
|
49 |
// ---------------------------------------------------------
|
|
50 |
// CMsgSentItemsObserver::NewL
|
|
51 |
//
|
|
52 |
// Two-phased constructor.
|
|
53 |
// ---------------------------------------------------------
|
|
54 |
//
|
|
55 |
CMsgSentItemsObserver* CMsgSentItemsObserver::NewL(
|
|
56 |
CMsgErrorWatcher* aWatcher,
|
|
57 |
CMsvSession* aSession )
|
|
58 |
{
|
|
59 |
CMsgSentItemsObserver* self = new ( ELeave )
|
|
60 |
CMsgSentItemsObserver( aWatcher, aSession );
|
|
61 |
|
|
62 |
CleanupStack::PushL( self );
|
|
63 |
self->ConstructL();
|
|
64 |
CleanupStack::Pop( self );
|
|
65 |
return self;
|
|
66 |
}
|
|
67 |
|
|
68 |
// ---------------------------------------------------------
|
|
69 |
// CMsgSentItemsObserver::CMsgSentItemsObserver
|
|
70 |
//
|
|
71 |
// C++ default constructor can NOT contain any code, that
|
|
72 |
// might leave.
|
|
73 |
// ---------------------------------------------------------
|
|
74 |
//
|
|
75 |
CMsgSentItemsObserver::CMsgSentItemsObserver( CMsgErrorWatcher* aWatcher, CMsvSession* aSession )
|
|
76 |
: CActive( EPriorityStandard ),
|
|
77 |
iWatcher( aWatcher ),
|
|
78 |
iSession( aSession )
|
|
79 |
{
|
|
80 |
CActiveScheduler::Add( this );
|
|
81 |
}
|
|
82 |
|
|
83 |
// ---------------------------------------------------------
|
|
84 |
// CMsgSentItemsObserver::~CMsgSentItemsObserver
|
|
85 |
//
|
|
86 |
// Destructor
|
|
87 |
// ---------------------------------------------------------
|
|
88 |
//
|
|
89 |
CMsgSentItemsObserver::~CMsgSentItemsObserver()
|
|
90 |
{
|
|
91 |
Cancel();
|
|
92 |
if ( iNotifyHandler )
|
|
93 |
{
|
|
94 |
iNotifyHandler->StopListening();
|
|
95 |
}
|
|
96 |
delete iNotifyHandler;
|
|
97 |
delete iRepository;
|
|
98 |
if( iCleanupFlags & ESentItemsFolderObserverAdded )
|
|
99 |
{
|
|
100 |
iSentItemsFolder->RemoveObserver( *this );
|
|
101 |
}
|
|
102 |
delete iSentItemsFolder;
|
|
103 |
delete iEntry;
|
|
104 |
delete iOp;
|
|
105 |
|
|
106 |
delete iQudUPEntryBeingDeleted;
|
|
107 |
delete iQueuedUPEntriesToBeDeleted;
|
|
108 |
|
|
109 |
}
|
|
110 |
|
|
111 |
// ---------------------------------------------------------
|
|
112 |
// CMsgSentItemsObserver::ConstructL
|
|
113 |
//
|
|
114 |
// Symbian OS default constructor can leave.
|
|
115 |
// ---------------------------------------------------------
|
|
116 |
//
|
|
117 |
void CMsgSentItemsObserver::ConstructL()
|
|
118 |
{
|
|
119 |
#ifdef USE_LOGGER
|
|
120 |
MEWLOGGER_ENTERFN( "SentItems: ConstructL" );
|
|
121 |
#endif
|
|
122 |
//iMsgStoreDrive = MessageServer::CurrentDriveL( iSession->FileSession() );
|
|
123 |
//register as observer of sent items folder
|
|
124 |
TMsvSelectionOrdering sort( KMsvNoGrouping, EMsvSortByDateReverse );
|
|
125 |
iSentItemsFolder = CMsvEntry::NewL( *iSession, KWatcherSentFolderId, sort );
|
|
126 |
iSentItemsFolder->AddObserverL( *this );
|
|
127 |
iCleanupFlags |= ESentItemsFolderObserverAdded;
|
|
128 |
|
|
129 |
//register as observer of user settings for sent folder
|
|
130 |
|
|
131 |
// Central Repository
|
|
132 |
iRepository = CRepository::NewL( KCRUidMuiuSettings );
|
|
133 |
iNotifyHandler = CCenRepNotifyHandler::NewL( *this, *iRepository );
|
|
134 |
iNotifyHandler->StartListeningL();
|
|
135 |
|
|
136 |
iQudUPEntryBeingDeleted = new(ELeave) CMsvEntrySelection;
|
|
137 |
iQueuedUPEntriesToBeDeleted= new(ELeave) CMsvEntrySelection;
|
|
138 |
|
|
139 |
#ifdef USE_LOGGER
|
|
140 |
MEWLOGGER_LEAVEFN( "SentItems: ConstructL" );
|
|
141 |
#endif
|
|
142 |
}
|
|
143 |
|
|
144 |
// ---------------------------------------------------------
|
|
145 |
// CMsgSentItemsObserver::RunL
|
|
146 |
//
|
|
147 |
// Will only run when messages have been deleted
|
|
148 |
// ---------------------------------------------------------
|
|
149 |
//
|
|
150 |
void CMsgSentItemsObserver::RunL()
|
|
151 |
{
|
|
152 |
//iSession->FileSession().ReleaseReserveAccess( iMsgStoreDrive );
|
|
153 |
//Check if new messages have appeared into Sent Items while deleting
|
|
154 |
//the previous one(s).
|
|
155 |
#ifdef USE_LOGGER
|
|
156 |
MEWLOGGER_WRITEF( _L("SentItems: Retries: %d"), iRetryCounter );
|
|
157 |
#endif
|
|
158 |
if ( iRetryCounter < KMaxRetries )
|
|
159 |
{
|
|
160 |
iRetryCounter++;
|
|
161 |
TRAP_IGNORE( DeleteOldMessagesFromSentFolderL() );
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
const TDesC8& temp = iOp->ProgressL();
|
|
166 |
TMsvLocalOperationProgress* prog = ( (TMsvLocalOperationProgress*) temp.Ptr());
|
|
167 |
|
|
168 |
#ifdef USE_LOGGER
|
|
169 |
MEWLOGGER_WRITEF( _L("SentItems: deletion iStatus %d"), iStatus.Int() );
|
|
170 |
// prog->iNumberFailed should be either 0(if successful) or 1(if failed) as we are deletingone entry at a time
|
|
171 |
MEWLOGGER_WRITEF( _L("Failed Deletions: %d"), prog->iNumberFailed );
|
|
172 |
MEWLOGGER_WRITEF( _L("Delete Error Code: %d"), prog->iError );
|
|
173 |
#endif
|
|
174 |
|
|
175 |
if(iStatus.Int() == KErrNone && prog->iNumberFailed && prog->iError == KErrInUse && iRetryCounterIfEntryDelFailed < KMaxRetries)
|
|
176 |
{
|
|
177 |
DeleteSingleEntryL(iQudUPEntryBeingDeleted);
|
|
178 |
iRetryCounterIfEntryDelFailed ++;
|
|
179 |
#ifdef USE_LOGGER
|
|
180 |
MEWLOGGER_WRITEF( _L("iRetryCounterIfEntryDelFailed : %d"), iRetryCounterIfEntryDelFailed );
|
|
181 |
#endif
|
|
182 |
return;
|
|
183 |
}
|
|
184 |
iRetryCounterIfEntryDelFailed = 0;
|
|
185 |
|
|
186 |
if( iQueuedUPEntriesToBeDeleted->Count() )
|
|
187 |
{
|
|
188 |
iQudUPEntryBeingDeleted->Reset();
|
|
189 |
iQudUPEntryBeingDeleted->AppendL(iQueuedUPEntriesToBeDeleted->At(0));
|
|
190 |
iQueuedUPEntriesToBeDeleted->Delete(0);
|
|
191 |
|
|
192 |
DeleteSingleEntryL(iQudUPEntryBeingDeleted);
|
|
193 |
}
|
|
194 |
|
|
195 |
}
|
|
196 |
|
|
197 |
// ---------------------------------------------------------
|
|
198 |
// CMsgSentItemsObserver::DoCancel
|
|
199 |
//
|
|
200 |
// From active object framework
|
|
201 |
// ---------------------------------------------------------
|
|
202 |
//
|
|
203 |
void CMsgSentItemsObserver::DoCancel()
|
|
204 |
{
|
|
205 |
//iSession->FileSession().ReleaseReserveAccess( iMsgStoreDrive );
|
|
206 |
if ( iOp )
|
|
207 |
{
|
|
208 |
iOp->Cancel();
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
// ---------------------------------------------------------
|
|
213 |
// CMsgSentItemsObserver::HandleNotifyInt
|
|
214 |
//
|
|
215 |
// Notification from central repository
|
|
216 |
// ---------------------------------------------------------
|
|
217 |
//
|
|
218 |
void CMsgSentItemsObserver::HandleNotifyInt( TUint32 aId, TInt aNewValue )
|
|
219 |
{
|
|
220 |
if ( ( aId == KMuiuSentItemsInUse && aNewValue != 0 ) ||
|
|
221 |
( aId == KMuiuSentItemsCount && SentFolderIsBeingUsed() ) )
|
|
222 |
{
|
|
223 |
iRetryCounter = 0;
|
|
224 |
//This is non-leaving function...
|
|
225 |
TRAPD( dummy, DeleteOldMessagesFromSentFolderL() );
|
|
226 |
dummy=dummy; //Prevent arm5 build warning
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
// ---------------------------------------------------------
|
|
231 |
// CMsgSentItemsObserver::HandleNotifyGeneric
|
|
232 |
// ---------------------------------------------------------
|
|
233 |
//
|
|
234 |
void CMsgSentItemsObserver::HandleNotifyGeneric( TUint32 /*aId*/ )
|
|
235 |
{
|
|
236 |
//Nothing.
|
|
237 |
}
|
|
238 |
|
|
239 |
// ---------------------------------------------------------
|
|
240 |
// CMsgSentItemsObserver::HandleNotifyError
|
|
241 |
// ---------------------------------------------------------
|
|
242 |
//
|
|
243 |
void CMsgSentItemsObserver::HandleNotifyError( TUint32 /*aId*/, TInt /*error*/, CCenRepNotifyHandler* /*aHandler*/ )
|
|
244 |
{
|
|
245 |
//Nothing.
|
|
246 |
}
|
|
247 |
|
|
248 |
// ---------------------------------------------------------
|
|
249 |
// CMsgSentItemsObserver::HandleEntryEventL
|
|
250 |
//
|
|
251 |
// Call back from msg that the sent items folder has changed
|
|
252 |
// so check if messages need to be deleted.
|
|
253 |
// ---------------------------------------------------------
|
|
254 |
//
|
|
255 |
void CMsgSentItemsObserver::HandleEntryEventL( TMsvEntryEvent aEvent, TAny* aArg1, TAny* /*aArg2*/, TAny* /*aArg3*/ )
|
|
256 |
{
|
|
257 |
switch( aEvent )
|
|
258 |
{
|
|
259 |
case EMsvNewChildren:
|
|
260 |
{
|
|
261 |
if( !SentFolderIsBeingUsed() )
|
|
262 |
{
|
|
263 |
//not taking ownership
|
|
264 |
CMsvEntrySelection* selection =
|
|
265 |
static_cast<CMsvEntrySelection*>( aArg1 );
|
|
266 |
iRetryCounter = 0;
|
|
267 |
DeleteMessagesFromSentFolderL( selection );
|
|
268 |
return;
|
|
269 |
}
|
|
270 |
if( iSentItemsFolder->Count() > SentFolderUpperLimit() )
|
|
271 |
{
|
|
272 |
iRetryCounter = 0;
|
|
273 |
DeleteOldMessagesFromSentFolderL();
|
|
274 |
return;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
break;
|
|
278 |
case EMsvEntryChanged:
|
|
279 |
case EMsvDeletedChildren:
|
|
280 |
case EMsvChildrenChanged:
|
|
281 |
case EMsvEntryDeleted:
|
|
282 |
case EMsvContextInvalid:
|
|
283 |
case EMsvChildrenMissing:
|
|
284 |
case EMsvChildrenInvalid:
|
|
285 |
case EMsvEntryMoved:
|
|
286 |
default:
|
|
287 |
break;
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
// ---------------------------------------------------------
|
|
292 |
// CMsgSentItemsObserver::SentFolderIsBeingUsed
|
|
293 |
//
|
|
294 |
// Returns the user setting for use of the sent folder. If there
|
|
295 |
// is an error then the default value of ON (i.e ETrue) will
|
|
296 |
// be returned.
|
|
297 |
// ---------------------------------------------------------
|
|
298 |
//
|
|
299 |
TBool CMsgSentItemsObserver::SentFolderIsBeingUsed()
|
|
300 |
{
|
|
301 |
TInt isUsed = 1;
|
|
302 |
iRepository->Get( KMuiuSentItemsInUse, isUsed );
|
|
303 |
return isUsed;
|
|
304 |
}
|
|
305 |
|
|
306 |
// ---------------------------------------------------------
|
|
307 |
// CMsgSentItemsObserver::SentFolderUpperLimit
|
|
308 |
//
|
|
309 |
// Returns the user setting for max limit to the number of messages
|
|
310 |
// in the sent folder. If there is an error then the default value of
|
|
311 |
// KMaxNumOfItems will be returned
|
|
312 |
// ---------------------------------------------------------
|
|
313 |
//
|
|
314 |
TInt CMsgSentItemsObserver::SentFolderUpperLimit()
|
|
315 |
{
|
|
316 |
TInt numItems = KMaxNumOfItems;
|
|
317 |
iRepository->Get( KMuiuSentItemsCount, numItems );
|
|
318 |
return numItems;
|
|
319 |
}
|
|
320 |
|
|
321 |
// ---------------------------------------------------------
|
|
322 |
// CMsgSentItemsObserver::DeleteOldMessagesFromSentFolderL
|
|
323 |
//
|
|
324 |
// If there are more messages in the sent folder than the upper
|
|
325 |
// limit set by the user then delete the oldest messages.
|
|
326 |
// ---------------------------------------------------------
|
|
327 |
//
|
|
328 |
void CMsgSentItemsObserver::DeleteOldMessagesFromSentFolderL()
|
|
329 |
{
|
|
330 |
CMsvEntrySelection* children = iSentItemsFolder->ChildrenL();
|
|
331 |
CleanupStack::PushL( children );
|
|
332 |
TInt limit = SentFolderUpperLimit();
|
|
333 |
TInt sentMessageCount = children->Count();
|
|
334 |
if( sentMessageCount <= limit )
|
|
335 |
{
|
|
336 |
CleanupStack::PopAndDestroy( children );
|
|
337 |
return;
|
|
338 |
}
|
|
339 |
CMsvEntrySelection* messagesToBeDeleted = new ( ELeave ) CMsvEntrySelection;
|
|
340 |
CleanupStack::PushL( messagesToBeDeleted );
|
|
341 |
//oldest messages are at the end of the array
|
|
342 |
for( TInt index = sentMessageCount - 1; index >= limit; index-- )
|
|
343 |
{
|
|
344 |
messagesToBeDeleted->AppendL( children->At( index ) );
|
|
345 |
}
|
|
346 |
DeleteMessagesFromSentFolderL( messagesToBeDeleted );
|
|
347 |
CleanupStack::PopAndDestroy( messagesToBeDeleted );
|
|
348 |
CleanupStack::PopAndDestroy( children );
|
|
349 |
}
|
|
350 |
|
|
351 |
// ---------------------------------------------------------
|
|
352 |
// CMsgSentItemsObserver::DeleteMessagesFromSentFolderL
|
|
353 |
//
|
|
354 |
// Delete the messages specified by aSelection
|
|
355 |
// ---------------------------------------------------------
|
|
356 |
//
|
|
357 |
void CMsgSentItemsObserver::DeleteMessagesFromSentFolderL( CMsvEntrySelection* aSelection )
|
|
358 |
{
|
|
359 |
iQueuedUPEntriesToBeDeleted->AppendL(aSelection->Back(0), aSelection->Count());
|
|
360 |
if ( !IsActive() )
|
|
361 |
{
|
|
362 |
iQudUPEntryBeingDeleted->Reset();
|
|
363 |
iQudUPEntryBeingDeleted->AppendL(iQueuedUPEntriesToBeDeleted->At(0));
|
|
364 |
iQueuedUPEntriesToBeDeleted->Delete(0);
|
|
365 |
|
|
366 |
// If multiple entries are sent for deletion, then it won't be possible to track
|
|
367 |
// the entries that failed to delete
|
|
368 |
DeleteSingleEntryL(iQudUPEntryBeingDeleted);
|
|
369 |
}
|
|
370 |
|
|
371 |
}
|
|
372 |
|
|
373 |
|
|
374 |
void CMsgSentItemsObserver::DeleteSingleEntryL(CMsvEntrySelection* aSelection)
|
|
375 |
{
|
|
376 |
|
|
377 |
#ifdef USE_LOGGER
|
|
378 |
MEWLOGGER_WRITEF( _L("SentItems: deleting %d messages from sent folder"), aSelection->Count() );
|
|
379 |
#endif
|
|
380 |
|
|
381 |
const TMsvId id = aSelection->At( 0 );
|
|
382 |
delete iEntry;
|
|
383 |
iEntry = 0;
|
|
384 |
iEntry = iSession->GetEntryL( id );
|
|
385 |
iEntry->SetEntryL( iEntry->Entry().Parent() );
|
|
386 |
|
|
387 |
//if ( iSession->FileSession().ReserveDriveSpace( iMsgStoreDrive, KMsgErrorDiskSpaceForDelete ) == KErrNone )
|
|
388 |
// {
|
|
389 |
// iSession->FileSession().GetReserveAccess( iMsgStoreDrive );
|
|
390 |
// }
|
|
391 |
|
|
392 |
delete iOp;
|
|
393 |
iOp = 0;
|
|
394 |
//TRAPD( err,
|
|
395 |
// {
|
|
396 |
iOp = iEntry->DeleteL( *aSelection, iStatus ); //delete acts on returned CMsvOperation*
|
|
397 |
// } );
|
|
398 |
//if ( err )
|
|
399 |
// {
|
|
400 |
// iSession->FileSession().ReleaseReserveAccess( iMsgStoreDrive );
|
|
401 |
// User::Leave( err );
|
|
402 |
// }
|
|
403 |
//else
|
|
404 |
// {
|
|
405 |
SetActive();
|
|
406 |
// }
|
|
407 |
|
|
408 |
}
|
|
409 |
|
|
410 |
|
|
411 |
// End of File
|
|
412 |
|