meetingrequest/mrgui/src/cesmrmemotimevalidator.cpp
changeset 0 8466d47a6819
child 12 4ce476e64c59
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  ESMR time and date sanity checks and saving
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include <eikmfne.h>
       
    21 #include <calentry.h>
       
    22 #include <e32std.h>
       
    23 #include "cesmrmemotimevalidator.h"
       
    24 #include "cesmrglobalnote.h"
       
    25 #include "mesmrfieldstorage.h"
       
    26 #include "mesmrcalentry.h"
       
    27 
       
    28 // Unnamed namespace for local definitions
       
    29 namespace {
       
    30 
       
    31 const TInt KMaxHours(23);
       
    32 const TInt KMaxMinutes(59);
       
    33 const TInt KMaxSeconds(59);
       
    34 
       
    35 // Definition fom max possible time
       
    36 #define KMaxTTime (TTime(TDateTime(2100, EDecember, 31-2, 23, 59, 59, 999999)))
       
    37 
       
    38 // Definition fom min possible time
       
    39 #define KMinTTime (TTime(TDateTime(1900, EJanuary, 2-1, 0, 0, 0, 0)))
       
    40 
       
    41 #ifdef _DEBUG
       
    42 
       
    43 // Literal for panics
       
    44 _LIT( KESMRMemoTimeValidator, "ESMRMemoTimeValidator" );
       
    45 
       
    46 /** Panic code definitions */
       
    47 enum TESMRMemoTimeValidator
       
    48     {
       
    49     /** Start date field not set */
       
    50     EESMRMemoValidatorNullStartDate,
       
    51     /** Stop date field not set */
       
    52     EESMRMemoValidatorNullStopDate,
       
    53     /** Relative alarm called for non-relative alarm event */
       
    54     EESMRMemoValidatorRelativeAlarm
       
    55     };
       
    56 
       
    57 /**
       
    58  * Raises system panic.
       
    59  * @param aPanic Panic code
       
    60  * @see TESMRRecurrentEditValidatorPanic
       
    61  */
       
    62 void Panic( TESMRMemoTimeValidator aPanic )
       
    63     {
       
    64     User::Panic( KESMRMemoTimeValidator, aPanic );
       
    65     }
       
    66 
       
    67 #endif // _DEBUG
       
    68 
       
    69 /**
       
    70  * Sets date to editor. Date is checked and adjusted
       
    71  * between min and max before setting
       
    72  * @param aEditor Reference to time editor
       
    73  * @param aDate On return contains the set date.
       
    74  */
       
    75 void SetDateToEditor(
       
    76         CEikDateEditor& aEditor,
       
    77         TTime& aDate )
       
    78     {
       
    79     if ( aDate < KMinTTime )
       
    80         {
       
    81         aDate = KMinTTime;
       
    82         }
       
    83     else if ( aDate > KMaxTTime )
       
    84         {
       
    85         aDate = KMaxTTime;
       
    86         }
       
    87 
       
    88     if ( aEditor.IsVisible() )
       
    89         {
       
    90         aEditor.SetDate( aDate );
       
    91         }
       
    92     }
       
    93 
       
    94 }//namespace
       
    95 
       
    96 // ======== MEMBER FUNCTIONS ========
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // CESMRMemoTimeValidator::~CESMRMemoTimeValidator
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 inline CESMRMemoTimeValidator::CESMRMemoTimeValidator( )
       
   103     {
       
   104     FUNC_LOG;
       
   105     // Do nothing
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CESMRMemoTimeValidator::~CESMRMemoTimeValidator
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 CESMRMemoTimeValidator::~CESMRMemoTimeValidator( )
       
   113     {
       
   114     FUNC_LOG;
       
   115     // Do nothing
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CESMRMemoTimeValidator::NewL
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CESMRMemoTimeValidator* CESMRMemoTimeValidator::NewL( )
       
   123     {
       
   124     FUNC_LOG;
       
   125     CESMRMemoTimeValidator* self = new (ELeave) CESMRMemoTimeValidator();
       
   126     return self;
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CESMRMemoTimeValidator::ValidateL
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 MESMRFieldValidator::TESMRFieldValidatorError
       
   134         CESMRMemoTimeValidator::ValidateL( TBool aCorrectAutomatically )
       
   135     {
       
   136     FUNC_LOG;
       
   137     TTime start = StartDateTimeL();
       
   138     TTime end = EndDateTimeL();
       
   139 
       
   140     PreValidateEditorContent();
       
   141 
       
   142     if ( aCorrectAutomatically )
       
   143         {
       
   144         const CCalEntry& orginalEntry( iEntry->OriginalEntry() );
       
   145         if ( end < start )
       
   146             {
       
   147             if ( iEntry->IsStoredL() )
       
   148                 {
       
   149                 start = orginalEntry.StartTimeL().TimeLocalL();
       
   150                 end = orginalEntry.EndTimeL().TimeLocalL();
       
   151                 }
       
   152             else
       
   153                 {
       
   154                 end = start;
       
   155                 }
       
   156             }
       
   157 
       
   158         iCurrentStartTime = start;
       
   159         iCurrentEndTime = end;
       
   160 
       
   161         SetDateToEditor( *iStartDate, iCurrentStartTime );
       
   162         SetDateToEditor( *iEndDate, iCurrentEndTime );
       
   163         }
       
   164     else
       
   165         {
       
   166         if ( end < start )
       
   167             {
       
   168             return MESMRFieldValidator::EErrorEndEarlierThanStart;
       
   169             }
       
   170         }
       
   171 
       
   172     return MESMRFieldValidator::EErrorNone;
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CESMRMemoTimeValidator::ReadValuesFromEntryL
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CESMRMemoTimeValidator::ReadValuesFromEntryL(
       
   180         MESMRCalEntry& aEntry )
       
   181     {
       
   182     FUNC_LOG;
       
   183     if ( MESMRCalEntry::EESMRCalEntryMemo == aEntry.Type() )
       
   184         {
       
   185         __ASSERT_DEBUG( iStartDate, Panic( EESMRMemoValidatorNullStartDate) );
       
   186         __ASSERT_DEBUG( iEndDate, Panic( EESMRMemoValidatorNullStopDate) );
       
   187 
       
   188         iEntry = &aEntry;
       
   189         CCalEntry& entry( iEntry->Entry() );
       
   190 
       
   191         iCurrentStartTime = entry.StartTimeL().TimeLocalL();
       
   192         
       
   193         if ( aEntry.IsAllDayEventL() )
       
   194             {
       
   195             // if the event is allday event, previous day for end date will be
       
   196             // shown in UI. See Outlook for reference...
       
   197             iCurrentEndTime = iCurrentStartTime;
       
   198             }
       
   199         else
       
   200             {
       
   201             iCurrentEndTime = entry.EndTimeL().TimeLocalL();            
       
   202             }
       
   203 
       
   204         SetDateToEditor( *iStartDate, iCurrentStartTime );
       
   205         SetDateToEditor( *iEndDate, iCurrentEndTime );
       
   206 
       
   207         DrawEditorsDeferred();
       
   208         }
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CESMRMemoTimeValidator::StoreValuesToEntryL
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CESMRMemoTimeValidator::StoreValuesToEntryL(
       
   216         MESMRCalEntry& aEntry )
       
   217     {
       
   218     FUNC_LOG;
       
   219     if ( MESMRCalEntry::EESMRCalEntryMemo == aEntry.Type() )
       
   220         {
       
   221         PreValidateEditorContent();
       
   222 
       
   223         CCalEntry& entry( aEntry.Entry() );
       
   224 
       
   225         TTime start = StartDateTimeL();
       
   226         TTime end = EndDateTimeL();
       
   227 
       
   228         if ( end < start )
       
   229             {
       
   230             User::Leave( KErrArgument );
       
   231             }
       
   232 
       
   233         TCalTime startTime;
       
   234         TCalTime endTime;
       
   235 
       
   236         // The default mode for memo is EFloating,
       
   237         // But some 3rd party application might have saved a different type
       
   238         // for one reason or another. In that case we are using
       
   239         // the existing value.
       
   240         if ( aEntry.IsStoredL() )
       
   241             {
       
   242             TCalTime::TTimeMode timeMode =
       
   243                         aEntry.Entry().StartTimeL().TimeMode();
       
   244 
       
   245             switch ( timeMode )
       
   246                 {
       
   247                 case TCalTime::EFixedUtc:
       
   248                 	{
       
   249 					startTime.SetTimeUtcL( start );
       
   250 					endTime.SetTimeUtcL( end );
       
   251 					break;
       
   252                 	}
       
   253                 case TCalTime::EFixedTimeZone:
       
   254                 	{
       
   255 					startTime.SetTimeLocalL( start );
       
   256 					endTime.SetTimeLocalL( end );
       
   257 					break;
       
   258                 	}
       
   259                 case TCalTime::EFloating: // Fall through
       
   260                 default:
       
   261                 	{
       
   262 					startTime.SetTimeLocalFloatingL( start );
       
   263 					endTime.SetTimeLocalFloatingL( end );
       
   264 					break;
       
   265                 	}
       
   266                 }
       
   267             }
       
   268         else
       
   269             {
       
   270             startTime.SetTimeLocalFloatingL( start );
       
   271             endTime.SetTimeLocalFloatingL( end );
       
   272             }
       
   273 
       
   274         entry.SetStartAndEndTimeL( startTime, endTime );
       
   275         }
       
   276     }
       
   277 
       
   278 // ---------------------------------------------------------------------------
       
   279 // CESMRMemoTimeValidator::SetStartTimeFieldL
       
   280 // ---------------------------------------------------------------------------
       
   281 //
       
   282 void CESMRMemoTimeValidator::SetStartTimeFieldL(
       
   283         CEikTimeEditor& /*aStartTime*/ )
       
   284     {
       
   285     FUNC_LOG;
       
   286     // No implementation for memo
       
   287     }
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // CESMRMemoTimeValidator::SetEndTimeFieldL
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CESMRMemoTimeValidator::SetEndTimeFieldL(
       
   294         CEikTimeEditor& /*aEndTime*/ )
       
   295     {
       
   296     FUNC_LOG;
       
   297     // No implementation for memo
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CESMRMemoTimeValidator::SetStartDateFieldL
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CESMRMemoTimeValidator::SetStartDateFieldL(
       
   305         CEikDateEditor& aStartDate )
       
   306     {
       
   307     FUNC_LOG;
       
   308     iStartDate = &aStartDate;
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // CESMRMemoTimeValidator::SetEndDateFieldL
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void CESMRMemoTimeValidator::SetEndDateFieldL(
       
   316         CEikDateEditor& aEndDate )
       
   317     {
       
   318     FUNC_LOG;
       
   319     iEndDate = &aEndDate;
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // CESMRMemoTimeValidator::SetAlarmTimeFieldL
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 void CESMRMemoTimeValidator::SetAlarmTimeFieldL(
       
   327         CEikTimeEditor& /*aAlarmTime*/ )
       
   328     {
       
   329     FUNC_LOG;
       
   330     // No implementation for memo
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CESMRMemoTimeValidator::SetAlarmDateFieldL
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 void CESMRMemoTimeValidator::SetAlarmDateFieldL(
       
   338         CEikDateEditor& /*aAlarmDate*/ )
       
   339     {
       
   340     FUNC_LOG;
       
   341     // No implementation for memo
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // CESMRMemoTimeValidator::SetRecurrenceUntilDateFieldL
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 void CESMRMemoTimeValidator::SetRecurrenceUntilDateFieldL(
       
   349             CEikDateEditor& /*aRecurrenceUntil*/ )
       
   350     {
       
   351     FUNC_LOG;
       
   352     // No implementation for memo
       
   353     }
       
   354 
       
   355 // ---------------------------------------------------------------------------
       
   356 // CESMRMemoTimeValidator::StartTimeChangedL
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 void CESMRMemoTimeValidator::StartTimeChangedL()
       
   360     {
       
   361     FUNC_LOG;
       
   362     StartDateChandedL();
       
   363     }
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 // CESMRMemoTimeValidator::EndTimeChangedL
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CESMRMemoTimeValidator::EndTimeChangedL()
       
   370     {
       
   371     FUNC_LOG;
       
   372     EndDateChangedL();
       
   373     }
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CESMRMemoTimeValidator::StartDateChandedL
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 void CESMRMemoTimeValidator::StartDateChandedL()
       
   380     {
       
   381     FUNC_LOG;
       
   382     PreValidateEditorContent();
       
   383 
       
   384     TTime start = StartDateTimeL();
       
   385     TTime end = EndDateTimeL();
       
   386 
       
   387     TTimeIntervalMinutes diff;
       
   388     start.MinutesFrom( iCurrentStartTime, diff );
       
   389 
       
   390     end += diff;
       
   391 
       
   392     iCurrentStartTime = start;
       
   393     iCurrentEndTime = end;
       
   394 
       
   395     SetDateToEditor( *iStartDate, iCurrentStartTime );
       
   396     SetDateToEditor( *iEndDate, iCurrentEndTime );
       
   397 
       
   398     DrawEditorsDeferred();
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CESMRMemoTimeValidator::EndDateChangedL
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 void CESMRMemoTimeValidator::EndDateChangedL()
       
   406     {
       
   407     FUNC_LOG;
       
   408     TInt err( KErrNone );
       
   409 
       
   410     PreValidateEditorContent();
       
   411 
       
   412     TTime start = StartDateTimeL();
       
   413     TTime end = EndDateTimeL();
       
   414 
       
   415     if ( end < start )
       
   416         {
       
   417         err = KErrArgument;
       
   418         }
       
   419     else
       
   420         {
       
   421         iCurrentEndTime = end;
       
   422         }
       
   423 
       
   424     SetDateToEditor( *iStartDate, iCurrentStartTime );
       
   425     SetDateToEditor( *iEndDate, iCurrentEndTime );
       
   426 
       
   427     DrawEditorsDeferred();
       
   428 
       
   429     User::LeaveIfError( err );
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // CESMRMemoTimeValidator::AlarmTimeChangedL
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CESMRMemoTimeValidator::AlarmTimeChangedL()
       
   437     {
       
   438     FUNC_LOG;
       
   439     // No implementation for memo
       
   440     }
       
   441 
       
   442 // ---------------------------------------------------------------------------
       
   443 // CESMRMemoTimeValidator::AlarmDateChangedL
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 void CESMRMemoTimeValidator::AlarmDateChangedL()
       
   447     {
       
   448     FUNC_LOG;
       
   449     // No implementation for memo
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // CESMRMemoTimeValidator::RelativeAlarmChangedL
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 void CESMRMemoTimeValidator::RelativeAlarmChangedL(
       
   457         TTimeIntervalMinutes /*aCurrentAlarmTimeOffset*/,
       
   458         TBool /*aHandleAlarmChange*/,
       
   459         TBool& /*aRelativeAlarmValid*/ )
       
   460     {
       
   461     FUNC_LOG;
       
   462     // No implementation for memo
       
   463     }
       
   464 
       
   465 // ---------------------------------------------------------------------------
       
   466 // CESMRMemoTimeValidator::SetAllDayEventL
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 void CESMRMemoTimeValidator::SetAllDayEventL(
       
   470         TBool /*aAlldayEvent*/ )
       
   471     {
       
   472     FUNC_LOG;
       
   473     // No implementation for memo
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // CESMRMemoTimeValidator::SetAlarmOnOffL
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 void CESMRMemoTimeValidator::SetAlarmOnOffL(
       
   481         TBool /*aAlarmOn*/ )
       
   482     {
       
   483     FUNC_LOG;
       
   484     // No implementation for memo
       
   485     }
       
   486 
       
   487 // ---------------------------------------------------------------------------
       
   488 // CESMRMemoTimeValidator::RecurrenceChangedL
       
   489 // ---------------------------------------------------------------------------
       
   490 //
       
   491 void CESMRMemoTimeValidator::RecurrenceChangedL(
       
   492         TESMRRecurrenceValue /*aRecurrence*/ )
       
   493     {
       
   494     FUNC_LOG;
       
   495     // No implementation for memo
       
   496     }
       
   497 
       
   498 // ---------------------------------------------------------------------------
       
   499 // CESMRMemoTimeValidator::RecurrenceEndDateChangedL
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 void CESMRMemoTimeValidator::RecurrenceEndDateChangedL()
       
   503     {
       
   504     FUNC_LOG;
       
   505     // No implementation for memo
       
   506     }
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // CESMRMemoTimeValidator::IsRelativeAlarmValid
       
   510 // ---------------------------------------------------------------------------
       
   511 //
       
   512 TBool CESMRMemoTimeValidator::IsRelativeAlarmValid(
       
   513 		TTimeIntervalMinutes /*aAlarmTimeOffset*/ )
       
   514     {
       
   515     FUNC_LOG;
       
   516     __ASSERT_DEBUG( ETrue, Panic( EESMRMemoValidatorRelativeAlarm ) );
       
   517     return EFalse;
       
   518     }
       
   519 
       
   520 // ---------------------------------------------------------------------------
       
   521 // CESMRMemoTimeValidator::PreValidateEditorContent
       
   522 // ---------------------------------------------------------------------------
       
   523 //
       
   524 TInt CESMRMemoTimeValidator::PreValidateEditorContent()
       
   525     {
       
   526     FUNC_LOG;
       
   527     TInt err( KErrNone );
       
   528     TRAP( err, PreValidateEditorContentL() );
       
   529     return err;
       
   530     }
       
   531 
       
   532 // ---------------------------------------------------------------------------
       
   533 // CESMRMemoTimeValidator::PreValidateEditorContentL
       
   534 // ---------------------------------------------------------------------------
       
   535 //
       
   536 void CESMRMemoTimeValidator::PreValidateEditorContentL()
       
   537     {
       
   538     FUNC_LOG;
       
   539     if ( iStartDate && iStartDate->IsVisible() )
       
   540         {
       
   541         iStartDate->PrepareForFocusLossL();
       
   542         }
       
   543 
       
   544     if ( iEndDate && iEndDate->IsVisible() )
       
   545         {
       
   546         iEndDate->PrepareForFocusLossL();
       
   547         }
       
   548     }
       
   549 
       
   550 // ---------------------------------------------------------------------------
       
   551 // CESMRMemoTimeValidator::DrawEditorsDeferred
       
   552 // ---------------------------------------------------------------------------
       
   553 //
       
   554 void CESMRMemoTimeValidator::DrawEditorsDeferred()
       
   555     {
       
   556     FUNC_LOG;
       
   557     if ( iStartDate && iStartDate->IsVisible() )
       
   558         {
       
   559         iStartDate->DrawDeferred();
       
   560         }
       
   561 
       
   562     if ( iEndDate && iEndDate->IsVisible() )
       
   563         {
       
   564         iEndDate->DrawDeferred();
       
   565         }
       
   566     }
       
   567 
       
   568 // ---------------------------------------------------------------------------
       
   569 // CESMRMemoTimeValidator::StartDateTimeL
       
   570 // ---------------------------------------------------------------------------
       
   571 //
       
   572 TDateTime CESMRMemoTimeValidator::StartDateTimeL()
       
   573     {
       
   574     FUNC_LOG;
       
   575     return iStartDate->Date().DateTime();
       
   576     }
       
   577 
       
   578 // ---------------------------------------------------------------------------
       
   579 // CESMRMemoTimeValidator::EndDateTimeL
       
   580 // ---------------------------------------------------------------------------
       
   581 //
       
   582 TDateTime CESMRMemoTimeValidator::EndDateTimeL()
       
   583     {
       
   584     FUNC_LOG;
       
   585     TDateTime endTime = iEndDate->Date().DateTime();
       
   586     // these need to be set; calendar views does not show
       
   587     // the memo for last date if end date is set different
       
   588     // from start date.
       
   589 
       
   590     endTime.SetHour(KMaxHours);
       
   591     endTime.SetMinute(KMaxMinutes);
       
   592     endTime.SetSecond(KMaxSeconds);
       
   593 
       
   594     return endTime;
       
   595     }
       
   596 
       
   597 // EOF
       
   598