NS_IMETHODIMP RenameFolderTask::Run() { int ret = EWS_FAIL; nsresult rv; if (EWS_FAIL == NotifyBegin(this)) { return NS_OK; } ews_session * session = NULL; nsCOMPtr<IMailEwsMsgIncomingServer> ewsServer(do_QueryInterface(m_pIncomingServer, &rv)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<IMailEwsService> ewsService; rv = ewsServer->GetService(getter_AddRefs(ewsService)); NS_ENSURE_SUCCESS(rv, rv); nsresult rv1 = ewsService->GetNewSession(&session); if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(rv1) && session) { nsCOMPtr<IMailEwsMsgFolder> curFolder(do_QueryInterface(m_Folder)); nsCString savedFolderId; rv = curFolder->GetFolderId(savedFolderId); if (NS_FAILED(rv)) { NotifyError(this, EWS_FAIL, "unable to get folder id"); return NS_OK; } nsCString change_key; rv = curFolder->GetChangeKey(change_key); if (NS_FAILED(rv)) { NotifyError(this, EWS_FAIL, "unable to get folder change key"); return NS_OK; } ews_folder * folder = (ews_folder *)malloc(sizeof(char) * sizeof(ews_folder)); memset(folder, 0, sizeof(ews_folder)); char * display_name = ToNewUTF8String(m_NewName); folder->display_name = strdup(display_name); folder->id = strdup(savedFolderId.get()); folder->change_key = strdup(change_key.get()); NS_Free(display_name); char * err_msg = NULL; ret = ews_update_folder(session, folder, &err_msg); if (ret != EWS_SUCCESS) { mailews_logger << "rename folder on server fail:" << ret << "," << err_msg << std::endl; NotifyError(this, ret, err_msg); } if (err_msg) free(err_msg); ews_free_folder(folder); } else { NotifyError(this, session ? EWS_FAIL : 401, NULL); } ewsService->ReleaseSession(session); nsCOMPtr<nsIRunnable> resultrunnable = new RenameFolderDoneTask(ret, m_Folder, m_NewName, m_MsgWindow, m_pEwsTaskCallbacks, this); NS_DispatchToMainThread(resultrunnable); return NS_OK; }
static void ImportMailThread(void *stuff) { ImportThreadData *pData = (ImportThreadData *)stuff; IMPORT_LOG0("ImportMailThread: Starting..."); nsresult rv = NS_OK; nsCOMPtr<nsIMsgFolder> destRoot(pData->destRoot); uint32_t count = 0; rv = pData->boxes->Count(&count); uint32_t i; bool import; uint32_t size; uint32_t depth = 1; uint32_t newDepth; nsString lastName; PRUnichar * pName; nsCOMPtr<nsIMsgFolder> curFolder(destRoot); nsCOMPtr<nsIMsgFolder> newFolder; nsCOMPtr<nsIMsgFolder> subFolder; nsCOMPtr<nsISimpleEnumerator> enumerator; bool exists; nsString success; nsString error; // GetSubFolders() will initialize folders if they are not already initialized. ProxyGetSubFolders(curFolder, getter_AddRefs(enumerator)); IMPORT_LOG1("ImportMailThread: Total number of folders to import = %d.", count); // Note that the front-end js script only displays one import result string so // we combine both good and bad import status into one string (in var 'success'). for (i = 0; (i < count) && !(pData->abort); i++) { nsCOMPtr<nsIImportMailboxDescriptor> box = do_QueryElementAt(pData->boxes, i); if (box) { pData->currentMailbox = i; import = false; size = 0; rv = box->GetImport(&import); if (import) rv = box->GetSize(&size); rv = box->GetDepth(&newDepth); if (newDepth > depth) { // OK, we are going to add a subfolder under the last/previous folder we processed, so // find this folder (stored in 'lastName') who is going to be the new parent folder. IMPORT_LOG1("ImportMailThread: Processing child folder '%s'.", NS_ConvertUTF16toUTF8(lastName).get()); rv = ProxyGetChildNamed(curFolder, lastName, getter_AddRefs(subFolder)); if (NS_FAILED(rv)) { IMPORT_LOG1("*** ImportMailThread: Failed to get the interface for child folder '%s'.", NS_ConvertUTF16toUTF8(lastName).get()); nsImportGenericMail::ReportError(IMPORT_ERROR_MB_FINDCHILD, lastName.get(), &error, pData->stringBundle); pData->fatalError = true; break; } curFolder = subFolder; // Make sure this new parent folder obj has the correct subfolder list so far. rv = ProxyGetSubFolders(curFolder, getter_AddRefs(enumerator)); } else if (newDepth < depth) { rv = NS_OK; while ((newDepth < depth) && NS_SUCCEEDED(rv)) { rv = curFolder->GetParent(getter_AddRefs(curFolder)); if (NS_FAILED(rv)) { IMPORT_LOG1("*** ImportMailThread: Failed to get the interface for parent folder '%s'.", lastName.get()); nsImportGenericMail::ReportError(IMPORT_ERROR_MB_FINDCHILD, lastName.get(), &error, pData->stringBundle); pData->fatalError = true; break; } depth--; } if (NS_FAILED(rv)) { IMPORT_LOG1("*** ImportMailThread: Failed to get the proxy interface for parent folder '%s'.", lastName.get()); nsImportStringBundle::GetStringByID(IMPORT_ERROR_MB_NOPROXY, pData->stringBundle, error); pData->fatalError = true; break; } } depth = newDepth; pName = nullptr; box->GetDisplayName(&pName); if (pName) { lastName = pName; NS_Free(pName); } else lastName.AssignLiteral("Unknown!"); // translate the folder name if we are doing migration, but // only for special folders which are at the root level if (pData->performingMigration && depth == 1) pData->mailImport->TranslateFolderName(lastName, lastName); exists = false; rv = ProxyContainsChildNamed(curFolder, lastName, &exists); // If we are performing profile migration (as opposed to importing) then we are starting // with empty local folders. In that case, always choose to over-write the existing local folder // with this name. Don't create a unique subfolder name. Otherwise you end up with "Inbox, Inbox0" // or "Unsent Folders, UnsentFolders0" if (exists && !pData->performingMigration) { nsString subName; ProxyGenerateUniqueSubfolderName(curFolder, lastName, nullptr, subName); if (!subName.IsEmpty()) lastName.Assign(subName); } IMPORT_LOG1("ImportMailThread: Creating new import folder '%s'.", NS_ConvertUTF16toUTF8(lastName).get()); ProxyCreateSubfolder(curFolder, lastName); // this may fail if the folder already exists..that's ok rv = ProxyGetChildNamed(curFolder, lastName, getter_AddRefs(newFolder)); if (NS_FAILED(rv)) { IMPORT_LOG1("*** ImportMailThread: Failed to locate subfolder '%s' after it's been created.", lastName.get()); nsImportGenericMail::ReportError(IMPORT_ERROR_MB_CREATE, lastName.get(), &error, pData->stringBundle); } if (size && import && newFolder && NS_SUCCEEDED(rv)) { bool fatalError = false; pData->currentSize = size; PRUnichar *pSuccess = nullptr; PRUnichar *pError = nullptr; rv = pData->mailImport->ImportMailbox(box, newFolder, &pError, &pSuccess, &fatalError); if (pError) { error.Append(pError); NS_Free(pError); } if (pSuccess) { success.Append(pSuccess); NS_Free(pSuccess); } pData->currentSize = 0; pData->currentTotal += size; // commit to the db synchronously, but using a proxy since it doesn't like being used // elsewhere than from the main thread. // OK, we've copied the actual folder/file over if the folder size is not 0 // (ie, the msg summary is no longer valid) so close the msg database so that // when the folder is reopened the folder db can be reconstructed (which // validates msg summary and forces folder to be reparsed). rv = ProxyForceDBClosed(newFolder); fatalError = NS_FAILED(rv); if (fatalError) { IMPORT_LOG1("*** ImportMailThread: ImportMailbox returned fatalError, mailbox #%d\n", (int) i); pData->fatalError = true; break; } } } } // Now save the new acct info to pref file. nsCOMPtr <nsIMsgAccountManager> accMgr = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); if (NS_SUCCEEDED(rv) && accMgr) { rv = accMgr->SaveAccountInfo(); NS_ASSERTION(NS_SUCCEEDED(rv), "Can't save account info to pref file"); } nsImportGenericMail::SetLogs(success, error, pData->successLog, pData->errorLog); if (pData->abort || pData->fatalError) { IMPORT_LOG0("*** ImportMailThread: Abort or fatalError flag was set\n"); if (pData->ownsDestRoot) { IMPORT_LOG0("Calling destRoot->RecursiveDelete\n"); destRoot->RecursiveDelete(true, nullptr); } else { // FIXME: just delete the stuff we created? } } IMPORT_LOG1("Import mailbox thread done: %d\n", (int) pData->currentTotal); pData->ThreadDelete(); }