void nsImportGenericAddressBooks::GetDefaultFieldMap(void) { if (!m_pInterface || !m_pLocation) return; NS_IF_RELEASE(m_pFieldMap); nsresult rv; nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Unable to get nsIImportService.\n"); return; } rv = impSvc->CreateNewFieldMap(&m_pFieldMap); if (NS_FAILED(rv)) return; int32_t sz = 0; rv = m_pFieldMap->GetNumMozFields(&sz); if (NS_SUCCEEDED(rv)) rv = m_pFieldMap->DefaultFieldMap(sz); if (NS_SUCCEEDED(rv)) rv = m_pInterface->InitFieldMap(m_pFieldMap); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Error: Unable to initialize field map\n"); NS_IF_RELEASE(m_pFieldMap); } }
NS_IMETHODIMP nsImportGenericAddressBooks::GetProgress(int32_t *_retval) { // This returns the progress from the the currently // running import mail or import address book thread. NS_PRECONDITION(_retval != nullptr, "null ptr"); if (!_retval) return NS_ERROR_NULL_POINTER; if (!m_pThreadData || !(m_pThreadData->threadAlive)) { *_retval = 100; return NS_OK; } uint32_t sz = 0; if (m_pThreadData->currentSize && m_pInterface) { if (NS_FAILED(m_pInterface->GetImportProgress(&sz))) sz = 0; } if (m_totalSize) *_retval = ((m_pThreadData->currentTotal + sz) * 100) / m_totalSize; else *_retval = 0; // never return less than 5 so it looks like we are doing something! if (*_retval < 5) *_retval = 5; // as long as the thread is alive don't return completely // done. if (*_retval > 99) *_retval = 99; return NS_OK; }
NS_IMETHODIMP nsImportGenericAddressBooks::SetData( const char *dataId, nsISupports *item) { NS_PRECONDITION(dataId != nsnull, "null ptr"); if (!dataId) return NS_ERROR_NULL_POINTER; if (!PL_strcasecmp( dataId, "addressInterface")) { NS_IF_RELEASE( m_pInterface); if (item) item->QueryInterface( NS_GET_IID(nsIImportAddressBooks), (void **) &m_pInterface); } if (!PL_strcasecmp( dataId, "addressBooks")) { NS_IF_RELEASE( m_pBooks); if (item) item->QueryInterface( NS_GET_IID(nsISupportsArray), (void **) &m_pBooks); } if (!PL_strcasecmp( dataId, "addressLocation")) { m_pLocation = nsnull; if (item) { nsresult rv; m_pLocation = do_QueryInterface(item, &rv); NS_ENSURE_SUCCESS(rv,rv); } if (m_pInterface) m_pInterface->SetSampleLocation(m_pLocation); } if (!PL_strcasecmp( dataId, "addressDestination")) { if (item) { nsCOMPtr<nsISupportsCString> abString; item->QueryInterface( NS_GET_IID(nsISupportsCString), getter_AddRefs( abString)); if (abString) { if (m_pDestinationUri) NS_Free( m_pDestinationUri); m_pDestinationUri = nsnull; nsCAutoString tempUri; abString->GetData(tempUri); m_pDestinationUri = ToNewCString(tempUri); } } } if (!PL_strcasecmp( dataId, "fieldMap")) { NS_IF_RELEASE( m_pFieldMap); if (item) item->QueryInterface( NS_GET_IID(nsIImportFieldMap), (void **) &m_pFieldMap); } return( NS_OK); }
NS_IMETHODIMP nsImportGenericAddressBooks::GetStatus(const char *statusKind, int32_t *_retval) { NS_PRECONDITION(statusKind != nullptr, "null ptr"); NS_PRECONDITION(_retval != nullptr, "null ptr"); if (!statusKind || !_retval) return NS_ERROR_NULL_POINTER; *_retval = 0; if (!PL_strcasecmp(statusKind, "isInstalled")) { GetDefaultLocation(); *_retval = (int32_t) m_found; } if (!PL_strcasecmp(statusKind, "canUserSetLocation")) { GetDefaultLocation(); *_retval = (int32_t) m_userVerify; } if (!PL_strcasecmp(statusKind, "autoFind")) { GetDefaultLocation(); *_retval = (int32_t) m_autoFind; } if (!PL_strcasecmp(statusKind, "supportsMultiple")) { bool multi = false; if (m_pInterface) m_pInterface->GetSupportsMultiple(&multi); *_retval = (int32_t) multi; } if (!PL_strcasecmp(statusKind, "needsFieldMap")) { bool needs = false; if (m_pInterface && m_pLocation) m_pInterface->GetNeedsFieldMap(m_pLocation, &needs); *_retval = (int32_t) needs; } return NS_OK; }
void nsImportGenericAddressBooks::GetDefaultBooks(void) { if (!m_pInterface || m_Books) return; if (!m_pLocation && !m_autoFind) return; nsresult rv = m_pInterface->FindAddressBooks(m_pLocation, getter_AddRefs(m_Books)); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Error: FindAddressBooks failed\n"); } }
void nsImportGenericAddressBooks::GetDefaultLocation(void) { if (!m_pInterface) return; if ((m_pLocation && m_gotLocation) || m_autoFind) return; if (m_description) NS_Free(m_description); m_description = nullptr; m_pInterface->GetAutoFind(&m_description, &m_autoFind); m_gotLocation = true; if (m_autoFind) { m_found = true; m_userVerify = false; return; } nsCOMPtr <nsIFile> pLoc; m_pInterface->GetDefaultLocation(getter_AddRefs(pLoc), &m_found, &m_userVerify); if (!m_pLocation) m_pLocation = pLoc; }
NS_IMETHODIMP nsImportGenericAddressBooks::BeginImport(nsISupportsString *successLog, nsISupportsString *errorLog, bool *_retval) { NS_PRECONDITION(_retval != nullptr, "null ptr"); if (!_retval) return NS_ERROR_NULL_POINTER; nsString success; nsString error; if (!m_doImport) { *_retval = true; nsImportStringBundle::GetStringByID(IMPORT_NO_ADDRBOOKS, m_stringBundle, success); SetLogs(success, error, successLog, errorLog); return NS_OK; } if (!m_pInterface || !m_Books) { nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED, m_stringBundle, error); SetLogs(success, error, successLog, errorLog); *_retval = false; return NS_OK; } bool needsFieldMap = false; if (NS_FAILED(m_pInterface->GetNeedsFieldMap(m_pLocation, &needsFieldMap)) || (needsFieldMap && !m_pFieldMap)) { nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED, m_stringBundle, error); SetLogs(success, error, successLog, errorLog); *_retval = false; return NS_OK; } NS_IF_RELEASE(m_pSuccessLog); NS_IF_RELEASE(m_pErrorLog); m_pSuccessLog = successLog; m_pErrorLog = errorLog; NS_IF_ADDREF(m_pSuccessLog); NS_IF_ADDREF(m_pErrorLog); // create the info need to drive address book import. We're // not going to create a new thread for this since address books // don't tend to be large, and import is rare. m_pThreadData = new AddressThreadData(); m_pThreadData->books = m_Books; NS_ADDREF(m_Books); m_pThreadData->addressImport = m_pInterface; NS_ADDREF(m_pInterface); m_pThreadData->fieldMap = m_pFieldMap; NS_IF_ADDREF(m_pFieldMap); m_pThreadData->errorLog = m_pErrorLog; NS_IF_ADDREF(m_pErrorLog); m_pThreadData->successLog = m_pSuccessLog; NS_IF_ADDREF(m_pSuccessLog); if (m_pDestinationUri) m_pThreadData->pDestinationUri = strdup(m_pDestinationUri); uint32_t count = 0; m_Books->GetLength(&count); // Create/obtain any address books that we need here, so that we don't need // to do so inside the import thread which would just proxy the create // operations back to the main thread anyway. nsCOMPtr<nsIAddrDatabase> db = GetAddressBookFromUri(m_pDestinationUri); for (uint32_t i = 0; i < count; ++i) { nsCOMPtr<nsIImportABDescriptor> book = do_QueryElementAt(m_Books, i); if (book) { if (!db) { nsString name; book->GetPreferredName(name); db = GetAddressBook(name.get(), true); } m_DBs.AppendObject(db); } } m_pThreadData->dBs = &m_DBs; NS_IF_ADDREF(m_pThreadData->stringBundle = m_stringBundle); nsresult rv; m_pThreadData->ldifService = do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv); ImportAddressThread(m_pThreadData); delete m_pThreadData; m_pThreadData = nullptr; *_retval = true; return NS_OK; }
NS_IMETHODIMP nsImportGenericAddressBooks::GetData(const char *dataId, nsISupports **_retval) { NS_PRECONDITION(_retval != nullptr, "null ptr"); if (!_retval) return NS_ERROR_NULL_POINTER; nsresult rv; *_retval = nullptr; if (!PL_strcasecmp(dataId, "addressInterface")) { *_retval = m_pInterface; NS_IF_ADDREF(m_pInterface); } if (!PL_strcasecmp(dataId, "addressLocation")) { if (!m_pLocation) GetDefaultLocation(); NS_IF_ADDREF(*_retval = m_pLocation); } if (!PL_strcasecmp(dataId, "addressBooks")) { if (!m_pLocation) GetDefaultLocation(); if (!m_Books) GetDefaultBooks(); *_retval = m_Books; } if (!PL_strcasecmp(dataId, "addressDestination")) { if (m_pDestinationUri) { nsCOMPtr<nsISupportsCString> abString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); abString->SetData(nsDependentCString(m_pDestinationUri)); NS_IF_ADDREF(*_retval = abString); } } if (!PL_strcasecmp(dataId, "fieldMap")) { if (m_pFieldMap) { *_retval = m_pFieldMap; m_pFieldMap->AddRef(); } else { if (m_pInterface && m_pLocation) { bool needsIt = false; m_pInterface->GetNeedsFieldMap(m_pLocation, &needsIt); if (needsIt) { GetDefaultFieldMap(); if (m_pFieldMap) { *_retval = m_pFieldMap; m_pFieldMap->AddRef(); } } } } } if (!PL_strncasecmp(dataId, "sampleData-", 11)) { // extra the record number const char *pNum = dataId + 11; int32_t rNum = 0; while (*pNum) { rNum *= 10; rNum += (*pNum - '0'); pNum++; } IMPORT_LOG1("Requesting sample data #: %ld\n", (long)rNum); if (m_pInterface) { nsCOMPtr<nsISupportsString> data = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; char16_t * pData = nullptr; bool found = false; rv = m_pInterface->GetSampleData(rNum, &found, &pData); if (NS_FAILED(rv)) return rv; if (found) { data->SetData(nsDependentString(pData)); *_retval = data; NS_ADDREF(*_retval); } NS_Free(pData); } } return NS_OK; }
NS_IMETHODIMP nsImportGenericAddressBooks::BeginImport(nsISupportsString *successLog, nsISupportsString *errorLog, PRBool isAddrLocHome, PRBool *_retval) { NS_PRECONDITION(_retval != nsnull, "null ptr"); if (!_retval) return NS_ERROR_NULL_POINTER; nsString success; nsString error; if (!m_doImport) { *_retval = PR_TRUE; nsImportStringBundle::GetStringByID(IMPORT_NO_ADDRBOOKS, m_stringBundle, success); SetLogs( success, error, successLog, errorLog); return( NS_OK); } if (!m_pInterface || !m_pBooks) { nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED, m_stringBundle, error); SetLogs( success, error, successLog, errorLog); *_retval = PR_FALSE; return( NS_OK); } PRBool needsFieldMap = PR_FALSE; if (NS_FAILED(m_pInterface->GetNeedsFieldMap(m_pLocation, &needsFieldMap)) || (needsFieldMap && !m_pFieldMap)) { nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED, m_stringBundle, error); SetLogs(success, error, successLog, errorLog); *_retval = PR_FALSE; return NS_OK; } if (m_pThreadData) { m_pThreadData->DriverAbort(); m_pThreadData = nsnull; } NS_IF_RELEASE( m_pSuccessLog); NS_IF_RELEASE( m_pErrorLog); m_pSuccessLog = successLog; m_pErrorLog = errorLog; NS_IF_ADDREF( m_pSuccessLog); NS_IF_ADDREF( m_pErrorLog); // kick off the thread to do the import!!!! m_pThreadData = new AddressThreadData(); m_pThreadData->books = m_pBooks; NS_ADDREF( m_pBooks); m_pThreadData->addressImport = m_pInterface; NS_ADDREF( m_pInterface); m_pThreadData->fieldMap = m_pFieldMap; NS_IF_ADDREF( m_pFieldMap); m_pThreadData->errorLog = m_pErrorLog; NS_IF_ADDREF( m_pErrorLog); m_pThreadData->successLog = m_pSuccessLog; NS_IF_ADDREF( m_pSuccessLog); if (m_pDestinationUri) m_pThreadData->pDestinationUri = strdup( m_pDestinationUri); m_pThreadData->bAddrLocInput = isAddrLocHome ; NS_IF_ADDREF(m_pThreadData->stringBundle = m_stringBundle); nsresult rv; nsCOMPtr<nsIAbLDIFService> ldifService(do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIProxyObjectManager> proxyObjectManager = do_GetService(NS_XPCOMPROXY_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIAbLDIFService> proxyLDIFService; rv = proxyObjectManager->GetProxyForObject(NS_PROXY_TO_CURRENT_THREAD, NS_GET_IID(nsIAbLDIFService), ldifService, NS_PROXY_SYNC | NS_PROXY_ALWAYS, getter_AddRefs(proxyLDIFService)); NS_ENSURE_SUCCESS(rv, rv); NS_IF_ADDREF(m_pThreadData->ldifService = proxyLDIFService); PRThread *pThread = PR_CreateThread( PR_USER_THREAD, &ImportAddressThread, m_pThreadData, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (!pThread) { m_pThreadData->ThreadDelete(); m_pThreadData->DriverDelete(); m_pThreadData = nsnull; *_retval = PR_FALSE; nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTHREAD, m_stringBundle, error); SetLogs( success, error, successLog, errorLog); } else *_retval = PR_TRUE; return( NS_OK); }