emailcontacts/contactactionservice/src/cfscactionutils.cpp
branchRCL_3
changeset 26 968773a0b6ef
parent 25 3533d4323edc
--- a/emailcontacts/contactactionservice/src/cfscactionutils.cpp	Wed Sep 01 12:28:57 2010 +0100
+++ b/emailcontacts/contactactionservice/src/cfscactionutils.cpp	Tue Sep 14 20:48:24 2010 +0300
@@ -31,9 +31,7 @@
 #include <centralrepository.h>
 #include <CoreApplicationUIsSDKCRKeys.h> 
 #include <settingsinternalcrkeys.h>
-//<cmail> Header neither in Cmail nor in the platform
 #include <crcseprofileregistry.h>
-//</cmail>
 #include <NetworkHandlingDomainPSKeys.h>
 #include <MVPbkStoreContact.h>
 #include <MVPbkStoreContactFieldCollection.h>
@@ -42,10 +40,8 @@
 #include <TVPbkFieldVersitProperty.h>
 #include <MVPbkContactFieldData.h>
 #include <MVPbkContactFieldTextData.h>
-//<cmail>
 #include "fscactionplugincrkeys.h"
 #include "fsccontactactionservicedefines.h"
-//</cmail>
 #include <FscActionUtils.rsg>
 #include <barsread.h>
 #include <Pbk2ContactNameFormatterFactory.h>
@@ -56,11 +52,8 @@
 #include <CVPbkFieldTypeSelector.h>
 #include <CVPbkFieldFilter.h>
 #include <commonphoneparser.h>
-#include <bautils.h> // Fix for EMZG-7M23KX
-//<cmail> hardcoded paths removal
+#include <bautils.h>
 #include <data_caging_path_literals.hrh>
-//</cmail>
-
 #include "cfscactionutils.h"
 #include "cfsccontactaction.h"
 #include "mfscreasoncallback.h"
@@ -87,15 +80,11 @@
 const TUint32 KPoCDefaultSettings = 0x199;
 
 const TInt KMaxLengthOfNumber = 100;
-
-//Fix for: ECWG-7QYAVS
-const TInt KMaxLengthOfName = 100;
-//end for fix
+const TInt KMaxLengthOfAddrData = 255;
+const TInt KMaxLengthOfName = 256; // same as max addr len + extra whitespace
 
 _LIT( KSpace, " " );
-//<cmail> hard coded path removed
 _LIT( KFscRscFileName, "fscactionutils.rsc" );
-//</cmail>
 
 // ======== MEMBER FUNCTIONS ========
 
@@ -135,9 +124,7 @@
     delete iRep;
     if ( iRCSEProfileRegistry != NULL )
         {
-        //<cmail>
         delete iRCSEProfileRegistry;
-        //</cmail>
         }
     }
 
@@ -160,7 +147,6 @@
     FUNC_LOG;
     HBufC* noteText = NULL;
     
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
     
     if ( aContactName )
@@ -189,7 +175,6 @@
     FUNC_LOG;
     HBufC* noteText = NULL;
     
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
     
     noteText = StringLoader::LoadL( aNote, aCounter );
@@ -210,7 +195,6 @@
     {
     FUNC_LOG;
     
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
     
     HBufC* noteText = NULL;
@@ -269,102 +253,38 @@
 HBufC* CFscActionUtils::GetContactNameL( MVPbkStoreContact& aStoreContact )
     {
     FUNC_LOG;
-    HBufC* contactName = HBufC::NewLC( KMaxLengthOfName + 1 ); // one ' ' char
+
+    TBuf<KMaxLengthOfName> contactName;
     
-    // Search for the contact name
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
     
-    TResourceReader selectorReader;
-    CCoeEnv::Static()->CreateResourceReaderLC( selectorReader,
-            R_FSC_FIRST_NAME_SELECTOR );
-
-    CVPbkFieldTypeSelector* fieldTypeSelector = CVPbkFieldTypeSelector::NewL(
-            selectorReader, iContactManager.FieldTypes() );
-    CleanupStack::PopAndDestroy(); // selectorReader
-
-    CVPbkFieldFilter::TConfig config(
-            const_cast<MVPbkStoreContactFieldCollection&> (
-                    aStoreContact.Fields() ), fieldTypeSelector, NULL );
-
-    CVPbkFieldFilter* fieldFilter = CVPbkFieldFilter::NewL( config );
-    TInt fieldCount = fieldFilter->FieldCount();
-    const MVPbkStoreContactField* field = NULL;
-    if ( fieldCount == 1 )
+    TInt err( KErrNone );
+    TRAP( err, AppendFieldToContactL( 
+            contactName, R_FSC_FIRST_NAME_SELECTOR, aStoreContact ) );
+    if ( err == KErrNone )
         {
-        field = fieldFilter->FieldAtLC(0);
-        contactName->Des().Append(
-                MVPbkContactFieldTextData::Cast( 
-                        field->FieldData() ).Text() );
-        CleanupStack::PopAndDestroy();//field
+        TRAP( err, AppendFieldToContactL( 
+                contactName, R_FSC_LAST_NAME_SELECTOR, aStoreContact, 
+                ETrue ) );
+        }
+    if ( err == KErrNone && contactName.Length() <= 0 )
+        {
+        // If no first or last name was found, try to append company name
+        TRAP_IGNORE( AppendFieldToContactL( 
+                contactName, R_FSC_COMPANY_NAME_SELECTOR, aStoreContact ) );
         }
 
-    delete fieldFilter;
-    delete fieldTypeSelector;
-
-    // Search for the contact second name
-    CCoeEnv::Static()->CreateResourceReaderLC( selectorReader,
-            R_FSC_LAST_NAME_SELECTOR );
-    fieldTypeSelector = CVPbkFieldTypeSelector::NewL( selectorReader,
-            iContactManager.FieldTypes() );
-    CleanupStack::PopAndDestroy();//selectorReader
-    config.iFieldSelector = fieldTypeSelector;
-
-    fieldFilter = CVPbkFieldFilter::NewL( config );
-    fieldCount = fieldFilter->FieldCount();
-    if ( fieldCount == 1 )
+    // If there was an error or the contact name is zero length, use
+    // pre-defined unnamed string instead..
+    if ( err != KErrNone || contactName.Length() <= 0 )
         {
-        field = fieldFilter->FieldAtLC(0);
-        if ( contactName->Des().Length() > 0 )
-            {
-            contactName->Des().Append( KSpace );
-            }
-        contactName->Des().Append(
-                MVPbkContactFieldTextData::Cast( 
-                        field->FieldData() ).Text() );
-        CleanupStack::PopAndDestroy();//field
-        }
-
-    // If first or second name hasn't been found -> find company name
-    if ( contactName->Des().Length() <= 0 )
-        {
-        delete fieldFilter;
-        delete fieldTypeSelector;
-
-        CCoeEnv::Static()->CreateResourceReaderLC( selectorReader,
-                R_FSC_COMPANY_NAME_SELECTOR );
-        fieldTypeSelector = CVPbkFieldTypeSelector::NewL( selectorReader,
-                iContactManager.FieldTypes() );
-        CleanupStack::PopAndDestroy();//selectorReader
-        config.iFieldSelector = fieldTypeSelector;
-
-        fieldFilter = CVPbkFieldFilter::NewL( config );
-        fieldCount = fieldFilter->FieldCount();
-        if ( fieldCount == 1 )
-            {
-            field = fieldFilter->FieldAtLC(0);
-            contactName->Des().Append(
-                    MVPbkContactFieldTextData::Cast(
-                            field->FieldData() ).Text() );
-            CleanupStack::PopAndDestroy();//field
-            }
-        }
-    // no first or second name, nor company name -> return "unnamed" string
-    if ( contactName->Des().Length() <= 0 )
-        {
-        //append (Unnamed)
         HBufC* unnamed = StringLoader::LoadL( R_QTN_FS_UNNAMED );
-        contactName->Des().Append( *unnamed );
+        contactName.Zero();
+        contactName.Append( *unnamed );
         delete unnamed;
         }
-
-    delete fieldFilter;
-    delete fieldTypeSelector;
-    CleanupStack::Pop( contactName );
-
     CCoeEnv::Static()->DeleteResourceFile( offset );
-
-    return contactName;
+    return contactName.AllocL();
     }
 
 // ---------------------------------------------------------------------------
@@ -388,7 +308,6 @@
     CFscFieldPropertyArray* fieldProperties =
             CFscFieldPropertyArray::NewL( iContactManager.FieldTypes() );
 
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
 
     // Create resource reader for new resource
@@ -440,7 +359,6 @@
             {
             defaultPrioritiesArray.Append( EVPbkDefaultTypeSms );
             defaultPrioritiesArray.Append( EVPbkDefaultTypeMms );
-            // Fix for ELWG-7SPH5H 
             params.SetUseDefaultDirectly( ETrue );
             break;
             }
@@ -518,10 +436,8 @@
             }
 
         // Format phone number
-        //Fix for: MHOA-7SQF6T
         CommonPhoneParser::ParsePhoneNumber( aNumber,
                             CommonPhoneParser::EPlainPhoneNumber );
-        //end for fix
         }
 
     // Destroy objects
@@ -569,10 +485,8 @@
             }
 
         // Format phone number
-        //Fix for: MHOA-7SQF6T
         CommonPhoneParser::ParsePhoneNumber( aNumber,
                             CommonPhoneParser::EPlainPhoneNumber );
-        //end for fix
         }
 
     // Destroy objects
@@ -624,10 +538,8 @@
         }
 
     // Format phone number
-    //Fix for: MHOA-7SQF6T
     CommonPhoneParser::ParsePhoneNumber( aVoipAddress,
                         CommonPhoneParser::EPlainPhoneNumber );
-    //end for fix
     
     // Destroy objects
     delete selectedField;
@@ -655,7 +567,6 @@
     TInt ret = KErrNone;
     
     // Search for conference number
-    // Fix for EMZG-7M23KX
     TInt offset = FindAndAddResourceFileL();
     TResourceReader selectorReader;
     CCoeEnv::Static()->CreateResourceReaderLC( selectorReader,
@@ -825,7 +736,7 @@
     }
 
 // ---------------------------------------------------------------------------
-// CFscActionUtils::GetEmailAddressL
+// CFscActionUtils::GetMessageAddressL
 // ---------------------------------------------------------------------------
 //
 TInt CFscActionUtils::GetMessageAddressL( MVPbkStoreContact& aStoreContact,
@@ -858,7 +769,8 @@
         TPtrC msgAddress = MVPbkContactFieldTextData::Cast(
                 selectedField->FieldData() ).Text();
         
-        if ( msgAddress.Length() )
+        if ( msgAddress.Length() && 
+             msgAddress.Length() <= KMaxLengthOfAddrData )
             {
             aMsgAddress.Copy( msgAddress );
             }
@@ -907,7 +819,8 @@
         TPtrC emailAddress = MVPbkContactFieldTextData::Cast(
                 selectedField->FieldData() ).Text();
         
-        if ( emailAddress.Length() )
+        if ( emailAddress.Length() && 
+             emailAddress.Length() <= KMaxLengthOfAddrData )
             {
             aEmailAddress.Copy( emailAddress );
             }
@@ -953,13 +866,11 @@
     TBool available( EFalse );
 
     // Search for the contact name
-    // Fix for EMZG-7M23KX  
     TInt offset = FindAndAddResourceFileL();
     
     TResourceReader selectorReader;
     CVPbkFieldTypeSelector* fieldTypeSelector = NULL;
     CVPbkFieldFilter* fieldFilter = NULL;
-    TInt fieldCount = 0;
     TInt selector = 0;
 
     switch ( aContactNumberType )
@@ -1025,15 +936,16 @@
         fieldTypeSelector = CVPbkFieldTypeSelector::NewL( selectorReader,
                 iContactManager.FieldTypes() );
         CleanupStack::PopAndDestroy(); // selectorReader
-
+        CleanupStack::PushL( fieldTypeSelector );
+        
         CVPbkFieldFilter::TConfig config(
                 const_cast<MVPbkStoreContactFieldCollection&> (
                         aStoreContact.Fields() ), fieldTypeSelector, NULL);
 
         fieldFilter = CVPbkFieldFilter::NewL( config );
-        fieldCount = fieldFilter->FieldCount();
+        CleanupStack::PushL( fieldFilter );
         
-        if ( fieldCount )
+        if ( fieldFilter->FieldCount() )
             {
             if ( selector == R_FSC_CONF_NUMBER_SELECTOR )
                 {
@@ -1042,7 +954,7 @@
                 field = fieldFilter->FieldAtLC(0);
                 confNum.Append( MVPbkContactFieldTextData::Cast( 
                         field->FieldData() ).Text() );
-                CleanupStack::PopAndDestroy();//field
+                CleanupStack::PopAndDestroy(); // field
                 
                 if ( CommonPhoneParser::IsValidPhoneNumber( confNum, 
                         CommonPhoneParser::EContactCardNumber ) )
@@ -1055,9 +967,7 @@
                 available = ETrue;
                 }
             }
-
-        delete fieldFilter;
-        delete fieldTypeSelector;
+        CleanupStack::PopAndDestroy( 2 ); // fieldFilter, fieldTypeSelector
         }
     
     CCoeEnv::Static()->DeleteResourceFile( offset );
@@ -1133,7 +1043,7 @@
         TInt err = cenRep->Get( KPoCDefaultSettings, settingsId );
         if ( err == KErrNone )
             {
-            result = (settingsId != KErrNotFound );
+            result = ( settingsId != KErrNotFound );
             }
         delete cenRep;
         }
@@ -1154,9 +1064,7 @@
         {
         RArray<TUint32> ids;
         CleanupClosePushL( ids );
-        //<cmail>
         iRCSEProfileRegistry->GetAllIdsL( ids );
-        //</cmail>
         configured = ids.Count() > 0;
         CleanupStack::PopAndDestroy( &ids );
         }
@@ -1251,15 +1159,8 @@
 
     if ( FeatureManager::FeatureSupported( KFeatureIdCommonVoip ) )
         {
-        //<cmail>
         TRAP_IGNORE( iRCSEProfileRegistry = CRCSEProfileRegistry::NewL() );
-        //</cmail>
         }
-    
-    //<cmail>
-    //iRCSEProfileRegistry = NULL;
-    //</cmail>
-
     }
 
 // ---------------------------------------------------------------------------
@@ -1291,7 +1192,6 @@
     return fileName;
     }
 
-// Fix for EMZG-7M23KX
 // ---------------------------------------------------------------------------
 // CFscActionUtils::FindAndAddResourceFileL
 // ---------------------------------------------------------------------------
@@ -1299,14 +1199,10 @@
 TInt CFscActionUtils::FindAndAddResourceFileL()
     {
     FUNC_LOG;
-    //<cmail> hardcoded removed
     TFileName resourceFileName( KDC_RESOURCE_FILES_DIR );
-    resourceFileName.Append(KFscRscFileName);
-    //</cmail>
+    resourceFileName.Append( KFscRscFileName );
     BaflUtils::NearestLanguageFile( CCoeEnv::Static()->FsSession(), resourceFileName );
-    TInt offset = CCoeEnv::Static()->AddResourceFileL( resourceFileName );
-    
-    return offset;
+    return CCoeEnv::Static()->AddResourceFileL( resourceFileName );
     }
 // ---------------------------------------------------------------------------
 // CFscActionUtils::CloseSelectDialogL
@@ -1321,6 +1217,52 @@
         }
     }
 
+// ---------------------------------------------------------------------------
+// CFscActionUtils::AppendFieldToContactL
+// ---------------------------------------------------------------------------
+//
+void CFscActionUtils::AppendFieldToContactL( TDes& aContact,
+        TInt aFieldResourceId,
+        MVPbkStoreContact& aStoreContact,
+        TBool aAddSpace )
+    {
+    FUNC_LOG;
 
-// ======== GLOBAL FUNCTIONS ========
+    TResourceReader selectorReader;
+    CCoeEnv::Static()->CreateResourceReaderLC( selectorReader,
+            aFieldResourceId );
+
+    CVPbkFieldTypeSelector* fieldTypeSelector = CVPbkFieldTypeSelector::NewL(
+            selectorReader, iContactManager.FieldTypes() );
+    CleanupStack::PopAndDestroy(); // selectorReader
+    CleanupStack::PushL( fieldTypeSelector );
+
+    CVPbkFieldFilter::TConfig config(
+            const_cast<MVPbkStoreContactFieldCollection&> (
+                    aStoreContact.Fields() ), fieldTypeSelector, NULL );
+
+    CVPbkFieldFilter* fieldFilter = CVPbkFieldFilter::NewL( config );
+    CleanupStack::PushL( fieldFilter );
 
+    if ( fieldFilter->FieldCount() == 1 )
+        {
+        MVPbkStoreContactField* field = fieldFilter->FieldAtLC( 0 );
+        if ( aAddSpace && aContact.Length() > 0 )
+            {
+            aContact.Append( KSpace );
+            }
+        TPtrC castFieldText = MVPbkContactFieldTextData::Cast( 
+                field->FieldData() ).Text();
+
+        if ( ( aContact.Length() + castFieldText.Length() ) > KMaxLengthOfName )
+            {
+            User::Leave( KErrOverflow );
+            }
+
+        aContact.Append( MVPbkContactFieldTextData::Cast(
+                field->FieldData() ).Text() );
+        CleanupStack::PopAndDestroy(); // field
+        }
+    CleanupStack::PopAndDestroy( 2 ); // fieldFilter, fieldTypeSelector    
+    }
+