meetingrequest/mrservices/src/cesmrconflictchecker.cpp
branchRCL_3
changeset 18 6b8f3b30d0ec
parent 12 4ce476e64c59
equal deleted inserted replaced
17:67369d1b217f 18:6b8f3b30d0ec
     1 /*
     1 /*
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 *  Description : ESMR conflict checker implementation
    14 *  Description : ESMR conflict checker implementation
    15 *  Version     : %version: e002sa33#7 %
    15 *  Version     : %version: e002sa33#9 %
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 #include "emailtrace.h"
    19 #include "emailtrace.h"
    20 #include "cesmrconflictchecker.h"
    20 #include "cesmrconflictchecker.h"
    27 #include <calinstance.h>
    27 #include <calinstance.h>
    28 #include <calentryview.h>
    28 #include <calentryview.h>
    29 #include <calinstanceview.h>
    29 #include <calinstanceview.h>
    30 #include <calinstanceiterator.h>
    30 #include <calinstanceiterator.h>
    31 #include <ct/rcpointerarray.h>
    31 #include <ct/rcpointerarray.h>
       
    32 #include <calrrule.h>
    32 
    33 
    33 /// Unnamed namespace for local definitions
    34 /// Unnamed namespace for local definitions
    34 namespace { // codescanner::namespace
    35 namespace { // codescanner::namespace
    35 
    36 
    36 // Definition for zero
    37 // Definition for zero
    92         TTime aStart,
    93         TTime aStart,
    93         TTime aEnd,
    94         TTime aEnd,
    94         CCalInstanceView* aInstanceView )
    95         CCalInstanceView* aInstanceView )
    95     {
    96     {
    96     FUNC_LOG;
    97     FUNC_LOG;
    97     const CalCommon::TCalViewFilter instanceFilter = 
    98     const CalCommon::TCalViewFilter instanceFilter =
    98             CalCommon::EIncludeAppts;
    99             CalCommon::EIncludeAppts;
    99 
   100 
   100     TDateTime start = aStart.DateTime();
   101     TDateTime start = aStart.DateTime();
   101     TDateTime end   = aEnd.DateTime();
   102     TDateTime end   = aEnd.DateTime();
   102 
   103 
   108     end.SetHour( KZero );
   109     end.SetHour( KZero );
   109     end.SetMinute( KZero );
   110     end.SetMinute( KZero );
   110     end.SetSecond( KZero );
   111     end.SetSecond( KZero );
   111     end.SetMicroSecond( KZero );
   112     end.SetMicroSecond( KZero );
   112     TTime endTime = end;
   113     TTime endTime = end;
   113     
   114 
   114     if ( TTime(start) == endTime || 
   115     if ( TTime(start) == endTime ||
   115          endTime < aEnd )
   116          endTime < aEnd )
   116         {
   117         {
   117         
   118 
   118         endTime += TTimeIntervalDays(1);
   119         endTime += TTimeIntervalDays(1);
   119         end = endTime.DateTime();
   120         end = endTime.DateTime();
   120         }
   121         }
   121 
   122 
   122     TCalTime rangeStart;
   123     TCalTime rangeStart;
   164     FUNC_LOG;
   165     FUNC_LOG;
   165     TInt index(0);
   166     TInt index(0);
   166 
   167 
   167     TTime startTimeLocal = aStartTime.TimeLocalL();
   168     TTime startTimeLocal = aStartTime.TimeLocalL();
   168     TTime endTimeLocal = aEndTime.TimeLocalL();
   169     TTime endTimeLocal = aEndTime.TimeLocalL();
   169     
   170 
   170     while( index < instanceArray.Count() )
   171     while( index < instanceArray.Count() )
   171         {
   172         {
   172         TBool conflictingInstance( ETrue );
   173         TBool conflictingInstance( ETrue );
   173         CCalInstance* instance = instanceArray[index];
   174         CCalInstance* instance = instanceArray[index];
   174         
   175 
   175         TTime entryStartTimeLocal = instance->StartTimeL().TimeLocalL();
   176         TTime entryStartTimeLocal = instance->StartTimeL().TimeLocalL();
   176         TTime entryEndTimeLocal = instance->EndTimeL().TimeLocalL();            
   177         TTime entryEndTimeLocal = instance->EndTimeL().TimeLocalL();
   177 
   178 
   178         TPtrC8 uid( instance->Entry().UidL() );
   179         TPtrC8 uid( instance->Entry().UidL() );
   179         if ( ERemoveSameUID == aFilterType && 
   180         if ( ERemoveSameUID == aFilterType &&
   180              0 == aUid.CompareF(uid) )
   181              0 == aUid.CompareF(uid) )
   181             {
   182             {
   182             conflictingInstance = EFalse;
   183             conflictingInstance = EFalse;
   183             }
   184             }
   184         else if ( entryStartTimeLocal >= startTimeLocal &&
   185         else if ( entryStartTimeLocal >= startTimeLocal &&
   199             // Antry starts and ends during another entry
   200             // Antry starts and ends during another entry
   200             index++;
   201             index++;
   201             }
   202             }
   202         else
   203         else
   203             {
   204             {
   204             conflictingInstance = EFalse;            
   205             conflictingInstance = EFalse;
   205             }
   206             }
   206         
   207 
   207         // Remove non-conflicting instance from instance array
   208         // Remove non-conflicting instance from instance array
   208         if ( !conflictingInstance )
   209         if ( !conflictingInstance )
   209             {
   210             {
   210             instanceArray.Remove(index);
   211             instanceArray.Remove(index);
   211             delete instance; 
   212             delete instance;
   212             instance = NULL;
   213             instance = NULL;
   213             }
   214             }
   214         }
   215         }
   215     }
   216     }
   216 
   217 
   224 void CreateEntriesFromInstancesL(
   225 void CreateEntriesFromInstancesL(
   225         RPointerArray<CCalInstance>& instanceArray,
   226         RPointerArray<CCalInstance>& instanceArray,
   226         RPointerArray<CCalEntry>& aConflictingEntries )
   227         RPointerArray<CCalEntry>& aConflictingEntries )
   227     {
   228     {
   228     FUNC_LOG;
   229     FUNC_LOG;
   229     TInt instanceCount( instanceArray.Count() );    
   230     TInt instanceCount( instanceArray.Count() );
   230     for ( TInt i(0); i < instanceCount; ++i )
   231     for ( TInt i(0); i < instanceCount; ++i )
   231         {        
   232         {
   232         CCalEntry& parent( instanceArray[i]->Entry() );
   233         CCalEntry& parent( instanceArray[i]->Entry() );
   233         CCalEntry* entry = ESMRHelper::CopyEntryLC( parent, 
   234         CCalEntry* entry = ESMRHelper::CopyEntryLC( parent,
   234                                                     parent.MethodL(),
   235                                                     parent.MethodL(),
   235                                                     ESMRHelper::ECopyFull );
   236                                                     ESMRHelper::ECopyFull );
   236         
   237 
   237         entry->SetStartAndEndTimeL( instanceArray[i]->StartTimeL(),
   238         entry->SetStartAndEndTimeL( instanceArray[i]->StartTimeL(),
   238                                     instanceArray[i]->EndTimeL() );
   239                                     instanceArray[i]->EndTimeL() );
   239 
   240 
   240         User::LeaveIfError( aConflictingEntries.Append( entry ) );
   241         aConflictingEntries.AppendL( entry );
   241         CleanupStack::Pop( entry );
   242         CleanupStack::Pop( entry );
       
   243         }
       
   244     }
       
   245 
       
   246 /**
       
   247  * Checks if entry is repeating.
       
   248  * @return ETrue if entry is repeating
       
   249  */
       
   250 TBool IsRepeatingMeetingL( const CCalEntry& aEntry,
       
   251                            MESMRCalDbMgr& aDb )
       
   252     {
       
   253     FUNC_LOG;
       
   254 
       
   255     TBool recurrent( EFalse );
       
   256 
       
   257     CCalInstance* instance = aDb.FindInstanceL( aEntry );
       
   258 
       
   259     if ( instance )
       
   260         {
       
   261         CleanupStack::PushL( instance );
       
   262         recurrent = ESMREntryHelper::IsRepeatingMeetingL( instance->Entry() );
       
   263         CleanupStack::PopAndDestroy( instance );
       
   264         }
       
   265     else
       
   266         {
       
   267         recurrent = ESMREntryHelper::IsRepeatingMeetingL( aEntry );
       
   268         }
       
   269 
       
   270     return recurrent;
       
   271     }
       
   272 
       
   273 /**
       
   274  * Finds conflicts for entry
       
   275  */
       
   276 void FindConflictsForEntryL(
       
   277         const CCalEntry& aEntry,
       
   278         RPointerArray< CCalInstance >& aInstances,
       
   279         MESMRCalDbMgr& aDb )
       
   280     {
       
   281     FUNC_LOG;
       
   282 
       
   283     // Get instance views of all calendar
       
   284     RPointerArray<CCalInstanceView> allCalenInstanceView =
       
   285             aDb.NormalDbAllCalenInstanceView();
       
   286 
       
   287     // Check if there is any conflict in each calendar
       
   288     for( TInt i = 0; i < allCalenInstanceView.Count(); i++ )
       
   289         {
       
   290         CalCommon::TCalTimeRange timeRange =
       
   291                     ResolveFetchTimeRangeL(
       
   292                                     aEntry.StartTimeL().TimeUtcL(),
       
   293                                     aEntry.EndTimeL().TimeUtcL(),
       
   294                                     allCalenInstanceView[i] );
       
   295 
       
   296         allCalenInstanceView[i]->FindInstanceL(
       
   297                 aInstances,
       
   298                 CalCommon::EIncludeAppts,
       
   299                 timeRange );
       
   300         }
       
   301 
       
   302     RemoveAndDeleteNonConflictingInstancesL(
       
   303             aInstances,
       
   304             aEntry.StartTimeL(),
       
   305             aEntry.EndTimeL(),
       
   306             aEntry.UidL(),
       
   307             ERemoveSameUID );
       
   308     }
       
   309 
       
   310 /**
       
   311  * Moves instances from an array to another.
       
   312  */
       
   313 void MoveInstancesL(
       
   314         RPointerArray< CCalInstance >& aFrom,
       
   315         RPointerArray< CCalInstance >& aTo )
       
   316     {
       
   317     FUNC_LOG;
       
   318 
       
   319     aTo.ReserveL( aTo.Count() + aFrom.Count() );
       
   320     while ( aFrom.Count() )
       
   321         {
       
   322         aTo.AppendL( aFrom[ 0 ] );
       
   323         aFrom.Remove( 0 );
       
   324         }
       
   325     }
       
   326 
       
   327 /**
       
   328  * Finds conflicts based on Daily recurrence.
       
   329  *
       
   330  */
       
   331 void FindConflictsForDailyRRuleL(
       
   332         CCalEntry& aEntry,
       
   333         RPointerArray< CCalInstance >& aInstances,
       
   334         MESMRCalDbMgr& aDb,
       
   335         TBool aFindAllConflicts )
       
   336     {
       
   337     FUNC_LOG;
       
   338 
       
   339     TCalRRule rRule;
       
   340     aEntry.GetRRuleL( rRule );
       
   341     RCPointerArray< CCalInstance > tmpInstanceArray;
       
   342     CleanupClosePushL( tmpInstanceArray );
       
   343 
       
   344     // Entry start and end time
       
   345     TTime start( aEntry.StartTimeL().TimeUtcL() );
       
   346     TTime end( aEntry.EndTimeL().TimeUtcL() );
       
   347 
       
   348     if ( rRule.Count() )
       
   349         {
       
   350         for ( TInt i = 0; i < rRule.Count(); ++i )
       
   351             {
       
   352             // Set each occurence start and end time on entry
       
   353             TTimeIntervalDays interval( i * rRule.Interval() );
       
   354             TCalTime startTime;
       
   355             startTime.SetTimeUtcL( start + interval );
       
   356             TCalTime endTime;
       
   357             endTime.SetTimeUtcL( end + interval );
       
   358             aEntry.SetStartAndEndTimeL(
       
   359                     startTime,
       
   360                     endTime );
       
   361 
       
   362             // Find conflicts for this occurence
       
   363             FindConflictsForEntryL( aEntry, tmpInstanceArray, aDb );
       
   364 
       
   365             if ( tmpInstanceArray.Count() )
       
   366                 {
       
   367                 MoveInstancesL( tmpInstanceArray, aInstances );
       
   368                 if ( !aFindAllConflicts )
       
   369                     {
       
   370                     break;
       
   371                     }
       
   372                 }
       
   373             }
       
   374         }
       
   375     else if ( rRule.Until().TimeUtcL() != Time::NullTTime() )
       
   376         {
       
   377         TTime until( rRule.Until().TimeUtcL() );
       
   378         TTime start( aEntry.StartTimeL().TimeUtcL() );
       
   379         TTime end( aEntry.EndTimeL().TimeUtcL() );
       
   380         TTimeIntervalDays interval( rRule.Interval() );
       
   381 
       
   382         // Loop while start time is before until time
       
   383         while ( start <= until )
       
   384             {
       
   385             // Set start and end time for occurence
       
   386             TCalTime startTime;
       
   387             startTime.SetTimeUtcL( start );
       
   388             TCalTime endTime;
       
   389             endTime.SetTimeUtcL( end );
       
   390             aEntry.SetStartAndEndTimeL(
       
   391                     startTime,
       
   392                     endTime );
       
   393 
       
   394             // Find conflicts
       
   395             FindConflictsForEntryL( aEntry, tmpInstanceArray, aDb );
       
   396 
       
   397             if ( tmpInstanceArray.Count() )
       
   398                 {
       
   399                 MoveInstancesL( tmpInstanceArray, aInstances );
       
   400                 if ( !aFindAllConflicts )
       
   401                     {
       
   402                     break;
       
   403                     }
       
   404                 }
       
   405 
       
   406             // Move to next occurence
       
   407             start += interval;
       
   408             end += interval;
       
   409             }
       
   410         }
       
   411 
       
   412     CleanupStack::PopAndDestroy( &tmpInstanceArray );
       
   413     }
       
   414 
       
   415 /**
       
   416  * Finds conflicts based on Weekly recurrence.
       
   417  */
       
   418 void FindConflictsForWeeklyRRuleL(
       
   419         CCalEntry& aEntry,
       
   420         RPointerArray< CCalInstance >& aInstances,
       
   421         MESMRCalDbMgr& aDb,
       
   422         TBool aFindAllConflicts )
       
   423     {
       
   424     FUNC_LOG;
       
   425 
       
   426     TCalRRule rRule;
       
   427     aEntry.GetRRuleL( rRule );
       
   428 
       
   429     // Tmp array for conflic instances
       
   430     RCPointerArray< CCalInstance > tmpInstanceArray;
       
   431     CleanupClosePushL( tmpInstanceArray );
       
   432 
       
   433     // Array of recurrence days
       
   434     RArray< TDay > days;
       
   435     CleanupClosePushL( days );
       
   436     rRule.GetByDayL( days );
       
   437 
       
   438     // Recurrence start time
       
   439     TTime start( aEntry.StartTimeL().TimeUtcL() );
       
   440     // Recurrence end time
       
   441     TTime end( aEntry.EndTimeL().TimeUtcL() );
       
   442     const TTimeIntervalDays KWeek( 7 );
       
   443     TDay startDayOfWeek( start.DayNoInWeek() );
       
   444 
       
   445     // Instance duration
       
   446     TTimeIntervalMinutes duration;
       
   447     end.MinutesFrom( start, duration );
       
   448 
       
   449     if ( rRule.Count() )
       
   450         {
       
   451         for ( TInt i = 0; i < rRule.Count(); ++i )
       
   452             {
       
   453             // Calculate weekly start time
       
   454             TTimeIntervalDays interval( i* KWeek.Int() );
       
   455             TDateTime date( TTime( start + interval ).DateTime() );
       
   456             date.SetDay( date.Day() - startDayOfWeek );
       
   457             TTime weekStartTime( date );
       
   458 
       
   459             for ( TInt j = 0; j < days.Count(); ++j )
       
   460                 {
       
   461                 // Iterate through days of week
       
   462                 TTime entryStartTime( weekStartTime + TTimeIntervalDays( days[ j ] ) );
       
   463 
       
   464                 if ( start <= entryStartTime )
       
   465                     {
       
   466                     // Start time is in recurrence range
       
   467                     // Calcualte end time
       
   468                     TCalTime startCalTime;
       
   469                     startCalTime.SetTimeUtcL( entryStartTime );
       
   470                     TCalTime endCalTime;
       
   471                     endCalTime.SetTimeUtcL( entryStartTime + duration );
       
   472                     aEntry.SetStartAndEndTimeL( startCalTime, endCalTime );
       
   473 
       
   474                     // Find conflicts of for entry and move them to result array
       
   475                     FindConflictsForEntryL( aEntry, tmpInstanceArray, aDb );
       
   476 
       
   477                     if ( tmpInstanceArray.Count() )
       
   478                         {
       
   479                         MoveInstancesL( tmpInstanceArray, aInstances );
       
   480                         if ( !aFindAllConflicts )
       
   481                             {
       
   482                             break;
       
   483                             }
       
   484                         }
       
   485                     }
       
   486                 }
       
   487 
       
   488             if ( !aFindAllConflicts && aInstances.Count() )
       
   489                 {
       
   490                 break;
       
   491                 }
       
   492             }
       
   493         }
       
   494     else if ( rRule.Until().TimeUtcL() != Time::NullTTime() )
       
   495         {
       
   496         TDateTime date( start.DateTime() );
       
   497         date.SetDay( date.Day() - startDayOfWeek );
       
   498         TTime weekStartTime( date );
       
   499         TTime until( rRule.Until().TimeUtcL() );
       
   500 
       
   501         while ( weekStartTime < until )
       
   502             {
       
   503             for ( TInt j = 0; j < days.Count(); ++j )
       
   504                 {
       
   505                 // Iterate through days of week
       
   506                 TTime entryStartTime( weekStartTime + TTimeIntervalDays( days[ j ] ) );
       
   507 
       
   508                 if ( start <= entryStartTime )
       
   509                     {
       
   510                     // Start time is in recurrence range
       
   511                     // Calculate end time
       
   512                     TCalTime startCalTime;
       
   513                     startCalTime.SetTimeUtcL( entryStartTime );
       
   514                     TCalTime endCalTime;
       
   515                     endCalTime.SetTimeUtcL( entryStartTime + duration );
       
   516                     aEntry.SetStartAndEndTimeL( startCalTime, endCalTime );
       
   517 
       
   518                     // Find conflicts of for entry and move them to result array
       
   519                     FindConflictsForEntryL( aEntry, tmpInstanceArray, aDb );
       
   520 
       
   521                     if ( tmpInstanceArray.Count() )
       
   522                         {
       
   523                         MoveInstancesL( tmpInstanceArray, aInstances );
       
   524                         if ( !aFindAllConflicts )
       
   525                             {
       
   526                             break;
       
   527                             }
       
   528                         }
       
   529                     }
       
   530                 }
       
   531 
       
   532             if ( !aFindAllConflicts && aInstances.Count() )
       
   533                 {
       
   534                 break;
       
   535                 }
       
   536             else
       
   537                 {
       
   538                 weekStartTime += KWeek;
       
   539                 }
       
   540             }
       
   541         }
       
   542 
       
   543     CleanupStack::PopAndDestroy( &days );
       
   544     CleanupStack::PopAndDestroy( &tmpInstanceArray );
       
   545     }
       
   546 
       
   547 /**
       
   548  * Finds conflict for recurrent entry
       
   549  */
       
   550 void FindConflictsForRepeatingMeetingL(
       
   551         const CCalEntry& aEntry,
       
   552         RPointerArray< CCalInstance >& aInstances,
       
   553         MESMRCalDbMgr& aDb,
       
   554         TBool aFindAllConflicts )
       
   555     {
       
   556     FUNC_LOG;
       
   557 
       
   558     CCalInstance* instance = aDb.FindInstanceL( aEntry );
       
   559 
       
   560     if ( instance ) // Instance is stored
       
   561         {
       
   562         CleanupStack::PushL( instance );
       
   563         RCPointerArray< CCalInstance > tmpInstanceArray;
       
   564         CleanupClosePushL( tmpInstanceArray );
       
   565 
       
   566         CCalEntry& parent = instance->Entry();
       
   567         CCalInstanceView* instanceView = aDb.InstanceViewL( parent );
       
   568         CCalInstanceIterator* iterator = instanceView->FindInstanceByUidL(
       
   569                         parent.UidL(),
       
   570                         parent.StartTimeL() );
       
   571         CleanupStack::PushL( iterator );
       
   572         CCalEntry* entry = ESMRHelper::CopyEntryLC(
       
   573                             parent,
       
   574                             parent.MethodL(),
       
   575                             ESMRHelper::ECopyFull );
       
   576 
       
   577         while ( iterator->HasMore() )
       
   578             {
       
   579             CCalInstance* next = iterator->NextL();
       
   580             CleanupStack::PushL( next );
       
   581             entry->SetStartAndEndTimeL( next->StartTimeL(), next->EndTimeL() );
       
   582             CleanupStack::PopAndDestroy( next );
       
   583             FindConflictsForEntryL( *entry,
       
   584                                     tmpInstanceArray,
       
   585                                     aDb );
       
   586 
       
   587             if ( tmpInstanceArray.Count() )
       
   588                 {
       
   589                 MoveInstancesL( tmpInstanceArray, aInstances );
       
   590 
       
   591                 if ( !aFindAllConflicts )
       
   592                     {
       
   593                     break;
       
   594                     }
       
   595                 }
       
   596             }
       
   597 
       
   598         CleanupStack::PopAndDestroy( entry );
       
   599 
       
   600         CleanupStack::PopAndDestroy( iterator );
       
   601 
       
   602         if ( aFindAllConflicts
       
   603              || !aInstances.Count() )
       
   604             {
       
   605             // Find conflicts also for parent entry
       
   606             FindConflictsForEntryL( parent, tmpInstanceArray, aDb );
       
   607             MoveInstancesL( tmpInstanceArray, aInstances );
       
   608             }
       
   609 
       
   610         CleanupStack::PopAndDestroy( &tmpInstanceArray );
       
   611         CleanupStack::PopAndDestroy( instance );
       
   612         }
       
   613     else // Entry is not stored yet
       
   614         {
       
   615         CCalEntry* entry = ESMRHelper::CopyEntryLC(
       
   616                 aEntry,
       
   617                 aEntry.MethodL(),
       
   618                 ESMRHelper::ECopyFull );
       
   619 
       
   620         TCalRRule rRule;
       
   621         if ( aEntry.GetRRuleL( rRule ) )
       
   622             {
       
   623             switch ( rRule.Type() )
       
   624                 {
       
   625                 case TCalRRule::EDaily:
       
   626                     {
       
   627                     FindConflictsForDailyRRuleL(
       
   628                             *entry,
       
   629                             aInstances,
       
   630                             aDb,
       
   631                             aFindAllConflicts );
       
   632                     break;
       
   633                     }
       
   634                 case TCalRRule::EWeekly:
       
   635                     {
       
   636                     FindConflictsForWeeklyRRuleL(
       
   637                             *entry,
       
   638                             aInstances,
       
   639                             aDb,
       
   640                             aFindAllConflicts );
       
   641                     break;
       
   642                     }
       
   643                 default:
       
   644                     break;
       
   645                 }
       
   646             }
       
   647         else
       
   648             {
       
   649             // Entry has RDates set
       
   650             RCPointerArray< CCalInstance > tmpInstanceArray;
       
   651             CleanupClosePushL( tmpInstanceArray );
       
   652 
       
   653             RArray< TCalTime > rDates;
       
   654             CleanupClosePushL( rDates );
       
   655             aEntry.GetRDatesL( rDates );
       
   656 
       
   657             // Get entry start time, end time and duration
       
   658             TTime start( aEntry.StartTimeL().TimeUtcL() );
       
   659             TTime end( aEntry.EndTimeL().TimeUtcL() );
       
   660             TTimeIntervalMinutes duration(0);
       
   661             end.MinutesFrom(
       
   662                     start,
       
   663                     duration );
       
   664 
       
   665             for ( TInt i = 0; i < rDates.Count(); ++i )
       
   666                 {
       
   667                 // Set start and end times for entry
       
   668                 TCalTime startTime( rDates[ i ] );
       
   669                 TCalTime endTime;
       
   670                 endTime.SetTimeUtcL( startTime.TimeUtcL() + duration );
       
   671                 entry->SetStartAndEndTimeL( startTime, endTime );
       
   672 
       
   673                 // Find conflicts
       
   674                 FindConflictsForEntryL( *entry, tmpInstanceArray, aDb );
       
   675 
       
   676                 if ( tmpInstanceArray.Count() )
       
   677                     {
       
   678                     MoveInstancesL( tmpInstanceArray, aInstances );
       
   679                     if ( !aFindAllConflicts )
       
   680                         {
       
   681                         break;
       
   682                         }
       
   683                     }
       
   684                 }
       
   685 
       
   686             CleanupStack::PopAndDestroy( &rDates );
       
   687 
       
   688             if ( aFindAllConflicts
       
   689                  || aInstances.Count() == 0 )
       
   690                 {
       
   691                 // Find conflicts for parent entry
       
   692                 FindConflictsForEntryL( aEntry, tmpInstanceArray, aDb );
       
   693                 MoveInstancesL( tmpInstanceArray, aInstances );
       
   694                 }
       
   695 
       
   696             CleanupStack::PopAndDestroy( &tmpInstanceArray );
       
   697             }
       
   698 
       
   699         CleanupStack::PopAndDestroy( entry );
   242         }
   700         }
   243     }
   701     }
   244 
   702 
   245 }  // namespace
   703 }  // namespace
   246 
   704 
   305         Leave( KErrArgument );
   763         Leave( KErrArgument );
   306         }
   764         }
   307 
   765 
   308 #endif
   766 #endif
   309 
   767 
   310     // Get instance views of all calendar
       
   311     RPointerArray<CCalInstanceView> allCalenInstanceView = 
       
   312                                     iDbMgr.NormalDbAllCalenInstanceView();
       
   313     
       
   314     RCPointerArray<CCalInstance> instanceArray;
   768     RCPointerArray<CCalInstance> instanceArray;
   315     CleanupClosePushL( instanceArray );
   769     CleanupClosePushL( instanceArray );
   316     
   770 
   317     // Check if there is any conflict in each calendar
   771     if ( IsRepeatingMeetingL( aEntry, iDbMgr ) )
   318     for( TInt i = 0; i < allCalenInstanceView.Count(); i++ )
   772         {
   319         {
   773         FindConflictsForRepeatingMeetingL(
   320         CalCommon::TCalTimeRange timeRange =
   774                 aEntry,
   321                     ResolveFetchTimeRangeL( 
   775                 instanceArray,
   322                                     aEntry.StartTimeL().TimeUtcL(),
   776                 iDbMgr,
   323                                     aEntry.EndTimeL().TimeUtcL(), 
   777                 EFalse );
   324                                     allCalenInstanceView[i] );
   778         }
   325         
   779     else
   326         allCalenInstanceView[i]->FindInstanceL( instanceArray,
   780         {
   327                                                 CalCommon::EIncludeAppts,
   781         FindConflictsForEntryL(
   328                                                 timeRange );
   782                 aEntry,
   329 
   783                 instanceArray,
   330 
   784                 iDbMgr );
   331         }
   785         }
   332     
   786 
   333     RemoveAndDeleteNonConflictingInstancesL(
       
   334                                     instanceArray,
       
   335                                     aEntry.StartTimeL(),
       
   336                                     aEntry.EndTimeL(),
       
   337                                     aEntry.UidL(),
       
   338                                     ERemoveSameUID );
       
   339     
       
   340     CreateEntriesFromInstancesL( instanceArray, aConflicts );
   787     CreateEntriesFromInstancesL( instanceArray, aConflicts );
   341     CleanupStack::PopAndDestroy(); // instanceArray
   788     CleanupStack::PopAndDestroy( &instanceArray);
   342     }
   789     }
   343 
   790 
   344 // ---------------------------------------------------------------------------
   791 // ---------------------------------------------------------------------------
   345 // CESMRConflictChecker::FindInstancesForEntry
   792 // CESMRConflictChecker::FindInstancesForEntry
   346 // ---------------------------------------------------------------------------
   793 // ---------------------------------------------------------------------------
   347 //
   794 //
   348 EXPORT_C void CESMRConflictChecker::FindInstancesForEntryL(
   795 EXPORT_C void CESMRConflictChecker::FindInstancesForEntryL(
   349         TTime aStart, 
   796         TTime aStart,
   350         TTime aEnd,
   797         TTime aEnd,
   351         const CCalEntry& aEntry,
   798         const CCalEntry& aEntry,
   352         TCalCollectionId aColId, 
   799         TCalCollectionId aColId,
   353         RPointerArray<CCalEntry>& aInstances )
   800         RPointerArray<CCalEntry>& aInstances )
   354     {
   801     {
   355     FUNC_LOG;
   802     FUNC_LOG;
   356     CCalInstanceView* instanceView = iDbMgr.InstanceViewL( aEntry );
   803     CCalInstanceView* instanceView = iDbMgr.InstanceViewL( aEntry );
   357 
   804 
   358     RCPointerArray<CCalInstance> instanceArray;
   805     RCPointerArray<CCalInstance> instanceArray;
   359     CleanupClosePushL( instanceArray );
   806     CleanupClosePushL( instanceArray );
   360 
   807 
   361     // First we need the parent entry of the series ...
   808     // First we need the parent entry of the series ...
   362     CCalInstance* entryInstance = instanceView->FindInstanceL( 
   809     CCalInstance* entryInstance = instanceView->FindInstanceL(
   363     		aEntry.LocalUidL(), 
   810     		aEntry.LocalUidL(),
   364     		aEntry.StartTimeL() ); //Ownership gained
   811     		aEntry.StartTimeL() ); //Ownership gained
   365     CleanupStack::PushL( entryInstance );
   812     CleanupStack::PushL( entryInstance );
   366 
   813 
   367     CCalInstance* parentEntryInstance = instanceView->FindInstanceL( 
   814     CCalInstance* parentEntryInstance = instanceView->FindInstanceL(
   368     		aEntry.LocalUidL(), 
   815     		aEntry.LocalUidL(),
   369     		entryInstance->Entry().StartTimeL() );
   816     		entryInstance->Entry().StartTimeL() );
   370     CleanupStack::PopAndDestroy( entryInstance );
   817     CleanupStack::PopAndDestroy( entryInstance );
   371     CleanupStack::PushL( parentEntryInstance );
   818     CleanupStack::PushL( parentEntryInstance );
   372     
   819 
   373     // ... And the parent entry instances start time
   820     // ... And the parent entry instances start time
   374     TCalTime firstIntanceStartTime( parentEntryInstance->StartTimeL() );
   821     TCalTime firstIntanceStartTime( parentEntryInstance->StartTimeL() );
   375 
   822 
   376     CleanupStack::Pop( parentEntryInstance ); // ownership given to instanceArray
   823     CleanupStack::Pop( parentEntryInstance ); // ownership given to instanceArray
   377     instanceArray.Append( parentEntryInstance );
   824     instanceArray.Append( parentEntryInstance );
   378 
   825 
   379     // Let's get all instances which have same uid and collection id
   826     // Let's get all instances which have same uid and collection id
   380     // as the aEntry to an iterator.
   827     // as the aEntry to an iterator.
   381     CCalInstanceIterator* instanceIterator = instanceView->FindInstanceByUidL( 
   828     CCalInstanceIterator* instanceIterator = instanceView->FindInstanceByUidL(
   382     		aColId, 
   829     		aColId,
   383     		aEntry.UidL(), 
   830     		aEntry.UidL(),
   384     		firstIntanceStartTime );
   831     		firstIntanceStartTime );
   385     CleanupStack::PushL( instanceIterator );
   832     CleanupStack::PushL( instanceIterator );
   386     
   833 
   387     TInt count( instanceIterator->Count() );
   834     TInt count( instanceIterator->Count() );
   388     
   835 
   389     for( TInt i = 0; i < count; ++i )
   836     for( TInt i = 0; i < count; ++i )
   390     	{
   837     	{
   391 		CCalInstance* instance = NULL;
   838 		CCalInstance* instance = NULL;
   392 		TRAPD( err, instance = instanceIterator->NextL() ); //Ownership gained
   839 		TRAPD( err, instance = instanceIterator->NextL() ); //Ownership gained
   393 		if( !err && instance )
   840 		if( !err && instance )
   394 			{
   841 			{
   395 			instanceArray.Append( instance );	
   842 			instanceArray.Append( instance );
   396 			}
   843 			}
   397     	}
   844     	}
   398 
   845 
   399     CleanupStack::PopAndDestroy( instanceIterator );
   846     CleanupStack::PopAndDestroy( instanceIterator );
   400 
   847 
   401     // Now the instanceArray has all instances of the aEntry, 
   848     // Now the instanceArray has all instances of the aEntry,
   402     // let's remove this instance == instance of aEntry, from the array
   849     // let's remove this instance == instance of aEntry, from the array
   403 	TInt i( 0 );
   850 	TInt i( 0 );
   404     while( i < instanceArray.Count() )
   851     while( i < instanceArray.Count() )
   405     	{
   852     	{
   406 		CCalInstance* instance = instanceArray[i];
   853 		CCalInstance* instance = instanceArray[i];
   407 		
   854 
   408         TBool thisInstance( 
   855         TBool thisInstance(
   409                 instance->StartTimeL().TimeLocalL() == 
   856                 instance->StartTimeL().TimeLocalL() ==
   410                 aEntry.StartTimeL().TimeLocalL() &&
   857                 aEntry.StartTimeL().TimeLocalL() &&
   411                 instance->EndTimeL().TimeLocalL() == 
   858                 instance->EndTimeL().TimeLocalL() ==
   412                 aEntry.EndTimeL().TimeLocalL() );
   859                 aEntry.EndTimeL().TimeLocalL() );
   413         
   860 
   414         if( thisInstance )
   861         if( thisInstance )
   415         	{
   862         	{
   416 			delete instance;
   863 			delete instance;
   417 			instanceArray.Remove( i );  
   864 			instanceArray.Remove( i );
   418         	}
   865         	}
   419         else
   866         else
   420         	{
   867         	{
   421 			++i;
   868 			++i;
   422         	}
   869         	}
   423     	}
   870     	}
   424   
   871 
   425     TCalTime start;
   872     TCalTime start;
   426     start.SetTimeLocalL( aStart );
   873     start.SetTimeLocalL( aStart );
   427     
   874 
   428     TCalTime end;
   875     TCalTime end;
   429     end.SetTimeLocalL( aEnd );
   876     end.SetTimeLocalL( aEnd );
   430 
   877 
   431     RemoveAndDeleteNonConflictingInstancesL(
   878     RemoveAndDeleteNonConflictingInstancesL(
   432             instanceArray,
   879             instanceArray,
   433             start,
   880             start,
   434             end,
   881             end,
   435             aEntry.UidL(),
   882             aEntry.UidL(),
   436             EIncludeSameUID );
   883             EIncludeSameUID );
   437     
   884 
   438     CreateEntriesFromInstancesL( instanceArray, aInstances );
   885     CreateEntriesFromInstancesL( instanceArray, aInstances );
   439     
   886 
   440     CleanupStack::PopAndDestroy(); // instanceArray    
   887     CleanupStack::PopAndDestroy(); // instanceArray
   441     }
   888     }
   442 
   889 
   443 // EOF
   890 // EOF
   444 
   891