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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "csvtsipuriparser.h" |
|
20 #include "svtphonenumbervalidator.h" |
|
21 |
|
22 _LIT( KColon, ":"); |
|
23 _LIT( KAtSign, "@"); |
|
24 _LIT( KLeftElbow, "<" ); |
|
25 _LIT( KRightElbow, ">" ); |
|
26 |
|
27 // ======== MEMBER FUNCTIONS ======== |
|
28 |
|
29 // --------------------------------------------------------------------------- |
|
30 // CSvtSipUriParser::CSvtSipUriParser |
|
31 // --------------------------------------------------------------------------- |
|
32 // |
|
33 CSvtSipUriParser::CSvtSipUriParser( TDomainPartClippingSetting aOptions ) |
|
34 : |
|
35 iOptions( aOptions ) |
|
36 { |
|
37 |
|
38 } |
|
39 |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // CSvtSipUriParser::ConstructL |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 void CSvtSipUriParser::ConstructL( const TDesC& aSipUri ) |
|
46 { |
|
47 ParseSipUriL( aSipUri ); |
|
48 } |
|
49 |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // CSvtSipUriParser::NewL |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 CSvtSipUriParser* CSvtSipUriParser::NewL( const TDesC& aSipUri, |
|
56 TDomainPartClippingSetting aOptions ) |
|
57 { |
|
58 CSvtSipUriParser* self = CSvtSipUriParser::NewLC( aSipUri, aOptions ); |
|
59 CleanupStack::Pop( self ); |
|
60 return self; |
|
61 } |
|
62 |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // CSvtSipUriParser::NewLC |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 CSvtSipUriParser* CSvtSipUriParser::NewLC( const TDesC& aSipUri, |
|
69 TDomainPartClippingSetting aOptions ) |
|
70 { |
|
71 CSvtSipUriParser* self = new( ELeave ) CSvtSipUriParser( aOptions ); |
|
72 CleanupStack::PushL( self ); |
|
73 self->ConstructL( aSipUri ); |
|
74 return self; |
|
75 } |
|
76 |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // CSvtSipUriParser::~CSvtSipUriParser |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 CSvtSipUriParser::~CSvtSipUriParser() |
|
83 { |
|
84 iUserName.Close(); |
|
85 iDomain.Close(); |
|
86 iDisplayName.Close(); |
|
87 } |
|
88 |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // CSvtSipUriParser::GetVoipAddress |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 TInt CSvtSipUriParser::GetVoipAddress( RBuf& aVoipAddress ) const |
|
95 { |
|
96 aVoipAddress = KNullDesC(); |
|
97 |
|
98 if ( IsAnonymousUri( iUserName ) ) |
|
99 { |
|
100 return KErrNone; |
|
101 } |
|
102 |
|
103 TInt result( KErrNone ); |
|
104 TInt addressLength = AddressLength( iUserName, iDomain, iOptions ); |
|
105 if ( aVoipAddress.MaxLength() < addressLength ) |
|
106 { |
|
107 result = aVoipAddress.ReAlloc( addressLength ); |
|
108 } |
|
109 |
|
110 if ( KErrNone == result ) |
|
111 { |
|
112 aVoipAddress.Append( iUserName ); |
|
113 |
|
114 if ( iUserName.Length() < addressLength ) |
|
115 { |
|
116 // domain part clipping isn't used |
|
117 aVoipAddress.Append( KAtSign() ); |
|
118 aVoipAddress.Append( iDomain ); |
|
119 } |
|
120 } |
|
121 |
|
122 return result; |
|
123 } |
|
124 |
|
125 |
|
126 // --------------------------------------------------------------------------- |
|
127 // CSvtSipUriParser::GetPhoneNumber |
|
128 // --------------------------------------------------------------------------- |
|
129 // |
|
130 TInt CSvtSipUriParser::GetPhoneNumber( RBuf& aPhoneNumber ) const |
|
131 { |
|
132 aPhoneNumber = KNullDesC(); |
|
133 |
|
134 TInt result( KErrNone ); |
|
135 if ( SvtPhoneNumberValidator::IsValidNumber( iUserName ) |
|
136 && ( ENoClipping != iOptions ) ) |
|
137 { |
|
138 if ( aPhoneNumber.MaxLength() < iUserName.Length() ) |
|
139 { |
|
140 result = aPhoneNumber.ReAlloc( iUserName.Length() ); |
|
141 } |
|
142 |
|
143 if ( KErrNone == result ) |
|
144 { |
|
145 aPhoneNumber.Append( iUserName ); |
|
146 } |
|
147 } |
|
148 |
|
149 return result; |
|
150 } |
|
151 |
|
152 |
|
153 // --------------------------------------------------------------------------- |
|
154 // CSvtSipUriParser::GetDisplayName |
|
155 // --------------------------------------------------------------------------- |
|
156 // |
|
157 TInt CSvtSipUriParser::GetDisplayName( RBuf& aDisplayName ) const |
|
158 { |
|
159 aDisplayName = KNullDesC(); |
|
160 |
|
161 TInt result( KErrNone ); |
|
162 if ( aDisplayName.MaxLength() < iDisplayName.Length() ) |
|
163 { |
|
164 result = aDisplayName.ReAlloc( iDisplayName.Length() ); |
|
165 } |
|
166 |
|
167 if ( KErrNone == result ) |
|
168 { |
|
169 aDisplayName = iDisplayName; |
|
170 } |
|
171 |
|
172 return result; |
|
173 } |
|
174 |
|
175 |
|
176 // --------------------------------------------------------------------------- |
|
177 // CSvtSipUriParser::ParseSipUriL |
|
178 // Accepted URI forms are: [schema:]user@host[:port] and |
|
179 // displayname <[schema:]user@host[:port]>. URI parameters and headers are |
|
180 // ripped off in MCE & SVP. |
|
181 // --------------------------------------------------------------------------- |
|
182 // |
|
183 void CSvtSipUriParser::ParseSipUriL( const TDesC& aSipUri ) |
|
184 { |
|
185 if ( IsAnonymousUri( aSipUri ) ) |
|
186 { |
|
187 ParsePrivateAddress(); |
|
188 } |
|
189 else |
|
190 { |
|
191 RBuf sipUri( aSipUri.AllocL() ); |
|
192 CleanupClosePushL( sipUri ); |
|
193 |
|
194 // remove trailing and leading white spaces from input uri |
|
195 sipUri.TrimAll(); |
|
196 ParseDisplayNameL( sipUri ); |
|
197 RemoveElbows( sipUri ); |
|
198 ParseUserNameL( sipUri ); |
|
199 ParseDomainL( sipUri ); |
|
200 |
|
201 CleanupStack::PopAndDestroy( &sipUri ); |
|
202 } |
|
203 } |
|
204 |
|
205 |
|
206 // --------------------------------------------------------------------------- |
|
207 // CSvtSipUriParser::ParseDisplayNameL |
|
208 // --------------------------------------------------------------------------- |
|
209 // |
|
210 void CSvtSipUriParser::ParseDisplayNameL( const TDesC& aSipUri ) |
|
211 { |
|
212 iDisplayName.Close(); |
|
213 |
|
214 TInt leftElbowIndex = aSipUri.Find( KLeftElbow ); |
|
215 TInt rightElbowIndex = aSipUri.Find( KRightElbow ); |
|
216 if ( ( 0 < leftElbowIndex ) && ( KErrNotFound != rightElbowIndex ) ) |
|
217 { |
|
218 // display name is available |
|
219 iDisplayName.Assign( aSipUri.Left( leftElbowIndex ).AllocL() ); |
|
220 |
|
221 // remove whitespace around display name |
|
222 iDisplayName.TrimAll(); |
|
223 |
|
224 // remove quatation marks around display name |
|
225 _LIT( KQuatationMark, "\""); |
|
226 if ( iDisplayName.Length() && iDisplayName[0] == '"' ) |
|
227 { |
|
228 iDisplayName.Delete( 0, KQuatationMark().Length() ); |
|
229 } |
|
230 |
|
231 if ( iDisplayName.Length() |
|
232 && iDisplayName[iDisplayName.Length() - 1] == '"') |
|
233 { |
|
234 iDisplayName.Delete( |
|
235 iDisplayName.Length() - 1, KQuatationMark().Length() ); |
|
236 } |
|
237 } |
|
238 } |
|
239 |
|
240 |
|
241 // --------------------------------------------------------------------------- |
|
242 // CSvtSipUriParser::RemoveElbows |
|
243 // --------------------------------------------------------------------------- |
|
244 // |
|
245 void CSvtSipUriParser::RemoveElbows( TDes& sipUri ) const |
|
246 { |
|
247 TInt leftElbowIndex = sipUri.Find( KLeftElbow ); |
|
248 if ( KErrNotFound != leftElbowIndex ) |
|
249 { |
|
250 sipUri.Delete( leftElbowIndex, KLeftElbow().Length() ); |
|
251 } |
|
252 |
|
253 TInt rightElbowIndex = sipUri.Find( KRightElbow ); |
|
254 if ( KErrNotFound != rightElbowIndex ) |
|
255 { |
|
256 sipUri.Delete( rightElbowIndex, KRightElbow().Length() ); |
|
257 } |
|
258 } |
|
259 |
|
260 |
|
261 // --------------------------------------------------------------------------- |
|
262 // CSvtSipUriParser::ParseUserNameL |
|
263 // --------------------------------------------------------------------------- |
|
264 // |
|
265 void CSvtSipUriParser::ParseUserNameL( const TDesC& aSipUri ) |
|
266 { |
|
267 // If uri does not specify schema, user name starts from the beginning of |
|
268 // the uri. Otherwise additional calculations are done. |
|
269 TInt userNameStartInd = 0; |
|
270 TInt indexOfColon = aSipUri.Find( KColon() ); |
|
271 if ( KErrNotFound != indexOfColon ) |
|
272 { |
|
273 userNameStartInd = aSipUri.Find( KColon() ) + KColon().Length(); |
|
274 __ASSERT_ALWAYS( KColon().Length() <= userNameStartInd, |
|
275 User::Leave( KErrArgument ) ); |
|
276 } |
|
277 |
|
278 TInt userNameEndInd = aSipUri.Find( KAtSign() ) - KAtSign().Length(); |
|
279 __ASSERT_ALWAYS( 0 <= userNameEndInd, User::Leave( KErrArgument ) ); |
|
280 |
|
281 TInt userNameLength = ( userNameEndInd - userNameStartInd ) + 1; |
|
282 __ASSERT_ALWAYS( 0 < userNameLength, User::Leave( KErrArgument ) ); |
|
283 |
|
284 iUserName.Close(); |
|
285 iUserName.Assign( aSipUri.Mid( userNameStartInd, userNameLength ).AllocL() ); |
|
286 } |
|
287 |
|
288 |
|
289 // --------------------------------------------------------------------------- |
|
290 // CSvtSipUriParser::ParseDomainL |
|
291 // --------------------------------------------------------------------------- |
|
292 // |
|
293 void CSvtSipUriParser::ParseDomainL( const TDesC& aSipUri ) |
|
294 { |
|
295 TInt hostNameStartInd = aSipUri.Find( KAtSign() ) + KAtSign().Length(); |
|
296 __ASSERT_ALWAYS( KAtSign().Length() <= hostNameStartInd, |
|
297 User::Leave( KErrArgument ) ); |
|
298 |
|
299 TInt hostNameEndInd = aSipUri.Length() - 1; |
|
300 TInt hostNameLength = ( hostNameEndInd - hostNameStartInd ) + 1; |
|
301 __ASSERT_ALWAYS( 0 < hostNameLength, User::Leave( KErrArgument ) ); |
|
302 |
|
303 iDomain.Close(); |
|
304 iDomain.Assign( aSipUri.Mid( hostNameStartInd, hostNameLength ).AllocL() ); |
|
305 } |
|
306 |
|
307 |
|
308 // --------------------------------------------------------------------------- |
|
309 // CSvtSipUriParser::ParsePrivateAddress |
|
310 // --------------------------------------------------------------------------- |
|
311 // |
|
312 void CSvtSipUriParser::ParsePrivateAddress() |
|
313 { |
|
314 iUserName.Close(); |
|
315 iUserName = KNullDesC(); |
|
316 |
|
317 iDomain.Close(); |
|
318 iDomain = KNullDesC(); |
|
319 } |
|
320 |
|
321 |
|
322 // --------------------------------------------------------------------------- |
|
323 // CSvtSipUriParser::AddressLength |
|
324 // --------------------------------------------------------------------------- |
|
325 // |
|
326 TInt CSvtSipUriParser::AddressLength( const TDesC& aUserName, |
|
327 const TDesC& aDomain, TDomainPartClippingSetting aClipSetting ) const |
|
328 { |
|
329 TInt addressLength( 0 ); |
|
330 TBool isPhoneNumber( SvtPhoneNumberValidator::IsValidNumber( aUserName ) ); |
|
331 switch ( aClipSetting ) |
|
332 { |
|
333 case ENoClipping: |
|
334 addressLength = |
|
335 aUserName.Length() + KAtSign().Length() + aDomain.Length(); |
|
336 break; |
|
337 |
|
338 case EClipDomainIfNumber: |
|
339 addressLength = isPhoneNumber |
|
340 ? aUserName.Length() |
|
341 : aUserName.Length() + KAtSign().Length() + aDomain.Length(); |
|
342 break; |
|
343 |
|
344 case EClipDomain: |
|
345 addressLength = aUserName.Length(); |
|
346 break; |
|
347 |
|
348 default: |
|
349 ASSERT( EFalse ); |
|
350 } |
|
351 |
|
352 return addressLength; |
|
353 } |
|
354 |
|
355 |
|
356 // --------------------------------------------------------------------------- |
|
357 // CSvtSipUriParser::IsAnonymousUri |
|
358 // --------------------------------------------------------------------------- |
|
359 // |
|
360 TBool CSvtSipUriParser::IsAnonymousUri( const TDesC& aCandidate ) const |
|
361 { |
|
362 if ( aCandidate.Length() == 0 ) |
|
363 { |
|
364 return ETrue; |
|
365 } |
|
366 else |
|
367 { |
|
368 return EFalse; |
|
369 } |
|
370 } |
|