contactextensions/predefinedcontacts/src/PdcVCardImporter.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:    Responsible for the importing of vCards
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <s32file.h>                    // RFileReadStream
       
    20 #include <f32file.h>                    // RFs 
       
    21 #include <MVPbkContactStore.h>          // MVPbkContactStore
       
    22 #include <CVPbkVCardEng.h>              // CVPbkVCardEng
       
    23 #include <MVPbkContactOperationBase.h>  // MVPbkContactOperationBase
       
    24 #include <MVPbkContactLinkArray.h>      // MVPbkContactLinkArray
       
    25 #include <CVPbkContactLinkArray.h>      // CVPbkContactLinkArray
       
    26 #include <MVPbkContactLink.h>           // MVPbkContactLink
       
    27 
       
    28 // User includes
       
    29 #include "PdcVCardImporter.h"
       
    30 #include "pdclogger.h"
       
    31 
       
    32 // Constants
       
    33 _LIT( KVCardWildCard, "*.vcf" ); // Wild card extension for vCards
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CPdcVCardImporter::NewL
       
    39 // Symbian 1st phase constructor
       
    40 // @param    aFs     file system
       
    41 // @param    aContactStore   contacts manager
       
    42 // @param    aContactStore   contacts store
       
    43 // @param    aLinkArray  links to contacts added.
       
    44 // @return a CPdcVCardImporter object.
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CPdcVCardImporter* CPdcVCardImporter::NewL( RFs& aFs,
       
    48                                         CVPbkContactManager& aContactManager,
       
    49                                         MVPbkContactStore& aContactStore,
       
    50                                         CVPbkContactLinkArray& aLinkArray )
       
    51     {
       
    52     CPdcVCardImporter* self = new( ELeave ) CPdcVCardImporter( aFs,
       
    53             aContactStore, aLinkArray );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL( aContactManager ); 
       
    56     CleanupStack::Pop( self );
       
    57     return self;
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CPdcVCardImporter::~CPdcVCardImporter
       
    63 // Destructor
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CPdcVCardImporter::~CPdcVCardImporter()
       
    67     {
       
    68     delete iVCardEngine;
       
    69     delete iFileDirectory;
       
    70     delete iImportOperation;
       
    71     delete iVCardList;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CPdcVCardImporter::CPdcVCardImporter
       
    76 // C++ constructor
       
    77 // @param    aFs     file system
       
    78 // @param    aContactStore   contacts store
       
    79 // @param    aLinkArray  links to contacts added.
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CPdcVCardImporter::CPdcVCardImporter( RFs& aFs,
       
    83                                           MVPbkContactStore& aContactStore,
       
    84                                           CVPbkContactLinkArray& aLinkArray )
       
    85     : CActive( EPriorityNormal),
       
    86       iContactStore( aContactStore ),
       
    87       iFs( aFs ),
       
    88       iLinkArray( aLinkArray )
       
    89     {
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 //  CPdcXmlImporter::ConstructL
       
    94 //  Second-phase constructor
       
    95 //  @param    aContactManager   contacts manager
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CPdcVCardImporter::ConstructL( CVPbkContactManager& aContactManager )
       
    99     {
       
   100     iVCardEngine = CVPbkVCardEng::NewL( aContactManager );
       
   101     CActiveScheduler::Add( this );
       
   102     }
       
   103     
       
   104 // ---------------------------------------------------------------------------
       
   105 // CPdcVCardImporter::GetVCardsL  
       
   106 //  Starts the syncronously importation of all the vCards in the 
       
   107 //  given directory.
       
   108 //  @param    aFileDirectory    directory containg vCards.
       
   109 //  @param    aCallerStatus   caller's TRequestStatus
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CPdcVCardImporter::GetVCardsL(const TDesC& aFileDirectory,
       
   113                                      TRequestStatus& aCallerStatus )
       
   114     {
       
   115     LOGTEXT(_L("CPdcVCardImporter::GetVCardsL"));
       
   116     aCallerStatus = KRequestPending;
       
   117     
       
   118     // Get list of vCards in directory
       
   119     TFindFile fileFinder( iFs );
       
   120     TInt error = fileFinder.FindWildByDir( KVCardWildCard, 
       
   121                                                     aFileDirectory, iVCardList);
       
   122     
       
   123     // Find the number of vCards in the directory
       
   124     if ( error == KErrNotFound )
       
   125         {
       
   126         // If the are no vCards in the directory, complete the
       
   127         // caller status
       
   128         TRequestStatus* s = &aCallerStatus;
       
   129 		User::RequestComplete( s, KErrNone );
       
   130         }
       
   131     else
       
   132         {
       
   133         // Store the callers TRequestStatus
       
   134         iCallerStatus = &aCallerStatus;
       
   135         
       
   136         // Store the directory
       
   137         iFileDirectory = aFileDirectory.AllocL();
       
   138         
       
   139         // Start reading in the vCards.
       
   140         SelfComplete();
       
   141         }
       
   142     }
       
   143       
       
   144 // ---------------------------------------------------------------------------
       
   145 // CPdcVCardImporter::ImportContactsL
       
   146 // Imports the contacts contained within a vCard.
       
   147 // @param   aFileName filename of vCard to import
       
   148 // ---------------------------------------------------------------------------
       
   149 //  
       
   150 void CPdcVCardImporter::ImportContactsL( const TDesC& aFileName )
       
   151     {
       
   152     LOGTEXT(_L("CPdcVCardImporter::ImportContactsL"));
       
   153     // Create the full path to the vCard (Note: TParse uses a TBuf<256)
       
   154     // internally ).
       
   155     TParse fileAndPath;
       
   156     fileAndPath.Set( aFileName, iFileDirectory, NULL );
       
   157     const TDesC& fullName = fileAndPath.FullName();
       
   158     
       
   159     // open the vCard into a file stream
       
   160     RFileReadStream vCard;
       
   161 	User::LeaveIfError( vCard.Open( iFs, fullName, EFileRead ) );
       
   162 	CleanupClosePushL( vCard );
       
   163 
       
   164     // Import the vCard into the contacts database
       
   165     iImportOperation = iVCardEngine->ImportVCardL(
       
   166             iContactStore, vCard , *this );
       
   167 	                                                       
       
   168 	// Cleanup
       
   169 	CleanupStack::PopAndDestroy(); // vCard
       
   170     }
       
   171     
       
   172 // ---------------------------------------------------------------------------
       
   173 // CPdcVCardImporter::SelfComplete
       
   174 // Signals that the AO should be run again by completing the outstanding
       
   175 // TRequestStatus.
       
   176 // ---------------------------------------------------------------------------
       
   177 //  
       
   178 void CPdcVCardImporter::SelfComplete()
       
   179     {
       
   180     LOGTEXT(_L("CPdcVCardImporter::SelfComplete"));
       
   181     // Begin the process of adding of the prefined contacts to contacts
       
   182     TRequestStatus* status = &iStatus;
       
   183     User::RequestComplete( status, KErrNone );
       
   184     SetActive(); 
       
   185     }
       
   186  
       
   187 // ---------------------------------------------------------------------------
       
   188 // CPdcVCardImporter::DoCancel
       
   189 // From CActive
       
   190 // Completes the caller's TRequestStatus with KErrCancel
       
   191 // ---------------------------------------------------------------------------
       
   192 //  
       
   193 void CPdcVCardImporter::DoCancel()
       
   194     {
       
   195     delete iImportOperation;
       
   196     iImportOperation = NULL;
       
   197     
       
   198     // If the vCard importer is cancelled, we need to complete the
       
   199     // callers TRequestStatus.
       
   200     User::RequestComplete( iCallerStatus, KErrCancel );
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CPdcVCardImporter::RunL
       
   205 // From CActive
       
   206 // When the request is completed, the next vCard is imported. 
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 void CPdcVCardImporter::RunL()
       
   210     {
       
   211     // Import the vCard
       
   212     const TDesC& fileName = (*iVCardList)[iVCardIndex].iName;
       
   213     ImportContactsL( fileName );
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // CPdcVCardImporter::RunError
       
   218 // From CActive
       
   219 // If RunL leaves, the caller's TRequestStatus is completed with the error.
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 TInt CPdcVCardImporter::RunError( TInt aError )
       
   223     {
       
   224     LOGTEXT(_L("CPdcVCardImporter::RunError"));
       
   225     // Report the error to the caller of the getVcards function by 
       
   226     // completing the callers activeObject.
       
   227     User::RequestComplete( iCallerStatus, aError );
       
   228     
       
   229     return KErrNone;
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // CPdcVCardImporter::AddToLinkArray
       
   234 // Creates copies of contact links and adds them to the contact array.
       
   235 // @param   aResults array of links to the contacts items that have been 
       
   236 //                   added.
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CPdcVCardImporter::AddToLinkArrayL( const MVPbkContactLinkArray& aResults)
       
   240     {
       
   241     // Store the links that have been added. A MVPbkContactLinkArray array
       
   242     // is read-only so copies of the links need to be made and added
       
   243     // to iLinkArray.
       
   244     TInt linkCount = aResults.Count();
       
   245     for ( TInt index = 0; index < linkCount; index++ )
       
   246         {
       
   247         const MVPbkContactLink& link = aResults[index];
       
   248         MVPbkContactLink* copiedLink = link.CloneLC();
       
   249         iLinkArray.AppendL( copiedLink );
       
   250         CleanupStack::Pop(); // copiedLink 
       
   251         }
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // CPdcVCardImporter::ContactsSaved
       
   256 // from MVPbkContactCopyObserver
       
   257 // Called when the contact has been successfully commited. If any contacts are 
       
   258 // created links to them are added to the link array. The next vCard is then 
       
   259 // queued for importing, if there are no more vCards to add, the caller's 
       
   260 // TRequestStatus is completed.
       
   261 //
       
   262 // @param aOperation    The operation that this observer monitors.
       
   263 // @param aResults  An array of links to copied contacts.
       
   264 //                  Caller takes the ownership of the 
       
   265 //                  object immediately.
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CPdcVCardImporter::ContactsSaved( 
       
   269         MVPbkContactOperationBase& /*aOperation*/,
       
   270         MVPbkContactLinkArray* aResults )
       
   271     {
       
   272     LOGTEXT(_L("CPdcVCardImporter::ContactsSaved"));
       
   273     delete iImportOperation;
       
   274     iImportOperation = NULL;
       
   275     
       
   276     if ( aResults )
       
   277         {
       
   278         // Store any links
       
   279         TRAP_IGNORE( AddToLinkArrayL( *aResults ) )
       
   280         
       
   281         // delete the results array
       
   282         delete aResults;
       
   283         }
       
   284 
       
   285     // Increase the index
       
   286     iVCardIndex++;
       
   287     
       
   288     // If last vCard has been imported,  
       
   289     // inform the caller or self complete to import the next vCard.
       
   290     if ( iVCardIndex < iVCardList->Count() )
       
   291         {
       
   292         SelfComplete();
       
   293         }
       
   294     else
       
   295         {
       
   296         User::RequestComplete( iCallerStatus, KErrNone );
       
   297         }
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CPdcVCardImporter::
       
   302 // from MVPbkContactCopyObserver
       
   303 // Called when there was en error while saving contact(s).
       
   304 // @param aOperation    The operation that this observer monitors.
       
   305 // @param aError an error that occured.
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 void CPdcVCardImporter::ContactsSavingFailed( 
       
   309         MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   310     {
       
   311     LOGTEXT2(_L("CPdcVCardImporter::ContactsSavingFailed aError = %d"), aError);
       
   312     // If last vCard has been handled, inform the caller or self complete to 
       
   313     // import the next vCard.   
       
   314         
       
   315     User::RequestComplete( iCallerStatus, aError );
       
   316     }
       
   317 
       
   318