|
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: This file implements class CEmailMailboxCache. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "emailmailboxcache.h" |
|
19 #include "emailapiutils.h" |
|
20 |
|
21 static const TInt KCacheGranularity = 2; |
|
22 |
|
23 // --------------------------------------------------------------------------- |
|
24 // |
|
25 // --------------------------------------------------------------------------- |
|
26 // |
|
27 CEmailMailboxCache* CEmailMailboxCache::NewL() |
|
28 { |
|
29 CEmailMailboxCache* cache = new ( ELeave ) CEmailMailboxCache(); |
|
30 return cache; |
|
31 } |
|
32 |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 // --------------------------------------------------------------------------- |
|
36 // |
|
37 CEmailMailboxCache::~CEmailMailboxCache() |
|
38 { |
|
39 iEntries.Close(); |
|
40 } |
|
41 |
|
42 // --------------------------------------------------------------------------- |
|
43 // Start of cache transaction with clenanup support |
|
44 // --------------------------------------------------------------------------- |
|
45 // |
|
46 void CEmailMailboxCache::StartCachingPushL() |
|
47 { |
|
48 TCleanupItem item( &CEmailMailboxCache::CleanupOp, this ); |
|
49 CleanupStack::PushL( item ); |
|
50 iState = ECaching; |
|
51 iEntries.Reset(); |
|
52 } |
|
53 |
|
54 // --------------------------------------------------------------------------- |
|
55 // End of cache transaction |
|
56 // --------------------------------------------------------------------------- |
|
57 // |
|
58 void CEmailMailboxCache::EndCachingPop() |
|
59 { |
|
60 iState = EComplete; |
|
61 CleanupStack::Pop(); |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 TBool CEmailMailboxCache::IsCached() const |
|
69 { |
|
70 return iState == EComplete; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CEmailMailboxCache::AddMailboxL( |
|
78 CPluginData& aPluginData, |
|
79 const TMailboxId& aMailboxId ) |
|
80 { |
|
81 if ( iState != ECaching ) |
|
82 { |
|
83 User::Leave( KErrNotReady ); |
|
84 } |
|
85 if ( FindById( aMailboxId ) == KErrNotFound ) |
|
86 { |
|
87 TCacheEntry entry( &aPluginData, aMailboxId ); |
|
88 iEntries.AppendL( entry ); |
|
89 } |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 CPluginData* CEmailMailboxCache::PluginDataL( const TMailboxId& aMailboxId ) const |
|
97 { |
|
98 CPluginData* pdata = NULL; |
|
99 const TInt index( FindById( aMailboxId ) ); |
|
100 if ( index != KErrNotFound ) |
|
101 { |
|
102 const TCacheEntry& entry = iEntries[ index ]; |
|
103 pdata = entry.iPluginData; |
|
104 } |
|
105 return pdata; |
|
106 } |
|
107 |
|
108 // --------------------------------------------------------------------------- |
|
109 // |
|
110 // --------------------------------------------------------------------------- |
|
111 // |
|
112 CPluginData* CEmailMailboxCache::PluginDataL( const TUid& aPluginId ) const |
|
113 { |
|
114 CPluginData* pdata = NULL; |
|
115 const TInt index( FindByPluginIdL( aPluginId ) ); |
|
116 if ( index != KErrNotFound ) |
|
117 { |
|
118 const TCacheEntry& entry = iEntries[ index ]; |
|
119 pdata = entry.iPluginData; |
|
120 } |
|
121 return pdata; |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------------------------- |
|
125 // |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 void CEmailMailboxCache::GetIdsL( REmailMailboxIdArray& aIdArray ) const |
|
129 { |
|
130 if ( iState != EComplete ) |
|
131 { |
|
132 // cache not up to date |
|
133 User::Leave( KErrNotReady ); |
|
134 } |
|
135 for ( TInt i=0; i < iEntries.Count(); i++ ) |
|
136 { |
|
137 TMailboxId id = iEntries[i].iMailboxId; |
|
138 aIdArray.AppendL( id ); |
|
139 } |
|
140 } |
|
141 |
|
142 // --------------------------------------------------------------------------- |
|
143 // |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 TInt CEmailMailboxCache::FindById( const TMailboxId& aMailboxId ) const |
|
147 { |
|
148 TIdentityRelation<TCacheEntry> rel( CEmailMailboxCache::Equals ); |
|
149 |
|
150 // don't care about plugin data because this is seach key only and |
|
151 // mailbox id is used for search |
|
152 TCacheEntry entry( NULL, aMailboxId ); |
|
153 return iEntries.Find( entry, rel ); |
|
154 } |
|
155 |
|
156 // --------------------------------------------------------------------------- |
|
157 // |
|
158 // --------------------------------------------------------------------------- |
|
159 // |
|
160 TInt CEmailMailboxCache::FindByPluginIdL( const TUid& aPluginId ) const |
|
161 { |
|
162 TIdentityRelation<TCacheEntry> rel( CEmailMailboxCache::PluginEquals ); |
|
163 CPluginData* key = new ( ELeave ) CPluginData( aPluginId ); |
|
164 TCacheEntry entry( key, TMailboxId() ); |
|
165 TInt index = iEntries.Find( entry, rel ); |
|
166 delete key; |
|
167 return index; |
|
168 } |
|
169 |
|
170 // --------------------------------------------------------------------------- |
|
171 // |
|
172 // --------------------------------------------------------------------------- |
|
173 // |
|
174 TBool CEmailMailboxCache::Equals( const TCacheEntry& a1, const TCacheEntry& a2 ) |
|
175 { |
|
176 return ( a1.iMailboxId == a2.iMailboxId ); |
|
177 } |
|
178 |
|
179 // --------------------------------------------------------------------------- |
|
180 // |
|
181 // --------------------------------------------------------------------------- |
|
182 // |
|
183 TBool CEmailMailboxCache::PluginEquals( const TCacheEntry& a1, const TCacheEntry& a2 ) |
|
184 { |
|
185 return ( a1.iPluginData->Uid() == a2.iPluginData->Uid() ); |
|
186 } |
|
187 |
|
188 // --------------------------------------------------------------------------- |
|
189 // Cleanup |
|
190 // --------------------------------------------------------------------------- |
|
191 // |
|
192 void CEmailMailboxCache::CleanupOp( TAny* aAny ) |
|
193 { |
|
194 CEmailMailboxCache* cache = reinterpret_cast<CEmailMailboxCache*>( aAny ); |
|
195 cache->iEntries.Reset(); |
|
196 cache->iState = EEmpty; |
|
197 } |
|
198 |
|
199 // --------------------------------------------------------------------------- |
|
200 // |
|
201 // --------------------------------------------------------------------------- |
|
202 // |
|
203 CEmailMailboxCache::CEmailMailboxCache() : |
|
204 iState( EEmpty ), |
|
205 iEntries( KCacheGranularity ) |
|
206 { |
|
207 } |
|
208 |
|
209 // End of file. |