63
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-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: Phonebook 2 contact view sort policy.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "CPbk2ContactViewSortPolicy.h"
|
|
20 |
|
|
21 |
// Phonebook2
|
|
22 |
#include "CPbk2SortKeyArray.h"
|
|
23 |
#include "RPbk2LocalizedResourceFile.h"
|
|
24 |
#include "Pbk2DataCaging.hrh"
|
|
25 |
#include <Pbk2UIPolicyRes.rsg>
|
|
26 |
|
|
27 |
// Virtual Phonebook
|
|
28 |
#include <MVPbkFieldType.h>
|
|
29 |
#include <MVPbkViewContact.h>
|
|
30 |
#include <MVPbkBaseContactField.h>
|
|
31 |
#include <MVPbkContactFieldTextData.h>
|
|
32 |
#include <MVPbkBaseContactFieldCollection.h>
|
|
33 |
#include <CVPbkContactManager.h>
|
|
34 |
#include <CVPbkFieldTypeSelector.h>
|
|
35 |
#include <CVPbkFieldFilter.h>
|
|
36 |
#include <CVPbkSortOrder.h>
|
|
37 |
|
|
38 |
// System includes
|
|
39 |
#include <coemain.h>
|
|
40 |
#include <barsread.h>
|
|
41 |
#include <SortUtil.h>
|
|
42 |
|
|
43 |
// Debugging headers
|
|
44 |
#include <Pbk2Profile.h>
|
|
45 |
|
|
46 |
#include <vpbkeng.rsg>
|
|
47 |
|
|
48 |
// Greater than any practical top contact number string
|
|
49 |
// which is in form like '0000000001'.
|
|
50 |
_LIT(KBigNumStr, "9999999999");
|
|
51 |
|
|
52 |
|
|
53 |
/// Unnamed namespace for local definitions
|
|
54 |
namespace {
|
|
55 |
|
|
56 |
_LIT(KResourceFile, "Pbk2UIPolicyRes.rsc");
|
|
57 |
const TInt KNegative = -1;
|
|
58 |
const TInt KPositive = 1;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Sort key array.
|
|
62 |
*/
|
|
63 |
class TPbk2SortKeyArray : public MSortKeyArray
|
|
64 |
{
|
|
65 |
public: // Interface
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Constructor.
|
|
69 |
*
|
|
70 |
* @param aArray Array.
|
|
71 |
*/
|
|
72 |
TPbk2SortKeyArray(
|
|
73 |
const MVPbkSortKeyArray& aArray );
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Returns the count of the keys.
|
|
77 |
*
|
|
78 |
* @return Count of the keys.
|
|
79 |
*/
|
|
80 |
TInt SortKeyCount() const
|
|
81 |
{
|
|
82 |
return iArray.Count();
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Returns the key at aIndex.
|
|
87 |
*
|
|
88 |
* @param aIndex Index to query.
|
|
89 |
* @return Key at queried index.
|
|
90 |
*/
|
|
91 |
TSortKey SortKeyAt(
|
|
92 |
TInt aIndex ) const;
|
|
93 |
|
|
94 |
private: // Data
|
|
95 |
/// Ref: Array
|
|
96 |
const MVPbkSortKeyArray& iArray;
|
|
97 |
|
|
98 |
};
|
|
99 |
|
|
100 |
// --------------------------------------------------------------------------
|
|
101 |
// TPbk2SortKeyArray::TPbk2SortKeyArray
|
|
102 |
// --------------------------------------------------------------------------
|
|
103 |
//
|
|
104 |
TPbk2SortKeyArray::TPbk2SortKeyArray( const MVPbkSortKeyArray& aArray ) :
|
|
105 |
iArray( aArray )
|
|
106 |
{
|
|
107 |
}
|
|
108 |
|
|
109 |
// --------------------------------------------------------------------------
|
|
110 |
// TPbk2SortKeyArray::SortKeyAt
|
|
111 |
// --------------------------------------------------------------------------
|
|
112 |
//
|
|
113 |
TSortKey TPbk2SortKeyArray::SortKeyAt( TInt aIndex ) const
|
|
114 |
{
|
|
115 |
TSortKeyType sortKeyType( ESortKeyBasic );
|
|
116 |
if ( iArray.SortKeyAt( aIndex ).Type() == EVPbkSortKeyPronounciation )
|
|
117 |
{
|
|
118 |
sortKeyType = ESortKeyPronounciation;
|
|
119 |
}
|
|
120 |
|
|
121 |
return TSortKey( iArray.SortKeyAt( aIndex ).Text(), sortKeyType );
|
|
122 |
}
|
|
123 |
|
|
124 |
} /// namespace
|
|
125 |
|
|
126 |
|
|
127 |
// --------------------------------------------------------------------------
|
|
128 |
// CPbk2ContactViewSortPolicy::CPbk2ContactViewSortPolicy
|
|
129 |
// --------------------------------------------------------------------------
|
|
130 |
//
|
|
131 |
CPbk2ContactViewSortPolicy::CPbk2ContactViewSortPolicy()
|
|
132 |
{
|
|
133 |
}
|
|
134 |
|
|
135 |
// --------------------------------------------------------------------------
|
|
136 |
// CPbk2ContactViewSortPolicy::~CPbk2ContactViewSortPolicy
|
|
137 |
// --------------------------------------------------------------------------
|
|
138 |
//
|
|
139 |
CPbk2ContactViewSortPolicy::~CPbk2ContactViewSortPolicy()
|
|
140 |
{
|
|
141 |
delete iPronunciationFieldSelector;
|
|
142 |
delete iRightSortKeyArray;
|
|
143 |
delete iLeftSortKeyArray;
|
|
144 |
delete iSortUtil;
|
|
145 |
delete iSortOrder;
|
|
146 |
}
|
|
147 |
|
|
148 |
// --------------------------------------------------------------------------
|
|
149 |
// CPbk2ContactViewSortPolicy::NewL
|
|
150 |
// --------------------------------------------------------------------------
|
|
151 |
//
|
|
152 |
CPbk2ContactViewSortPolicy* CPbk2ContactViewSortPolicy::NewL
|
|
153 |
( TParam* aParam )
|
|
154 |
{
|
|
155 |
CPbk2ContactViewSortPolicy* self =
|
|
156 |
new ( ELeave ) CPbk2ContactViewSortPolicy;
|
|
157 |
CleanupStack::PushL( self );
|
|
158 |
self->ConstructL( aParam );
|
|
159 |
CleanupStack::Pop( self );
|
|
160 |
return self;
|
|
161 |
}
|
|
162 |
|
|
163 |
// --------------------------------------------------------------------------
|
|
164 |
// CPbk2ContactViewSortPolicy::ConstructL
|
|
165 |
// --------------------------------------------------------------------------
|
|
166 |
//
|
|
167 |
void CPbk2ContactViewSortPolicy::ConstructL( TParam* aParam )
|
|
168 |
{
|
|
169 |
iSortUtil = CSortUtil::NewL();
|
|
170 |
|
|
171 |
if ( aParam )
|
|
172 |
{
|
|
173 |
iSortPolicyParameterReceived = ETrue;
|
|
174 |
|
|
175 |
iLeftSortKeyArray = CPbk2SortKeyArray::NewL();
|
|
176 |
iRightSortKeyArray = CPbk2SortKeyArray::NewL();
|
|
177 |
|
|
178 |
iSortOrder = CVPbkSortOrder::NewL(aParam->iSortOrder);
|
|
179 |
iFieldMapper.SetSortOrder(*iSortOrder);
|
|
180 |
|
|
181 |
iTopContactId = GetTopContactFieldIndex();
|
|
182 |
|
|
183 |
RPbk2LocalizedResourceFile resFile(aParam->iFs);
|
|
184 |
resFile.OpenLC( KPbk2RomFileDrive, KDC_RESOURCE_FILES_DIR,
|
|
185 |
KResourceFile );
|
|
186 |
|
|
187 |
HBufC8* selectorBuf = resFile.AllocReadLC
|
|
188 |
( R_PRONUNCIATION_FIELD_SELECTOR );
|
|
189 |
TResourceReader reader;
|
|
190 |
reader.SetBuffer( selectorBuf );
|
|
191 |
iPronunciationFieldSelector = CVPbkFieldTypeSelector::NewL
|
|
192 |
( reader, aParam->iFieldTypes );
|
|
193 |
|
|
194 |
CleanupStack::PopAndDestroy( 2 ); // buffer, resFile
|
|
195 |
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
// --------------------------------------------------------------------------
|
|
200 |
// CPbk2ContactViewSortPolicy::SetSortOrderL
|
|
201 |
// --------------------------------------------------------------------------
|
|
202 |
//
|
|
203 |
void CPbk2ContactViewSortPolicy::SetSortOrderL
|
|
204 |
( const MVPbkFieldTypeList& aSortOrder )
|
|
205 |
{
|
|
206 |
if ( !iSortPolicyParameterReceived )
|
|
207 |
{
|
|
208 |
// This class has been initialized without TParam
|
|
209 |
User::Leave( KErrNotSupported );
|
|
210 |
}
|
|
211 |
|
|
212 |
CVPbkSortOrder* sortOrder = CVPbkSortOrder::NewL( aSortOrder );
|
|
213 |
delete iSortOrder;
|
|
214 |
iSortOrder = sortOrder;
|
|
215 |
iFieldMapper.SetSortOrder( *iSortOrder );
|
|
216 |
|
|
217 |
iTopContactId = GetTopContactFieldIndex();
|
|
218 |
}
|
|
219 |
|
|
220 |
// --------------------------------------------------------------------------
|
|
221 |
// CPbk2ContactViewSortPolicy::SortStartL
|
|
222 |
// --------------------------------------------------------------------------
|
|
223 |
//
|
|
224 |
void CPbk2ContactViewSortPolicy::SortStartL()
|
|
225 |
{
|
|
226 |
if ( !iSortPolicyParameterReceived )
|
|
227 |
{
|
|
228 |
// This class has been initialized without TParam
|
|
229 |
User::Leave( KErrNotSupported );
|
|
230 |
}
|
|
231 |
|
|
232 |
iLeftSortKeyArray->Reset();
|
|
233 |
iRightSortKeyArray->Reset();
|
|
234 |
|
|
235 |
const TInt typeCount( iSortOrder->FieldTypeCount() );
|
|
236 |
for ( TInt i( 0 ); i < typeCount; ++i )
|
|
237 |
{
|
|
238 |
const MVPbkFieldType& fieldType = iSortOrder->FieldTypeAt( i );
|
|
239 |
if ( iPronunciationFieldSelector->IsFieldTypeIncluded( fieldType ) )
|
|
240 |
{
|
|
241 |
iLeftSortKeyArray->AppendL(
|
|
242 |
TSortKey( KNullDesC, ESortKeyPronounciation ) );
|
|
243 |
iRightSortKeyArray->AppendL(
|
|
244 |
TSortKey( KNullDesC, ESortKeyPronounciation ) );
|
|
245 |
}
|
|
246 |
else
|
|
247 |
{
|
|
248 |
iLeftSortKeyArray->AppendL(
|
|
249 |
TSortKey( KNullDesC, ESortKeyBasic ) );
|
|
250 |
iRightSortKeyArray->AppendL(
|
|
251 |
TSortKey( KNullDesC, ESortKeyBasic ) );
|
|
252 |
}
|
|
253 |
}
|
|
254 |
}
|
|
255 |
|
|
256 |
// --------------------------------------------------------------------------
|
|
257 |
// CPbk2ContactViewSortPolicy::SortCompleted
|
|
258 |
// --------------------------------------------------------------------------
|
|
259 |
//
|
|
260 |
void CPbk2ContactViewSortPolicy::SortCompleted()
|
|
261 |
{
|
|
262 |
iLeftSortKeyArray->Reset();
|
|
263 |
iRightSortKeyArray->Reset();
|
|
264 |
}
|
|
265 |
|
|
266 |
// --------------------------------------------------------------------------
|
|
267 |
// CPbk2ContactViewSortPolicy::CompareContacts
|
|
268 |
// --------------------------------------------------------------------------
|
|
269 |
//
|
|
270 |
TInt CPbk2ContactViewSortPolicy::CompareContacts
|
|
271 |
( const MVPbkViewContact& aLhs, const MVPbkViewContact& aRhs )
|
|
272 |
{
|
|
273 |
TInt ret = 0;
|
|
274 |
|
|
275 |
if ( iSortPolicyParameterReceived )
|
|
276 |
{
|
|
277 |
// Change the text in the sortkeys to correspond
|
|
278 |
// fields in the contacts
|
|
279 |
|
|
280 |
TInt i = 0;
|
|
281 |
|
|
282 |
PBK2_PROFILE_START( Pbk2Profile::ESortPolicyCompareContacts );
|
|
283 |
|
|
284 |
iFieldMapper.SetContactFields( aLhs.Fields() );
|
|
285 |
const TInt lhsCount = iFieldMapper.FieldCount();
|
|
286 |
for ( i = 0; i < lhsCount; ++i )
|
|
287 |
{
|
|
288 |
SetSortKey( iLeftSortKeyArray, iFieldMapper.FieldAt(i), i );
|
|
289 |
}
|
|
290 |
|
|
291 |
iFieldMapper.SetContactFields( aRhs.Fields() );
|
|
292 |
const TInt rhsCount = iFieldMapper.FieldCount();
|
|
293 |
for ( i = 0; i < rhsCount; ++i )
|
|
294 |
{
|
|
295 |
SetSortKey( iRightSortKeyArray, iFieldMapper.FieldAt(i), i );
|
|
296 |
}
|
|
297 |
|
|
298 |
PBK2_PROFILE_END( Pbk2Profile::ESortPolicyCompareContacts );
|
|
299 |
|
|
300 |
|
|
301 |
PBK2_PROFILE_START( Pbk2Profile::ESortPolicySortUtil );
|
|
302 |
|
|
303 |
TInt result( iSortUtil->Interface()->CompareItems
|
|
304 |
( *iLeftSortKeyArray, *iRightSortKeyArray ) );
|
|
305 |
|
|
306 |
PBK2_PROFILE_END( Pbk2Profile::ESortPolicySortUtil );
|
|
307 |
|
|
308 |
ret = PostProcessResult( result );
|
|
309 |
}
|
|
310 |
|
|
311 |
return ret;
|
|
312 |
}
|
|
313 |
|
|
314 |
// --------------------------------------------------------------------------
|
|
315 |
// CPbk2ContactViewSortPolicy::CompareItems
|
|
316 |
// --------------------------------------------------------------------------
|
|
317 |
//
|
|
318 |
TInt CPbk2ContactViewSortPolicy::CompareItems
|
|
319 |
( const MVPbkSortKeyArray& aLhs,
|
|
320 |
const MVPbkSortKeyArray& aRhs ) const
|
|
321 |
{
|
|
322 |
const TPbk2SortKeyArray lhsKeyArray( aLhs );
|
|
323 |
const TPbk2SortKeyArray rhsKeyArray( aRhs );
|
|
324 |
return iSortUtil->Interface()->CompareItems( lhsKeyArray, rhsKeyArray );
|
|
325 |
}
|
|
326 |
|
|
327 |
// --------------------------------------------------------------------------
|
|
328 |
// CPbk2ContactViewSortPolicy::PostProcessResult
|
|
329 |
// --------------------------------------------------------------------------
|
|
330 |
//
|
|
331 |
TInt CPbk2ContactViewSortPolicy::PostProcessResult( TInt aSortUtilResult )
|
|
332 |
{
|
|
333 |
// With this function we correct sort util result because it does not
|
|
334 |
// give right answers if contact is "Unnamed".
|
|
335 |
// So here is clarified how this function corrects the result.
|
|
336 |
// If rightside has no data left is less (-1) otherwise greater (1).
|
|
337 |
// "<Unnamed>" vs. "<Unnamed>" = -1, leftside comes before rightside
|
|
338 |
// "a" vs. "<Unnamed>" = -1, leftside comes before rightside
|
|
339 |
// "<Unnamed>" vs. "a" = 1 rightside comes before leftside
|
|
340 |
//
|
|
341 |
TInt result = aSortUtilResult;
|
|
342 |
const TInt rhsCount( iRightSortKeyArray->SortKeyCount() );
|
|
343 |
TBool rhsHasData( EFalse );
|
|
344 |
for ( TInt i = 0; i < rhsCount; ++i )
|
|
345 |
{
|
|
346 |
TSortKey key = iRightSortKeyArray->SortKeyAt( i );
|
|
347 |
if ( key.Text().Length() > 0 )
|
|
348 |
{
|
|
349 |
rhsHasData = ETrue;
|
|
350 |
break;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
if ( !rhsHasData )
|
|
355 |
{
|
|
356 |
// Here we have situation "something" vs "<Unnamed>" .
|
|
357 |
// We want leftside to come before rightside.
|
|
358 |
result = KNegative;
|
|
359 |
}
|
|
360 |
else
|
|
361 |
{
|
|
362 |
TBool lhsHasData( EFalse );
|
|
363 |
const TInt lhsCount( iLeftSortKeyArray->SortKeyCount() );
|
|
364 |
for ( TInt i = 0; i < lhsCount; ++i )
|
|
365 |
{
|
|
366 |
TSortKey key = iLeftSortKeyArray->SortKeyAt( i );
|
|
367 |
if ( key.Text().Length() > 0 )
|
|
368 |
{
|
|
369 |
lhsHasData = ETrue;
|
|
370 |
break;
|
|
371 |
}
|
|
372 |
}
|
|
373 |
|
|
374 |
if ( !lhsHasData )
|
|
375 |
{
|
|
376 |
// Here we have situation "<Unnamed>" vs. "something".
|
|
377 |
// We want rightside to come before leftside.
|
|
378 |
result = KPositive;
|
|
379 |
}
|
|
380 |
}
|
|
381 |
|
|
382 |
return result;
|
|
383 |
}
|
|
384 |
|
|
385 |
// --------------------------------------------------------------------------
|
|
386 |
// CPbk2ContactViewSortPolicy::GetTopContactFieldIndex
|
|
387 |
// --------------------------------------------------------------------------
|
|
388 |
//
|
|
389 |
TInt CPbk2ContactViewSortPolicy::GetTopContactFieldIndex()
|
|
390 |
{
|
|
391 |
TInt index = KErrNotFound;
|
|
392 |
for ( TInt i=0; i<iSortOrder->FieldTypeCount(); i++ )
|
|
393 |
{
|
|
394 |
// Resource id of top contact field is R_VPBK_FIELD_TYPE_TOPCONTACT.
|
|
395 |
if ( iSortOrder->FieldTypeAt( i ).FieldTypeResId() ==
|
|
396 |
R_VPBK_FIELD_TYPE_TOPCONTACT )
|
|
397 |
{
|
|
398 |
index = i;
|
|
399 |
break;
|
|
400 |
}
|
|
401 |
}
|
|
402 |
return index;
|
|
403 |
}
|
|
404 |
|
|
405 |
// --------------------------------------------------------------------------
|
|
406 |
// CPbk2ContactViewSortPolicy::SetSortKey
|
|
407 |
// --------------------------------------------------------------------------
|
|
408 |
//
|
|
409 |
void CPbk2ContactViewSortPolicy::SetSortKey(
|
|
410 |
CPbk2SortKeyArray* aSortKeyArray,
|
|
411 |
const MVPbkBaseContactField* aField,
|
|
412 |
TInt aIndex )
|
|
413 |
{
|
|
414 |
TInt textLen = 0;
|
|
415 |
if ( aField )
|
|
416 |
{
|
|
417 |
TPtrC text = MVPbkContactFieldTextData::Cast
|
|
418 |
( aField->FieldData() ).Text();
|
|
419 |
aSortKeyArray->SetText( text, aIndex );
|
|
420 |
textLen = text.Length();
|
|
421 |
}
|
|
422 |
if ( textLen == 0 )
|
|
423 |
{
|
|
424 |
if ( aIndex == iTopContactId )
|
|
425 |
{
|
|
426 |
// For some variants such as Chinese variant,
|
|
427 |
// empty string is less than not empty string.
|
|
428 |
// This will make topic contacts which have substantial top contact
|
|
429 |
// id strings after those with empty id strings.
|
|
430 |
// To prevent this, use a big number string instead of empty string
|
|
431 |
// and make sure top contacts in front of other contacts.
|
|
432 |
aSortKeyArray->SetText( KBigNumStr, aIndex );
|
|
433 |
}
|
|
434 |
else
|
|
435 |
{
|
|
436 |
aSortKeyArray->SetText( KNullDesC, aIndex );
|
|
437 |
}
|
|
438 |
}
|
|
439 |
}
|
|
440 |
|
|
441 |
// End of File
|