00001
00002
00003
00004
00005
00006 #include <calsession.h>
00007 #include <calentryview.h>
00008 #include <CalInstance.h>
00009 #include <calinstanceview.h>
00010 #include <txtfmlyr.h>
00011 #include <calalarm.h>
00012 #include <CalendarAPIexample.rsg>
00013 #include "CalendarAPIexampleEngine.h"
00014 #include "CalendarAPIexample.hrh"
00015
00016 #include "CalendarAPIexampleEntryView.h"
00017 #include "CalendarAPIexampleEntryContainer.h"
00018 #include "CalendarAPIexampleEntriesView.h"
00019 #include "CalendarAPIexampleSearchView.h"
00020
00021 #include "CalendarAPIexample.pan"
00022
00023 #include "CalendarAPIexampleEntryView.h"
00024
00025
00026 const TInt KProgressFinalValue = 100;
00027 _LIT(KDefaultTodoListName, "TODO");
00028
00029 #define KDefaultEventDisplayTime TTimeIntervalMinutes(660)
00030 #define KDefaultAnnivDisplayTime TTimeIntervalMinutes(900)
00031 #define KDefaultDayNoteDisplayTime TTimeIntervalMinutes(1000)
00032
00033
00034
00035
00036 CCalendarAPIexampleEngine::CCalendarAPIexampleEngine(
00037 MCalenderEngineObserverUI& aAppUi)
00038 : iAppUi(aAppUi)
00039 {
00040
00041
00042 }
00043
00044
00045 CCalendarAPIexampleEngine::~CCalendarAPIexampleEngine()
00046 {
00047
00048 if( iWait )
00049 {
00050 if( iWait->IsStarted() )
00051 {
00052 iWait->AsyncStop();
00053
00054 }
00055 delete iWait;
00056 iWait = NULL;
00057 }
00058 if (iEntryView)
00059 {
00060 delete iEntryView;
00061 iEntryView = 0;
00062 }
00063
00064 if (iInstanceView)
00065 {
00066 delete iInstanceView;
00067 iInstanceView = 0;
00068 }
00069
00070 iEntries.ResetAndDestroy();
00071
00072 delete iEntry;
00073 iEntry = NULL;
00074
00075 delete iProgressDialog;
00076 iProgressDialog = NULL;
00077
00078 if (iCalSession)
00079 {
00080 delete iCalSession;
00081 iCalSession = 0;
00082 }
00083 }
00084
00085
00086
00087 CCalendarAPIexampleEngine* CCalendarAPIexampleEngine::NewL(
00088 MCalenderEngineObserverUI& aAppUi)
00089 {
00090 CCalendarAPIexampleEngine* self =
00091 new (ELeave) CCalendarAPIexampleEngine(aAppUi);
00092 CleanupStack::PushL(self);
00093 self->ConstructL();
00094 CleanupStack::Pop(self);
00095 return self;
00096 }
00097
00098
00099 void CCalendarAPIexampleEngine::ConstructL()
00100 {
00101 iModifyIndex = KUndefinedModifyIndex;
00102
00103
00104 iCalSession = CCalSession::NewL();
00105
00106 const TDesC& file = iCalSession->DefaultFileNameL();
00107
00108 TRAPD(err,iCalSession->OpenL(file) );
00109 if( err == KErrNotFound)
00110 {
00111 iCalSession->CreateCalFileL(file);
00112 iCalSession->OpenL(file);
00113 }
00114 else
00115 {
00116 User::LeaveIfError(err);
00117 }
00118
00119 iWait = new (ELeave) CActiveSchedulerWait;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 void CCalendarAPIexampleEngine::Progress(TInt aPercentageCompleted)
00135 {
00136 if (!iProgressDialog)
00137 {
00138
00139 TRAPD
00140 (
00141 error,
00142 iProgressDialog = new (ELeave) CAknProgressDialog(
00143 REINTERPRET_CAST(CEikDialog**, &iProgressDialog), ETrue);
00144 iProgressDialog->PrepareLC(R_CALENDARAPIEXAMPLE_PROGRESS_NOTE);
00145 iProgressInfo = iProgressDialog->GetProgressInfoL();
00146 iProgressDialog->RunLD();
00147 )
00148 if (KErrNone == error)
00149 {
00150 iProgressInfo->SetFinalValue(KProgressFinalValue);
00151 iProgressInfo->SetAndDraw(aPercentageCompleted);
00152 }
00153 else
00154 {
00155 delete iProgressDialog;
00156 iProgressDialog = NULL;
00157 }
00158 }
00159 else
00160 {
00161 iProgressInfo->SetAndDraw(aPercentageCompleted);
00162 }
00163 }
00164
00170 void CCalendarAPIexampleEngine::Completed(TInt )
00171 {
00172 if (iCreatingEntryView)
00173 {
00174 if( iWait->IsStarted() )
00175 {
00176 iWait->AsyncStop();
00177 }
00178 else
00179 {
00180 Panic(EWaitNotStarted);
00181 }
00182 iCreatingEntryView = EFalse;
00183 }
00184
00185 if (iProgressDialog)
00186 {
00187 iProgressInfo->SetAndDraw(100);
00188 TRAPD
00189 (
00190 error,
00191 iProgressDialog->ProcessFinishedL();
00192 )
00193 iProgressInfo = NULL;
00194 iProgressDialog = NULL;
00195 }
00196 }
00197
00202 TBool CCalendarAPIexampleEngine::NotifyProgress()
00203 {
00204 return ETrue;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 void CCalendarAPIexampleEngine::AddAnniversaryL(CCalEntry* aAnniv)
00219 {
00220 if (!iEntryView)
00221 {
00222 iEntryView = CCalEntryView::NewL(*iCalSession, *this);
00223 iCreatingEntryView = ETrue;
00224
00225
00226
00227 if( !iWait->IsStarted() )
00228 {
00229 iWait->Start();
00230 }
00231 else
00232 {
00233 Panic(EWaitAlreadyStarted);
00234 }
00235 }
00236
00237 RPointerArray<CCalEntry> calEntryList;
00238 calEntryList.Append(aAnniv);
00239 TInt num;
00240
00241 iEntryView->StoreL(calEntryList, num);
00242
00243 calEntryList.Reset();
00244
00245 }
00246
00247
00248
00249
00250
00251
00252
00253 void CCalendarAPIexampleEngine::UpdateAnniversaryL( CCalEntry* aAnniv)
00254 {
00255 if (!iEntryView)
00256 {
00257 iEntryView = CCalEntryView::NewL(*iCalSession, *this);
00258 iCreatingEntryView = ETrue;
00259
00260
00261
00262 if( !iWait->IsStarted() )
00263 {
00264 iWait->Start();
00265 }
00266 else
00267 {
00268 Panic(EWaitAlreadyStarted);
00269 }
00270 }
00271 RPointerArray<CCalEntry> calEntryList;
00272 calEntryList.Append(aAnniv);
00273 TInt num;
00274
00275 iEntryView->UpdateL(calEntryList, num);
00276
00277 calEntryList.Reset();
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 void CCalendarAPIexampleEngine::DoSearchL(const TSearchType& aType)
00290 {
00291 iEntries.ResetAndDestroy();
00292
00293 iCurrentSearchType = aType;
00294 TTime now;
00295 now.HomeTime();
00296 TDateTime from = now.DateTime();
00297 User::LeaveIfError(from.SetHour(0));
00298 User::LeaveIfError(from.SetMinute(0));
00299 User::LeaveIfError(from.SetSecond(0));
00300 User::LeaveIfError(from.SetMicroSecond(0));
00301
00302
00303 switch (aType)
00304 {
00305 case EWeek:
00306 now += TTimeIntervalDays(7);
00307 break;
00308 case EMonth:
00309 now += TTimeIntervalMonths(1);
00310 break;
00311 case ESixMonths:
00312 now += TTimeIntervalMonths(6);
00313 break;
00314 case EYear:
00315 now += TTimeIntervalYears(1);
00316 break;
00317 }
00318
00319 TDateTime to = now.DateTime();
00320
00321 if (!iInstanceView)
00322 {
00323 iInstanceView = CCalInstanceView::NewL(*iCalSession, *this);
00324 iCreatingEntryView = ETrue;
00325
00326
00327
00328 if( !iWait->IsStarted() )
00329 {
00330 iWait->Start();
00331 }
00332 else
00333 {
00334 Panic(EWaitAlreadyStarted);
00335 }
00336 }
00337
00338 RPointerArray<CCalInstance> instanceArray;
00339 TTime nextDate(from);
00340
00341
00342 nextDate -= TTimeIntervalDays(1);
00343 TTime toDate(to);
00344 TCalTime fromCalTime;
00345 fromCalTime.SetTimeLocalL(from);
00346 TCalTime toCalTime;
00347 toCalTime.SetTimeLocalL(to);
00348 CalCommon::TCalTimeRange timeRange(fromCalTime, toCalTime);
00349
00350 iInstanceView->FindInstanceL(instanceArray, CalCommon::EIncludeAnnivs, timeRange);
00351
00352
00353 for (TInt i = 0; i < instanceArray.Count(); i++)
00354 {
00355 CCalHelperEntry* entry = CCalHelperEntry::NewLC(instanceArray[i]);
00356 User::LeaveIfError(iEntries.Append(entry));
00357 CleanupStack::Pop(entry);
00358 }
00359
00360
00361 instanceArray.Reset();
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 void CCalendarAPIexampleEngine::DeleteEntryL(const TInt& aIndex)
00374 {
00375
00376
00377 if( aIndex == -1)
00378 {
00379 delete iEntry;
00380 iEntry = NULL;
00381 return;
00382 }
00383
00384 if (aIndex < 0 || aIndex >= iEntries.Count())
00385 {
00386 Panic(KInvalidEntryIndex);
00387 }
00388
00389 CCalHelperEntry* entry = iEntries[aIndex];
00390
00391 CCalEntry* anniv = entry->Anniv();
00392
00393 if (!iEntryView)
00394 {
00395 iEntryView = CCalEntryView::NewL(*iCalSession, *this);
00396 iCreatingEntryView = ETrue;
00397
00398
00399
00400
00401 if( !iWait->IsStarted() )
00402 {
00403 iWait->Start();
00404 }
00405 else
00406 {
00407 Panic(EWaitAlreadyStarted);
00408 }
00409 }
00410
00411 iEntryView->DeleteL(*anniv);
00412
00413 iEntries.Remove(aIndex);
00414
00415 delete entry;
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 void CCalendarAPIexampleEngine::DoAddL()
00429 {
00430
00431 iModifyIndex = KUndefinedModifyIndex;
00432
00433 iAppUi.ActivateView(KEntryViewId);
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443 void CCalendarAPIexampleEngine::DoSaveL()
00444 {
00445
00446
00447
00448
00449 if (iEntry)
00450
00451 {
00452 if (iEntry->Modified())
00453 {
00454 iEntry->SaveValuesL();
00455
00456 CCalEntry* anniv = iEntry->Anniv();
00457
00458 if (!iEntryView)
00459 {
00460 iEntryView = CCalEntryView::NewL(*iCalSession, *this);
00461 iCreatingEntryView = ETrue;
00462
00463
00464
00465 if( !iWait->IsStarted() )
00466 {
00467 iWait->Start();
00468 }
00469 else
00470 {
00471 Panic(EWaitAlreadyStarted);
00472 }
00473 }
00474
00475 RPointerArray<CCalEntry> calEntryList;
00476 calEntryList.Append(anniv);
00477 TInt num;
00478
00479 iEntryView->StoreL(calEntryList, num);
00480
00481 calEntryList.Reset();
00482
00483 }
00484 }
00485
00486 else
00487 {
00488 if (0 > iModifyIndex || iEntries.Count() <= iModifyIndex)
00489 {
00490 Panic(KInvalidModifyIndex);
00491 }
00492
00493 CCalHelperEntry* entry = iEntries[iModifyIndex];
00494
00495 if (entry->Modified())
00496 {
00497 if (entry->DateHasChanged())
00498 {
00499 CCalEntry* updateAnniv = entry->Anniv();
00500 TTime startDate(entry->Date());
00501 TCalTime time;
00502 time.SetTimeLocalL(startDate);
00503 updateAnniv->SetStartAndEndTimeL(time,time);
00504 UpdateAnniversaryL(updateAnniv);
00505 }
00506
00507 else
00508 {
00509 entry->SaveValuesL();
00510 CCalEntry* updateAnniv = entry->Anniv();
00511 UpdateAnniversaryL(updateAnniv);
00512 }
00513 DoSearchL(iCurrentSearchType);
00514 }
00515 }
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 void CCalendarAPIexampleEngine::SetModifyIndex(const TInt& aIndex)
00529 {
00530 if (aIndex < 0 || aIndex >= iEntries.Count())
00531 {
00532 Panic(KInvalidModifyIndex);
00533 }
00534
00535 iModifyIndex = aIndex;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 void CCalendarAPIexampleEngine::CreateEntryForModificationL(TBool& aModify)
00551 {
00552 delete iEntry;
00553 iEntry = NULL;
00554
00555
00556 if (iModifyIndex < 0)
00557 {
00558 aModify = EFalse;
00559 CCalEntry* nulli = NULL;
00560
00561 iEntry = CCalHelperEntry::NewL(nulli);
00562 }
00563 else
00564 {
00565 aModify = ETrue;
00566 }
00567
00568 if (iModifyIndex >= iEntries.Count())
00569 {
00570 Panic(KInvalidModifyIndex);
00571 }
00572 }
00573
00574 TInt CCalendarAPIexampleEngine::EntryCount() const
00575 {
00576 return iEntries.Count();
00577 }
00578
00579 TBool CCalendarAPIexampleEngine::SetValuesToNewEntry( const TDesC& aName,
00580 const TDateTime& aDate,
00581 const TBool& aAlarm,
00582 const TDateTime& aAlarmTime,
00583 const TInt& aSynchronizationMethod)
00584
00585 {
00586 CCalHelperEntry *entry=NULL;
00587 if( iEntry )
00588 {
00589 entry = iEntry;
00590 }
00591
00592 else
00593 entry = iEntries[iModifyIndex];
00594
00595 return entry->SetValues(aName,aDate,aAlarm,aAlarmTime,aSynchronizationMethod);
00596
00597 }
00598
00599 void CCalendarAPIexampleEngine::GetValuesToSet(TDes& aName, TTime& aDate,
00600 TBool& aAlarm, TTime& aAlarmTime,
00601 TInt& aSync)
00602 {
00603 CCalHelperEntry *entry=NULL;
00604 if( iEntry )
00605 {
00606 entry = iEntry;
00607 }
00608 else
00609 entry = iEntries[iModifyIndex];
00610
00611
00612 aName = entry->Name();
00613 aDate = entry->Date();
00614 aAlarm = entry->Alarm();
00615 aAlarmTime = entry->AlarmTime();
00616 aSync = entry->SynchronizationMethod();
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629 CCalHelperEntry& CCalendarAPIexampleEngine::Entry(const TInt& aIndex)
00630 {
00631 if (0 > aIndex || iEntries.Count() <= aIndex)
00632 {
00633 Panic(EOutOfEntriesArray);
00634 }
00635
00636 return *iEntries[aIndex];
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 TInt CCalendarAPIexampleEngine::ModifyIndex() const
00650 {
00651 return iModifyIndex;
00652 }
00653
00654 void CCalendarAPIexampleEngine::ExecuteDeletionL()
00655 {
00656 DeleteEntryL(ModifyIndex());
00657
00658
00659 if (EntryCount() == 0)
00660 {
00661 iAppUi.ActivateView(KSearchViewId);
00662 }
00663
00664 else
00665 {
00666 iAppUi.ActivateView(KEntriesViewId);
00667 }
00668 }
00669
00670
00671