examples/ForumNokia/CalendarExample/src/CalendarAPIexampleEngine.cpp

00001 /*
00002  * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include <calsession.h> //CCalSession
00032 #include <calentryview.h>
00033 #include <CalInstance.h>
00034 #include <calinstanceview.h>
00035 #include <txtfmlyr.h>   //CParaFormatLayer
00036 #include <calalarm.h>   //CCalAlarm 
00037 #include <CalendarAPIexample.rsg>
00038 #include "CalendarAPIexampleEngine.h"
00039 #include "CalendarAPIexample.hrh"
00040 
00041 #include "CalendarAPIexampleEntryView.h"
00042 #include "CalendarAPIexampleEntryContainer.h"
00043 #include "CalendarAPIexampleEntriesView.h"
00044 #include "CalendarAPIexampleSearchView.h" 
00045 
00046 #include "CalendarAPIexample.pan"
00047 
00048 #include "CalendarAPIexampleEntryView.h"
00049 
00050 // CONSTANTS
00051 const TInt KProgressFinalValue = 100;
00052 
00053 #define KDefaultEventDisplayTime TTimeIntervalMinutes(660)
00054 #define KDefaultAnnivDisplayTime TTimeIntervalMinutes(900)
00055 #define KDefaultDayNoteDisplayTime TTimeIntervalMinutes(1000)
00056 
00057 // ================= MEMBER FUNCTIONS =======================
00058 
00059 // constructor
00060 CCalendarAPIexampleEngine::CCalendarAPIexampleEngine(
00061                                             MCalenderEngineObserverUI& aAppUi)
00062     : iAppUi(aAppUi)
00063     {
00064     
00065     
00066     }
00067     
00068 // destructor
00069 CCalendarAPIexampleEngine::~CCalendarAPIexampleEngine()
00070     {
00071     
00072     if( iWait )
00073         {
00074         if( iWait->IsStarted() )
00075             {
00076             iWait->AsyncStop();
00077             //it's not propably safe to delete right after this
00078             }
00079         delete iWait;
00080         iWait = NULL;   
00081         }
00082     if (iEntryView)
00083         {
00084         delete iEntryView;
00085         iEntryView = 0;
00086         }
00087         
00088     if (iInstanceView)
00089         {
00090         delete iInstanceView;
00091         iInstanceView = 0;
00092         }
00093 
00094     iEntries.ResetAndDestroy();
00095     
00096     delete iEntry;
00097     iEntry = NULL;
00098 
00099     delete iProgressDialog;     
00100     iProgressDialog = NULL;
00101          
00102     if (iCalSession)
00103         {
00104         delete iCalSession;
00105         iCalSession = 0;
00106         }
00107     }
00108     
00109     
00110 // Two-phased constructor.
00111 CCalendarAPIexampleEngine* CCalendarAPIexampleEngine::NewL(
00112                                             MCalenderEngineObserverUI& aAppUi)
00113     {
00114     CCalendarAPIexampleEngine* self = 
00115         new (ELeave) CCalendarAPIexampleEngine(aAppUi);
00116     CleanupStack::PushL(self);
00117     self->ConstructL();
00118     CleanupStack::Pop(self);
00119     return self;
00120     }
00121     
00122 // Symbian OS default constructor can leave.    
00123 void CCalendarAPIexampleEngine::ConstructL()
00124     {
00125     iModifyIndex = KUndefinedModifyIndex;
00126     
00127     // allocate and construct server
00128     iCalSession = CCalSession::NewL();      
00129     
00130     const TDesC& file = iCalSession->DefaultFileNameL();
00131     // connect to the cal server
00132     TRAPD(err,iCalSession->OpenL(file) );   
00133     if( err == KErrNotFound)
00134         {
00135         iCalSession->CreateCalFileL(file);
00136         iCalSession->OpenL(file);
00137         }
00138     else
00139         {
00140         User::LeaveIfError(err);   
00141         }
00142 
00143     iWait = new (ELeave) CActiveSchedulerWait;
00144     }
00145 
00146 /*******************OBSERVER functions***********************/
00147     
00148 // ----------------------------------------------------
00149 // CCalendarAPIexampleSearchContainer::Progress()
00150 // Monitors the progress of agenda model operations.
00151 // Creates a progress dialog when first called and 
00152 // updates its status on later calls. Note that if the 
00153 // agenda models operation completes quickly, for 
00154 // instance when opening a small file, then Progress() 
00155 // will probably not be called at all.
00156 // ----------------------------------------------------
00157 //                  
00158 void CCalendarAPIexampleEngine::Progress(TInt aPercentageCompleted)
00159     {
00160     if (!iProgressDialog)
00161         {
00162         
00163         TRAPD
00164             (   
00165             error,
00166             iProgressDialog = new (ELeave) CAknProgressDialog(
00167                     REINTERPRET_CAST(CEikDialog**, &iProgressDialog), ETrue);
00168             iProgressDialog->PrepareLC(R_CALENDARAPIEXAMPLE_PROGRESS_NOTE);
00169             iProgressInfo = iProgressDialog->GetProgressInfoL();
00170             iProgressDialog->RunLD();
00171             )
00172         if (KErrNone == error)
00173             {
00174             iProgressInfo->SetFinalValue(KProgressFinalValue);
00175             iProgressInfo->SetAndDraw(aPercentageCompleted);
00176             }
00177         else
00178             {
00179             delete iProgressDialog;
00180             iProgressDialog = NULL;
00181             }
00182         }
00183     else
00184         {
00185         iProgressInfo->SetAndDraw(aPercentageCompleted);
00186         }
00187     }
00188 
00194 void CCalendarAPIexampleEngine::Completed(TInt )
00195     {
00196     if (iCreatingEntryView)
00197         {       
00198         if( iWait->IsStarted() )
00199             {
00200             iWait->AsyncStop();
00201             }
00202         else
00203             {
00204             Panic(EWaitNotStarted);
00205             }
00206         iCreatingEntryView = EFalse;        
00207         }
00208         
00209     if (iProgressDialog)
00210         {
00211         iProgressInfo->SetAndDraw(100);
00212         TRAPD
00213             (
00214             error,
00215             iProgressDialog->ProcessFinishedL();
00216             )
00217         iProgressInfo = NULL;
00218         iProgressDialog = NULL;
00219         }
00220     }
00221 
00226 TBool CCalendarAPIexampleEngine::NotifyProgress()
00227     {       
00228     return ETrue;
00229     }
00230 /****************END OF OBSERVER functions***********************/
00231 
00232 
00233 /************************HELPER functions***********************/
00234 
00235 // ----------------------------------------------------
00236 // CCalendarAPIexampleEngine::AddAnniversaryL()
00237 // adds an anniversary to the agenda file
00238 // ----------------------------------------------------
00239 //  
00240 //A Helper, only called from SaveL
00241 //
00242 void CCalendarAPIexampleEngine::AddAnniversaryL(CCalEntry* aAnniv) 
00243     {
00244     if (!iEntryView)
00245         {
00246         iEntryView = CCalEntryView::NewL(*iCalSession, *this); 
00247         iCreatingEntryView = ETrue;
00248         
00249         //After scheduler starting, the call back will be 
00250         //called when the task is completed.
00251         if( !iWait->IsStarted() )
00252             {
00253             iWait->Start();
00254             }
00255         else
00256             {
00257             Panic(EWaitAlreadyStarted);
00258             }
00259         }
00260 
00261     RPointerArray<CCalEntry> calEntryList;
00262     calEntryList.Append(aAnniv);
00263     TInt num;
00264 
00265     iEntryView->StoreL(calEntryList, num);
00266     
00267     calEntryList.Reset();
00268     
00269     }
00270 
00271 // ----------------------------------------------------
00272 // CCalendarAPIexampleEngine::UpdateAnniversaryL()
00273 // updates an anniversary
00274 // ----------------------------------------------------
00275 //  A helper, only called from SaveL
00276 //
00277 void CCalendarAPIexampleEngine::UpdateAnniversaryL( CCalEntry* aAnniv) 
00278     {
00279     if (!iEntryView)
00280         {
00281         iEntryView = CCalEntryView::NewL(*iCalSession, *this); 
00282         iCreatingEntryView = ETrue;
00283         
00284         //After scheduler starting, the call back will be called when the task is completed.
00285         //CActiveScheduler::Start();
00286         if( !iWait->IsStarted() )
00287             {
00288             iWait->Start();
00289             }
00290         else
00291             {
00292             Panic(EWaitAlreadyStarted);
00293             }
00294         }
00295     RPointerArray<CCalEntry> calEntryList;
00296     calEntryList.Append(aAnniv);
00297     TInt num;
00298 
00299     iEntryView->UpdateL(calEntryList, num);
00300 
00301     calEntryList.Reset();
00302     }
00303 
00304 /************MCalendarEngineCommandsInterface functions***********************/
00305 
00306 // ----------------------------------------------------
00307 // CCalendarAPIexampleEngine::DoSearchL()
00308 // Searches all anniversaries in given time interval.
00309 // ----------------------------------------------------
00310 // Called from CCalendarAPIexampleSearchView::DoSearchL()
00311 // and also from DoSaveL if anniversaries are modified
00312 //
00313 void CCalendarAPIexampleEngine::DoSearchL(const TSearchType& aType)
00314     {
00315     iEntries.ResetAndDestroy(); 
00316 
00317     iCurrentSearchType = aType;
00318     TTime now;
00319     now.HomeTime();
00320     TDateTime from = now.DateTime();
00321     User::LeaveIfError(from.SetHour(0));
00322     User::LeaveIfError(from.SetMinute(0));
00323     User::LeaveIfError(from.SetSecond(0));
00324     User::LeaveIfError(from.SetMicroSecond(0));
00325 
00326     //set the time interval
00327     switch (aType)
00328         {
00329         case EWeek:
00330             now += TTimeIntervalDays(7);
00331             break;
00332         case EMonth:
00333             now += TTimeIntervalMonths(1);
00334             break;
00335         case ESixMonths:
00336             now += TTimeIntervalMonths(6);
00337             break;
00338         case EYear:
00339             now += TTimeIntervalYears(1);
00340             break;
00341         }
00342 
00343     TDateTime to = now.DateTime();
00344 
00345     if (!iInstanceView)
00346         {
00347         iInstanceView = CCalInstanceView::NewL(*iCalSession, *this); 
00348         iCreatingEntryView = ETrue;
00349         
00350         //After scheduler starting, the call back will be 
00351         //called when the task is completed.
00352         if( !iWait->IsStarted() )
00353             {
00354             iWait->Start();
00355             }
00356         else
00357             {
00358             Panic(EWaitAlreadyStarted);
00359             }
00360         }
00361 
00362     RPointerArray<CCalInstance> instanceArray;
00363     TTime nextDate(from);
00364     // decrease 'from' date by one day, so that 'from' date 
00365     //will be included in search.
00366     nextDate -= TTimeIntervalDays(1);
00367     TTime toDate(to);
00368     TCalTime fromCalTime;
00369     fromCalTime.SetTimeLocalL(from);
00370     TCalTime toCalTime;
00371     toCalTime.SetTimeLocalL(to);
00372     CalCommon::TCalTimeRange timeRange(fromCalTime, toCalTime);
00373     
00374     iInstanceView->FindInstanceL(instanceArray, CalCommon::EIncludeAnnivs, timeRange);
00375 
00376     //create new CCalHelperEntry for each CCalEntry.
00377     for (TInt i = 0; i < instanceArray.Count(); i++)
00378         {
00379         CCalHelperEntry* entry = CCalHelperEntry::NewLC(instanceArray[i]);
00380         User::LeaveIfError(iEntries.Append(entry));
00381         CleanupStack::Pop(entry);
00382         }
00383     //now all the instances are in iEntries List
00384     
00385     instanceArray.Reset();
00386     }
00387 
00388 // ----------------------------------------------------
00389 // CCalendarAPIexampleEngine::DeleteEntryL()
00390 // deletes an anniversary from the agenda file
00391 //
00392 // Called from CCalendarAPIexampleEntriesView when
00393 // the delete command has been selected
00394 // And from CCalendarAPIexampleEntryView
00395 // ----------------------------------------------------
00396 
00397 void CCalendarAPIexampleEngine::DeleteEntryL(const TInt& aIndex)
00398     {
00399     //This is the case when user is adding an entry and desides
00400     //to cancel the addition
00401     if( aIndex == -1)
00402         {
00403         delete iEntry;
00404         iEntry = NULL;
00405         return;
00406         }
00407         
00408     if (aIndex < 0 || aIndex >= iEntries.Count())
00409         {
00410         Panic(KInvalidEntryIndex);
00411         }
00412 
00413     CCalHelperEntry* entry = iEntries[aIndex];
00414     
00415     CCalEntry* anniv = entry->Anniv();
00416 
00417     if (!iEntryView)
00418         {
00419         iEntryView = CCalEntryView::NewL(*iCalSession, *this); 
00420         iCreatingEntryView = ETrue;
00421         
00422         //After scheduler starting, the call back will be called 
00423         //when the task is completed.
00424         //CActiveScheduler::Start();
00425         if( !iWait->IsStarted() )
00426             {
00427             iWait->Start();
00428             }
00429         else
00430             {
00431             Panic(EWaitAlreadyStarted);
00432             }
00433         }
00434 
00435     iEntryView->DeleteL(*anniv);
00436         
00437     iEntries.Remove(aIndex);
00438 
00439     delete entry; //now the CCalinstance is deleted if there's one
00440     }
00441 
00442 // ----------------------------------------------------
00443 // CCalendarAPIexampleSearchView::DoAddL()
00444 // Resets models modify index, so that next call to 
00445 // models EntryForModifycation will create a new entry.
00446 // Activates the entry view.
00447 //
00448 // Called from CCalendarAPIexampleSearchView when
00449 // the menu option for adding has been selected
00450 // ----------------------------------------------------
00451 //      
00452 void CCalendarAPIexampleEngine::DoAddL()
00453     {
00454     //Reset the modify index
00455     iModifyIndex = KUndefinedModifyIndex;
00456     
00457     iAppUi.ActivateView(KEntryViewId);  
00458     }
00459 
00460 // ----------------------------------------------------
00461 // CCalendarAPIexampleEngine::SaveL()
00462 // Adds or updates the current entry to the agenda file.
00463 //
00464 // Called from CCalendarAPIexampleEntryView when the
00465 // view is closed.
00466 // ---------------------------------------------------- 
00467 void CCalendarAPIexampleEngine::DoSaveL()
00468     {
00469     //If iEntry is created in CreateEntryForModificationL then
00470     //there's a new anniversary being added. 
00471     //add a new anniversary
00472     
00473     if (iEntry) //this has been created in CreateEntryForModificationL
00474                 //and the data has been stored by a call to iEntry.SetValuesL
00475         {
00476         if (iEntry->Modified())
00477             {
00478             iEntry->SaveValuesL();
00479             
00480             CCalEntry* anniv = iEntry->Anniv();
00481 
00482             if (!iEntryView)
00483                 {
00484                 iEntryView = CCalEntryView::NewL(*iCalSession, *this); 
00485                 iCreatingEntryView = ETrue;
00486                 
00487                 //After scheduler starting, the call back will be called 
00488                 //when the task is completed.
00489                 if( !iWait->IsStarted() )
00490                     {
00491                     iWait->Start();
00492                     }
00493                 else
00494                     {
00495                     Panic(EWaitAlreadyStarted);
00496                     }
00497                 }
00498 
00499             RPointerArray<CCalEntry> calEntryList;
00500             calEntryList.Append(anniv);
00501             TInt num;
00502 
00503             iEntryView->StoreL(calEntryList, num);
00504             
00505             calEntryList.Reset();
00506             
00507             }
00508         }
00509     //modify existing anniversary
00510     else
00511         {
00512         if (0 > iModifyIndex || iEntries.Count() <= iModifyIndex)
00513             {
00514             Panic(KInvalidModifyIndex);
00515             }
00516         //Get the entry which is being modified
00517         CCalHelperEntry* entry = iEntries[iModifyIndex];
00518 
00519         if (entry->Modified()) //are there any changes
00520             {
00521             if (entry->DateHasChanged())
00522                 {                
00523                 CCalEntry* updateAnniv = entry->Anniv();
00524                 TTime startDate(entry->Date());
00525                 TCalTime time;
00526                 time.SetTimeLocalL(startDate);
00527                 updateAnniv->SetStartAndEndTimeL(time,time);
00528                 UpdateAnniversaryL(updateAnniv);
00529                 }
00530             //the date of the entry hasn't changed, the entry is just updated.
00531             else 
00532                 {
00533                 entry->SaveValuesL();
00534                 CCalEntry* updateAnniv = entry->Anniv();
00535                 UpdateAnniversaryL(updateAnniv);
00536                 }
00537             DoSearchL(iCurrentSearchType);
00538             }
00539         }
00540     }   
00541      
00542 // ----------------------------------------------------
00543 // CCalendarAPIexampleEngine::SetModifyIndex()
00544 // Sets the modify index. Panics with KInvalidModifyIndex
00545 // if parameter aIndex is negative or greater than the
00546 // number of entries.
00547 //
00548 // Called only from CCalendarAPIexampleEntriesView when the
00549 // Edit command has been selected
00550 // ----------------------------------------------------
00551 //
00552 void CCalendarAPIexampleEngine::SetModifyIndex(const TInt& aIndex)
00553     {
00554     if (aIndex < 0 || aIndex >= iEntries.Count())
00555         {
00556         Panic(KInvalidModifyIndex);
00557         }
00558 
00559     iModifyIndex = aIndex;
00560     }     
00561 
00562 // ----------------------------------------------------
00563 // CCalendarAPIexampleEngine::CreateEntryForModificationL()
00564 // Returns an entry for modification. Creates a new
00565 // entry if iModifyIndex hasn't been set. Panics with
00566 // KInvalidModifyIndex if iModifyIndex is greater than
00567 // the number of entries.
00568 //
00569 // Called from CCalendarAPIexampleEntryView when
00570 // the container is created. 
00571 // ----------------------------------------------------
00572 //
00573 
00574 void CCalendarAPIexampleEngine::CreateEntryForModificationL(TBool& aModify)
00575     {
00576     delete iEntry;
00577     iEntry = NULL;
00578 
00579     //No selected entry, create a new one for adding an entry.
00580     if (iModifyIndex < 0)
00581         {
00582         aModify = EFalse;
00583         CCalEntry* nulli = NULL;
00584         
00585         iEntry = CCalHelperEntry::NewL(nulli);
00586         }
00587     else
00588         {
00589         aModify = ETrue;
00590         }
00591 
00592     if (iModifyIndex >= iEntries.Count())
00593         {
00594         Panic(KInvalidModifyIndex);
00595         }
00596     }
00597     
00598 TInt CCalendarAPIexampleEngine::EntryCount() const
00599     {
00600     return iEntries.Count();
00601     }   
00602 
00603 TBool CCalendarAPIexampleEngine::SetValuesToNewEntry( const TDesC& aName,
00604                         const TDateTime& aDate,
00605                         const TBool& aAlarm,
00606                         const TDateTime& aAlarmTime,
00607                         const TInt& aSynchronizationMethod)
00608 
00609     {
00610     CCalHelperEntry *entry=NULL;
00611     if( iEntry )
00612         {
00613         entry = iEntry;
00614         }
00615         
00616     else 
00617        entry = iEntries[iModifyIndex];    
00618     
00619     return entry->SetValues(aName,aDate,aAlarm,aAlarmTime,aSynchronizationMethod);
00620        
00621     }
00622 
00623 void CCalendarAPIexampleEngine::GetValuesToSet(TDes& aName, TTime& aDate,
00624                    TBool& aAlarm, TTime& aAlarmTime,
00625                             TInt& aSync)
00626     {
00627     CCalHelperEntry *entry=NULL;
00628     if( iEntry )
00629         {
00630         entry = iEntry;
00631         }        
00632     else 
00633         entry = iEntries[iModifyIndex];
00634 
00635 
00636     aName = entry->Name();
00637     aDate = entry->Date();
00638     aAlarm = entry->Alarm();
00639     aAlarmTime = entry->AlarmTime();
00640     aSync = entry->SynchronizationMethod();    
00641     }
00642 
00643 // ----------------------------------------------------
00644 // CCalendarAPIexampleEngine::Entry()
00645 // returns a reference to an entry for reading the values
00646 // of the entry. Panics with EOutOfEntriesArray if parameter
00647 // aIndex is negative or greater than the number of entries
00648 //
00649 // Called ONLY from CCalendarAPIexampleEntriesContainer when
00650 // populating the list box
00651 // ----------------------------------------------------
00652 //
00653 CCalHelperEntry& CCalendarAPIexampleEngine::Entry(const TInt& aIndex)
00654     {
00655     if (0 > aIndex || iEntries.Count() <= aIndex)
00656         {
00657         Panic(EOutOfEntriesArray);
00658         }
00659 
00660     return *iEntries[aIndex];
00661     }
00662 
00663 
00664 // ----------------------------------------------------
00665 // CCalendarAPIexampleEngine::ModifyIndex()
00666 // Returns the modify index.
00667 //
00668 // Called only from CCalendarAPIexampleEntryView
00669 // When deleting the entry.
00670 // ----------------------------------------------------
00671 //
00672 
00673 TInt CCalendarAPIexampleEngine::ModifyIndex() const
00674     {
00675     return iModifyIndex;
00676     }
00677     
00678 void CCalendarAPIexampleEngine::ExecuteDeletionL()
00679     {       
00680     DeleteEntryL(ModifyIndex());
00681     
00682     // If no more entries exist in the listbox, activate search view.
00683     if (EntryCount() == 0)
00684         {
00685         iAppUi.ActivateView(KSearchViewId);
00686         }
00687     // activate entries view
00688     else
00689         {
00690         iAppUi.ActivateView(KEntriesViewId);
00691         }    
00692     }
00693 /*******END OF CalendarEngineCommandsInterface functions**********************/
00694 
00695 // End of file

Generated by  doxygen 1.6.2