|
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: command line parse utils |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 //debug |
|
20 #include "calendarui_debug.h" |
|
21 |
|
22 // INCLUDE FILES |
|
23 #include "calencmdlineparser.h" |
|
24 |
|
25 // ================= MEMBER FUNCTIONS ======================= |
|
26 |
|
27 // --------------------------------------------------------- |
|
28 // CCalenCmdLineParser::CCalenCmdLineParser |
|
29 // Constructor |
|
30 // (other items were commented in a header). |
|
31 // Status : Draft/Proposal/Approved |
|
32 // --------------------------------------------------------- |
|
33 // |
|
34 CCalenCmdLineParser::CCalenCmdLineParser() |
|
35 { |
|
36 TRACE_ENTRY_POINT; |
|
37 TRACE_EXIT_POINT; |
|
38 } |
|
39 |
|
40 // --------------------------------------------------------- |
|
41 // CCalenCmdLineParser::NewL |
|
42 // Static constructor |
|
43 // (other items were commented in a header). |
|
44 // Status : Draft/Proposal/Approved |
|
45 // --------------------------------------------------------- |
|
46 // |
|
47 CCalenCmdLineParser* CCalenCmdLineParser::NewL() |
|
48 { |
|
49 TRACE_ENTRY_POINT; |
|
50 |
|
51 CCalenCmdLineParser* self = new( ELeave ) CCalenCmdLineParser(); |
|
52 |
|
53 TRACE_EXIT_POINT; |
|
54 return self; |
|
55 } |
|
56 |
|
57 // --------------------------------------------------------- |
|
58 // CCalenCmdLineParser::~CCalenCmdLineParser |
|
59 // Destructor |
|
60 // (other items were commented in a header). |
|
61 // Status : Draft/Proposal/Approved |
|
62 // --------------------------------------------------------- |
|
63 // |
|
64 CCalenCmdLineParser::~CCalenCmdLineParser() |
|
65 { |
|
66 TRACE_ENTRY_POINT; |
|
67 |
|
68 iStrings.ResetAndDestroy(); |
|
69 delete iCommandLine; |
|
70 |
|
71 TRACE_EXIT_POINT; |
|
72 } |
|
73 |
|
74 // --------------------------------------------------------- |
|
75 // CCalenCmdLineParser::AddL |
|
76 // Add string to the list. In this case it can be descriptor. |
|
77 // (other items were commented in a header). |
|
78 // Status : Draft/Proposal/Approved |
|
79 // --------------------------------------------------------- |
|
80 // |
|
81 void CCalenCmdLineParser::AddL( const TDesC& aString ) |
|
82 { |
|
83 TRACE_ENTRY_POINT; |
|
84 |
|
85 if( aString.Length() ) |
|
86 { |
|
87 HBufC* b = aString.AllocLC(); |
|
88 iStrings.AppendL( b ); |
|
89 CleanupStack::Pop( b ); |
|
90 } |
|
91 |
|
92 TRACE_EXIT_POINT; |
|
93 } |
|
94 |
|
95 // --------------------------------------------------------- |
|
96 // CCalenCmdLineParser::ItemL |
|
97 // Return item at specific position |
|
98 // (other items were commented in a header). |
|
99 // Status : Draft/Proposal/Approved |
|
100 // --------------------------------------------------------- |
|
101 // |
|
102 TPtr CCalenCmdLineParser::ItemL( TInt aIndex ) |
|
103 { |
|
104 TRACE_ENTRY_POINT; |
|
105 |
|
106 if( aIndex >= iStrings.Count() ) |
|
107 { |
|
108 User::Leave( KErrArgument ); |
|
109 } |
|
110 |
|
111 TRACE_EXIT_POINT; |
|
112 return iStrings[aIndex]->Des(); |
|
113 } |
|
114 |
|
115 // --------------------------------------------------------- |
|
116 // CCalenCmdLineParser::ParseStringL |
|
117 // Parse given sting in to substrings which are added to the list |
|
118 // (other items were commented in a header). |
|
119 // Status : Draft/Proposal/Approved |
|
120 // --------------------------------------------------------- |
|
121 // |
|
122 void CCalenCmdLineParser::ParseStringL( const TDesC& aString, |
|
123 const TDesC& aDelimiter ) |
|
124 { |
|
125 TRACE_ENTRY_POINT; |
|
126 |
|
127 // create copy of string to be parsed |
|
128 HBufC* copy = aString.AllocLC(); |
|
129 |
|
130 // clean possible previous items |
|
131 iStrings.ResetAndDestroy(); |
|
132 |
|
133 TInt stop; |
|
134 |
|
135 while( ( stop = copy->Des().Find( aDelimiter ) ) != KErrNotFound ) |
|
136 { |
|
137 HBufC* current = HBufC::NewLC( stop ); |
|
138 current->Des().Copy( copy->Des().Ptr(), stop ); |
|
139 AddL( current->Des() ); |
|
140 CleanupStack::PopAndDestroy( current ); |
|
141 copy->Des().Delete( 0, stop + aDelimiter.Length() ); |
|
142 } |
|
143 |
|
144 AddL( copy->Des() ); // add the last parameter |
|
145 CleanupStack::PopAndDestroy( copy ); |
|
146 |
|
147 if( iStrings.Count() > ECalCmdMaxParamCount ) |
|
148 { |
|
149 User::Leave( KErrArgument ); |
|
150 } |
|
151 |
|
152 TRACE_EXIT_POINT; |
|
153 } |
|
154 |
|
155 // --------------------------------------------------------- |
|
156 // CCalenCmdLineParser::ParseCommandLineL |
|
157 // Parse commandline parameters and store in iStrings |
|
158 // (other items were commented in a header). |
|
159 // Status : Draft/Proposal/Approved |
|
160 // --------------------------------------------------------- |
|
161 // |
|
162 void CCalenCmdLineParser::ParseCommandLineL( const TDesC& aCommandLine ) |
|
163 { |
|
164 TRACE_ENTRY_POINT; |
|
165 |
|
166 iCmdLineParameters.iCommandType = 0; |
|
167 iCmdLineParameters.iLocalUid = 0; |
|
168 iCmdLineParameters.iFlag = 0; |
|
169 iCmdLineParameters.iTime = Time::NullTTime(); |
|
170 |
|
171 HBufC* buf = aCommandLine.AllocLC(); |
|
172 |
|
173 TPtrC spc = KSpace(); |
|
174 |
|
175 ParseStringL( buf->Des(), spc ); |
|
176 |
|
177 if( iStrings.Count() > ECalCmdMaxParamCount || !iStrings.Count() ) |
|
178 { |
|
179 User::Leave( KErrArgument ); |
|
180 } |
|
181 |
|
182 iCmdLineParameters.iCommandType = DesToCmdParamTypeL( ItemL(ECalCmdLineCmd) ); |
|
183 User::LeaveIfError( iCmdLineParameters.iCommandType ); |
|
184 |
|
185 // If we are NOT opening using a Local UID we need to add an extra (empty) |
|
186 // string into the array to keep the date parsing indexes correct eg: |
|
187 // MONTH 2006 11 15 |
|
188 // LUID 1 2006 11 15 |
|
189 if( ( iCmdLineParameters.iCommandType != EStartTypeUid ) && |
|
190 ( iCmdLineParameters.iCommandType != EStartTypeUidViewer )&& |
|
191 (iCmdLineParameters.iCommandType != EStartTypeUidAlarmViewer) && |
|
192 (iCmdLineParameters.iCommandType != EStartTypeUidAlarmViewerNoSnooze)) |
|
193 { |
|
194 // Create a string and add to array at position ECalCmdLineUid |
|
195 TBufC<ECalCmdLineUid> dummyBuf( KSpace ); |
|
196 HBufC* dummyString = dummyBuf.AllocLC(); |
|
197 iStrings.InsertL( dummyString, ECalCmdLineUid ); |
|
198 CleanupStack::Pop( dummyString ); |
|
199 } |
|
200 else |
|
201 { |
|
202 // Get the UID |
|
203 TLex uid; |
|
204 uid.Assign( ItemL( ECalCmdLineUid ) ); |
|
205 uid.Val( iCmdLineParameters.iLocalUid, EDecimal ); |
|
206 } |
|
207 |
|
208 if( ( iCmdLineParameters.iCommandType != EStartTypeNewMeetingRequest ) && |
|
209 ( iCmdLineParameters.iCommandType != EStartTypeNewMeeting ) && |
|
210 ( iCmdLineParameters.iCommandType != EStartTypeNewAnniv ) && |
|
211 ( iCmdLineParameters.iCommandType != EStartTypeNewTodo ) ) |
|
212 { |
|
213 // Create a string and add to array at position ECalCmdLineFlag |
|
214 TBufC<1> dummyBuf( KSpace ); |
|
215 HBufC* dummyString = dummyBuf.AllocLC(); |
|
216 iStrings.InsertL( dummyString, ECalCmdLineFlag ); |
|
217 CleanupStack::Pop( dummyString ); |
|
218 } |
|
219 else |
|
220 { |
|
221 // Get the flag. This flag is used whether to exit application on dialog close or not. |
|
222 TLex uid; |
|
223 uid.Assign( ItemL( ECalCmdLineFlag ) ); |
|
224 uid.Val( iCmdLineParameters.iFlag ); |
|
225 } |
|
226 |
|
227 if( ( iCmdLineParameters.iCommandType != EStartTypeUidViewer ) && |
|
228 ( iCmdLineParameters.iCommandType != EStartTypeUidAlarmViewer ) && |
|
229 ( iCmdLineParameters.iCommandType != EStartTypeUidAlarmViewerNoSnooze ) ) |
|
230 { |
|
231 // Create a string and add to array at position ECalCmdLineFlag |
|
232 TBufC<1> dummyBuf( KSpace ); |
|
233 HBufC* dummyString = dummyBuf.AllocLC(); |
|
234 iStrings.InsertL( dummyString, ECalCmdCalFileName ); |
|
235 CleanupStack::Pop( dummyString ); |
|
236 } |
|
237 else |
|
238 { |
|
239 // Get the Calen database name of the calendar entry. |
|
240 iCmdLineParameters.iCalenFileName = ItemL(ECalCmdCalFileName); |
|
241 } |
|
242 // Get the time if there is one |
|
243 if( iStrings.Count() > ECalCmdLineFlag ) |
|
244 { |
|
245 iCmdLineParameters.iTime = TimeFromParamsL(); |
|
246 } |
|
247 else if( iCmdLineParameters.iCommandType == EStartTypeDate ) |
|
248 { |
|
249 // time is needed for EStartTypeDate! |
|
250 User::Leave( KErrArgument ); |
|
251 } |
|
252 else |
|
253 { |
|
254 // Do nothing. |
|
255 // final else added to remove lint warning |
|
256 } |
|
257 |
|
258 CleanupStack::PopAndDestroy( buf ); |
|
259 |
|
260 TRACE_EXIT_POINT; |
|
261 } |
|
262 |
|
263 // --------------------------------------------------------- |
|
264 // CCalenCmdLineParser::TimeFromParamsL |
|
265 // Get time value from parameters |
|
266 // (other items were commented in a header). |
|
267 // --------------------------------------------------------- |
|
268 // |
|
269 TTime CCalenCmdLineParser::TimeFromParamsL() |
|
270 { |
|
271 TRACE_ENTRY_POINT; |
|
272 |
|
273 TLex lex; |
|
274 TInt year, month(1), day(1), hour(0), min(0), sec(0), msec(0); |
|
275 |
|
276 TInt argCount = iStrings.Count(); |
|
277 argCount = argCount - 2; |
|
278 switch( argCount ) |
|
279 { |
|
280 // Set as many time properties as we can find. |
|
281 case ECalCmdLineMicroSeconds: |
|
282 { |
|
283 // Add microseconds - unlikely anyone would bother with these though |
|
284 lex.Assign( ItemL( ECalCmdLineMicroSeconds ) ); |
|
285 lex.Val( msec ); |
|
286 } |
|
287 //lint -fallthrough |
|
288 case ECalCmdLineSeconds: |
|
289 { |
|
290 // Add seconds |
|
291 lex.Assign(ItemL( ECalCmdLineSeconds ) ); |
|
292 lex.Val( sec ); |
|
293 } |
|
294 //lint -fallthrough |
|
295 case ECalCmdLineMinute: |
|
296 { |
|
297 // Add minutes |
|
298 lex.Assign( ItemL( ECalCmdLineMinute ) ); |
|
299 lex.Val( min ); |
|
300 } |
|
301 //lint -fallthrough |
|
302 case ECalCmdLineHour: |
|
303 { |
|
304 // Add hours |
|
305 lex.Assign( ItemL( ECalCmdLineHour ) ); |
|
306 lex.Val( hour ); |
|
307 } |
|
308 //lint -fallthrough |
|
309 case ECalCmdLineDay: |
|
310 { |
|
311 // Add day |
|
312 lex.Assign( ItemL(ECalCmdLineDay ) ); |
|
313 lex.Val( day ); |
|
314 } |
|
315 //lint -fallthrough |
|
316 case ECalCmdLineMonth: |
|
317 { |
|
318 // Add month |
|
319 lex.Assign( ItemL( ECalCmdLineMonth ) ); |
|
320 lex.Val( month ); |
|
321 } |
|
322 //lint -fallthrough |
|
323 case ECalCmdLineYear: |
|
324 { |
|
325 // Add year |
|
326 lex.Assign( ItemL( ECalCmdLineYear ) ); |
|
327 lex.Val( year ); |
|
328 } |
|
329 break; |
|
330 |
|
331 default: |
|
332 // No time component found.... |
|
333 TRACE_EXIT_POINT; |
|
334 return Time::NullTTime(); |
|
335 } |
|
336 |
|
337 // prevent panic during TDateTime construction! |
|
338 month = month > 0 && month <= 12 ? month : 1; |
|
339 |
|
340 TInt maxDaysInMonth( Time::DaysInMonth( year, TMonth(month-1) ) ); |
|
341 day = day > 0 && day <= maxDaysInMonth ? day : 1; |
|
342 |
|
343 hour = hour >= 0 && hour <= 23 ? hour : 0; |
|
344 min = min >= 0 && min <= 59 ? min : 0; |
|
345 sec = sec >= 0 && sec <= 59 ? sec : 0; |
|
346 msec = msec >= 0 && msec <= 999999 ? msec : 0; |
|
347 |
|
348 TDateTime dt( year, TMonth( --month ), --day, hour, min, sec, msec ); |
|
349 |
|
350 TRACE_EXIT_POINT; |
|
351 return TTime( dt ); |
|
352 } |
|
353 |
|
354 // --------------------------------------------------------- |
|
355 // CCalenCmdLineParser::CommandLineParameters |
|
356 // Return commandline parameters |
|
357 // (other items were commented in a header). |
|
358 // Status : Draft/Proposal/Approved |
|
359 // --------------------------------------------------------- |
|
360 // |
|
361 TCalenCmdParameters CCalenCmdLineParser::CommandLineParameters() |
|
362 { |
|
363 TRACE_ENTRY_POINT; |
|
364 |
|
365 TRACE_EXIT_POINT; |
|
366 return iCmdLineParameters; |
|
367 } |
|
368 |
|
369 // --------------------------------------------------------- |
|
370 // CCalenCmdLineParser::DesToCmdParamTypeL |
|
371 // Convert descriptor to parameter type |
|
372 // (other items were commented in a header). |
|
373 // Status : Draft/Proposal/Approved |
|
374 // --------------------------------------------------------- |
|
375 // |
|
376 TInt CCalenCmdLineParser::DesToCmdParamTypeL( const TDesC& aCmdParamType ) |
|
377 { |
|
378 TRACE_ENTRY_POINT; |
|
379 |
|
380 HBufC* temp = aCmdParamType.AllocLC(); |
|
381 TPtr des = temp->Des(); |
|
382 des.UpperCase(); |
|
383 |
|
384 TInt res( 0 ); |
|
385 |
|
386 if( des.Find( KCommandDefault ) != KErrNotFound ) |
|
387 { |
|
388 res = EStartTypeToday; |
|
389 } |
|
390 else if( des.Find( KCommandNewMeetingRequest ) != KErrNotFound ) |
|
391 { |
|
392 res = EStartTypeNewMeetingRequest; |
|
393 } |
|
394 else if( des.Find( KCommandNewMeeting ) != KErrNotFound ) |
|
395 { |
|
396 res = EStartTypeNewMeeting; |
|
397 } |
|
398 else if( des.Find( KCommandNewAnniv ) != KErrNotFound ) |
|
399 { |
|
400 res = EStartTypeNewAnniv; |
|
401 } |
|
402 else if( des.Find( KCommandNewTodo ) != KErrNotFound ) |
|
403 { |
|
404 res = EStartTypeNewTodo; |
|
405 } |
|
406 else if( des.Find( KCommandViewer ) != KErrNotFound ) |
|
407 { |
|
408 res = EStartTypeUidViewer; |
|
409 } |
|
410 else if( des.Find(KCommandAlarmViewer) != KErrNotFound ) |
|
411 { |
|
412 res = EStartTypeUidAlarmViewer; |
|
413 } |
|
414 else if( des.Find(KCommandAlarmViewerNoSnooze) != KErrNotFound ) |
|
415 { |
|
416 res = EStartTypeUidAlarmViewerNoSnooze; |
|
417 } |
|
418 else if( des.Find( KCommandUid ) != KErrNotFound ) |
|
419 { |
|
420 res = EStartTypeUid; |
|
421 } |
|
422 else if( des.Find( KCommandTime ) != KErrNotFound ) |
|
423 { |
|
424 res = EStartTypeDate; |
|
425 } |
|
426 else if( des.Find( KCommandToDo ) != KErrNotFound ) |
|
427 { |
|
428 res = EStartTypeToDo; |
|
429 } |
|
430 else if( des.Find( KCommandDay ) != KErrNotFound ) |
|
431 { |
|
432 res = EStartTypeDay; |
|
433 } |
|
434 else if( des.Find( KCommandWeek ) != KErrNotFound ) |
|
435 { |
|
436 res = EStartTypeWeek; |
|
437 } |
|
438 else if( des.Find( KCommandMonth ) != KErrNotFound ) |
|
439 { |
|
440 res = EStartTypeMonth; |
|
441 } |
|
442 else if( des.Find( KCommandDefaultView ) != KErrNotFound ) |
|
443 { |
|
444 res = EStartTypeDefault; |
|
445 } |
|
446 else |
|
447 { |
|
448 res = -1; |
|
449 } |
|
450 |
|
451 CleanupStack::PopAndDestroy( temp ); |
|
452 |
|
453 TRACE_EXIT_POINT; |
|
454 return res; |
|
455 } |
|
456 |
|
457 // End of File |