examples/AppServices/versit/VersitExample.cpp

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

Generated by  doxygen 1.6.2