|
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 * Phonebook contact name formatting base |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "CPbkContactNameFormatterBase.h" |
|
22 #include <cntviewbase.h> |
|
23 #include <PbkEngUtils.h> |
|
24 #include "MPbkFieldData.h" |
|
25 #include "PbkContactNameFormattingUtil.h" |
|
26 |
|
27 |
|
28 namespace { |
|
29 |
|
30 // LOCAL CONSTANTS AND MACROS |
|
31 |
|
32 #ifdef _DEBUG |
|
33 enum TPanicCode |
|
34 { |
|
35 EPanic_InvalidNameFormat = 1, |
|
36 EPanicPostCond_GetContactTitleOrNullL, |
|
37 EPanicPostCond_GetContactTitle, |
|
38 EPanicPreCond_PbkFieldAt, |
|
39 EPanicPreCond_PbkFieldTime |
|
40 }; |
|
41 #endif |
|
42 |
|
43 enum TTitleType |
|
44 { |
|
45 ETitleTypeNormal = 1, |
|
46 ETitleTypeList |
|
47 }; |
|
48 |
|
49 |
|
50 // ==================== LOCAL FUNCTIONS ==================== |
|
51 |
|
52 #ifdef _DEBUG |
|
53 void Panic(TInt aReason) |
|
54 { |
|
55 _LIT(KPanicText, "CPbkContactNameFormatterBase"); |
|
56 User::Panic(KPanicText, aReason); |
|
57 } |
|
58 #endif |
|
59 |
|
60 // MODULE DATA STRUCTURES |
|
61 |
|
62 } // namespace |
|
63 |
|
64 |
|
65 // ==================== MEMBER FUNCTIONS ==================== |
|
66 |
|
67 CPbkContactNameFormatterBase::CPbkContactNameFormatterBase() |
|
68 { |
|
69 // CBase::operator new(TLeave) resets all members |
|
70 } |
|
71 |
|
72 void CPbkContactNameFormatterBase::BaseConstructL |
|
73 (const TDesC& aUnnamedText) |
|
74 { |
|
75 iUnnamedText = aUnnamedText.AllocL(); |
|
76 } |
|
77 |
|
78 CPbkContactNameFormatterBase::~CPbkContactNameFormatterBase() |
|
79 { |
|
80 delete iUnnamedText; |
|
81 } |
|
82 |
|
83 HBufC* CPbkContactNameFormatterBase::GetContactTitleOrNullL |
|
84 (const MPbkFieldDataArray& aContactData) const |
|
85 { |
|
86 // Calculate needed buffer length |
|
87 const TInt titleLength = ContactTitleLength(aContactData); |
|
88 if (titleLength > 0) |
|
89 { |
|
90 // Allocate buffer |
|
91 HBufC* titleHeap = HBufC::NewL(titleLength); |
|
92 TPtr title(titleHeap->Des()); |
|
93 // Format title to buffer |
|
94 GetContactTitle(aContactData, title); |
|
95 |
|
96 __ASSERT_DEBUG |
|
97 (titleHeap && |
|
98 titleHeap->Length() == titleLength && |
|
99 !PbkEngUtils::IsEmptyOrAllSpaces(*titleHeap), |
|
100 Panic(EPanicPostCond_GetContactTitleOrNullL)); |
|
101 return titleHeap; |
|
102 } |
|
103 |
|
104 return NULL; |
|
105 } |
|
106 |
|
107 HBufC* CPbkContactNameFormatterBase::GetContactTitleL |
|
108 (const MPbkFieldDataArray& aContactData) const |
|
109 { |
|
110 HBufC* title = GetContactTitleOrNullL(aContactData); |
|
111 if (!title) |
|
112 { |
|
113 title = iUnnamedText->AllocL(); |
|
114 } |
|
115 __ASSERT_DEBUG |
|
116 (title && |
|
117 (!PbkEngUtils::IsEmptyOrAllSpaces(*title) || *title==*iUnnamedText), |
|
118 Panic(EPanicPostCond_GetContactTitle)); |
|
119 return title; |
|
120 } |
|
121 |
|
122 const TDesC& CPbkContactNameFormatterBase::UnnamedText() const |
|
123 { |
|
124 return (*iUnnamedText); |
|
125 } |
|
126 |
|
127 inline TInt CPbkContactNameFormatterBase::TrimNameLength |
|
128 (TInt aTitleType, |
|
129 const TDesC& aName) const |
|
130 { |
|
131 TInt result = 0; |
|
132 if (aTitleType == ETitleTypeList) |
|
133 { |
|
134 result += PbkEngUtils::TrimRightLength(aName); |
|
135 } |
|
136 else |
|
137 { |
|
138 result += PbkEngUtils::TrimAllLength(aName); |
|
139 } |
|
140 return result; |
|
141 } |
|
142 |
|
143 inline TInt CPbkContactNameFormatterBase::CalculateContactTitleLength |
|
144 (const MPbkFieldDataArray& aContactData, |
|
145 TInt aTitleType) const |
|
146 { |
|
147 TInt result = 0; |
|
148 |
|
149 // This algorithm is sort order independent, |
|
150 // the length is not dependent of the name ordering |
|
151 const TDesC& lastName = PbkContactNameFormattingUtil::FieldText( |
|
152 aContactData, EPbkFieldIdLastName); |
|
153 |
|
154 // The trimming depends on needed title type |
|
155 result += TrimNameLength(aTitleType, lastName); |
|
156 |
|
157 const TDesC& firstName = PbkContactNameFormattingUtil::FieldText( |
|
158 aContactData, EPbkFieldIdFirstName); |
|
159 if (RequiresSpaceBetweenNames(lastName,firstName)) |
|
160 { |
|
161 ++result; // count space |
|
162 } |
|
163 |
|
164 result += TrimNameLength(aTitleType, firstName); |
|
165 |
|
166 if (result == 0) |
|
167 { |
|
168 const TDesC& companyName = PbkContactNameFormattingUtil::FieldText( |
|
169 aContactData, EPbkFieldIdCompanyName); |
|
170 result += TrimNameLength(aTitleType, companyName); |
|
171 } |
|
172 |
|
173 return result; |
|
174 } |
|
175 |
|
176 TInt CPbkContactNameFormatterBase::ContactTitleLength |
|
177 (const MPbkFieldDataArray& aContactData) const |
|
178 { |
|
179 return CalculateContactTitleLength(aContactData, ETitleTypeNormal); |
|
180 } |
|
181 |
|
182 |
|
183 TInt CPbkContactNameFormatterBase::ContactListTitleLength |
|
184 (const MPbkFieldDataArray& aContactData) const |
|
185 { |
|
186 return CalculateContactTitleLength(aContactData, ETitleTypeList); |
|
187 } |
|
188 |
|
189 |
|
190 TBool CPbkContactNameFormatterBase::IsTitleField |
|
191 (TPbkFieldId aFieldId) const |
|
192 { |
|
193 switch (aFieldId) |
|
194 { |
|
195 case EPbkFieldIdLastName: // FALLTHROUGH |
|
196 case EPbkFieldIdFirstName: // FALLTHROUGH |
|
197 case EPbkFieldIdCompanyName: |
|
198 { |
|
199 return ETrue; |
|
200 } |
|
201 default: |
|
202 { |
|
203 return EFalse; |
|
204 } |
|
205 } |
|
206 } |
|
207 |
|
208 HBufC* CPbkContactNameFormatterBase::FormatNameDescriptorL |
|
209 (const TDesC& aTitle) const |
|
210 { |
|
211 HBufC* formattedNameBuf = aTitle.AllocL(); |
|
212 TPtr formattedName(formattedNameBuf->Des()); |
|
213 formattedName.TrimAll(); |
|
214 return formattedNameBuf; |
|
215 } |
|
216 |
|
217 /** |
|
218 * Returns true if a space is needed between aLastName and aFirstName in the |
|
219 * formatted name. |
|
220 */ |
|
221 TBool CPbkContactNameFormatterBase::RequiresSpaceBetweenNames |
|
222 (const TDesC& aLastName, |
|
223 const TDesC& aFirstName) const |
|
224 { |
|
225 return (aLastName.Length() > 0 && aFirstName.Length() > 0); |
|
226 } |
|
227 |
|
228 // End of File |