examples/AppServices/versit/VersitExample.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 // This example demonstrates a vCard parser
00015 //
00016 
00017 
00018 #include <e32base.h>
00019 #include <e32cons.h>
00020 #include <vcard.h>
00021 #include <versit.h>
00022 #include "VersitExample.h"
00023 
00024 // Global definition
00025 static CConsoleBase* gConsole;
00026 
00027 // name of file to write the vCard to
00028 _LIT(KVCardFileJIS,"\\Private\\E8000094\\charsetJIS.vcf");
00029 
00030 // string for display
00031 _LIT(KMsgPressAnyKey,"\n\nPress any key to continue\n");
00032 
00033 CExampleVersit* CExampleVersit::NewL()
00034         {
00035         CExampleVersit* self = new (ELeave) CExampleVersit();
00036         CleanupStack::PushL(self);
00037         self->ConstructL();
00038         CleanupStack::Pop(self);
00039         return self;
00040         }
00041         
00042 void CExampleVersit::ConstructL()
00043         {
00044         iParser = CParserVCard::NewL();
00045         }       
00046         
00047 CExampleVersit::~CExampleVersit()       
00048         {
00049         delete iParser;
00050         iFsSession .Close();
00051         }
00052 
00053 // Externalises a vCard to a file
00054 void CExampleVersit::ExternalizeToFileL(const TDesC& aFileName)
00055         {
00056         RFile file;
00057         iFsSession.Connect();
00058         // create the private directory (\Private\E8000094\) on writable drive
00059         User::LeaveIfError(iFsSession.CreatePrivatePath(RFs::GetSystemDrive()));
00060         User::LeaveIfError(file.Replace(iFsSession, aFileName, EFileWrite));
00061         CleanupClosePushL(file);
00062         iParser->ExternalizeL(file);
00063         CleanupStack::PopAndDestroy(&file);
00064         }
00065 
00066 // Internalises a vCard from a file
00067 void CExampleVersit::InternalizeFromFileL(RFile& aInputFile)
00068         {
00069         TInt size;
00070         if (aInputFile.Size(size)==KErrNone)
00071                 {
00072                 delete iParser;
00073                 iParser = NULL;
00074 
00075                 iParser = CParserVCard::NewL();
00076                 RFileReadStream stream(aInputFile);
00077                 CleanupClosePushL(stream);
00078                 iParser->InternalizeL(stream);
00079                 CleanupStack::PopAndDestroy(&stream);
00080                 }
00081         }
00082 
00083 // Creates a vCard containing a note property and a character set property parameter.
00084 // Then externalizes the vCard to a file.
00085 void CExampleVersit::CreateAndExternalizeVCardL()
00086         {
00087         //create a property value to hold some text
00088         _LIT(KNote,"\x4e79\x4f19\x5032");
00089         CParserPropertyValue* value=CParserPropertyValueHBufC::NewL(KNote);
00090         CleanupStack::PushL(value);
00091 
00092         CArrayPtr<CParserParam>* arrayOfParams = new(ELeave)CArrayPtrFlat<CParserParam>(5);
00093         CleanupStack::PushL(arrayOfParams);
00094 
00095         // Add a character set property parameter
00096         CParserParam* parserParam=CParserParam::NewL(KVersitTokenCHARSET,KVersitTokenJIS);
00097         CleanupStack::PushL(parserParam);
00098         arrayOfParams->AppendL(parserParam);
00099         CleanupStack::Pop(parserParam);
00100 
00101         // create the NOTE property
00102         CParserGroupedProperty* property=CParserGroupedProperty::NewL(*value,KVersitTokenNOTE,NULL,arrayOfParams);
00103 
00104         CleanupStack::Pop(2,value); // value, arrayOfParams
00105         CleanupStack::PushL(property);
00106 
00107         // Add the property to the vCard
00108         iParser->AddPropertyL(property);
00109         CleanupStack::Pop(property);
00110         //Sets the default transformation format
00111         iParser->SetDefaultCharSet(Versit::EJISCharSet);
00112         ExternalizeToFileL(KVCardFileJIS);
00113         }
00114         
00115 //Internalize the VCard
00116 void CExampleVersit::InternalizeVCardL()
00117         {
00118         RFile file;
00119         TInt err=file.Open(iFsSession,KVCardFileJIS,EFileRead);
00120         User::LeaveIfError(err);
00121         CleanupClosePushL(file);
00122         InternalizeFromFileL(file);
00123         CleanupStack::PopAndDestroy(&file);
00124         
00125         _LIT(KConsoleMessage1, "vCard has been successfully internalised from a file");
00126         gConsole->Printf(KConsoleMessage1);
00127         }
00128 
00129 void CExampleVersit::EgVersitL()
00130         {
00131         CreateAndExternalizeVCardL();
00132         InternalizeVCardL();
00133         }
00134 
00135 static void DoExampleL()
00136         {
00137         // Create the console to print the messages to.
00138         _LIT(KConsoleMessageDisplay, "Versit Example");
00139         _LIT(KConsoleStars,"\n*************************\n");
00140         gConsole = Console::NewL(KConsoleMessageDisplay,TSize(KConsFullScreen,KConsFullScreen));
00141         CleanupStack::PushL(gConsole);
00142         gConsole->Printf(KConsoleMessageDisplay);
00143         gConsole->Printf(KConsoleStars);
00144 
00145         CExampleVersit* egVersit= CExampleVersit::NewL();
00146         TRAPD(err, egVersit->EgVersitL());
00147         if (err)
00148                 {
00149                 _LIT(KFailed,"\n\nExample failed: leave code=%d");
00150                 gConsole->Printf(KFailed, err);
00151                 }
00152         delete egVersit;        
00153         // wait for user to press a key before destroying gConsole
00154         gConsole->Printf(KMsgPressAnyKey);
00155         gConsole->Getch();
00156         CleanupStack::PopAndDestroy(gConsole);
00157         }
00158 
00159 // Standard entry point function
00160 TInt E32Main()
00161         {
00162         __UHEAP_MARK;
00163         // Active scheduler required as this is a console app
00164         CActiveScheduler* scheduler=new CActiveScheduler;
00165         // If active scheduler has been created, install it.
00166         if (scheduler)
00167                 {
00168                 CActiveScheduler::Install(scheduler);
00169                 // Cleanup stack needed
00170                 CTrapCleanup* cleanup=CTrapCleanup::New();
00171                 if (cleanup)
00172                         {
00173                         TRAP_IGNORE(DoExampleL());
00174                         delete cleanup;
00175                         }
00176                 delete scheduler;
00177                 } 
00178         __UHEAP_MARKEND;
00179         return KErrNone;
00180         }

Generated by  doxygen 1.6.2