/*=========================================================================== FUNCTION: ABR_AddContactRec DESCRIPTION: This function add a record to address book PARAMETERS: pMe [in] - Pointer to the CAddrBookRuim structure. This structure contains information specific to this applet. pTextName[in]- name field of record pTextNum[in]- telephone number field of record DEPENDENCIES: None RETURN VALUE: AEE_SUCCESS - IADDRBOOK_CreateRec () invocation was successful other - fail SIDE EFFECTS: None ===========================================================================*/ int32 ABR_AddContactRec( CAddrBookRuim *pMe, AEECLSID ContactCLS, AECHAR *pTextName, AECHAR *pTextNum) { AEEAddrField field[2]; // fields of a record int32 nRet = 0; // return code IAddrBook *pAddrBook; // check point if(pMe==NULL || pMe->pIShell==NULL || pTextName==NULL || pTextNum==NULL) { return -1; } // create instance of ADDRESS BOOK for RUIM if(!ISHELL_CreateInstance(pMe->pIShell, ContactCLS, (void **)&pAddrBook)) { IAddrRec *pRec; //Num field[0].fID = (ContactCLS==AEECLSID_ADDRBOOK_RUIM)? AEE_ADDRFIELD_PHONE_GENERIC:AEE_ADDRFIELD_PHONE_WORK; field[0].fType = AEEDB_FT_PHONE; field[0].pBuffer= pTextNum; field[0].wDataLen = (WSTRLEN(pTextNum)+1)*sizeof(AECHAR); //Name field[1].fID = AEE_ADDRFIELD_NAME; field[1].fType = AEEDB_FT_STRING; field[1].pBuffer= pTextName; field[1].wDataLen = (WSTRLEN(pTextName)+1)*sizeof(AECHAR); // create a record with 2 fields pRec = IADDRBOOK_CreateRec(pAddrBook, AEE_ADDR_CAT_NONE, (AEEAddrField*)field, 2); if(pRec) { IADDRREC_Release(pRec); nRet=0; // success } else { nRet=2; // IADDRBOOK_CreateRec fail; } IADDRBOOK_Release(pAddrBook); } else { nRet=1; // error : ISHELL_CreateInstance fail } DBGPRINTF("Add contact nRet = %d", nRet); return nRet; }
// ================================================================================ // FUNCTION : CopyAddrBookToBackupDb // DESCRIPTION : Copy all contacts in addrressbook to backup database. // ================================================================================ bool CDbHandler::CopyAddrBookToBackupDb() { IDatabase *pBkpDatabase; IAddrBook * pIAddrBook=NULL; IAddrRec* rec=NULL; CContactInfo *pContact; //delete previous backup database. //this should change to: create a tempory backup database. after successfully creating this. //rename current bakup to old backup and temp backup to backup and delete old backup if ( !DeleteDatabase( SM_BACKUPDB )) return false; pContact = new CContactInfo(); if ( NULL==pContact ) return false; if ( !OpenBackupDb(&pBkpDatabase)) { delete pContact; return false; } //enumerate addressbook if ( SUCCESS != ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pIAddrBook)) { delete pContact; IDATABASE_Release(pBkpDatabase); return false; } if( AEE_SUCCESS == IADDRBOOK_EnumRecInit(pIAddrBook,AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE,NULL,0) ) { while( NULL != (rec=IADDRBOOK_EnumNextRec(pIAddrBook)) ) { pContact->SetContactInfo(rec); //Add contact to backup database AddToBackupDb( pBkpDatabase, pContact); IADDRREC_Release(rec); } } delete pContact; //release address book IADDRBOOK_Release(pIAddrBook); IDATABASE_Release(pBkpDatabase); return true; }
// ================================================================================ // FUNCTION : GetContactList // DESCRIPTION : Get the addressbook contacts // ================================================================================ int CDbHandler::GetContactList(AECHAR **pszContactList) { //create address book IAddrBook * pIAddrBook=NULL; if ( SUCCESS != ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pIAddrBook)) { return 0; } //get the number of records in addressbook and create a buffer to hold the names int nSize = IADDRBOOK_GetNumRecs(pIAddrBook); *pszContactList = (AECHAR*)MALLOC(nSize*100*sizeof(AECHAR));//assume approximate name length + mobile number is 100 if ( NULL==*pszContactList ) goto error; if( AEE_SUCCESS == IADDRBOOK_EnumRecInit(pIAddrBook,AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE,NULL,0) ) { IAddrRec* rec=NULL; while( NULL != (rec=IADDRBOOK_EnumNextRec(pIAddrBook)) ) { AEEAddrField* pField; int noOfFilelds = IADDRREC_GetFieldCount(rec); for ( int i=0; i<noOfFilelds; i++) { pField = IADDRREC_GetField(rec, i); //append first name and last name to the buffer if ( SM_ADDRFIELD_FIRSTNAME==pField->fID ) { WSTRCAT(*pszContactList, (AECHAR*)pField->pBuffer); WSTRCAT(*pszContactList, L" "); } //append the last name to the buffer if ( SM_ADDRFIELD_LASTNAME==pField->fID ) { WSTRCAT(*pszContactList, (AECHAR*)pField->pBuffer); } //append the mobile number if ( SM_ADDRFIELD_MOBILE==pField->fID ) { WSTRCAT(*pszContactList, L":"); WSTRCAT(*pszContactList, (AECHAR*)pField->pBuffer); } } WSTRCAT(*pszContactList, L";"); IADDRREC_Release(rec); } } error: IADDRBOOK_Release(pIAddrBook); return nSize; }
// ================================================================================ // FUNCTION : CreateChangeDb // DESCRIPTION : Create Changes database. This is the database synchronize with server. // ================================================================================ int CDbHandler::CreateChangeDb() { IDatabase * pChangeDb = NULL; IDatabase * pBackupDb = NULL; //if ChangeDb is alreday there. we return SUCCESS. //means that last backup is not success and we continue with it. if ( OpenChangeDb(&pChangeDb) ) return SM_SUCCESS; //Open Backup database if ( !OpenBackupDb(&pBackupDb)) return SM_ERROR; //enumerate Address book to create ChaneDb //create address book IAddrBook * pIAddrBook=NULL; if ( SUCCESS != ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pIAddrBook)) { //release changeDb IDATABASE_Release(pChangeDb); //release Backup Database IDATABASE_Release(pBackupDb); return SM_ERROR; } //Process-1 //Enumerate through addressbook, copmare it with last time backedup database //create ChangeDb. In this process we only do additions and updates to ChangeDb. if( AEE_SUCCESS == IADDRBOOK_EnumRecInit(pIAddrBook,AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE,NULL,0) ) { IAddrRec* rec=NULL; //enumerate records CContactInfo * pAddrBookContact = new CContactInfo(); CContactInfo * pBackedupContact = new CContactInfo(); if ( NULL==pAddrBookContact || NULL==pBackedupContact ) { //if only one memory alloc failed. if ( NULL!=pAddrBookContact ) delete pAddrBookContact; if ( NULL!=pBackedupContact ) delete pBackedupContact; //release changeDb IDATABASE_Release(pChangeDb); //release Backup Database IDATABASE_Release(pBackupDb); //release address book IADDRBOOK_Release(pIAddrBook); return SM_NOMEMORY; } uint16 nRecId = 0; DbSearchResult searchStatus; while( NULL != (rec=IADDRBOOK_EnumNextRec(pIAddrBook)) ) { //Fetch single contact info from Address book //Get current contact from address book nRecId = IADDRREC_GetRecID(rec); if ( SM_SUCCESS != pAddrBookContact->SetContactInfo(rec) ) { //cleanup and exit IADDRREC_Release(rec); delete pAddrBookContact; delete pBackedupContact; //release changeDb IDATABASE_Release(pChangeDb); //release Backup Database IDATABASE_Release(pBackupDb); //release address book IADDRBOOK_Release(pIAddrBook); return SM_ERROR; } //search the corresponding record in backup database. searchStatus = SearchContactInBackupDb(pBackupDb, nRecId, pAddrBookContact, pBackedupContact); //depending on search status add, update and delete the record to ChangedDb if ( !AddToChangesDb(pChangeDb, searchStatus, pAddrBookContact)) { //cleanup and exit IADDRREC_Release(rec); delete pAddrBookContact; delete pBackedupContact; //release changeDb IDATABASE_Release(pChangeDb); //release Backup Database IDATABASE_Release(pBackupDb); //release address book IADDRBOOK_Release(pIAddrBook); return SM_ERROR; } IADDRREC_Release(rec); } delete pAddrBookContact; delete pBackedupContact; } //End of Process-1 //Process-2 //Add deleted records(from last backup) to ChangeDb //After this process ChangeDb is completed. if ( !SetDeletedRecordsInAddrBook(pBackupDb, pChangeDb)) { //cleanup and exit } //end process-2 //release Backup Database IDATABASE_Release(pBackupDb); //release changeDb IDATABASE_Release(pChangeDb); //release address book IADDRBOOK_Release(pIAddrBook); return SM_SUCCESS; }
// ================================================================================ // FUNCTION : UpdateAddrBook // DESCRIPTION : Update phone addressbook. // ================================================================================ bool CDbHandler::UpdateAddrBook(CContactInfo *pContactAr, int nSize) { int nState=0; IAddrBook *pAddrBook=NULL; IAddrRec *pRec=NULL; AEEAddrField *field=NULL; int nCount=0; int nRet=0; uint16 wId=0; AECHAR* pwsGlobalId=NULL; if(SUCCESS!=ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pAddrBook)) return false; for (int i=0; i<nSize; i++) { nState = pContactAr[i].GetState(); if ( pContactAr[i].FillAddrFields(&field, nCount)) { switch (nState) { case SM_RECORD_ADD: pRec = IADDRBOOK_CreateRec(pAddrBook,AEE_ADDR_CAT_NONE, (AEEAddrField*)field, nCount); if (NULL==pRec) { IADDRBOOK_Release(pAddrBook); delete [] field; return false; } //set contact id of record. pContactAr[i].SetContactId( IADDRREC_GetRecID(pRec)); IADDRREC_Release(pRec); pRec=NULL; break; case SM_RECORD_UPDATE: wId = pContactAr[i].GetContactId(); pRec = IADDRBOOK_GetRecByID(pAddrBook, wId); if ( NULL!=pRec ) { if ( AEE_SUCCESS != IADDRREC_UpdateAllFields(pRec,field,nCount)) { IADDRREC_Release(pRec); IADDRBOOK_Release(pAddrBook); delete [] field; return false; } //set contact id of record. //pContactAr[i].SetContactId( IADDRREC_GetRecID(pRec)); IADDRREC_Release(pRec); pRec=NULL; } break; case SM_RECORD_DELETE: wId = pContactAr[i].GetContactId(); DBGPRINTF("Deleted rec id= %u", wId); pRec = IADDRBOOK_GetRecByID(pAddrBook, wId); if ( NULL!=pRec ) { //set contact id of record. pContactAr[i].SetContactId( IADDRREC_GetRecID(pRec)); if(AEE_SUCCESS !=IADDRREC_RemoveRec(pRec)) { IADDRBOOK_Release(pAddrBook); delete [] field; return false; } DBGPRINTF("deleted record"); //no need to relase the address record after deleting it. } break; default: break; } if ( NULL!=field ) { delete [] field; field=NULL; } } } IADDRBOOK_Release(pAddrBook); return true; }
// ================================================================================ // FUNCTION : SetDeletedRecordsInAddrBook // DESCRIPTION : Set deleted records in changes databse according to addressbook. // ================================================================================ bool CDbHandler::SetDeletedRecordsInAddrBook(IDatabase *pBackupDb, IDatabase *pCghangeDb) { IDBRecord* pRecord=NULL; uint16 nFieldLen; AEEDBFieldName rFieldName; AEEDBFieldType rFieldType; uint16 nId; uint16 nAddrRecId; IAddrBook * pIAddrBook=NULL; IAddrRec* rec=NULL; bool bStatus = false; //create address book if ( SUCCESS != ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pIAddrBook)) { return false; } //enumerate last backed up database and check any deleted records IDATABASE_Reset( pBackupDb ); int n = (int) IDATABASE_GetRecordCount(pBackupDb); while ( NULL != (pRecord=IDATABASE_GetNextRecord( pBackupDb )) ) { // Get the first field. DB_BACKUP_RECORD_ID. rFieldType = IDBRECORD_NextField( pRecord, &rFieldName, &nFieldLen ); if ( (nFieldLen>0) && (SMDB_FT_WORD==rFieldType) ) { IDBRECORD_GetFieldWord(pRecord, &nId); //check weather the record is present in the addressbook if( AEE_SUCCESS == IADDRBOOK_EnumRecInit(pIAddrBook,AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE,NULL,0) ) { while( NULL != (rec=IADDRBOOK_EnumNextRec(pIAddrBook)) ) { nAddrRecId = IADDRREC_GetRecID (rec); if ( nId==nAddrRecId ) //if we found the record in addrbook we stop searching { bStatus = true; IADDRREC_Release(rec); break; } IADDRREC_Release(rec); } } else { // Release the db record IDBRECORD_Release( pRecord ); return false; } } // Release the record IDBRECORD_Release( pRecord ); //if record didnt find in address book, add it to ChangeDb as a deleted record. if ( !bStatus ) { CContactInfo * pContact = new CContactInfo(); if ( NULL==pContact ) return false; pContact->SetDeletedContact(nId); if ( !AddToChangesDb(pCghangeDb, SM_RECORD_DELETE, pContact)) { delete pContact; //release address book IADDRBOOK_Release(pIAddrBook); return false; } delete pContact; } } //release address book IADDRBOOK_Release(pIAddrBook); return true; }