creator/engine/src/creator_phonebookapi.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     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     bool success = false;
       
    40     for(int i = 0 ; i < list.count() ; i++ )
       
    41     	{
       
    42 		QContactDetail cntdetail = list.at(i);
       
    43 		success = store.saveDetail(&cntdetail);
       
    44     	}
       
    45     /*foreach( QContactDetail cntdetail, list )
       
    46         {
       
    47         success = store.saveDetail( &cntdetail );
       
    48         }
       
    49     */
       
    50     success = mContactMngr->saveContact( &store );
       
    51     id = store.localId();
       
    52     return id;
       
    53     }
       
    54 
       
    55 quint32 CCreatorPhonebookAPI::createGroup( const QString& groupName )
       
    56 	{
       
    57 	QContact newGroup;
       
    58 	newGroup.setType(QContactType::TypeGroup);
       
    59 	QContactName newGroupName;
       
    60 	newGroupName.setCustomLabel( groupName );
       
    61 	newGroup.saveDetail(&newGroupName);
       
    62 	mContactMngr->saveContact(&newGroup);
       
    63 	return newGroup.localId();
       
    64 	}
       
    65 
       
    66 int CCreatorPhonebookAPI::numberOfContacts()
       
    67 	{
       
    68 	QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
    69 	return contacts.count();
       
    70 	
       
    71 	}
       
    72 
       
    73 bool CCreatorPhonebookAPI::IsContactGroupL( const QContact& contact )
       
    74 	{
       
    75 	 
       
    76 	if( contact.type() == QContactType::TypeGroup )
       
    77 		{
       
    78 		return true;
       
    79 		}
       
    80 	return false;
       
    81 	}
       
    82 
       
    83 int CCreatorPhonebookAPI::addContactToGroup( QContactLocalId group, QContactLocalId contact )
       
    84 	{
       
    85 	QContact newGroup = mContactMngr->contact( group );
       
    86 	QContact contactLink = mContactMngr->contact( contact );
       
    87 	int ret = 0;
       
    88 	if( contact && IsContactGroupL( contactLink ) == false )
       
    89 	    {
       
    90         QList<QContactRelationship> relationships = contactLink.relationships(QContactRelationship::HasMember);
       
    91         if( !relationships.count() && contactLink.type() == QContactType::TypeContact ) //just for contacts that are not in relationship - not in group yet
       
    92 			{
       
    93 			QContactRelationship* contactRel = new QContactRelationship();
       
    94 			contactRel->setRelationshipType(QContactRelationship::HasMember);
       
    95 			contactRel->setFirst(newGroup.id());
       
    96 			contactRel->setSecond( contactLink.id() );
       
    97 			mContactMngr->saveRelationship( contactRel );
       
    98 			delete contactRel;
       
    99 			ret++;
       
   100 			}         
       
   101 		}
       
   102 	return ret;
       
   103 	}
       
   104 int CCreatorPhonebookAPI::addToGroup(QContactLocalId group, int amount)
       
   105 	{
       
   106 	QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
   107 	int ret = 0;		
       
   108 	int tmp = 0;
       
   109 	int cnt = 0;
       
   110 	
       
   111 	for( int i=0; cnt < amount && i < contacts.count() ; i++ )
       
   112 		{
       
   113 		QContact contact = mContactMngr->contact( mContactMngr->contactIds().at(i) );
       
   114 		if( contact.type() == QContactType::TypeContact )
       
   115 			{
       
   116 			tmp = addContactToGroup(group,mContactMngr->contactIds().at(i)); 
       
   117 			ret += tmp;
       
   118 			if(tmp)
       
   119 				{
       
   120 				cnt++;
       
   121 				tmp = 0;
       
   122 				}
       
   123 			}
       
   124 		}
       
   125 	return ret;
       
   126 	}
       
   127 
       
   128 
       
   129 bool CCreatorPhonebookAPI::deleteAllContacts()
       
   130     {
       
   131     QList<QContactLocalId> all = mContactMngr->contactIds();
       
   132     return deleteContacts( all );
       
   133     }
       
   134 
       
   135 bool CCreatorPhonebookAPI::deleteAllContacts( const QString& type )
       
   136     {
       
   137     QList<QContactLocalId> contactsToDelete;
       
   138     QList<QContactLocalId> contacts = mContactMngr->contactIds();
       
   139     foreach(QContactLocalId contactId, contacts)
       
   140         {
       
   141         QContact contact = mContactMngr->contact( contactId );
       
   142         if( contact.type() == type )
       
   143             {
       
   144             contactsToDelete.append( contact.localId() );
       
   145             }
       
   146         }
       
   147     return deleteContacts( contactsToDelete );
       
   148     }
       
   149 
       
   150 
       
   151 bool CCreatorPhonebookAPI::deleteContacts( const QList<QContactLocalId>& list )
       
   152     {
       
   153     QMap<int, QContactManager::Error> errorMap;
       
   154     return mContactMngr->removeContacts( list, &errorMap );
       
   155     }
       
   156 
       
   157 QContact CCreatorPhonebookAPI::contact( const QContactLocalId& contactId )
       
   158     {
       
   159     return mContactMngr->contact( contactId );
       
   160     }
       
   161 // End of File