examples/AppEngine/CalInterimApi/CalExample.cpp

00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "CalExample.h"
00017 
00018 #include <calinstance.h>
00019 
00020 CCalExample::CCalExample()
00021         {
00022         }
00023 
00024 CCalExample::~CCalExample()
00025         {
00026         _LIT(KExitMsg,"\n\nPress any key to exit the application ");
00027         iConsole->Printf(KExitMsg); 
00028         iConsole->Getch();
00029         
00030         delete iCalEntryView;
00031         delete iCalSession;     
00032         delete iConsole; 
00033         }
00034 
00035 CCalExample* CCalExample::NewL()
00036         {
00037         CCalExample* self= new (ELeave) CCalExample();
00038         CleanupStack::PushL(self);
00039         self->ConstructL();
00040         CleanupStack::Pop();    
00041         return self;
00042         }
00043 
00044 void CCalExample::ConstructL()
00045         {
00046         _LIT(KTitle, "calexample" );
00047         iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00048 
00049         _LIT(KPressAKeyMsg, "\n\nPress any key to step through the example");
00050         iConsole->Printf ( KPressAKeyMsg );
00051         iConsole->Getch ();
00052         
00053         iCalSession = CCalSession::NewL();
00054         // Create and open a calendar file
00055         _LIT(KFileName,"calendarfile");
00056         TRAPD(err, iCalSession->CreateCalFileL(KFileName));
00057         // ignore KErrAlreadyExists leave code
00058         if (err != KErrAlreadyExists)
00059                 {
00060                 User::LeaveIfError(err);
00061                 }
00062         // Open the calendar file
00063         iCalSession->OpenL(KFileName);
00064         
00065         _LIT(KProgressMsg, "\nCalendar entry view creation is in progress...");
00066         iConsole->Printf(KProgressMsg); 
00067         // Create a calendar entry view to view the entries
00068         iCalEntryView = CCalEntryView::NewL(*iCalSession,*this);
00069         CActiveScheduler::Start();              
00070         }
00071 
00072 // Called during calendar entry view creation
00073 void CCalExample::Progress(TInt aProgress)
00074         {
00075         _LIT(KProgressVal,"\n%d%% complete");
00076         iConsole->Printf(KProgressVal,aProgress);
00077         }
00078 
00079 // Called on completion of calendar entry view creation
00080 void CCalExample::Completed(TInt /*aError*/)
00081         {
00082         CActiveScheduler::Stop();
00083         }
00084 
00085 // Returns whether or not progress notification is required
00086 TBool CCalExample::NotifyProgress()
00087         {
00088         // Progress notification is required
00089         return ETrue;
00090         }
00091                 
00092 // Sets some entry details - start and end date/time, description, categories and attendees
00093 void CCalExample::SetEntryDetailsL(CCalEntry* aEntry,const TDesC& aDescription, TDateTime& aStartTime,TDateTime& aEndTime)
00094         {
00095         TCalTime startTime;
00096         TCalTime endTime;
00097         
00098         // Set the start and end times using time values local to the current system time zone
00099         startTime.SetTimeLocalL(aStartTime);
00100         endTime.SetTimeLocalL(aEndTime);
00101         aEntry->SetStartAndEndTimeL(startTime, endTime);        
00102 
00103         // Set the description text
00104         aEntry->SetDescriptionL(aDescription);  
00105         
00106         // Add a category
00107         const RPointerArray<CCalCategory> categoryList = aEntry->CategoryListL();
00108         if(categoryList.Count() == 0)
00109                 {
00110                 TBuf<32> buf;
00111                 _LIT(KCategoryName, "Dummy Category");
00112                 buf.Copy(KCategoryName);
00113                 
00114                 CCalCategory* category = CCalCategory::NewL(buf);
00115                 CleanupStack::PushL(category);
00116                 aEntry->AddCategoryL(category);
00117                 CleanupStack::Pop(category);
00118                 } 
00119                 
00120         // Add some attendees
00121         const RPointerArray<CCalAttendee>attendeeList = aEntry->AttendeesL();
00122         if(attendeeList.Count() == 0)
00123                 {
00124                 _LIT(KAttendeeOne, "nokiauk@nokia.com");
00125                 _LIT(KAttendeeTwo, "nokiaindia@nokia.com");
00126 
00127                 CCalAttendee* attendeeOne = CCalAttendee::NewL(KAttendeeOne);
00128                 CleanupStack::PushL(attendeeOne);       
00129                 aEntry->AddAttendeeL(attendeeOne);      
00130                 CleanupStack::Pop(attendeeOne);
00131         
00132                 CCalAttendee* attendeeTwo = CCalAttendee::NewL(KAttendeeTwo);
00133                 CleanupStack::PushL(attendeeTwo);       
00134                 aEntry->AddAttendeeL(attendeeTwo);
00135                 CleanupStack::Pop(attendeeTwo);
00136                 }
00137         }
00138         
00139 // Add a non-repeating appointment to the calendar file
00140 void CCalExample::AddEntryL()
00141         { 
00142         const TDesC8& guidDes = KGuid;
00143         HBufC8* guid = guidDes.AllocL();
00144         
00145         CCalEntry* entry = CCalEntry::NewL(CCalEntry::EAppt, guid, CCalEntry::EMethodNone, 0);
00146         CleanupStack::PushL(entry);
00147                 
00148         // For an appointment, the time as well as the date is relevant
00149         TDateTime startTime(2006, EJanuary, 04, 10, 0, 0, 0);
00150         TDateTime endTime(2006, EJanuary, 05, 16, 0, 0, 0);
00151         
00152         _LIT(KAddingMsg, "\n\nAdding an entry...");
00153         iConsole->Printf ( KAddingMsg );
00154                 
00155         _LIT(KDescription,"Meeting is scheduled in 1st week of January");
00156         SetEntryDetailsL(entry,KDescription, startTime, endTime);
00157                 
00158         RPointerArray<CCalEntry> array;
00159         CleanupClosePushL(array);
00160         array.AppendL(entry);
00161 
00162         TInt success(0);
00163         // If StoreL() leaves, 'success' contains the number of entries that were
00164         // stored before it failed
00165         iCalEntryView->StoreL(array, success);
00166 
00167         CleanupStack::PopAndDestroy(&array);
00168         CleanupStack::PopAndDestroy(entry);
00169         }
00170 
00171 // Destroy the RPointerArray
00172 void DestroyRPointerArray(TAny* aPtr)
00173         {
00174         RPointerArray<CCalEntry>* self = static_cast<RPointerArray<CCalEntry>*> (aPtr);
00175         self->ResetAndDestroy();
00176         }
00177 
00178 // Fetch the entry and update some of its details
00179 void CCalExample::UpdateEntryL()
00180         {
00181         RPointerArray<CCalEntry> array;
00182         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &array));
00183         iCalEntryView->FetchL(KGuid, array);
00184 
00185         //Fetch the first entry in the list
00186         _LIT(KUpdatingMsg,"\n\nUpdating the entry...");
00187         iConsole->Printf(KUpdatingMsg);
00188         CCalEntry* entry = array[0];
00189         
00190         _LIT(KNewDescription,"Meeting rescheduled to 2nd week of January");
00191         TDateTime startTime(2006, EJanuary, 11, 10, 0, 0, 0); 
00192         TDateTime endTime(2006, EJanuary, 12, 16, 0, 0, 0);
00193         SetEntryDetailsL(entry,KNewDescription, startTime, endTime);    
00194                         
00195         TInt numberOfEntries(0);
00196         // If UpdateL() leaves, 'numberOfEntries' contains the number of entries that were
00197         // updated before it failed
00198         iCalEntryView->UpdateL(array,numberOfEntries);
00199         CleanupStack::PopAndDestroy(&array);
00200         }
00201         
00202 // Add a repeating appointment. This is called the originating entry.
00203 void CCalExample::AddOriginatingEntryL()
00204         {
00205         _LIT(KAddingMsg, "\n\nAdding a repeating entry...");
00206         iConsole->Printf ( KAddingMsg );
00207         
00208         TTime startTime(TDateTime(2006, EJanuary, 0, 14, 0, 0, 0)); // January 1st 2pm
00209         TTime endTime(startTime + TTimeIntervalHours(1)); // January 1st 3pm    
00210         
00211         const TDesC8& guidDes = KGuid;
00212         HBufC8* guid = guidDes.AllocL();
00213         CCalEntry* originatingEntry = CCalEntry::NewL(CCalEntry::EAppt, guid, CCalEntry::EMethodNone, 0);
00214         CleanupStack::PushL(originatingEntry);
00215         
00216         _LIT(KDescription,"An originating entry");
00217         originatingEntry->SetDescriptionL(KDescription);
00218         
00219         TCalTime entryStartTime;
00220         entryStartTime.SetTimeLocalL(startTime);
00221         TCalTime entryEndTime;
00222         entryEndTime.SetTimeLocalL(endTime);
00223         originatingEntry->SetStartAndEndTimeL(entryStartTime, entryEndTime);
00224         
00225         // Set the repeat rule
00226         // The entry repeats weekly with 3 instances being created
00227         TCalRRule weeklyRptRule(TCalRRule::EWeekly);
00228         weeklyRptRule.SetDtStart(entryStartTime);       
00229         weeklyRptRule.SetCount(3); 
00230         weeklyRptRule.SetInterval(1);
00231         // Also need to set the day that the entry repeats on
00232         RArray<TDay> days;
00233         CleanupClosePushL(days);
00234         days.AppendL(ESunday);
00235         weeklyRptRule.SetByDay(days);
00236         CleanupStack::PopAndDestroy(&days);
00237         originatingEntry->SetRRuleL(weeklyRptRule);
00238 
00239         // Now write the entry to the view
00240         RPointerArray<CCalEntry> array;
00241         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &array));
00242         array.AppendL(originatingEntry);
00243         TInt success(0);
00244         // If StoreL() leaves, 'success' contains the number of entries that were
00245         // stored before it failed
00246         iCalEntryView->StoreL(array, success);
00247         
00248         // clean up     
00249         array.Close();
00250         CleanupStack::PopAndDestroy(&array);
00251         CleanupStack::PopAndDestroy(originatingEntry);  
00252         }
00253         
00254 // Adds a modifying, or "child" entry.
00255 // A modifying entry modifies one or more instances of the "originating" repeating entry. 
00256 // It has the same UID, and is deleted when the originating entry is deleted.
00257 void CCalExample::AddmodifyingEntryL()
00258         {
00259         RPointerArray<CCalEntry> modifyingArray;
00260         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &modifyingArray));
00261         
00262         _LIT(KAddingMsg, "\n\nAdding a modifying entry...");
00263         iConsole->Printf ( KAddingMsg );
00264         // Create repeating modifying entry
00265         // Has the same recurrence Id as the second instance of the originating and
00266         // this and all future instances are modified.
00267         TTime recurrenceId(TDateTime(2006, EJanuary, 7, 14, 0, 0, 0)); // January 8th 2pm
00268         TCalTime recurrenceIdCal;
00269         recurrenceIdCal.SetTimeLocalL(recurrenceId);
00270         
00271         const TDesC8& guidDes = KGuid;
00272         HBufC8* guid = guidDes.AllocL();
00273 
00274         // Entry type is appointment, group scheduling method none and modifying this and all future instances 
00275         CCalEntry* modifyingEntry = CCalEntry::NewL(CCalEntry::EAppt, guid, CCalEntry::EMethodNone, 1, recurrenceIdCal, CalCommon::EThisAndFuture);
00276         CleanupStack::PushL(modifyingEntry);
00277 
00278         // The modifying entry has a different description to the originating entry
00279         _LIT(KChildDescription,"A modifying entry");
00280         modifyingEntry->SetDescriptionL(KChildDescription);
00281         // The modifying entry modifies the originating entry's date by one day
00282         TTime childStartTime(TDateTime(2006, EJanuary, 8, 14, 0, 0, 0)); // January 9th 2pm
00283         TTime childEndTime(childStartTime + TTimeIntervalHours(1)); // January 9th 3pm
00284         
00285         TCalTime childEntryStartTime;
00286         childEntryStartTime.SetTimeLocalL(childStartTime);
00287         TCalTime childEntryEndTime;
00288         childEntryEndTime.SetTimeLocalL(childEndTime);
00289         modifyingEntry->SetStartAndEndTimeL(childEntryStartTime, childEntryEndTime);
00290         // Target instance for recurrence range in the modifying entry is second instance
00291         TCalRRule childRptRule(TCalRRule::EWeekly);
00292         childRptRule.SetDtStart(childEntryStartTime);
00293         childRptRule.SetCount(2); 
00294         childRptRule.SetInterval(1);
00295         
00296         // The modifying entry modifies the originating entry by occurring on Mondays
00297         RArray<TDay> days;
00298         CleanupClosePushL(days);
00299         days.AppendL(EMonday);
00300         childRptRule.SetByDay(days);
00301         CleanupStack::PopAndDestroy(&days);
00302         modifyingEntry->SetRRuleL(childRptRule);
00303 
00304         // add the modifying entry to the entry view.
00305         modifyingArray.AppendL(modifyingEntry);
00306 
00307         TInt success(0);
00308         // If StoreL() leaves, 'success' contains the number of entries that were
00309         // stored before it failed
00310         iCalEntryView->StoreL(modifyingArray, success); 
00311 
00312         // clean up     
00313         CleanupStack::PopAndDestroy(modifyingEntry);
00314         modifyingArray.Reset();
00315         CleanupStack::PopAndDestroy(&modifyingArray);   
00316         }
00317         
00318 // Searches for all instances in January, then prints out their details.
00319 // Creates an instance view to display the instances. 
00320 void CCalExample::FindInstanceL()
00321         { 
00322         _LIT(KPressAKeyMsg, "\n\nPress any key to view the instances");
00323         iConsole->Printf(KPressAKeyMsg);
00324         iConsole->Getch ();
00325         _LIT(KProgressMsg, "\nCalendar instance view creation is in progress...");
00326         iConsole->Printf(KProgressMsg);         
00327         CCalInstanceView* instanceView = CCalInstanceView::NewL(*iCalSession, *this);
00328         CleanupStack::PushL(instanceView);
00329         CActiveScheduler::Start();
00330         
00331         RPointerArray<CCalInstance> instanceList;
00332         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &instanceList));
00333         CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
00334         TCalTime startDateForInstanceSearch;
00335         TCalTime endDateForInstanceSearch;
00336         
00337         // Set date and time range for searching for instances
00338         startDateForInstanceSearch.SetTimeLocalL(TDateTime( 2006, EJanuary, 0, 12, 0, 0, 0));  // 1st January 12 pm
00339         endDateForInstanceSearch.SetTimeLocalL(TDateTime( 2006, EJanuary, 30, 10, 0, 0, 0)); //  31st January 10 am
00340         CalCommon::TCalTimeRange searchTimeRange(startDateForInstanceSearch, endDateForInstanceSearch);
00341         
00342         // Searches for instances within the specified date range
00343         instanceView->FindInstanceL(instanceList, filter, searchTimeRange);
00344         
00345         _LIT(KInstancesFound,"\n\nFound %d instances:");
00346         iConsole->Printf(KInstancesFound,instanceList.Count());
00347                 
00348         TDateTime date; 
00349         for(TInt i = 0; i<instanceList.Count(); i++) 
00350                 {
00351                 TPtrC des = instanceList[i]->Entry().DescriptionL();
00352                 _LIT(KEntryDesc,"\nDescription: %S");
00353                 iConsole->Printf(KEntryDesc,&des); 
00354 
00355                 TTime time = instanceList[i]->StartTimeL().TimeLocalL();
00356                 TBuf<40> datetimeStr;           
00357                 _LIT(KFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S");
00358                 time.FormatL(datetimeStr,KFormat);
00359                 
00360                 // Print out the start date/time
00361                 _LIT(KStartDate,"\nLocal instance start date/time: %S ");
00362                 iConsole->Printf(KStartDate,&datetimeStr);
00363                 }
00364                 
00365         CleanupStack::PopAndDestroy(&instanceList);     
00366         CleanupStack::PopAndDestroy(instanceView);      
00367         }       
00368 
00369 // Fetch and print the entry details
00370 void CCalExample::PrintEntryDetailsL()
00371         {
00372         _LIT(KPressAKeyMsg, "\nPress any key to view entry details");
00373         iConsole->Printf ( KPressAKeyMsg );
00374         iConsole->Getch ();
00375 
00376         RPointerArray<CCalEntry> array;
00377         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &array));
00378         iCalEntryView->FetchL(KGuid, array);
00379         for(TInt i=0; i<array.Count(); i++)
00380                 {
00381                 CCalEntry* entry = array[i];
00382                 CleanupStack::PushL(entry);
00383                 
00384                 TPtrC des = entry->DescriptionL();
00385                 _LIT(KEntryDesc," \n\nDescription: %S");
00386                 iConsole->Printf(KEntryDesc,&des); 
00387 
00388                 TCalTime startTime = entry->StartTimeL();
00389                 TCalTime endTime = entry->EndTimeL();
00390                 TTime time = startTime.TimeLocalL();
00391                 
00392                 TBuf<40> datetimeStr;           
00393                 _LIT(KFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S");
00394                 time.FormatL(datetimeStr,KFormat);
00395                 
00396                 // Print out the start date/time
00397                 _LIT(KStartDate,"\nStart date/time: %S ");
00398                 iConsole->Printf(KStartDate,&datetimeStr);
00399                 
00400                 // Now, print out the end date/time
00401                 time = endTime.TimeLocalL();
00402                 time.FormatL(datetimeStr,KFormat);
00403                 _LIT(KEndDate,"\nEnd date/time: %S");
00404                 iConsole->Printf(KEndDate,&datetimeStr);
00405                 CleanupStack::Pop(entry);
00406                 
00407                 // Fetch category
00408                 RPointerArray<CCalCategory> categoryList = entry->CategoryListL();
00409                 for( int j=0; j<categoryList.Count(); j++)
00410                         {
00411                         const TDesC& category = categoryList[j]->ExtendedCategoryName();
00412                         HBufC* catbuf = category.AllocL();
00413                         _LIT(KCatType,"\nCategory type: %S");
00414                         iConsole->Printf(KCatType,catbuf);
00415                         delete catbuf;
00416                         } 
00417                                 
00418                 // Fetch attendee
00419                 RPointerArray<CCalAttendee> attendeeList;
00420                 attendeeList = entry->AttendeesL();
00421                 for( int k=0; k<attendeeList.Count(); k++)
00422                         {
00423                         const TDesC16& address = attendeeList[k]->Address();
00424                         HBufC16* addr = address.AllocL();
00425                         _LIT(KAttendee,"\nAttendee %d: %S");
00426                         iConsole->Printf(KAttendee,k+1, addr);
00427                         delete addr;            
00428                         }
00429                 }
00430         CleanupStack::PopAndDestroy(&array);
00431         }
00432 
00433 // Delete the originating entry 
00434 void CCalExample::DeleteEntryL()
00435         {
00436         _LIT(KPressAKeyMsg, "\n\nPress any key to delete the originating entry, (also deletes the modifying entry)");
00437         iConsole->Printf ( KPressAKeyMsg );
00438         iConsole->Getch ();
00439         
00440         RPointerArray<CCalEntry> entryArray;
00441         CleanupStack::PushL(TCleanupItem(DestroyRPointerArray, &entryArray));
00442         
00443         // Fetch the entry to be deleted
00444         iCalEntryView->FetchL(KGuid, entryArray);
00445         CCalEntry* entry = entryArray[0];
00446 
00447         // Delete originating entry, thereby also deleting the modifying entry
00448         iCalEntryView->DeleteL(*entry);
00449         CleanupStack::PopAndDestroy(&entryArray);                       
00450         }
00451 
00452 LOCAL_C void MainL()
00453         {
00454         CCalExample* app = CCalExample::NewL();
00455         CleanupStack::PushL(app);
00456         
00457         // Add an entry and display the entry details
00458         app->AddEntryL();
00459         app->PrintEntryDetailsL();      
00460 
00461         // Update an entry and display the updated entry details
00462         app->UpdateEntryL();
00463         app->PrintEntryDetailsL();
00464         
00465         // Create a repeating entry and display the entry details
00466         app->AddOriginatingEntryL();
00467         app->PrintEntryDetailsL(); 
00468         
00469         // Display the repeat instances
00470         app->FindInstanceL();           
00471         
00472         // Create a modifying entry and display the entry details
00473         app->AddmodifyingEntryL();
00474         app->PrintEntryDetailsL(); 
00475         
00476         // Display the repeat instances
00477         app->FindInstanceL();
00478         
00479         // Delete the originating thereby deleting the modifying entry and all repeat instances
00480         app->DeleteEntryL();             
00481         
00482         CleanupStack::PopAndDestroy(app);
00483         }
00484 
00485 GLDEF_C TInt E32Main()
00486         {
00487         __UHEAP_MARK;
00488         // Active scheduler required as this is a console app
00489         CActiveScheduler* scheduler=new CActiveScheduler;
00490         
00491         // If active scheduler has been created, install it.
00492         if (scheduler)
00493                 {
00494                 CActiveScheduler::Install(scheduler); 
00495                 
00496                 // Cleanup stack needed
00497                 CTrapCleanup* cleanup = CTrapCleanup::New();
00498                 if(cleanup)
00499                         {
00500                         TRAPD(err, MainL());
00501                         if(err != KErrNone)
00502                                 {
00503                                 _LIT(KUserPanic,"Failed to complete");  
00504                                 User::Panic(KUserPanic, err);
00505                                 }
00506                         delete cleanup;
00507                         }
00508                 }
00509         delete scheduler;
00510 
00511         __UHEAP_MARKEND;
00512         return KErrNone;
00513         }

Generated on Thu Jan 21 10:32:54 2010 for TB10.1 Example Applications by  doxygen 1.5.3