calendarengines/agnversit2/src/AgnParseRRule.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:   This file contains implementation of iCal parser which parses 
       
    15 *                the recurrence rule of iCal entry.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "AgnParseRRule.h"
       
    23 
       
    24 #include "calendarengines_debug.h"
       
    25 
       
    26 #include <calrrule.h>
       
    27 #include "ICalValue.h"		// For CICalValue
       
    28 
       
    29 /**
       
    30 Constructor
       
    31 
       
    32 @param aAgnImpUtil AgnVersit2Importer utility functions
       
    33 */
       
    34 CAgnParseRRule::CAgnParseRRule( MAgnImpUtil& aAgnImpUtil ) : 
       
    35                             iAgnImpUtil( aAgnImpUtil ),
       
    36                             iSetSegs( 0 )
       
    37     {
       
    38     TRACE_ENTRY_POINT;
       
    39     TRACE_EXIT_POINT;
       
    40     }
       
    41 
       
    42 
       
    43 /**
       
    44 Destructor
       
    45 */
       
    46 CAgnParseRRule::~CAgnParseRRule()
       
    47     {
       
    48     TRACE_ENTRY_POINT;
       
    49     TRACE_EXIT_POINT;
       
    50     }
       
    51 
       
    52 
       
    53 //=========================================================================================
       
    54 // Parsing functions 
       
    55 //=========================================================================================
       
    56 
       
    57 /**
       
    58 Parse the keywords and update RRule
       
    59 @param aRule recurrence rules
       
    60 @param aRecRules array of recurrence rule value segments.
       
    61 @param aStartTime The DTSTART of the parent component.
       
    62 @param aUid Entry Uid
       
    63 @param aRulevalues array of CICalValues
       
    64 @return True if parsed the keyword successfully
       
    65 */
       
    66 TBool CAgnParseRRule::ParseL( TCalRRule& aRule, RPointerArray<CICalRuleSegment>& aRecRules, 
       
    67                               const TCalTime& aStartTime, const TDesC8& aUid, 
       
    68                               const RPointerArray<CICalValue>& aRulevalues )
       
    69     {
       
    70     TRACE_ENTRY_POINT;
       
    71     
       
    72     aRule.SetDtStart( aStartTime );
       
    73 
       
    74 	TInt recCount = aRecRules.Count();
       
    75 	TBool parsed = EFalse;
       
    76 
       
    77     //Loop through the CICalRuleSegments
       
    78 	for (TInt rnum = 0; rnum < aRecRules.Count(); ++rnum)
       
    79 		{
       
    80 		parsed = EFalse;
       
    81 		
       
    82 		CICalRuleSegment* segment = aRecRules[rnum];
       
    83 		
       
    84 		if (segment->Values().Count() < 1)
       
    85 			{
       
    86 			iAgnImpUtil.ReportErrorL( MAgnImportObserver::EImpErrorMissingData, aUid, 
       
    87 			                          aRulevalues[0]->TextL() );
       
    88 			
       
    89 			// On continue, all we have is FREQ=DAILY.
       
    90 			}
       
    91 		
       
    92 		//Try to parse it with specific parser
       
    93 		TRAPD( error, parsed = DoParseL( *segment, aRule ) );
       
    94 		
       
    95 		//if the keyword was not parsed, try to parse it
       
    96 		if( error == KErrNone && !parsed )
       
    97 			{
       
    98 			//Update aRule with CICalRuleSegment			
       
    99 			switch (segment->Type())
       
   100 				{
       
   101 				case CICalRuleSegment::ESegFreq :
       
   102 					break;
       
   103 				
       
   104 				//Set Until
       
   105 				case CICalRuleSegment::ESegUntil :
       
   106 					{
       
   107 					SetRRuleUntilL( *segment, aRule );
       
   108 					break;
       
   109 					}
       
   110 				
       
   111 				//Set Count
       
   112 				case CICalRuleSegment::ESegCount :
       
   113 				    {
       
   114 				    aRule.SetCount(segment->Values()[0]->IntegerL());
       
   115 					break;
       
   116 				    }
       
   117 			    
       
   118 			    //Set Interval
       
   119 				case CICalRuleSegment::ESegInterval :
       
   120 				    {
       
   121 				    aRule.SetInterval(segment->Values()[0]->IntegerL());
       
   122 					break;
       
   123 				    }
       
   124 				
       
   125 				//Not supported keywords by freq, report error
       
   126 				case CICalRuleSegment::ESegByPos :		// not supported by freq, fall through
       
   127 				case CICalRuleSegment::ESegBySecond :	// not supported by freq, fall through
       
   128 				case CICalRuleSegment::ESegByMinute :	// not supported by freq, fall through
       
   129 				case CICalRuleSegment::ESegByHour :		// not supported by freq, fall through
       
   130 				case CICalRuleSegment::ESegByYearDay :	// not supported by freq, fall through
       
   131 				case CICalRuleSegment::ESegByWeekNo :	// not supported by freq, fall through
       
   132 				case CICalRuleSegment::ESegExtension :	// not supported by freq, fall through
       
   133 				case CICalRuleSegment::ESegWkSt :       // not supported by freq, fall through
       
   134 				case CICalRuleSegment::ESegByDay :      // not supported by freq, fall through
       
   135 				case CICalRuleSegment::ESegByMonthDay : // not supported by freq, fall through
       
   136 				case CICalRuleSegment::ESegByMonth :    // not supported by freq, fall through
       
   137 				default:
       
   138 				    {
       
   139 				    iAgnImpUtil.ReportErrorL( MAgnImportObserver::EImpErrorNotSupported, aUid, 
       
   140 				                              aRulevalues[0]->TextL() );
       
   141 				    }
       
   142 				}
       
   143 			}
       
   144 		    
       
   145 	    //TRAP returned error
       
   146 	    else if( error != KErrNone ) 
       
   147 	        {
       
   148 	        if( error == KErrAbort )//KErrAbort, report error and stop parsing
       
   149 	            {
       
   150 	            TRACE_EXIT_POINT;
       
   151 	            iAgnImpUtil.ReportErrorL( MAgnImportObserver::EImpErrorNotSupported, aUid, 
       
   152 	                                      aRulevalues[0]->TextL(), EFalse );
       
   153 	            }
       
   154 	        else //Otherwise leave with error
       
   155 	            {
       
   156 	            TRACE_EXIT_POINT;
       
   157 	            User::Leave( error );
       
   158 	            }
       
   159 	        }
       
   160 		
       
   161 		}//for end
       
   162 	
       
   163 	
       
   164 	//Finalize parsing	
       
   165 	parsed = FinalizeParsingL( aRule, aStartTime );
       
   166     
       
   167     TRACE_EXIT_POINT;
       
   168 	return parsed;
       
   169 	}
       
   170  
       
   171 
       
   172 /**
       
   173 Parse the keywords and update RRule
       
   174 @param aSegment recurrence rule value segment.
       
   175 @param aRule recurrence rules 
       
   176 @return true if parsed the keyword successfully
       
   177 */
       
   178 TBool CAgnParseRRule::DoParseL( CICalRuleSegment& /*aSegment*/, TCalRRule& /*aRule*/ ) 
       
   179     {
       
   180     TRACE_ENTRY_POINT;
       
   181     
       
   182     return EFalse;
       
   183     TRACE_EXIT_POINT;
       
   184     }
       
   185         
       
   186 /**
       
   187 Finalize the parsing
       
   188 @param aRule recurrence rules 
       
   189 @param aStartTime The DTSTART of the parent component.
       
   190 @return true if finalized parsing successfully
       
   191 */
       
   192 TBool CAgnParseRRule::FinalizeParsingL( TCalRRule& aRule, const TCalTime& /*aStartTime*/ )
       
   193     {
       
   194     TRACE_ENTRY_POINT;
       
   195     
       
   196     TBool finalized = ETrue;
       
   197     
       
   198     // Agenda requires that all RRULES have an interval of some sort.
       
   199 	// An interval of 1 is the default.
       
   200 	if (aRule.Interval() < 1)
       
   201 		{
       
   202 		aRule.SetInterval(1);
       
   203 		}
       
   204 
       
   205 	TRACE_EXIT_POINT;
       
   206 	return finalized;
       
   207 	}   
       
   208 
       
   209 
       
   210 
       
   211 //=========================================================================================
       
   212 // Following functions updates the TCalRRule
       
   213 //========================================================================================= 
       
   214 
       
   215 /**
       
   216 Update TCalRRule with Until value
       
   217 @param aSegment recurrence rule value segment.
       
   218 @param aRule recurrence rules 
       
   219 @return true if aRule updated successfully
       
   220 */
       
   221 TBool CAgnParseRRule::SetRRuleUntilL( const CICalRuleSegment& aSegment, TCalRRule& aRule )
       
   222     {
       
   223     TRACE_ENTRY_POINT;
       
   224     
       
   225 	TTime time;
       
   226 	TCalTime calTime;
       
   227 	
       
   228 	 // adding time and date information to the Repeat Until field
       
   229   CICalValue::TTimeZoneType type = CICalValue::EUtcTime;
       
   230 	aSegment.Values()[0]->GetDateTimeL(time, type);
       
   231 
       
   232 	calTime.SetTimeUtcL(time);
       
   233 	aRule.SetUntil(calTime);
       
   234 	
       
   235 	TRACE_EXIT_POINT;
       
   236 	return ETrue;
       
   237     }
       
   238 
       
   239 /**
       
   240 Update TCalRRule with week start value
       
   241 @param aSegment recurrence rule value segment.
       
   242 @param aRule recurrence rules 
       
   243 @return true if aRule updated successfully
       
   244 */
       
   245 TBool CAgnParseRRule::SetRRuleWkStL( const CICalRuleSegment& aSegment, TCalRRule& aRule )
       
   246     {
       
   247     TRACE_ENTRY_POINT;
       
   248     
       
   249     TDay dayval;
       
   250     TInt daypos;
       
   251     aSegment.Values()[0]->GetDayL(dayval, daypos);
       
   252     aRule.SetWkSt(dayval);
       
   253     //iSetSegs |= ESegFlagByWkSt; <-- Does not exist, a bug?
       
   254     
       
   255     TRACE_EXIT_POINT;
       
   256     return ETrue;
       
   257     }
       
   258     
       
   259 /**
       
   260 Update TCalRRule with by days
       
   261 @param aSegment recurrence rule value segment.
       
   262 @param aRule recurrence rules 
       
   263 @return true if aRule updated successfully
       
   264 */
       
   265 TBool CAgnParseRRule::SetRRuleByDayL( const CICalRuleSegment& aSegment, TCalRRule& aRule )
       
   266     {
       
   267     TRACE_ENTRY_POINT;
       
   268     
       
   269     RArray<TCalRRule::TDayOfMonth> daysOfMonth;
       
   270 	CleanupClosePushL(daysOfMonth);
       
   271 
       
   272 	const RPointerArray<CICalValue>& values = aSegment.Values();
       
   273 
       
   274 	TInt valCount = values.Count();
       
   275 	TDay dayval = EMonday;
       
   276 	TInt daypos = 0;
       
   277 
       
   278 	for (TInt day = 0; day < valCount; ++day)
       
   279 		{
       
   280 		values[day]->GetDayL(dayval, daypos);
       
   281 		if ((daypos == -1) || ((daypos >= 1) && (daypos <= KMaxWeekDayNum)))
       
   282 			{
       
   283 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, daypos)));
       
   284 			}
       
   285 		else if (daypos == 0)
       
   286 			//BYDAY applies to every day in month
       
   287 			{
       
   288 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, 1)));
       
   289 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, 2)));
       
   290 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, 3)));
       
   291 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, 4)));
       
   292 			User::LeaveIfError(daysOfMonth.Append(TCalRRule::TDayOfMonth(dayval, -1)));
       
   293 			}
       
   294 		else
       
   295 			{
       
   296 			//Invalid day - unrecoverable
       
   297 			User::Leave( KErrAbort );
       
   298 			}
       
   299 		}
       
   300 
       
   301 	aRule.SetByDay(daysOfMonth);
       
   302 	CleanupStack::PopAndDestroy(&daysOfMonth);
       
   303 	iSetSegs |= ESegFlagByDay;
       
   304 	
       
   305 	TRACE_EXIT_POINT;
       
   306 	return ETrue;
       
   307     }
       
   308 
       
   309 /**
       
   310 Update TCalRRule with by month
       
   311 @param aSegment recurrence rule value segment.
       
   312 @param aRule recurrence rules 
       
   313 @return true if aRule updated successfully
       
   314 */
       
   315 TBool CAgnParseRRule::SetRRuleByMonthL( const CICalRuleSegment& aSegment, TCalRRule& aRule )
       
   316     {
       
   317     TRACE_ENTRY_POINT;
       
   318     
       
   319     RArray<TMonth> theMonths;
       
   320 	CleanupClosePushL(theMonths);
       
   321 	const RPointerArray<CICalValue>& values = aSegment.Values();
       
   322 	TInt valCount = values.Count();
       
   323 
       
   324 	for (TInt month = 0; month < valCount; ++month)
       
   325 		{
       
   326 		User::LeaveIfError(theMonths.Append(values[month]->MonthL()));
       
   327 		}
       
   328 
       
   329 	iSetSegs |= ESegFlagByMonth;
       
   330 	aRule.SetByMonth(theMonths);
       
   331 	CleanupStack::PopAndDestroy(&theMonths);
       
   332 	
       
   333 	TRACE_EXIT_POINT;
       
   334 	return ETrue;
       
   335     }
       
   336         
       
   337 
       
   338 /**
       
   339 Update TCalRRule with by month day
       
   340 @param aSegment recurrence rule value segment.
       
   341 @param aRule recurrence rules 
       
   342 @return true if aRule updated successfully
       
   343 */
       
   344 TBool CAgnParseRRule::SetRRuleByMonthDayL( const CICalRuleSegment& aSegment, TCalRRule& aRule )
       
   345     {
       
   346     TRACE_ENTRY_POINT;
       
   347     
       
   348     RArray<TInt> days;
       
   349 	CleanupClosePushL(days);
       
   350 	const RPointerArray<CICalValue>& values = aSegment.Values();
       
   351 	TInt valCount = values.Count();
       
   352 
       
   353 	for (TInt day = 0; day < valCount; ++day)
       
   354 		{
       
   355 		//dates are represented as day - 1 (0 = 1) in TDateTime
       
   356 		User::LeaveIfError(days.Append(values[day]->IntegerL() - 1 ));
       
   357 		}
       
   358 
       
   359 	aRule.SetByMonthDay(days);
       
   360 	CleanupStack::PopAndDestroy(&days);
       
   361 	iSetSegs |= ESegFlagByMonthDay;
       
   362 	
       
   363 	TRACE_EXIT_POINT;
       
   364 	return ETrue;
       
   365     }