utilityapps/creator/engine/src/creator_phonebookapi.cpp
changeset 55 2d9cac8919d3
parent 51 b048e15729d6
equal deleted inserted replaced
53:819e59dfc032 55:2d9cac8919d3
       
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "creator_phonebookapi.h"
       
    19 
       
    20 CCreatorPhonebookAPI::CCreatorPhonebookAPI ()
       
    21     {
       
    22     mContactMngr = new QContactManager("symbian");
       
    23     }
       
    24 
       
    25 CCreatorPhonebookAPI::~CCreatorPhonebookAPI ()
       
    26     {
       
    27     if( mContactMngr )
       
    28         {
       
    29         delete mContactMngr;
       
    30         mContactMngr = NULL;
       
    31         }
       
    32     }
       
    33 
       
    34 quint32 CCreatorPhonebookAPI::saveContact( const QList<QContactDetail>& list )
       
    35     {
       
    36     // create a new contact item
       
    37     QContact store;
       
    38     quint32 id;
       
    39     for(int i = 0 ; i < list.count() ; i++ )
       
    40     	{
       
    41 		QContactDetail cntdetail = list.at(i);
       
    42 		store.saveDetail(&cntdetail);
       
    43     	}
       
    44     /*foreach( QContactDetail cntdetail, list )
       
    45         {
       
    46         store.saveDetail( &cntdetail );
       
    47         }
       
    48     */
       
    49     mContactMngr->saveContact( &store );
       
    50     id = store.localId();
       
    51     return id;
       
    52     }
       
    53 
       
    54 quint32 CCreatorPhonebookAPI::createGroup( const QString& groupName )
       
    55 	{
       
    56 	QContact newGroup;
       
    57 	newGroup.setType(QContactType::TypeGroup);
       
    58 	QContactName newGroupName;
       
    59 	newGroupName.setCustomLabel( groupName );
       
    60 	newGroup.saveDetail(&newGroupName);
       
    61 	mContactMngr->saveContact(&newGroup);
       
    62 	return newGroup.localId();
       
    63 	}
       
    64 
       
    65 int CCreatorPhonebookAPI::numberOfContacts()
       
    66 	{
       
    67 	QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
    68 	return contacts.count();
       
    69 	
       
    70 	}
       
    71 
       
    72 bool CCreatorPhonebookAPI::IsContactGroupL( const QContact& contact )
       
    73 	{
       
    74 	 
       
    75 	if( contact.type() == QContactType::TypeGroup )
       
    76 		{
       
    77 		return true;
       
    78 		}
       
    79 	return false;
       
    80 	}
       
    81 
       
    82 int CCreatorPhonebookAPI::addContactToGroup( QContactLocalId group, QContactLocalId contact )
       
    83 	{
       
    84 	QContact newGroup = mContactMngr->contact( group );
       
    85 	QContact contactLink = mContactMngr->contact( contact );
       
    86 	int ret = 0;
       
    87 	if( contact && IsContactGroupL( contactLink ) == false )
       
    88 	    {
       
    89         QList<QContactRelationship> relationships = contactLink.relationships(QContactRelationship::HasMember);
       
    90         if( !relationships.count() && contactLink.type() == QContactType::TypeContact ) //just for contacts that are not in relationship - not in group yet
       
    91 			{
       
    92 			QContactRelationship* contactRel = new QContactRelationship();
       
    93 			contactRel->setRelationshipType(QContactRelationship::HasMember);
       
    94 			contactRel->setFirst(newGroup.id());
       
    95 			contactRel->setSecond( contactLink.id() );
       
    96 			mContactMngr->saveRelationship( contactRel );
       
    97 			delete contactRel;
       
    98 			ret++;
       
    99 			}         
       
   100 		}
       
   101 	return ret;
       
   102 	}
       
   103 int CCreatorPhonebookAPI::addToGroup(QContactLocalId group, int amount)
       
   104 	{
       
   105 	QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
   106 	int ret = 0;		
       
   107 	int tmp = 0;
       
   108 	int cnt = 0;
       
   109 	
       
   110 	for( int i=0; cnt < amount && i < contacts.count() ; i++ )
       
   111 		{
       
   112 		QContact contact = mContactMngr->contact( mContactMngr->contactIds().at(i) );
       
   113 		if( contact.type() == QContactType::TypeContact )
       
   114 			{
       
   115 			tmp = addContactToGroup(group,mContactMngr->contactIds().at(i)); 
       
   116 			ret += tmp;
       
   117 			if(tmp)
       
   118 				{
       
   119 				cnt++;
       
   120 				tmp = 0;
       
   121 				}
       
   122 			}
       
   123 		}
       
   124 	return ret;
       
   125 	}
       
   126 
       
   127 
       
   128 bool CCreatorPhonebookAPI::deleteAllContacts()
       
   129     {
       
   130     QList<QContactLocalId> all = mContactMngr->contactIds();
       
   131     return deleteContacts( all );
       
   132     }
       
   133 
       
   134 bool CCreatorPhonebookAPI::deleteAllContacts( const QString& type )
       
   135     {
       
   136     QList<QContactLocalId> contactsToDelete;
       
   137     QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
   138     foreach(QContactLocalId contactId, contacts)
       
   139         {
       
   140         QContact contact = mContactMngr->contact( contactId );
       
   141         if( contact.type() == type )
       
   142             {
       
   143             contactsToDelete.append( contact.localId() );
       
   144             }
       
   145         }
       
   146     return deleteContacts( contactsToDelete );
       
   147     }
       
   148 
       
   149 
       
   150 bool CCreatorPhonebookAPI::deleteContacts( const QList<QContactLocalId>& list )
       
   151     {
       
   152     QMap<int, QContactManager::Error> errorMap;
       
   153     return mContactMngr->removeContacts( list, &errorMap );
       
   154     }
       
   155 
       
   156 QContact CCreatorPhonebookAPI::contact( const QContactLocalId& contactId )
       
   157     {
       
   158     return mContactMngr->contact( contactId );
       
   159     }
       
   160 
       
   161 QString CCreatorPhonebookAPI::phoneNumber( const QContactLocalId& contactId )
       
   162     {
       
   163     QString strNumber;
       
   164     QContact contact = mContactMngr->contact( contactId );
       
   165     if( !contact.isEmpty() )
       
   166         {
       
   167         QContactPhoneNumber phoneNumber = static_cast<QContactPhoneNumber>( contact.detail( QContactPhoneNumber::DefinitionName ) );
       
   168         strNumber = phoneNumber.number();
       
   169         }
       
   170     return strNumber;
       
   171     }
       
   172 
       
   173 bool CCreatorPhonebookAPI::contactDetails( const QContactLocalId& contactId, QString& name, QString& phoneNumber, QString& email )
       
   174     {
       
   175     bool success(false);
       
   176     QContact contact = mContactMngr->contact( contactId );
       
   177     if( !contact.isEmpty() )
       
   178         {
       
   179         QContactPhoneNumber contactPhoneNumber = static_cast<QContactPhoneNumber>( contact.detail( QContactPhoneNumber::DefinitionName ) );
       
   180         phoneNumber = contactPhoneNumber.number();
       
   181         QContactEmailAddress contactEmailAddress = static_cast<QContactEmailAddress>( contact.detail( QContactEmailAddress::DefinitionName ) );
       
   182         email = contactEmailAddress.emailAddress();
       
   183         QContactDisplayLabel contactDisplayLabel = static_cast<QContactDisplayLabel>( contact.detail( QContactDisplayLabel::DefinitionName ) );
       
   184         name = contactDisplayLabel.label();
       
   185         success = true;
       
   186         }
       
   187     return success;
       
   188     }
       
   189 // End of File