Display contacts from the phone book

Login to reply to this topic.
Tue, 2005-11-22 10:53
Joined: 2005-09-08
Forum posts: 24
Hi guys,

I am trying to access contacts for the phone book ,  the below code work for UIQ , 7710 not tested on S60 , but surly does not work for series 80 . Cant figure out why . I just want to display the list in the list box.




   /* Read name of the default database */
    _LIT( KOrgContactFile,"" );
    TBuf<128> orgContactFile( KOrgContactFile );
   
   /* Getting the original name of the database */
    CContactDatabase::GetDefaultNameL( orgContactFile );
   
   /*convert it into lower case*/
   orgContactFile.LowerCase();
   
   /*try to open the database if not say can not open phone book*/
   TRAPD(result, iContactDb = CContactDatabase::OpenL( orgContactFile )Wink;

   if(result == KErrNone)
   {
      /* View the contacts of type KUidContactCard */
      iContactDb->SetDbViewContactType( KUidContactCard );

      /*Filter the contacts that are smsables  */
      CContactDatabase::TContactViewFilter filter(CContactDatabase::ESmsable);

      /* Sort Contacts by surname and name*/
      TFieldType aFieldType1( KUidContactFieldFamilyName);
      TFieldType aFieldType2( KUidContactFieldGivenName );
   
      CContactDatabase::TSortPref sortPref1( aFieldType1 );
      CContactDatabase::TSortPref sortPref2( aFieldType2 );

      /* Sort contacts by Family and Given Name */
      CArrayFixFlat<CContactDatabase::TSortPref>* aSortOrder =
                  new (ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(2);

      CleanupStack::PushL( aSortOrder );

      aSortOrder->AppendL( sortPref1 );
      aSortOrder->AppendL( sortPref2 );

      /* The database takes ownership of the sort order array passed in */
      iContactDb->SortL( aSortOrder );

      /* The caller does not take ownership of this object.
       so do not push it onto the CleanupStack */
      const CContactIdArray* contacts = iContactDb->SortedItemsL();

      /* The Number of conatcts in the conatcs array list*/
      contactsCount =  contacts->Count() ;
      
      /* Resetting the Array list*/
      iContactsList->Reset();
            
      /*Iterating to each contact in this loop */
      for ( TInt i=0; i != ( contactsCount-1 ); i++)
      {
         /* The caller takes ownership of the returned object.
         So push it onto the CleanupStack*/
         TRAP(result,contact = iContactDb->OpenContactL( ( *contacts )[i] ));
         if( result == KErrInUse || result == KErrNotFound)
         {
            iContactDb->CloseContactL(( *contacts )[i] );
            contact = iContactDb->OpenContactL( ( *contacts )[i] );
            contact = iContactDb->ReadContactL( ( *contacts )[i] ) ;
         }
         else
         {
            contact = iContactDb->ReadContactL( ( *contacts )[i] ) ;      
         }

         CleanupStack::PushL( contact );

         /* settingthe contact Card Fields to the fieldset*/
         CContactItemFieldSet& fieldSet = contact->CardFields();

         /* Get first name*/
         TInt findpos( fieldSet.Find( KUidContactFieldGivenName ) );

         /* Check that the first name field is actually there.*/
         if ( (findpos > -1) || (findpos >= fieldSet.Count()) )
         {
            CContactItemField& nameField = fieldSet[findpos];
            contextTextFieldName = nameField.TextStorage();
            nameBuffer.Copy(contextTextFieldName->Text());
         }

         /*Getting the family name*/
         TInt familyfindpos( fieldSet.Find( KUidContactFieldFamilyName ) );
         if ( (familyfindpos > -1) || (familyfindpos >= fieldSet.Count()) )
         {
            CContactItemField& nameField = fieldSet[familyfindpos];
            contextTextFieldName = nameField.TextStorage();
            if(nameBuffer.Length()!=0)
            {
               nameBuffer.Append(_L(" "));
            }

            /* Appening the family name to the first to get the full name */
            nameBuffer.Append(contextTextFieldName->Text());
         }

         /* If no name found copying ing UnNamed */
         if(nameBuffer.Length()==0)
         {
            nameBuffer.Copy(_L("UnNamed"));
         }

         /* gets the position of the  phone number*/
         findpos = fieldSet.Find(KUidContactFieldPhoneNumber);
         if((findpos > -1) || (findpos >= fieldSet.Count()))
         {
            /* This loop gets each lable */
            while((findpos + fieldNumber) !=fieldSet.Count())
               {
                  CContactItemField& contactNumberField = fieldSet[ findpos + fieldNumber];
                  TBuf<100> fieldName;
                  /* Copy the lable in the field name */
                  fieldName.Copy( contactNumberField.Label() );
                  
                  /* Getting it the field name into lower case */
                  fieldName.LowerCase();
                  
                  /* compares it to the word "mobile":*/
                  if(fieldName.Find(_L("mobile"))!= KErrNotFound)
                  {
                     nameMobile.Copy(nameBuffer);
                     nameMobile.Append(_L(" - "));
                     nameMobile.Append(fieldName);
                     mobileNumberFound = ETrue;
                  }

                  /* if the word mobile found */
                  if(mobileNumberFound)
                  {
                     /* getts the corresponding contact number */
                     destMobileNumber = contactNumberField.TextStorage();
                     if(destMobileNumber->Text().Length()!=0)
                     {
                        /* Add the name to the list that have mobile number */
                        iContactsList->AppendL(nameMobile);
                     }
               
                     mobileNumberFound = EFalse;
                  }

                  /* This condition is for search for the name that is selected
                     if found , gets the corresponding conatct number and sends
                     sms to it*/
                  if(nameMobile.Compare(aName)==0 )
                     {
                        /* getts the contact Number*/
                        destMobileNumber = contactNumberField.TextStorage();
                        iRecipientNumber.Copy(destMobileNumber->Text());
                        iMessageText.Copy(_L("I need help"));
                        messageSent = SendL();
                        if(messageSent == KErrNone)
                        {
                           iState = EWaitingForMoving;
                           SetActive();
                        }
                        else
                        {
                           /* Some thing fucked up*/
                           OSD::MessageBox(_L("Unable to send SMS."),EFalse);
                        }
               
                        destMobileNumberFound =  ETrue;
                        break;
                     }

                  fieldNumber++;
               }
            }

         iContactDb->CloseContactL( contact->Id() );

Can some one help me with this ?

Thanks

Tue, 2005-11-22 13:48
NewLC AdministratorSymbian AccreditedForum Nokia Champion
Joined: 2003-01-14
Forum posts: 2006
Re: Display contacts from the phone book
What is the issue with S80 ?

Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant

Wed, 2005-11-23 02:58
Joined: 2005-09-08
Forum posts: 24
Re: Display contacts from the phone book
Hi ,

I cant display my contacts . A empty list box is displayed


Wed, 2005-11-23 08:10
Joined: 2005-09-08
Forum posts: 24
Re: Display contacts from the phone book
Hi again,

  Is there a difference in how series 80 and how other phones store their contacts. What I did was i overwrote  the contacts file from UIQ to the series 80 contacts file . And it work without changeing a bit of code on the emulator.

I am still woundering what format does series 80 store in . I cant do the above on the device right  Grin

Anyone with solutions?

Mobile_guy
Mon, 2005-11-28 22:37
Joined: 2004-08-06
Forum posts: 57
Re: Display contacts from the phone book
where exactly does it break down?
at some point the code must return different results than on other platforms, where is that?

from my experience dealing with contacts is similar (not exactly though) on uiq2 and series80.

regards,

Blizzz
Wed, 2005-12-21 05:56
Joined: 2005-09-08
Forum posts: 24
Re: Display contacts from the phone book
Hi,

I am trying to get the mobile label then compare and then get the corresponding mobile number

fails here :

CContactItemField& contactNumberField = fieldSet[ findpos + fieldNumber];
                  TBuf<100> fieldName;
                  /* Copy the lable in the field name */
                  fieldName.Copy( contactNumberField.Label() );

 destMobileNumber = contactNumberField.TextStorage();

please help ,

Need it fast

Thanks.

Mobile_guy
  • Login to reply to this topic.