# examples/ForumNokia/CalendarExample/src/CalendarAPIexampleEngine.cpp

    00001 /*
    00002  * Copyright © 2008 Nokia Corporation.
    00003  */
    00004
    00005 // INCLUDE FILES
    00006 #include <calsession.h> //CCalSession
    00007 #include <calentryview.h>
    00008 #include <CalInstance.h>
    00009 #include <calinstanceview.h>
    00010 #include <txtfmlyr.h>   //CParaFormatLayer
    00011 #include <calalarm.h>   //CCalAlarm
    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 // CONSTANTS
    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 // ================= MEMBER FUNCTIONS =======================
    00034
    00035 // constructor
    00036 CCalendarAPIexampleEngine::CCalendarAPIexampleEngine(
    00037                                             MCalenderEngineObserverUI& aAppUi)
    00038     : iAppUi(aAppUi)
    00039     {
    00040
    00041
    00042     }
    00043
    00044 // destructor
    00045 CCalendarAPIexampleEngine::~CCalendarAPIexampleEngine()
    00046     {
    00047
    00048     if( iWait )
    00049         {
    00050         if( iWait->IsStarted() )
    00051             {
    00052             iWait->AsyncStop();
    00053             //it's not propably safe to delete right after this
    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 // Two-phased constructor.
    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 // Symbian OS default constructor can leave.
    00099 void CCalendarAPIexampleEngine::ConstructL()
    00100     {
    00101     iModifyIndex = KUndefinedModifyIndex;
    00102
    00103     // allocate and construct server
    00104     iCalSession = CCalSession::NewL();
    00105
    00106     const TDesC& file = iCalSession->DefaultFileNameL();
    00107     // connect to the cal server
    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 /*******************OBSERVER functions***********************/
    00123
    00124 // ----------------------------------------------------
    00125 // CCalendarAPIexampleSearchContainer::Progress()
    00126 // Monitors the progress of agenda model operations.
    00127 // Creates a progress dialog when first called and
    00128 // updates its status on later calls. Note that if the
    00129 // agenda models operation completes quickly, for
    00130 // instance when opening a small file, then Progress()
    00131 // will probably not be called at all.
    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 /****************END OF OBSERVER functions***********************/
    00207
    00208
    00209 /************************HELPER functions***********************/
    00210
    00211 // ----------------------------------------------------
    00212 // CCalendarAPIexampleEngine::AddAnniversaryL()
    00213 // adds an anniversary to the agenda file
    00214 // ----------------------------------------------------
    00215 //
    00216 //A Helper, only called from SaveL
    00217 //
    00218 void CCalendarAPIexampleEngine::AddAnniversaryL(CCalEntry* aAnniv)
    00219     {
    00220     if (!iEntryView)
    00221         {
    00222         iEntryView = CCalEntryView::NewL(*iCalSession, *this);
    00223         iCreatingEntryView = ETrue;
    00224
    00225         //After scheduler starting, the call back will be
    00226         //called when the task is completed.
    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 // CCalendarAPIexampleEngine::UpdateAnniversaryL()
    00249 // updates an anniversary
    00250 // ----------------------------------------------------
    00251 //  A helper, only called from SaveL
    00252 //
    00253 void CCalendarAPIexampleEngine::UpdateAnniversaryL( CCalEntry* aAnniv)
    00254     {
    00255     if (!iEntryView)
    00256         {
    00257         iEntryView = CCalEntryView::NewL(*iCalSession, *this);
    00258         iCreatingEntryView = ETrue;
    00259
    00260         //After scheduler starting, the call back will be called when the task is completed.
    00261         //CActiveScheduler::Start();
    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 /************MCalendarEngineCommandsInterface functions***********************/
    00281
    00282 // ----------------------------------------------------
    00283 // CCalendarAPIexampleEngine::DoSearchL()
    00284 // Searches all anniversaries in given time interval.
    00285 // ----------------------------------------------------
    00286 // Called from CCalendarAPIexampleSearchView::DoSearchL()
    00287 // and also from DoSaveL if anniversaries are modified
    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     //set the time interval
    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         //After scheduler starting, the call back will be
    00327         //called when the task is completed.
    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     // decrease 'from' date by one day, so that 'from' date
    00341     //will be included in search.
    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     //create new CCalHelperEntry for each CCalEntry.
    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     //now all the instances are in iEntries List
    00360
    00361     instanceArray.Reset();
    00362     }
    00363
    00364 // ----------------------------------------------------
    00365 // CCalendarAPIexampleEngine::DeleteEntryL()
    00366 // deletes an anniversary from the agenda file
    00367 //
    00368 // Called from CCalendarAPIexampleEntriesView when
    00369 // the delete command has been selected
    00370 // And from CCalendarAPIexampleEntryView
    00371 // ----------------------------------------------------
    00372
    00373 void CCalendarAPIexampleEngine::DeleteEntryL(const TInt& aIndex)
    00374     {
    00375     //This is the case when user is adding an entry and desides
    00376     //to cancel the addition
    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         //After scheduler starting, the call back will be called
    00399         //when the task is completed.
    00400         //CActiveScheduler::Start();
    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; //now the CCalinstance is deleted if there's one
    00416     }
    00417
    00418 // ----------------------------------------------------
    00419 // CCalendarAPIexampleSearchView::DoAddL()
    00420 // Resets models modify index, so that next call to
    00421 // models EntryForModifycation will create a new entry.
    00422 // Activates the entry view.
    00423 //
    00424 // Called from CCalendarAPIexampleSearchView when
    00425 // the menu option for adding has been selected
    00426 // ----------------------------------------------------
    00427 //
    00428 void CCalendarAPIexampleEngine::DoAddL()
    00429     {
    00430     //Reset the modify index
    00431     iModifyIndex = KUndefinedModifyIndex;
    00432
    00433     iAppUi.ActivateView(KEntryViewId);
    00434     }
    00435
    00436 // ----------------------------------------------------
    00437 // CCalendarAPIexampleEngine::SaveL()
    00438 // Adds or updates the current entry to the agenda file.
    00439 //
    00440 // Called from CCalendarAPIexampleEntryView when the
    00441 // view is closed.
    00442 // ----------------------------------------------------
    00443 void CCalendarAPIexampleEngine::DoSaveL()
    00444     {
    00445     //If iEntry is created in CreateEntryForModificationL then
    00446     //there's a new anniversary being added.
    00447     //add a new anniversary
    00448
    00449     if (iEntry) //this has been created in CreateEntryForModificationL
    00450                 //and the data has been stored by a call to iEntry.SetValuesL
    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                 //After scheduler starting, the call back will be called
    00464                 //when the task is completed.
    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     //modify existing anniversary
    00486     else
    00487         {
    00488         if (0 > iModifyIndex || iEntries.Count() <= iModifyIndex)
    00489             {
    00490             Panic(KInvalidModifyIndex);
    00491             }
    00492         //Get the entry which is being modified
    00493         CCalHelperEntry* entry = iEntries[iModifyIndex];
    00494
    00495         if (entry->Modified()) //are there any changes
    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             //the date of the entry hasn't changed, the entry is just updated.
    00507             else
    00508                 {
    00509                 entry->SaveValuesL();
    00510                 CCalEntry* updateAnniv = entry->Anniv();
    00511                 UpdateAnniversaryL(updateAnniv);
    00512                 }
    00513             DoSearchL(iCurrentSearchType);
    00514             }
    00515         }
    00516     }
    00517
    00518 // ----------------------------------------------------
    00519 // CCalendarAPIexampleEngine::SetModifyIndex()
    00520 // Sets the modify index. Panics with KInvalidModifyIndex
    00521 // if parameter aIndex is negative or greater than the
    00522 // number of entries.
    00523 //
    00524 // Called only from CCalendarAPIexampleEntriesView when the
    00525 // Edit command has been selected
    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 // CCalendarAPIexampleEngine::CreateEntryForModificationL()
    00540 // Returns an entry for modification. Creates a new
    00541 // entry if iModifyIndex hasn't been set. Panics with
    00542 // KInvalidModifyIndex if iModifyIndex is greater than
    00543 // the number of entries.
    00544 //
    00545 // Called from CCalendarAPIexampleEntryView when
    00546 // the container is created.
    00547 // ----------------------------------------------------
    00548 //
    00549
    00550 void CCalendarAPIexampleEngine::CreateEntryForModificationL(TBool& aModify)
    00551     {
    00552     delete iEntry;
    00553     iEntry = NULL;
    00554
    00555     //No selected entry, create a new one for adding an entry.
    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 // CCalendarAPIexampleEngine::Entry()
    00621 // returns a reference to an entry for reading the values
    00622 // of the entry. Panics with EOutOfEntriesArray if parameter
    00623 // aIndex is negative or greater than the number of entries
    00624 //
    00625 // Called ONLY from CCalendarAPIexampleEntriesContainer when
    00626 // populating the list box
    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 // CCalendarAPIexampleEngine::ModifyIndex()
    00642 // Returns the modify index.
    00643 //
    00644 // Called only from CCalendarAPIexampleEntryView
    00645 // When deleting the entry.
    00646 // ----------------------------------------------------
    00647 //
    00648
    00649 TInt CCalendarAPIexampleEngine::ModifyIndex() const
    00650     {
    00651     return iModifyIndex;
    00652     }
    00653
    00654 void CCalendarAPIexampleEngine::ExecuteDeletionL()
    00655     {
    00656     DeleteEntryL(ModifyIndex());
    00657
    00658     // If no more entries exist in the listbox, activate search view.
    00659     if (EntryCount() == 0)
    00660         {
    00661         iAppUi.ActivateView(KSearchViewId);
    00662         }
    00663     // activate entries view
    00664     else
    00665         {
    00666         iAppUi.ActivateView(KEntriesViewId);
    00667         }
    00668     }
    00669 /*******END OF CalendarEngineCommandsInterface functions**********************/
    00670
    00671 // End of file