|
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 * Implementation for number formatting |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <NumberGrouping.h> |
|
22 #include <AknUtils.h> |
|
23 |
|
24 #include "CPhoneNumberFormat.h" |
|
25 #include "LogsConstants.hrh" |
|
26 |
|
27 #include <featmgr.h> |
|
28 #include <bldvariant.hrh> // IDs |
|
29 |
|
30 #include <centralrepository.h> |
|
31 #include <NumberGroupingCRKeys.h> |
|
32 |
|
33 |
|
34 // EXTERNAL DATA STRUCTURES |
|
35 |
|
36 // EXTERNAL FUNCTION PROTOTYPES |
|
37 |
|
38 // CONSTANTS |
|
39 |
|
40 const TInt KDTMF1 ='w'; |
|
41 const TInt KDTMF2 ='W'; |
|
42 const TInt KDTMF3 ='p'; |
|
43 const TInt KDTMF4 ='P'; |
|
44 |
|
45 const TInt KSS1 ='#'; |
|
46 const TInt KSS2 ='*'; |
|
47 |
|
48 // CONSTANTS FOR DEBUGGING |
|
49 #ifdef _DEBUG |
|
50 _LIT( KPanic, "CPhoneNumberFormat"); |
|
51 #endif |
|
52 |
|
53 // MACROS |
|
54 |
|
55 // LOCAL CONSTANTS AND MACROS |
|
56 |
|
57 // MODULE DATA STRUCTURES |
|
58 |
|
59 // LOCAL FUNCTION PROTOTYPES |
|
60 |
|
61 // ================= MEMBER FUNCTIONS ======================= |
|
62 |
|
63 /** |
|
64 * Class used for phone number formatting |
|
65 */ |
|
66 |
|
67 |
|
68 |
|
69 CPhoneNumberFormat* CPhoneNumberFormat::NewL() |
|
70 { |
|
71 CPhoneNumberFormat* self = new ( ELeave ) CPhoneNumberFormat(); |
|
72 CleanupStack::PushL( self ); |
|
73 self->ConstructL(); |
|
74 CleanupStack::Pop( self ); // self |
|
75 return self; |
|
76 } |
|
77 |
|
78 |
|
79 // C++ default constructor cannot contain any code that might leave |
|
80 CPhoneNumberFormat::CPhoneNumberFormat() |
|
81 { |
|
82 } |
|
83 |
|
84 |
|
85 // Destructor |
|
86 CPhoneNumberFormat::~CPhoneNumberFormat() |
|
87 { |
|
88 if (iNumberGrouping) |
|
89 { |
|
90 delete iNumberGrouping; |
|
91 } |
|
92 } |
|
93 |
|
94 |
|
95 // Constructor for the recent views, second phase |
|
96 void CPhoneNumberFormat::ConstructL() |
|
97 { |
|
98 CRepository* repository = NULL; |
|
99 TInt value( 0 ); |
|
100 TRAPD( err, repository = CRepository::NewL( KCRUidNumberGrouping ) ); |
|
101 |
|
102 if( err == KErrNone ) |
|
103 { |
|
104 err = repository->Get( KNumberGrouping, value ); |
|
105 } |
|
106 delete repository; |
|
107 |
|
108 if( value == 1 ) |
|
109 { |
|
110 iNumberGrouping = CPNGNumberGrouping::NewL( KLogsPhoneNumberMaxLen ); |
|
111 } |
|
112 else |
|
113 { |
|
114 iNumberGrouping = NULL; |
|
115 } |
|
116 } |
|
117 |
|
118 |
|
119 TFormatResult CPhoneNumberFormat::DescriptorCopy |
|
120 ( const TDesC& aSource |
|
121 , TDes& aDestination |
|
122 ) |
|
123 { |
|
124 TFormatResult rc( EResultOK ); |
|
125 |
|
126 if( aDestination.MaxLength() < aSource.Length() ) |
|
127 { |
|
128 rc = EResultTruncated; |
|
129 aDestination = aSource.Right( aSource.Length() - |
|
130 ( aSource.Length() - aDestination.MaxLength() ) |
|
131 ); |
|
132 } |
|
133 else |
|
134 { |
|
135 aDestination = aSource; |
|
136 } |
|
137 |
|
138 return rc; |
|
139 } |
|
140 |
|
141 |
|
142 TFormatResult CPhoneNumberFormat::PhoneNumberFormat |
|
143 ( TPtrC aNumberToFormat |
|
144 , TDes& aFormattedString |
|
145 ) |
|
146 { |
|
147 TFormatResult rc( EResultOK ); |
|
148 |
|
149 if( iNumberGrouping ) |
|
150 { |
|
151 TBuf<KLogsPhoneNumberMaxLen> buf; |
|
152 rc = DescriptorCopy( aNumberToFormat, buf ); //Src, dest |
|
153 AknTextUtils::LanguageSpecificNumberConversion( buf ); |
|
154 iNumberGrouping->Set( buf ); |
|
155 |
|
156 rc = DescriptorCopy( iNumberGrouping->FormattedNumber(), |
|
157 aFormattedString); |
|
158 } |
|
159 else |
|
160 { |
|
161 rc = DescriptorCopy( aNumberToFormat, aFormattedString ); //Src, dest |
|
162 AknTextUtils::LanguageSpecificNumberConversion( aFormattedString ); |
|
163 } |
|
164 |
|
165 return rc; |
|
166 } |
|
167 |
|
168 |
|
169 TFormatResult CPhoneNumberFormat::PhoneNumberFormat |
|
170 ( TPtrC aNumberToFormat |
|
171 , TDes& aFormattedString |
|
172 , const CFont* aFont |
|
173 , TInt aPixelWidth |
|
174 ) |
|
175 { |
|
176 TFormatResult rc = PhoneNumberFormat( aNumberToFormat, aFormattedString ); |
|
177 if( AknTextUtils::ClipToFit |
|
178 ( aFormattedString |
|
179 , *aFont |
|
180 , aPixelWidth |
|
181 , AknTextUtils::EClipFromBeginning ) ) |
|
182 { |
|
183 rc = EResultClipped; |
|
184 } |
|
185 |
|
186 return rc; |
|
187 } |
|
188 |
|
189 |
|
190 TFormatResult CPhoneNumberFormat::PhoneNumberFormat |
|
191 ( TPtrC aNumberToFormat |
|
192 , TDes& aFormattedString |
|
193 , AknTextUtils::TClipDirection aClipDirection |
|
194 , CEikFormattedCellListBox *aListBox |
|
195 , TInt aItemIndex |
|
196 , TInt aSubCellNumber |
|
197 ) |
|
198 { |
|
199 TFormatResult rc = PhoneNumberFormat( aNumberToFormat, aFormattedString ); |
|
200 if( AknTextUtils::ClipToFit |
|
201 ( aFormattedString |
|
202 , aClipDirection |
|
203 , aListBox |
|
204 , aItemIndex |
|
205 , aSubCellNumber |
|
206 ) ) |
|
207 { |
|
208 rc = EResultClipped; |
|
209 } |
|
210 |
|
211 return rc; |
|
212 } |
|
213 |
|
214 |
|
215 TFormatResult CPhoneNumberFormat::PhoneNumberFormat |
|
216 ( TPtrC aNumberToFormat |
|
217 , TDes& aFormattedString |
|
218 , AknTextUtils::TClipDirection aClipDirection |
|
219 , CEikColumnListBox *aListBox |
|
220 , TInt aItemIndex |
|
221 , TInt aColumnNumber |
|
222 ) |
|
223 { |
|
224 TFormatResult rc = PhoneNumberFormat( aNumberToFormat, aFormattedString ); |
|
225 if( AknTextUtils::ClipToFit |
|
226 ( aFormattedString |
|
227 , aClipDirection |
|
228 , aListBox |
|
229 , aItemIndex |
|
230 , aColumnNumber |
|
231 ) ) |
|
232 { |
|
233 rc = EResultClipped; |
|
234 } |
|
235 |
|
236 return rc; |
|
237 } |
|
238 |
|
239 TInt CPhoneNumberFormat::IsDtmf( TChar aChar ) |
|
240 { |
|
241 return aChar == KDTMF1 || aChar == KDTMF2 || aChar == KDTMF3 || aChar == KDTMF4; |
|
242 } |
|
243 |
|
244 TInt CPhoneNumberFormat::IsSS( TChar aChar ) |
|
245 { |
|
246 return aChar == KSS1 || aChar == KSS2; |
|
247 } |
|
248 |
|
249 TInt CPhoneNumberFormat::DTMFStrip( |
|
250 TPtrC aNumberToFormat, |
|
251 TDes& aFormattedString ) |
|
252 { |
|
253 |
|
254 // __ASSERT_DEBUG( aFormattedString.MaxLength() >= aNumberToFormat.Length(), User::Panic(KPanic, KErrCorrupt)); |
|
255 |
|
256 TInt start = 0; |
|
257 if ( aFormattedString.MaxLength() < aNumberToFormat.Length() ) |
|
258 { |
|
259 start = aNumberToFormat.Length() - aFormattedString.MaxLength(); |
|
260 } |
|
261 |
|
262 aFormattedString.Zero(); |
|
263 TBool ss(EFalse); |
|
264 TBool dtmf(EFalse); |
|
265 TChar prevChar(' '); |
|
266 for( TInt i = start ; i < aNumberToFormat.Length() && !dtmf ; i ++ ) |
|
267 { |
|
268 dtmf = IsDtmf( aNumberToFormat[i] ); |
|
269 if( IsSS( aNumberToFormat[i] ) && !IsSS(prevChar) ) |
|
270 { |
|
271 ss ? ss = EFalse: ss = ETrue; |
|
272 } |
|
273 |
|
274 if( !dtmf && !ss && !IsSS(aNumberToFormat[i])) |
|
275 { |
|
276 aFormattedString.Append(aNumberToFormat[i] ); |
|
277 } |
|
278 prevChar = aNumberToFormat[i]; |
|
279 } |
|
280 if( aFormattedString.Length() == 0 && (dtmf || ss) ) |
|
281 { // fallback, full string shown since invalid phone number. |
|
282 aFormattedString = aNumberToFormat; |
|
283 return KErrArgument; |
|
284 } |
|
285 return KErrNone; |
|
286 } |
|
287 |
|
288 // End of File |