examples/SFExamples/PIM/AgendaAdd/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::AddNewL(TTime aStartUtc, const TDesC& aDescription)
00053         {
00054         // create a new entry
00055         HBufC8* guid = MakeGuidLC();
00056         CCalEntry* entry = CCalEntry::NewL(CCalEntry::EAppt, guid,
00057                                                                                                 CCalEntry::EMethodNone, 0);
00058         CleanupStack::Pop(guid);
00059         CleanupStack::PushL(entry);
00060 
00061         // set some values
00062         TCalTime start;
00063         start.SetTimeUtcL(aStartUtc);
00064         TTime endUtc = aStartUtc;
00065         endUtc += TTimeIntervalHours(1); 
00066         TCalTime end;
00067         end.SetTimeUtcL(endUtc);
00068         entry->SetStartAndEndTimeL(start, end);
00069         entry->SetDescriptionL(aDescription);
00070 
00071         // add the entry to the database
00072         RPointerArray<CCalEntry> array;
00073         CleanupClosePushL(array);
00074         array.AppendL(entry);
00075         // we don’t transfer ownership of entry to array.
00076         TInt success = 0;
00077         iCalView->StoreL(array, success);
00078         if (success < 1)
00079                 {
00080                 User::Leave(KErrGeneral);
00081                 }
00082         CleanupStack::PopAndDestroy(&array);
00083         CleanupStack::PopAndDestroy(entry);
00084         }
00085 
00086 // We are going to make a simplified version of a unique Id
00087 // generator.
00088 // It transfers the ownership of its result to the caller
00089 
00090 const TInt KMaxGuidSize = 64;
00091 _LIT(KDateFormat,"%F%Y%M%D");
00092 _LIT(KTimeSeparator,"T");
00093 _LIT(KTimeFormat,"%F%H%T%S");
00094 _LIT(KTimeDivider,"-");
00095 _LIT(KAddress,"0x2000DF18_QuickRecipes@SymbianPress.com");
00096 
00097 HBufC8* CCalWrite::MakeGuidLC()
00098         {
00099         TTime now;
00100         now.UniversalTime();
00101         TBuf<KMaxGuidSize> time;
00102         now.FormatL(time,KTimeFormat);
00103         TBuf<KMaxGuidSize> date;
00104         now.FormatL(date,KDateFormat);
00105 
00106         HBufC8* guid = HBufC8::NewLC(KMaxGuidSize);
00107 
00108         TPtr8 guidPtr = guid->Des();
00109         guidPtr.Zero();
00110         guidPtr.Append(date);
00111         guidPtr.Append(KTimeSeparator());
00112         guidPtr.Append(time);
00113         guidPtr.Append(KTimeDivider());
00114         guidPtr.Append(KAddress());
00115 
00116         return guid;
00117         }
00118 
00119 
00120 // End of File

Generated by  doxygen 1.6.2