contactextensions/predefinedcontacts/src/pdcdata.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Predefined contacts engine (state machine)
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <cntdb.h>                  // CContactDatabase
       
    20 #include <centralrepository.h>      // CRepository
       
    21 #include <centralrepository.h>      // CRepository
       
    22 #include <f32file.h>                // RFs 
       
    23 #include <CVPbkContactLinkArray.h>  // CVPbkContactLinkArray
       
    24 
       
    25 // User includes
       
    26 #include "pdcdata.h"            // CPdcData
       
    27 
       
    28 
       
    29 // Constants
       
    30 
       
    31 /**
       
    32  * Cenrep key for the predefined contacts
       
    33  */
       
    34 const TInt KCRUidPredefinedContacts = 0x2000BEE5;
       
    35 
       
    36 /**
       
    37  * Default file location index in cenrep file
       
    38  */
       
    39 const TInt KPdcFileLocation = 2;
       
    40 
       
    41 /**
       
    42  * Pdc data file name
       
    43  */
       
    44 _LIT( KPdcDataFileName, "c:\\private\\2000BEE5\\pdcdatafile");
       
    45 _LIT( KPdcDataFilePath, "c:\\private\\2000BEE5\\");
       
    46 /** 
       
    47  * Pdc data file major version number
       
    48  */ 
       
    49 const TInt  KPdcMajorVersion = 1;
       
    50 
       
    51 /** 
       
    52  * Pdc data file minor version number
       
    53  */ 
       
    54 const TInt  KPdcMinorVersion = 0;
       
    55 
       
    56 // ======== MEMBER FUNCTIONS ========
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CPdcData::NewLC
       
    60 // Symbian 1st phase constructor
       
    61 // @param    aFs    file system
       
    62 // @param    aContactIds ids of any predefined contacts that have been added.
       
    63 // @return Self pointer to CPdcData pushed to
       
    64 // the cleanup stack.
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CPdcData* CPdcData::NewL( RFs& aFs, CVPbkContactLinkArray& aLinkArray )
       
    68     {
       
    69     CPdcData* self = new( ELeave ) CPdcData( aFs, aLinkArray );
       
    70     CleanupStack::PushL( self );
       
    71     self->ConstructL();
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CPdcData::~CPdcData
       
    78 // Destructor
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CPdcData::~CPdcData()
       
    82     {
       
    83     delete iRepository;
       
    84     delete iLinkBuffer;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CPdcData::CPdcData
       
    89 // C++ constructor
       
    90 // @param    aFs    file system
       
    91 // @param    aLinkArray  links to contacts that have been added.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CPdcData::CPdcData( RFs& aFs, CVPbkContactLinkArray& aLinkArray )
       
    95     : iFs( aFs ), iLinkArray( aLinkArray )
       
    96     {
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 //  CPdcData::ConstructL
       
   101 //  Second-phase constructor
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void CPdcData::ConstructL()
       
   105     {   
       
   106     iRepository = CRepository::NewL( TUid::Uid( KCRUidPredefinedContacts  ) );
       
   107     }
       
   108     
       
   109 // ---------------------------------------------------------------------------
       
   110 // CPdcData::ContactsUpToDateL() 
       
   111 // Checks if the predefined contacts need to be added.
       
   112 // @return ETrue if the predefined contacts need to be added
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 TBool CPdcData::ContactsUpToDateL()
       
   116     {
       
   117     TBool upDate = ETrue;
       
   118     
       
   119     // Does the pdcdata file exist?
       
   120     RFile pdcDataFile;
       
   121     
       
   122     TInt error = pdcDataFile.Open( iFs, KPdcDataFileName,
       
   123             EFileRead | EFileStreamText );
       
   124     CleanupClosePushL( pdcDataFile );
       
   125 
       
   126     if ( error == KErrNone )
       
   127         {
       
   128          // Open a read stream and internalise the file
       
   129         RFileReadStream stream( pdcDataFile );
       
   130         stream.PushL();
       
   131         
       
   132         InternalizeL( stream );
       
   133     
       
   134         stream.Pop();
       
   135         stream.Close();            
       
   136         }  
       
   137     else 
       
   138         {  
       
   139         // If the pdc file doe not exist, it indicates that
       
   140         // this is the first run, a firmware upgrade or a
       
   141         // file system format. Hence the predefined contacts 
       
   142         // are added. Leave if it is a different error.
       
   143         if ( error != KErrNotFound && error != KErrPathNotFound )
       
   144             {
       
   145             User::Leave( error );
       
   146             }
       
   147         }
       
   148 
       
   149     CleanupStack::PopAndDestroy(); // pdcDataFile
       
   150     
       
   151     // Get timestamp from cenrep
       
   152     return upDate;
       
   153     }
       
   154       
       
   155 // ---------------------------------------------------------------------------
       
   156 // CPdcData::StoreL() 
       
   157 // Stores the ids of anycontacts that have been added. Plus the most recent 
       
   158 // info about the firmware etc if needed 
       
   159 // ---------------------------------------------------------------------------
       
   160 //  
       
   161 void CPdcData::StoreL() 
       
   162     {   
       
   163     // Write out the new details
       
   164     RFile pdcDataFile;
       
   165     TInt error = pdcDataFile.Replace( iFs, KPdcDataFileName, EFileWrite );
       
   166     if ( error ==  KErrPathNotFound )
       
   167        {
       
   168        // Create the path if it can not be found
       
   169  	   User::LeaveIfError( iFs.MkDir( KPdcDataFilePath ) );
       
   170  	   error = pdcDataFile.Replace( iFs, KPdcDataFileName, EFileWrite );
       
   171        }
       
   172     // If there has been an error leave
       
   173     User::LeaveIfError( error );
       
   174     
       
   175     CleanupClosePushL( pdcDataFile );
       
   176     
       
   177      // Open a write stream and externalise the file
       
   178     RFileWriteStream stream( pdcDataFile );
       
   179     stream.PushL();
       
   180     
       
   181     // Externalise the data to the stream.
       
   182     ExternalizeL( stream );
       
   183 
       
   184     // Tidy up
       
   185     stream.CommitL();
       
   186     stream.Pop();
       
   187     stream.Close();    
       
   188     CleanupStack::PopAndDestroy(); // pdcDataFile
       
   189     }
       
   190     
       
   191 // ---------------------------------------------------------------------------
       
   192 // CPdcData::GetFileLocationL
       
   193 // Gets the file location of the predefined contacts from the central 
       
   194 // repository.
       
   195 // @return  location of the predefined contacts. 
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 HBufC* CPdcData::GetFileLocationL()
       
   199     { 
       
   200     // Get the filename
       
   201     TFileName fileName;
       
   202     TInt error = iRepository->Get( KPdcFileLocation, fileName );
       
   203     User::LeaveIfError( error );
       
   204     
       
   205     HBufC* fileLocation = fileName.AllocL();
       
   206     
       
   207     return fileLocation;
       
   208     } 
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CPdcData::InternalizeL
       
   212 // Reads in the list of added contacts and any other persisted data such as
       
   213 // timestamp, firmware id.
       
   214 // @param aStream   readstream to read data from
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void CPdcData::InternalizeL( RReadStream& aStream )
       
   218     {
       
   219     // Read the major and minor version numbers in case there is a future 
       
   220     // BC break that may need to be detected.
       
   221     TInt major = aStream.ReadInt8L();
       
   222     TInt minor = aStream.ReadInt8L();
       
   223     
       
   224     ASSERT( major == KPdcMajorVersion && minor == KPdcMinorVersion );
       
   225     
       
   226     // Get the size of the list of contact links and store it in 
       
   227     // a buffer
       
   228     TInt bufferSize = aStream.ReadUint16L();
       
   229     iLinkBuffer = HBufC8::NewL( bufferSize );
       
   230     TPtr8 bufferPtr = iLinkBuffer->Des();
       
   231     aStream >> bufferPtr;
       
   232     bufferPtr.SetLength( bufferSize );
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // CPdcData::ExternalizeL
       
   237 // Writes out the added contacts list and any other the persisted data such 
       
   238 // as timestamp, firmware id
       
   239 // 
       
   240 // @param aStream   writestream to write data to
       
   241 // ---------------------------------------------------------------------------
       
   242 //  
       
   243 void CPdcData::ExternalizeL( RWriteStream& aStream ) const
       
   244     {
       
   245     // Write out the major and minor version numbers in case there is a future 
       
   246     // BC break that may need to be detected.
       
   247     aStream.WriteInt8L( KPdcMajorVersion );
       
   248     aStream.WriteInt8L( KPdcMinorVersion );
       
   249     
       
   250     // Write out the list of contacts, first pack the link list into
       
   251     // a buffer
       
   252     HBufC8* buf = iLinkArray.PackLC();
       
   253     
       
   254     // Write the size of the descriptor, followed by the buffer
       
   255     aStream.WriteUint16L(buf->Length());
       
   256     aStream << *buf;
       
   257     
       
   258     // tidy up buffer
       
   259     CleanupStack::PopAndDestroy(); // buf
       
   260     }
       
   261     
       
   262 // ---------------------------------------------------------------------------
       
   263 // CPdcData::LinkArrayBuffer
       
   264 // Gets the buffer of contact links
       
   265 // @return  buffer of contact links
       
   266 // ---------------------------------------------------------------------------
       
   267 // 
       
   268 HBufC8* CPdcData::LinkArrayBuffer()
       
   269     {
       
   270     return iLinkBuffer;
       
   271     }