00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "CalendarHelperEntry.h"
00038
00039 #include <e32math.h>
00040 #include <calalarm.h>
00041 #include <CalInstance.h>
00042 #include "CalendarAPIexample.pan"
00043
00044 #include <calentry.h>
00045
00046
00047
00048 const TInt KDefaultAlarmHour = 8;
00049 const TInt KDefaultAnniversaryHour = 8;
00050 const TInt KMinutesInHour = 60;
00051 const TInt KGuidLength = 30;
00052
00053
00054 CCalHelperEntry* CCalHelperEntry::NewL(CCalEntry* aAnniv)
00055 {
00056 CCalHelperEntry* self = CCalHelperEntry::NewLC(aAnniv);
00057 CleanupStack::Pop(self);
00058 return self;
00059 }
00060
00061
00062 CCalHelperEntry* CCalHelperEntry::NewLC(CCalEntry* aAnniv)
00063 {
00064 CCalHelperEntry* self = new (ELeave) CCalHelperEntry;
00065 CleanupStack::PushL(self);
00066 self->ConstructL(aAnniv);
00067 return self;
00068 }
00069
00070
00071 CCalHelperEntry* CCalHelperEntry::NewL(CCalInstance* aAnniv)
00072 {
00073 CCalHelperEntry* self = CCalHelperEntry::NewLC(aAnniv);
00074 CleanupStack::Pop(self);
00075 return self;
00076 }
00077
00078
00079 CCalHelperEntry* CCalHelperEntry::NewLC(CCalInstance* aAnniv)
00080 {
00081 CCalHelperEntry* self = new (ELeave) CCalHelperEntry;
00082 CleanupStack::PushL(self);
00083 self->ConstructL(aAnniv);
00084 return self;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094 void CCalHelperEntry::ConstructL(CCalInstance* aAnniv)
00095 {
00096 iInstance = aAnniv;
00097 ConstructL(&aAnniv->Entry());
00098 }
00099
00100 void CCalHelperEntry::ConstructL(CCalEntry* aAnniv)
00101 {
00102
00103 TTime now;
00104 now.HomeTime();
00105 iDate = now.DateTime();
00106
00107 if ( aAnniv )
00108 {
00109 TTime starttime = aAnniv->StartTimeL().TimeLocalL();
00110 TDateTime time2 = starttime.DateTime();
00111
00112 TTime endtime = aAnniv->StartTimeL().TimeLocalL();
00113 TDateTime time3 = endtime.DateTime();
00114 }
00115
00116 iDate.SetHour(KDefaultAnniversaryHour);
00117 iDate.SetMinute(0);
00118 iDate.SetSecond(0);
00119 iDate.SetMicroSecond(0);
00120
00121 iAlarmTime = now.DateTime();
00122 User::LeaveIfError(iAlarmTime.SetHour(KDefaultAlarmHour));
00123 User::LeaveIfError(iAlarmTime.SetMinute(0));
00124 User::LeaveIfError(iAlarmTime.SetSecond(0));
00125 User::LeaveIfError(iAlarmTime.SetMicroSecond(0));
00126 iAlarmDays = TTimeIntervalDays(0);
00127
00128 iAlarmMinutes = TTimeIntervalMinutes(KDefaultAlarmHour*KMinutesInHour);
00129
00130 if (!aAnniv)
00131 {
00132 return;
00133 }
00134
00135 iAnniv = aAnniv;
00136
00137
00138 TBuf<KMaxNameLength> name;
00139 name = aAnniv->SummaryL();
00140 iName = name;
00141
00142
00143 iDate = aAnniv->StartTimeL().TimeLocalL().DateTime();
00144
00145 iAlarmPtr = iAnniv->AlarmL();
00146
00147 iAlarm = iAlarmPtr ? ETrue : EFalse;
00148 if (iAlarm)
00149 {
00150
00151 iAlarmMinutes = -iAlarmPtr->TimeOffset().Int();
00152 iAlarmTime = iDate;
00153 TTime alarm = iAlarmTime;
00154 alarm += iAlarmMinutes;
00155 iAlarmTime = alarm.DateTime();
00156 }
00157
00158
00159 CCalEntry::TReplicationStatus annivReplStatus =
00160 aAnniv->ReplicationStatusL();
00161 if (annivReplStatus == CCalEntry::EOpen)
00162 {
00163 SetSynchronizationMethod(KSyncPublic);
00164 }
00165 else if (annivReplStatus == CCalEntry::EPrivate)
00166 {
00167 SetSynchronizationMethod(KSyncPrivate);
00168 }
00169 else
00170 {
00171 SetSynchronizationMethod(KSyncNo);
00172 }
00173
00174 iModified = EFalse;
00175 }
00176
00177
00178 CCalHelperEntry::CCalHelperEntry()
00179 {
00180 }
00181
00182
00183 CCalHelperEntry::~CCalHelperEntry()
00184 {
00185
00186
00187 if( iInstance )
00188 {
00189 delete iInstance;
00190 iInstance = NULL;
00191 }
00192 else
00193 {
00194 if (iAnniv)
00195 delete iAnniv;
00196 iAnniv=NULL;
00197 }
00198 if (iAlarmPtr)
00199 delete iAlarmPtr;
00200 iAlarmPtr=NULL;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 CCalEntry* CCalHelperEntry::Anniv()
00210 {
00211 CCalEntry* anniv = iAnniv;
00212 return anniv;
00213 }
00214
00215
00216
00217
00218
00219
00220 TBuf<KMaxNameLength> CCalHelperEntry::Name() const
00221 {
00222 return iName;
00223 }
00224
00225
00226
00227
00228
00229
00230 TDateTime CCalHelperEntry::Date() const
00231 {
00232 return iDate;
00233 }
00234
00235
00236
00237
00238
00239
00240 TBool CCalHelperEntry::Alarm() const
00241 {
00242 return iAlarm;
00243 }
00244
00245
00246
00247
00248
00249
00250 TDateTime CCalHelperEntry::AlarmTime() const
00251 {
00252 return iAlarmTime;
00253 }
00254
00255
00256
00257
00258
00259
00260 TInt CCalHelperEntry::SynchronizationMethod() const
00261 {
00262 return iSynchronizationMethod;
00263 }
00264
00265
00266
00267
00268
00269
00270 TBool CCalHelperEntry::Modified() const
00271 {
00272 return iModified;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 TBool CCalHelperEntry::DateHasChanged() const
00282 {
00283 return iDateModified;
00284 }
00285
00286
00287
00288
00289
00290
00291 void CCalHelperEntry::ResetTimeL(TDateTime& aDate) const
00292 {
00293 User::LeaveIfError(aDate.SetHour(0));
00294 User::LeaveIfError(aDate.SetMinute(0));
00295 User::LeaveIfError(aDate.SetSecond(0));
00296 User::LeaveIfError(aDate.SetMicroSecond(0));
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 void CCalHelperEntry::GetDaysAndMinutesL(const TDateTime& aTime,
00306 const TDateTime& aFromTime,
00307 TTimeIntervalDays& aDays,
00308 TTimeIntervalMinutes& aMinutes) const
00309 {
00310
00311 TDateTime modifiableTime = aTime;
00312
00313 ResetTimeL(modifiableTime);
00314
00315 TTime resetTime(modifiableTime);
00316
00317
00318 TDateTime modifiableFromTime = aFromTime;
00319
00320 ResetTimeL(modifiableFromTime);
00321
00322 TTime resetFromTime(modifiableFromTime);
00323
00324
00325 TTime unresetTime(aTime);
00326
00327
00328
00329 aDays = resetFromTime.DaysFrom(resetTime);
00330
00331 TTimeIntervalMinutes minutes;
00332
00333
00334 User::LeaveIfError(unresetTime.MinutesFrom(resetTime, minutes));
00335
00336 aMinutes = minutes;
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 TBool CCalHelperEntry::SetValues( const TDesC& aName,
00349 const TDateTime& aDate,
00350 const TBool& aAlarm,
00351 const TDateTime& aAlarmTime,
00352 const TInt& aSync)
00353 {
00354 SetDate(aDate);
00355 SetName(aName);
00356 SetSynchronizationMethod(aSync);
00357 SetAlarm(aAlarm);
00358
00359 if (aAlarm)
00360 {
00361
00362 TTime startTime = iDate;
00363
00364
00365
00366
00367
00368 TDateTime alarmDay = iDate;
00369
00370
00371
00372 alarmDay.SetHour(0);
00373 alarmDay.SetMinute(0);
00374 alarmDay.SetMicroSecond(0);
00375
00376
00377 TTimeIntervalDays oneDay(1);
00378 TTime time = alarmDay;
00379 time += oneDay;
00380
00381 alarmDay = time.DateTime();
00382
00383
00384 TTime alarmTime = aAlarmTime;
00385 TTimeIntervalMinutes minutes;
00386 alarmTime.MinutesFrom( alarmDay, minutes );
00387
00388 if (minutes.Int() > 0)
00389 {
00390 return EFalse;
00391 }
00392
00393
00394 TTime now;
00395 now.HomeTime();
00396
00397 now.MinutesFrom(alarmTime, minutes);
00398
00399 if (minutes.Int() > 0)
00400 {
00401 return EFalse;
00402 }
00403
00404 TTime date = iDate;
00405 alarmTime = aAlarmTime;
00406 alarmTime.MinutesFrom(date, minutes);
00407
00408 iAlarmTime = aAlarmTime;
00409 iAlarmMinutes = minutes;
00410
00411 iModified = ETrue;
00412 }
00413
00414 return ETrue;
00415 }
00416
00417
00418
00419
00420
00421
00422
00423 void CCalHelperEntry::SetName(const TDesC& aName)
00424 {
00425 if (aName != iName)
00426 {
00427 iName = aName;
00428 iModified = ETrue;
00429 }
00430 }
00431
00432
00433
00434
00435
00436
00437
00438 void CCalHelperEntry::SetDate(const TDateTime& aDate)
00439 {
00440
00441 TTime currentDate(iDate);
00442 TTime newDate(aDate);
00443 if (newDate != currentDate)
00444 {
00445 iDate = aDate;
00446
00447 iDate.SetHour(KDefaultAnniversaryHour);
00448
00449 iModified = ETrue;
00450 iDateModified = ETrue;
00451 }
00452 }
00453
00454
00455
00456
00457
00458
00459
00460 void CCalHelperEntry::SetAlarm(const TBool& aAlarm)
00461 {
00462 if (aAlarm != iAlarm)
00463 {
00464 iAlarm = aAlarm;
00465
00466 iModified = ETrue;
00467 }
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 void CCalHelperEntry::SetSynchronizationMethod(const TInt& aSynchronizationMethod)
00479 {
00480 if (aSynchronizationMethod != iSynchronizationMethod)
00481 {
00482 iSynchronizationMethod = aSynchronizationMethod;
00483 iModified = ETrue;
00484 }
00485 }
00486
00487
00488
00489
00490
00491
00492
00493 CCalEntry* CCalHelperEntry::CreateAnnivL()
00494 {
00495
00496 TTime now;
00497 now.HomeTime();
00498 TInt64 seed = now.Int64();
00499 TInt randNum = Math::Rand( seed );
00500 HBufC8* guid = HBufC8::NewLC(KGuidLength);
00501 guid->Des().Num( randNum );
00502
00503
00504 CCalEntry* anniv = CCalEntry::NewL( CCalEntry::EAnniv, guid,
00505 CCalEntry::EMethodNone, 0 );
00506
00507 CleanupStack::Pop( guid );
00508
00509 CleanupStack::PushL(anniv);
00510 TTime startDate(iDate);
00511 TCalTime time;
00512 time.SetTimeLocalL(startDate);
00513 anniv->SetStartAndEndTimeL(time, time);
00514
00515 if (iAlarm)
00516 {
00517 delete iAlarmPtr;
00518 iAlarmPtr=NULL;
00519 iAlarmPtr = CCalAlarm::NewL();
00520
00521 iAlarmPtr->SetTimeOffset(-iAlarmMinutes.Int());
00522 anniv->SetAlarmL(iAlarmPtr);
00523 }
00524
00525 CleanupStack::Pop(anniv);
00526
00527 return anniv;
00528 }
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 void CCalHelperEntry::SaveValuesL()
00539 {
00540 TBool created = EFalse;
00541 if (iModified)
00542 {
00543 if (!iAnniv)
00544 {
00545
00546
00547
00548
00549
00550
00551
00552 iAnniv = CreateAnnivL();
00553
00554 created = ETrue;
00555 }
00556
00557 SaveValuesToAnnivL(iAnniv);
00558
00559 if( !created )
00560 {
00561 delete iAlarmPtr;
00562 iAlarmPtr = NULL;
00563
00564 if( !iAlarm )
00565 {
00566
00567 iAnniv->SetAlarmL(NULL);
00568 }
00569 else
00570 {
00571
00572 iAlarmPtr = CCalAlarm::NewL();
00573
00574 iAlarmPtr->SetTimeOffset(-iAlarmMinutes.Int());
00575
00576 iAnniv->SetAlarmL(iAlarmPtr);
00577 }
00578 }
00579
00580 iModified = EFalse;
00581 iDateModified = EFalse;
00582
00583 }
00584 }
00585
00586
00587
00588
00589
00590
00591 void CCalHelperEntry::SaveValuesToAnnivL(CCalEntry* aAnniv)
00592 {
00593 aAnniv->SetSummaryL(iName);
00594
00595 CCalEntry::TReplicationStatus sync;
00596 switch (iSynchronizationMethod)
00597 {
00598 case KSyncPublic:
00599 sync=CCalEntry::EOpen;
00600 break;
00601 case KSyncPrivate:
00602 sync=CCalEntry::EPrivate;
00603 break;
00604 case KSyncNo:
00605 sync=CCalEntry::ERestricted;
00606 break;
00607 default:
00608 Panic(KInvalidSyncValue);
00609 break;
00610 }
00611 aAnniv->SetReplicationStatusL(sync);
00612
00613 }
00614
00615
00616
00617
00618
00619
00620
00621 CCalEntry* CCalHelperEntry::NewAnnivLC()
00622 {
00623 CCalEntry* anniv = CreateAnnivL();
00624 CleanupStack::PushL(anniv);
00625 SaveValuesToAnnivL(anniv);
00626 return anniv;
00627 }
00628
00629