nsresult nsMsgSendLater::Init() { nsresult rv; nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); bool sendInBackground; rv = prefs->GetBoolPref("mailnews.sendInBackground", &sendInBackground); // If we're not sending in the background, don't do anything else if (NS_FAILED(rv) || !sendInBackground) return NS_OK; // We need to know when we're shutting down. nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService(); NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED); rv = observerService->AddObserver(this, "xpcom-shutdown", false); NS_ENSURE_SUCCESS(rv, rv); rv = observerService->AddObserver(this, "quit-application", false); NS_ENSURE_SUCCESS(rv, rv); rv = observerService->AddObserver(this, "msg-shutdown", false); NS_ENSURE_SUCCESS(rv, rv); // Subscribe to the unsent messages folder // XXX This code should be set up for multiple unsent folders, however we // don't support that at the moment, so for now just assume one folder. rv = GetUnsentMessagesFolder(nullptr, getter_AddRefs(mMessageFolder)); NS_ENSURE_SUCCESS(rv, rv); rv = mMessageFolder->AddFolderListener(this); NS_ENSURE_SUCCESS(rv, rv); // XXX may want to send messages X seconds after startup if there are any. return NS_OK; }
NS_IMETHODIMP nsMsgSendLater::HasUnsentMessages(nsIMsgIdentity *aIdentity, bool *aResult) { NS_ENSURE_ARG_POINTER(aResult); nsresult rv; nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIArray> accounts; accountManager->GetAccounts(getter_AddRefs(accounts)); NS_ENSURE_SUCCESS(rv, rv); uint32_t cnt = 0; rv = accounts->GetLength(&cnt); if (cnt == 0) { *aResult = false; return NS_OK; // no account set up -> no unsent messages } // XXX This code should be set up for multiple unsent folders, however we // don't support that at the moment, so for now just assume one folder. if (!mMessageFolder) { rv = GetUnsentMessagesFolder(nullptr, getter_AddRefs(mMessageFolder)); NS_ENSURE_SUCCESS(rv, rv); } rv = ReparseDBIfNeeded(nullptr); NS_ENSURE_SUCCESS(rv, rv); int32_t totalMessages; rv = mMessageFolder->GetTotalMessages(false, &totalMessages); NS_ENSURE_SUCCESS(rv, rv); *aResult = totalMessages > 0; return NS_OK; }
nsresult nsMsgCopy::StartCopyOperation(nsIMsgIdentity *aUserIdentity, nsIFile *aFile, nsMsgDeliverMode aMode, nsIMsgSend *aMsgSendObj, const char *aSavePref, nsIMsgDBHdr *aMsgToReplace) { nsCOMPtr<nsIMsgFolder> dstFolder; bool isDraft = false; bool waitForUrl = false; nsresult rv; if (!aMsgSendObj) return NS_ERROR_INVALID_ARG; // Store away the server location... if (aSavePref) mSavePref = PL_strdup(aSavePref); // // Vars for implementation... // // QueueForLater (Outbox) if (aMode == nsIMsgSend::nsMsgQueueForLater || aMode == nsIMsgSend::nsMsgDeliverBackground) { rv = GetUnsentMessagesFolder(aUserIdentity, getter_AddRefs(dstFolder), &waitForUrl); isDraft = false; if (!dstFolder || NS_FAILED(rv)) { return NS_MSG_UNABLE_TO_SEND_LATER; } } else if (aMode == nsIMsgSend::nsMsgSaveAsDraft) // SaveAsDraft (Drafts) { rv = GetDraftsFolder(aUserIdentity, getter_AddRefs(dstFolder), &waitForUrl); isDraft = true; if (!dstFolder || NS_FAILED(rv)) return NS_MSG_UNABLE_TO_SAVE_DRAFT; } else if (aMode == nsIMsgSend::nsMsgSaveAsTemplate) // SaveAsTemplate (Templates) { rv = GetTemplatesFolder(aUserIdentity, getter_AddRefs(dstFolder), &waitForUrl); isDraft = false; if (!dstFolder || NS_FAILED(rv)) return NS_MSG_UNABLE_TO_SAVE_TEMPLATE; } else // SaveInSentFolder (Sent) - nsMsgDeliverNow or nsMsgSendUnsent { rv = GetSentFolder(aUserIdentity, getter_AddRefs(dstFolder), &waitForUrl); isDraft = false; if (!dstFolder || NS_FAILED(rv)) return NS_MSG_COULDNT_OPEN_FCC_FOLDER; } nsCOMPtr <nsIMsgWindow> msgWindow; if (aMsgSendObj) { nsCOMPtr <nsIMsgProgress> progress; aMsgSendObj->GetProgress(getter_AddRefs(progress)); if (progress) progress->GetMsgWindow(getter_AddRefs(msgWindow)); } mMode = aMode; mFile = aFile; mDstFolder = dstFolder; mMsgToReplace = aMsgToReplace; mIsDraft = isDraft; mMsgSendObj = aMsgSendObj; if (!waitForUrl) { // cache info needed for DoCopy and call DoCopy when OnStopUrl is called. rv = DoCopy(aFile, dstFolder, aMsgToReplace, isDraft, msgWindow, aMsgSendObj); // N.B. "this" may be deleted when this call returns. } return rv; }
nsresult nsMsgSendLater::InternalSendMessages(bool aUserInitiated, nsIMsgIdentity *aIdentity) { if (WeAreOffline()) return NS_MSG_ERROR_OFFLINE; // Protect against being called whilst we're already sending. if (mSendingMessages) { NS_ERROR("nsMsgSendLater is already sending messages\n"); return NS_ERROR_FAILURE; } nsresult rv; // XXX This code should be set up for multiple unsent folders, however we // don't support that at the moment, so for now just assume one folder. if (!mMessageFolder) { rv = GetUnsentMessagesFolder(nullptr, getter_AddRefs(mMessageFolder)); NS_ENSURE_SUCCESS(rv, rv); } nsCOMPtr<nsIMsgDatabase> unsentDB; // Remember these in case we need to reparse the db. mUserInitiated = aUserInitiated; mIdentity = aIdentity; rv = ReparseDBIfNeeded(this); NS_ENSURE_SUCCESS(rv, rv); mIdentity = nullptr; // don't hold onto the identity since we're a service. nsCOMPtr<nsISimpleEnumerator> enumerator; rv = mMessageFolder->GetMessages(getter_AddRefs(enumerator)); NS_ENSURE_SUCCESS(rv, rv); // copy all the elements in the enumerator into our isupports array.... nsCOMPtr<nsISupports> currentItem; nsCOMPtr<nsIMsgDBHdr> messageHeader; bool hasMoreElements = false; while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreElements)) && hasMoreElements) { rv = enumerator->GetNext(getter_AddRefs(currentItem)); if (NS_SUCCEEDED(rv)) { messageHeader = do_QueryInterface(currentItem, &rv); if (NS_SUCCEEDED(rv)) { if (aUserInitiated) // If the user initiated the send, add all messages mMessagesToSend.AppendObject(messageHeader); else { // Else just send those that are NOT marked as Queued. uint32_t flags; rv = messageHeader->GetFlags(&flags); if (NS_SUCCEEDED(rv) && !(flags & nsMsgMessageFlags::Queued)) mMessagesToSend.AppendObject(messageHeader); } } } } // Now get an enumerator for our array. rv = NS_NewArrayEnumerator(getter_AddRefs(mEnumerator), mMessagesToSend); NS_ENSURE_SUCCESS(rv, rv); // We're now sending messages so its time to signal that and reset our counts. mSendingMessages = true; mTotalSentSuccessfully = 0; mTotalSendCount = 0; // Notify the listeners that we are starting a send. NotifyListenersOnStartSending(mMessagesToSend.Count()); return StartNextMailFileSend(NS_OK); }