ULONG CExMapi::GetContactCount()
{	
	BOOL doLog = m_pCtrl->IsEnableLog();
	if(doLog)
	{
		m_logHelper.LogPAB(L"------------------------------------");
		m_logHelper.LogPAB(L"Function GetPABCotactCount START:");
		m_logHelper.LogPAB(L"------------------------------------");
	}

	ULONG count = 0;
	LPMAPIFOLDER pFolder = NULL;
	LPMAPITABLE lpMsgStoreTable = NULL;
	lpMsgStoreTable = GetMessageStoresTable();	
	CString strFolder;
	int msgStoreCount = 0;
	wchar_t buffer[5];
	while(OpenNextMessageStore(lpMsgStoreTable))
	{			
		//pFolder = GetContactCount(OpenRootFolder(),count);			

		msgStoreCount++;
		_itow(msgStoreCount,buffer,10);
		if(doLog)
		{
			if(msgStoreCount > 1)
				m_logHelper.LogPAB(L"-------------------------------------");
			m_logHelper.LogPAB(L"Message Store No : "+CString(buffer));
			m_logHelper.LogPAB(L"-------------------------------------");
		}

		pFolder = OpenContacts();
		if(pFolder && IsContactFolder(pFolder))
		{
			strFolder = GetFolderName(pFolder);
			if(GetContents(pFolder))
			{
				count += GetRowCount();
			}		
			pFolder = GetContactCount(pFolder,count,doLog);
		}		
		if(m_pCtrl->IsPABContactCoutningAbort())
			break;
		__recursionCount = 0;
	}
	if(doLog)
	{
		m_logHelper.LogPAB(L"------------------------------------");
		m_logHelper.LogPAB(L"Function GetPABCotactCount END:");
		m_logHelper.LogPAB(L"------------------------------------");	
		m_logHelper.ClosePABLog();
	}
	__recursionCount = 0;
	RELEASE(lpMsgStoreTable);		
	
	return count;
}
BOOL CExMapi::GetContacts(CObArray& contactArray,CTRError& error)
{
	BOOL doLog = m_pCtrl->IsEnableLog();
	if(doLog)
	{
		m_logHelper.LogPAB(L"------------------------------------");
		m_logHelper.LogPAB(L"Function GetPABCotacts START:");
		m_logHelper.LogPAB(L"------------------------------------");
	}

	CString strFilterBy = m_pCtrl->GetPABSerachBy();
	CString strStartWith = m_pCtrl->GetPABSerachStartWith();
	strStartWith = strStartWith.MakeLower();
	ULONG pageSize;
	m_pCtrl->get_PABPageSize(&pageSize);
	ULONG pageNumber = m_pCtrl->GetPABPageNumber();
	LONG contactCount = 0;
	LONG skipCount = 0;
	LONG startIndex = -1;
	LONG contactIndex = 0;
	LONG folderCount = 0;
	if(pageSize>0)
	{
		startIndex = (pageSize * pageNumber) - pageSize;
	}

	CString strFolder;
	int msgStoreCount = 0;
	wchar_t buffer[5];

	LPMAPIFOLDER pSubFolder=NULL;	
	LPMAPITABLE lpMsgStoreTable = NULL;
	lpMsgStoreTable = GetMessageStoresTable();	
	if(lpMsgStoreTable)
	{
		while(OpenNextMessageStore(lpMsgStoreTable))
		{	
			//GetContacts(OpenRootFolder(),folderCount,contactCount,skipCount,contactIndex,contactArray);

			msgStoreCount++;
			_itow(msgStoreCount,buffer,10);
			if(doLog)
			{
				if(msgStoreCount > 1)
					m_logHelper.LogPAB(L"-------------------------------------");
				m_logHelper.LogPAB(L"Message Store No : "+CString(buffer));
				m_logHelper.LogPAB(L"-------------------------------------");
			}

			pSubFolder = OpenContacts();
			if(pSubFolder && IsContactFolder(pSubFolder))
			{
				if(doLog)
					m_logHelper.LogPAB(L"Open Main Contacts Folder");

				strFolder = GetFolderName(pSubFolder);
				folderCount++;
				if(GetContents(pSubFolder))
				{
					SortContents(TABLE_SORT_ASCEND,PR_DISPLAY_NAME);
					//SortContents(TABLE_SORT_ASCEND,PR_SUBJECT);

					CString strText;
					CExMapiContact mapiContact;
					CString name;
					CString email;
					BOOL mustFilter = FALSE;
					mustFilter = strStartWith != "";
					
					if(doLog)
						m_logHelper.LogPAB(L"Start reading contacts from main contact fodler");

					while(GetNextContact(mapiContact) && !m_pCtrl->IsPABSearchAbort()) 
					{
						if(pageSize > 0)
						{
							if(contactCount == pageSize)
							{
								break;								
							}
						}

						contactIndex++;
						if(mustFilter)
						{
							if(strFilterBy == "Name")
							{
								mapiContact.GetName(name,PR_GIVEN_NAME);
								name = name.MakeLower();
								if(name.Find(strStartWith) != 0)				
								{
									continue;									
								}
							}

							if(strFilterBy == "Email")
							{
								mapiContact.GetEmail(email);
								email = email.MakeLower();
								if(email.Find(strStartWith) != 0)
								{
									continue;
								}
							}																
						}
						if(startIndex > 0 && skipCount < startIndex)
						{
							skipCount++;
							continue;								
						}

						CContact* pContact = new CContact();

						pContact->SetId(contactIndex);

						pContact->SetFolderName(strFolder);

						mapiContact.GetName(strText);
						pContact->SetFullName(strText);

						mapiContact.GetCompany(strText);
						pContact->SetCompany(strText);

						mapiContact.GetIMAddress(strText);
						pContact->SetIMAddress(strText);

						mapiContact.GetName(strText,PR_GIVEN_NAME);
						pContact->SetFirstName(strText);

						mapiContact.GetName(strText,PR_MIDDLE_NAME);
						pContact->SetMiddleName(strText);

						mapiContact.GetName(strText,PR_SURNAME);
						pContact->SetLastName(strText);

						mapiContact.GetEmail(strText);
						pContact->SetEmail(strText);	

						mapiContact.GetEmail(strText,2);//Email 2
						pContact->SetEmail2(strText);

						mapiContact.GetEmail(strText,3);//Email 3
						pContact->SetEmail3(strText);

						mapiContact.GetPhoneNumber(strText,PR_BUSINESS_TELEPHONE_NUMBER);
						pContact->SetBusinessPhone(strText);

						mapiContact.GetPhoneNumber(strText,PR_COMPANY_MAIN_PHONE_NUMBER);
						pContact->SetCompanyPhone(strText);

						mapiContact.GetPhoneNumber(strText,PR_BUSINESS_FAX_NUMBER);
						pContact->SetFax(strText);

						mapiContact.GetPhoneNumber(strText,PR_MOBILE_TELEPHONE_NUMBER);
						pContact->SetMobilePhone(strText);

						mapiContact.GetPhoneNumber(strText,PR_HOME_TELEPHONE_NUMBER);
						pContact->SetHomePhone(strText);

						if(m_pCtrl->FetchUnique())
						{
							BOOL isContactExist = FALSE;
							for(int i=0;i<contactArray.GetCount();i++)
							{
								CContact* pTemp = (CContact*)contactArray.GetAt(i);

								if(pContact->GetEmail() != "" && pTemp->GetEmail() == pContact->GetEmail())
								{
									isContactExist = TRUE;
									break;																	
								}
								else if(pContact->GetFullName() != "" && pTemp->GetFullName() == pContact->GetFullName())
								{
									isContactExist = TRUE;
									break;
								}
								else if(pContact->GetMobilePhone() != "" && pTemp->GetMobilePhone() == pContact->GetMobilePhone())
								{
									isContactExist = TRUE;
									break;
								}
								else if(pContact->GetHomePhone() != "" && pTemp->GetHomePhone() == pContact->GetHomePhone())
								{
									isContactExist = TRUE;
									break;
								}
								else if(pContact->GetBusinessPhone() != "" && pTemp->GetBusinessPhone() == pContact->GetBusinessPhone())
								{
									isContactExist = TRUE;
									break;
								}
							}

							if(isContactExist)
							{
								continue;
							}
						}														
						contactArray.Add(pContact);							
						contactCount++;							
					}
				}
				if(doLog)
					m_logHelper.LogPAB(L"Open Sub Contacts Folder");
				GetContacts(pSubFolder,folderCount,contactCount,skipCount,contactIndex,contactArray,doLog);
			}
			if(pageSize > 0)
			{
				if(contactCount == pageSize)
					break;								
			}
			if(m_pCtrl->IsPABSearchAbort())
				break;
			__recursionCount = 0;
		}
	}	
	if(doLog)
	{
		m_logHelper.LogPAB(L"------------------------------------");
		m_logHelper.LogPAB(L"Function GetPABCotacts END:");
		m_logHelper.LogPAB(L"------------------------------------");	
		m_logHelper.ClosePABLog();
	}
	__recursionCount = 0;
	RELEASE(lpMsgStoreTable);		

	if(folderCount == 0 && !m_pCtrl->IsPABSearchAbort())
	{
		error.code = 107;
		error.err = "TRContactFinder Cannot find private folders.";
		return FALSE;
	}

	return TRUE;
}
Esempio n. 3
0
/*
 * \details Initialise root folder
 *
 * A context is linked to somewhat is a Folder, depending on information store,
 * we will use tdb, Ldap, MySql, or wathever needed regarding to what we have to do
 * Each functionnal system is separated in EasyLinux_<type>.c
 *
 */
int InitialiseRootFolder(struct EasyLinuxContext *elContext, TALLOC_CTX *mem_ctx, char *User, struct ldb_context *ldb,const char *uri)
{
const char *expression = "(MAPIStoreURI=EasyLinux://%s)";
char *Search;
const char * const Attribs[] = {"MAPIStoreURI","PidTagDisplayName","cn",NULL};
struct ldb_result *resultMsg;
struct ldb_message_element *MessageElement;
int i,j;

DEBUG(0,("MAPIEasyLinux :   InitialiseRootFolder\n")); 

// We need to find link between URI and FID (64bits)
Search = talloc_asprintf(mem_ctx,expression,uri);
if( LDB_SUCCESS != ldb_search(ldb, mem_ctx, &resultMsg, NULL, LDB_SCOPE_DEFAULT, Attribs, "%s", Search) )
  {
  DEBUG(0, ("ERROR - MAPIEasyLinux : cannot link FID and MAPISToreName - ldb_search\n"));
  return MAPISTORE_ERROR;
  }
if( resultMsg->count == 0 )
  {
  DEBUG(0, ("ERROR - MAPIEasyLinux : cannot link FID and MAPISToreName - No records found!\n"));
  return MAPISTORE_ERROR;
  }

// Extract from record information
for (i = 0; i < resultMsg->count; ++i) 
  {
  for( j=0 ; j<resultMsg->msgs[i]->num_elements ; j++)
    {
    MessageElement = &resultMsg->msgs[i]->elements[j];
    if( strcmp(MessageElement->name,"MAPIStoreURI") == 0 )  // MAPIStoreURI: EasyLinux://INBOX/
      elContext->RootFolder.Uri = talloc_strdup(mem_ctx, (char *)MessageElement->values[0].data);
    if( strcmp(MessageElement->name,"PidTagDisplayName") == 0 )  // PidTagDisplayName: INBOX
      elContext->RootFolder.displayName = talloc_strdup(mem_ctx, (char *)MessageElement->values[0].data);
    if( strcmp(MessageElement->name,"cn") == 0 )  // cn: 1946117988977475585
      elContext->RootFolder.FID = strtoull( (char *)MessageElement->values[0].data, NULL, 10);
    }
  }

DEBUG(0,("MAPIEasyLinux :   URI: %s  DisplayName: %s  cn: %lX \n",elContext->RootFolder.Uri, elContext->RootFolder.displayName, elContext->RootFolder.FID));

// Do whatever we need 
switch( GetBkType(&elContext->RootFolder.Uri[12]) )  // Uri is EasyLinux://INBOX/...
  {
  case EASYLINUX_FALLBACK:
    OpenFallBack(elContext);  // Folder represent rootfolder for libmapistore -> correspond to a xml file under FALLBACK
    break;

  case EASYLINUX_MAILDIR:
    OpenRootMailDir(elContext);
    break;
    
  case EASYLINUX_CALENDAR:  
    OpenCalendar(elContext);
    break;
    
  case EASYLINUX_CONTACTS:
    OpenContacts(elContext);
    break;
    
  case EASYLINUX_TASKS:
    OpenTasks(elContext);
    break;
    
  case EASYLINUX_NOTES:
    OpenNotes(elContext);
    break;
    
  case EASYLINUX_JOURNAL:  
    OpenJournal(elContext);
    break;

  default:
    DEBUG(0, ("MAPIEasyLinux : ERROR '%s' \n", &elContext->RootFolder.Uri[i]));  
    break;
  }
  
elContext->RootFolder.stType = EASYLINUX_FOLDER;  
elContext->RootFolder.Parent = NULL;
elContext->RootFolder.elContext = elContext;
  
//Dump((void *)&elContext->RootFolder);
talloc_unlink(mem_ctx, Search);
return MAPISTORE_SUCCESS;
}