// ================================================================================
// 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;
}
Exemplo n.º 2
0
/*===========================================================================

FUNCTION: ABR_GetContactRec

DESCRIPTION:
  This function get all record from address book and show every record as a manu item

PARAMETERS:
  pMe [in] - Pointer to the CAddrBookRuim structure. This structure contains 
    information specific to this applet. 
  pMenu[out] - menu show all address book menu  
  pnTotal[out] - all records count of address book

DEPENDENCIES:
   None

RETURN VALUE:
  AEE_SUCCESS -  successfully enumate all record in address book
  other - fail


SIDE EFFECTS:
   None
===========================================================================*/
int32 ABR_GetContactRec( CAddrBookRuim * pMe, AEECLSID ContactCLS, IMenuCtl* pMenu, uint32* pnTotal)
{
    IAddrBook *pb;
    IAddrRec *pR;
    int nRet = 0;
    uint32 nTempTotal;
    boolean bFlgNameGet;
    boolean bFlgNumGet;

    AECHAR *aszNum, *aszName;


    if(pMe==NULL || pMe->pIShell==NULL || pMenu==NULL || pnTotal==NULL)
    {
        return (-1);
    }

    aszNum = (AECHAR *)MALLOC(MAX_CONTACT_NUM_SIZE*sizeof(AECHAR));
    aszName = (AECHAR *)MALLOC(MAX_CONTACT_NAME_SIZE*sizeof(AECHAR));

    
    if(aszNum==NULL ||  aszName==NULL)
    {
        return (-2);
    }

    // Clear any items from the IMenuCtl
    IMENUCTL_Reset( pMenu );
    IMENUCTL_SetRect( pMenu, &pMe->m_rScreenRect );
    IMENUCTL_SetTitle( pMenu, ADDRBOOKRUIM_RES_FILE, IDS_VIEW_MENU, NULL);

    nTempTotal = 0;

    if(!ISHELL_CreateInstance(pMe->pIShell, ContactCLS, (void **)&pb))
    {
        nRet = IADDRBOOK_EnumRecInit(pb,  AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE, NULL, 0);
        if(nRet == AEE_SUCCESS)
        {
            while((pR = IADDRBOOK_EnumNextRec(pb)) != NULL)
            {
                AEEAddrField *ptr = NULL;
                int i;
                int nFields;

                bFlgNameGet = FALSE;
                bFlgNumGet = FALSE;
                MEMSET( aszName, 0, MAX_CONTACT_NAME_SIZE*sizeof(AECHAR));
                MEMSET( aszNum,  0, MAX_CONTACT_NUM_SIZE*sizeof(AECHAR));
                nFields= IADDRREC_GetFieldCount(pR);
                for(i = 0; i < nFields; i++)
                {
                    ptr = IADDRREC_GetField(pR,i);
                    DBGPRINTF("lintao fID   =%d", ptr->fID);
                    // find name field and save it;
                    if (   ptr->fID == AEE_ADDRFIELD_NAME  
                       ||ptr->fID == AEE_ADDRFIELD_FIRSTNAME
                       ||ptr->fID == AEE_ADDRFIELD_LASTNAME
                       ||ptr->fID == AEE_ADDRFIELD_NICKNAME)
                    { 
                        if(bFlgNameGet==FALSE)
                        {
                            bFlgNameGet = TRUE;
                            WSTRCPY(aszName, ptr->pBuffer);
                        }
                    }

                    // find number field and save it;
                    if (    ptr->fID >= AEE_ADDRFIELD_PHONE_WORK
                        && ptr->fID <= AEE_ADDRFIELD_PHONE_OTHER)
                    { 
                        if(bFlgNumGet==FALSE)
                        {
                            bFlgNumGet = TRUE;
                            WSTRCPY(aszNum, ptr->pBuffer);
                        }
                    }

                }

                if(bFlgNameGet && bFlgNumGet) 
                {
                    nTempTotal++;
                    IMENUCTL_AddItem(pMenu, 
                                     NULL, 
                                     0,  
                                     ABR_REC_LIST_ID + IADDRREC_GetRecID(pR), 
                                     aszName, 
                                     NULL);
                }
            }                  
        }

        IADDRBOOK_Release(pb);
    }
    else
    {
        nRet=1; // error : ISHELL_CreateInstance fail
    }

    *pnTotal = nTempTotal;
    IMENUCTL_SetActive( pMenu, TRUE );

    FREEIF(aszNum);
    FREEIF(aszName);
    return nRet;
}
// ================================================================================
// 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		: 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;
}