1 /* |
|
2 * Copyright (c) 2008 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: Contact sorter |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "cvimpststoragecontactsorter.h" |
|
21 #include "mvimpststoragecontact.h" |
|
22 #include "tvimpstenums.h" |
|
23 #include "cvimpststoragedefs.h" |
|
24 #include "vimpstdebugtrace.h" |
|
25 |
|
26 // The order of contacts if sorted by status |
|
27 const TInt KConstOnline = 1; |
|
28 const TInt KConstBusy = 2; |
|
29 const TInt KConstAway = 3; |
|
30 const TInt KConstServiceOut = 4; |
|
31 const TInt KConstOnPhone = 5; |
|
32 const TInt KConstInvisible = 6; |
|
33 const TInt KConstOffline = 7; |
|
34 const TInt KConstCallForward = 8; |
|
35 const TInt KConstUnknown = 9; |
|
36 |
|
37 // local functions |
|
38 // sorting algorithms, you can add new one to extend functionality |
|
39 |
|
40 |
|
41 // Compares alphabetically using MVIMPSTStorageContact::Identification and |
|
42 // TDesC::CompareC |
|
43 TInt CompareAlphabetically( const MVIMPSTStorageContact& aFirst, |
|
44 const MVIMPSTStorageContact& aSecond ) |
|
45 { |
|
46 return aFirst.Name().CompareC( aSecond.Name(), |
|
47 KCollationLevel, NULL ); |
|
48 } |
|
49 |
|
50 |
|
51 // Group the contact by it's status. |
|
52 TInt OrderContact( const MVIMPSTStorageContact& aContact ) |
|
53 { |
|
54 // Contact is not blocked, group by presence status |
|
55 switch( aContact.OnlineStatus() ) |
|
56 { |
|
57 case TVIMPSTEnums::EOnline: |
|
58 { |
|
59 return KConstOnline; // First group, contact is online |
|
60 } |
|
61 case TVIMPSTEnums::EDoNotDisturb: |
|
62 case TVIMPSTEnums::EBusy: |
|
63 { |
|
64 return KConstBusy; // second group, contact is busy/donotdisturb |
|
65 } |
|
66 case TVIMPSTEnums::EOnPhone: |
|
67 { |
|
68 return KConstOnPhone; //fifth group, contact is onphone |
|
69 } |
|
70 case TVIMPSTEnums::EInvisible: |
|
71 { |
|
72 return KConstInvisible; // sixth group, contact is invisible |
|
73 } |
|
74 case TVIMPSTEnums::EAway: |
|
75 { |
|
76 return KConstAway; // third group, contact is away |
|
77 } |
|
78 case TVIMPSTEnums::EOffline: |
|
79 { |
|
80 return KConstOffline; // seventh group, contact is offline |
|
81 } |
|
82 case TVIMPSTEnums::ECallForward: |
|
83 { |
|
84 return KConstCallForward; // eight group, contact state is callforward |
|
85 } |
|
86 case TVIMPSTEnums::EServiceOut: |
|
87 { |
|
88 return KConstServiceOut; // fourth group, contact is serviceout(has ony pstn or msisdn number) |
|
89 } |
|
90 default: |
|
91 { |
|
92 return KConstUnknown; // unknown status |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 // Compares by contact status, -1 = aFirst is greater, 1 = aSecond is greater. |
|
98 TInt CompareByPresence( const MVIMPSTStorageContact& aFirst, |
|
99 const MVIMPSTStorageContact& aSecond ) |
|
100 { |
|
101 TInt firstGroup( OrderContact( aFirst ) ); |
|
102 TInt secondGroup( OrderContact( aSecond ) ); |
|
103 |
|
104 // If both contacts belong to same group, sort alphabetically |
|
105 if( firstGroup == secondGroup ) |
|
106 { |
|
107 return CompareAlphabetically( aFirst, aSecond ); |
|
108 } |
|
109 |
|
110 // Otherwise return the difference of groups |
|
111 return firstGroup - secondGroup; |
|
112 } |
|
113 |
|
114 // ================= MEMBER FUNCTIONS ======================= |
|
115 |
|
116 // Two-phased constructor. |
|
117 CVIMPSTStorageContactSorter* CVIMPSTStorageContactSorter::NewL() |
|
118 { |
|
119 CVIMPSTStorageContactSorter* self = new( ELeave ) CVIMPSTStorageContactSorter(); |
|
120 return self; |
|
121 } |
|
122 |
|
123 // Destructor |
|
124 CVIMPSTStorageContactSorter::~CVIMPSTStorageContactSorter() |
|
125 { |
|
126 TRACE( T_LIT("CVIMPSTStorageContactSorter::~CVIMPSTStorageContactSorter") ); |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------- |
|
130 // CVIMPSTStorageContactSorter::Sort( RPointerArray<MVIMPSTStorageContact>& aList ) |
|
131 // Sorts given array. |
|
132 // --------------------------------------------------------- |
|
133 // |
|
134 void CVIMPSTStorageContactSorter::Sort( RPointerArray< MVIMPSTStorageContact >& aList ) |
|
135 { |
|
136 TRACE( T_LIT("CVIMPSTStorageContactSorter::Sort() begin") ); |
|
137 aList.Sort( iAlgorithm ); |
|
138 TRACE( T_LIT("CVIMPSTStorageContactSorter::Sort() end") ); |
|
139 } |
|
140 |
|
141 // --------------------------------------------------------- |
|
142 // CVIMPSTStorageContactSorter::Compare( const MVIMPSTStorageContact& aFirst, |
|
143 // const MVIMPSTStorageContact& aSecond ) const |
|
144 // Get function pointer from inside of TLinearOrder and |
|
145 // execute the function. |
|
146 // --------------------------------------------------------- |
|
147 // |
|
148 TInt CVIMPSTStorageContactSorter::Compare( const MVIMPSTStorageContact& aFirst, |
|
149 const MVIMPSTStorageContact& aSecond ) const |
|
150 { |
|
151 return iAlgorithm.operator TGeneralLinearOrder()( &aFirst, &aSecond ); |
|
152 } |
|
153 |
|
154 // C++ default constructor can NOT contain any code, that |
|
155 // might leave. |
|
156 // Default algorithm is alphabetic. It is changed later in ConstructL. |
|
157 // |
|
158 CVIMPSTStorageContactSorter::CVIMPSTStorageContactSorter() : |
|
159 iAlgorithm( *CompareByPresence ) |
|
160 { |
|
161 } |
|
162 |
|
163 // --------------------------------------------------------- |
|
164 // CVIMPSTStorageContactSorter::SetSortAlgorithm() |
|
165 // Get setting item value and choose algorithm. |
|
166 // New algorithms can be added to this method. |
|
167 // --------------------------------------------------------- |
|
168 // |
|
169 void CVIMPSTStorageContactSorter::SetSortAlgorithm( |
|
170 MVIMPSTStorageContact::TSortAlgorithm aAlgorithm ) |
|
171 { |
|
172 switch( aAlgorithm ) |
|
173 { |
|
174 case MVIMPSTStorageContact::ECompareByPresence: |
|
175 { |
|
176 iAlgorithm = |
|
177 TLinearOrder< MVIMPSTStorageContact > ( *CompareByPresence ); |
|
178 break; |
|
179 } |
|
180 |
|
181 case MVIMPSTStorageContact::ECompareAlphabetically: // flowthrough |
|
182 default : |
|
183 { |
|
184 iAlgorithm = |
|
185 TLinearOrder< MVIMPSTStorageContact > ( *CompareAlphabetically ); |
|
186 break; |
|
187 } |
|
188 } |
|
189 } |
|
190 |
|
191 // --------------------------------------------------------- |
|
192 // CVIMPSTStorageContactSorter::InsertOrder() |
|
193 // Get setting item value and choose algorithm. |
|
194 // New algorithms can be added to this method. |
|
195 // --------------------------------------------------------- |
|
196 // |
|
197 TLinearOrder< MVIMPSTStorageContact >& CVIMPSTStorageContactSorter::InsertOrder() |
|
198 { |
|
199 return iAlgorithm; |
|
200 } |
|
201 |
|
202 // End of file |
|