examples/SFExamples/PIM/AgendaRepeat/src/CalWrite.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 
00017 #include "CalWrite.h"
00018 
00019 CCalWrite* CCalWrite::NewL()
00020         {
00021         CCalWrite* self = new (ELeave) CCalWrite();
00022         CleanupStack::PushL(self);
00023         self->ConstructL();
00024         CleanupStack::Pop(self);
00025         return self;
00026         }
00027 
00028 CCalWrite::~CCalWrite()
00029         {
00030         delete iCalView;
00031         delete iCalSes;
00032         }
00033 
00034 void CCalWrite::ConstructL()
00035         {
00036         iCalSes = CCalSession::NewL();
00037         const TDesC& name = iCalSes->DefaultFileNameL();
00038         TRAPD(error, iCalSes->CreateCalFileL(name));
00039         if (error != KErrAlreadyExists)
00040                 {
00041                 User::LeaveIfError(error);
00042                 }
00043         iCalSes->OpenL(KNullDesC());
00044         iCalView = CCalEntryView::NewL(*iCalSes, *this);
00045         }
00046 
00047 CCalEntryView& CCalWrite::CalView()
00048         {
00049         return *iCalView;
00050         }
00051 
00052 void CCalWrite:: AddRepeatL(const TDesC& aDescription, TInt aWeeklyInterval)
00053         {
00054         // create a new entry
00055         HBufC8* guid = MakeGuidLC();
00056         CCalEntry* entry = CCalEntry::NewL(CCalEntry::EAppt, guid, CCalEntry::EMethodNone, 0);
00057         CleanupStack::Pop(guid);
00058         CleanupStack::PushL(entry);
00059 
00060         // set some values
00061         TTime now;
00062         now.UniversalTime();
00063         now += TTimeIntervalHours(1); // start in an hour
00064         TCalTime start;
00065         start.SetTimeUtcL(now);
00066         now += TTimeIntervalHours(1); // lasts an hour
00067         TCalTime end;
00068         end.SetTimeUtcL(now);
00069         entry->SetStartAndEndTimeL(start, end);
00070         entry->SetDescriptionL(aDescription);
00071 
00072         // make the repeat rule
00073         TCalRRule rule(TCalRRule::EWeekly);
00074         rule.SetDtStart(start);
00075         rule.SetCount(52/aWeeklyInterval); // for a year
00076         rule.SetInterval(aWeeklyInterval);
00077 
00078         RArray<TDay> days;
00079         CleanupClosePushL(days);
00080         days.AppendL(EMonday); 
00081         days.AppendL(ETuesday);
00082         days.AppendL(EWednesday);
00083         days.AppendL(EThursday);
00084         days.AppendL(EFriday);
00085 
00086         rule.SetByDay(days); // every weekday
00087         CleanupStack::PopAndDestroy(&days);
00088         entry->SetRRuleL(rule);
00089         
00090         // add the entry to the database
00091         RPointerArray<CCalEntry> array;
00092         CleanupClosePushL(array);
00093         array.AppendL(entry);
00094         // we don’t transfer ownership of entry to array.
00095         TInt success = 0;
00096         iCalView->StoreL(array, success);
00097         if (success < 1)
00098                 {
00099                 User::Leave(KErrGeneral);
00100                 }
00101         CleanupStack::PopAndDestroy(&array);
00102         CleanupStack::PopAndDestroy(entry);
00103         }
00104 
00105 const TInt KMaxGuidSize = 64;
00106 _LIT(KDateFormat,"%F%Y%M%D");
00107 _LIT(KTimeSeparator,"T");
00108 _LIT(KTimeFormat,"%F%H%T%S");
00109 _LIT(KTimeDivider,"-");
00110 _LIT(KAddress,"0x2000DF18_QuickRecipes@SymbianPress.com");
00111 
00112 HBufC8* CCalWrite::MakeGuidLC()
00113         {
00114         TTime now;
00115         now.UniversalTime();
00116         TBuf<KMaxGuidSize> time;
00117         now.FormatL(time,KTimeFormat);
00118         TBuf<KMaxGuidSize> date;
00119         now.FormatL(date,KDateFormat);
00120 
00121         HBufC8* guid = HBufC8::NewLC(KMaxGuidSize);
00122 
00123         TPtr8 guidPtr = guid->Des();
00124         guidPtr.Zero();
00125         guidPtr.Append(date);
00126         guidPtr.Append(KTimeSeparator());
00127         guidPtr.Append(time);
00128         guidPtr.Append(KTimeDivider());
00129         guidPtr.Append(KAddress());
00130 
00131         return guid;
00132         }
00133 
00134 // End of File

Generated by  doxygen 1.6.2