mailnews/news/src/nsNNTPArticleList.cpp
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Sat, 06 Mar 2021 08:36:30 +0000
changeset 41384 25f9f04bba9ef5d22527cae290e416b16dc47bc9
parent 40987 0be504cabfce3932adfaceb1cec92aaeb7d29386
permissions -rw-r--r--
no bug - Bumping Thunderbird l10n changesets r=release a=l10n-bump DONTBUILD cak -> f37fbac8ae72d3ecd819d6224a519b08ff41e4ca cy -> 7c9ee1d240d733756ac1d57db603bb7ce1b31ac8 de -> 5c2e50907f9b986e263c6c3ee3cdcc9d61b03b52 dsb -> 9c9c687d8792d2222f2a685e5aae2c7bd6d8cea7 fi -> 9586e6d5a4d441ac0c7df3446bb00ac147aaac47 hsb -> 26cf86464cae9f6bf8d0ea3ba8f97232014c87c9 it -> e80120436229ce8febc8ef5801bea5311a87d941 ka -> 2c4e76a22d27eecfb86f5132141f14ecab10b317 kab -> feea57862cb99cc45be3a9b5fd7f3a372ddc4d87 ko -> eefb1803c26e89273f4f0a3ce3079bc65d362ed8 nl -> 10ca045e001682fb7cf69c16beac98051209c893 pa-IN -> 290f751e36b955ff0820412c880d4995f3d36a5e pt-BR -> 07f029f2869ba00381a54a402eed642e49b254d5 uk -> f88cf7af88a9d1962f11f073c406486cbb3bfe38 zh-CN -> 59df069c5736df81677f3988278f754b178eafd3

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "msgCore.h"  // precompiled header...

#include "nsCOMPtr.h"
#include "nsNNTPArticleList.h"
#include "nsIMsgFolder.h"

NS_IMPL_ISUPPORTS(nsNNTPArticleList, nsINNTPArticleList)

nsNNTPArticleList::nsNNTPArticleList() {}

nsNNTPArticleList::~nsNNTPArticleList() {
  if (m_newsDB) {
    m_newsDB->Commit(nsMsgDBCommitType::kSessionCommit);
    m_newsDB->Close(true);
    m_newsDB = nullptr;
  }

  m_newsFolder = nullptr;
}

NS_IMETHODIMP
nsNNTPArticleList::Initialize(nsIMsgNewsFolder* newsFolder) {
  nsresult rv;
  NS_ENSURE_ARG_POINTER(newsFolder);

  m_dbIndex = 0;

  m_newsFolder = newsFolder;

  nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(m_newsFolder, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = folder->GetMsgDatabase(getter_AddRefs(m_newsDB));
  NS_ENSURE_SUCCESS(rv, rv);
  if (!m_newsDB) return NS_ERROR_UNEXPECTED;

  nsTArray<nsMsgKey> keys;
  rv = m_newsDB->ListAllKeys(keys);
  NS_ENSURE_SUCCESS(rv, rv);
  keys.Sort();
  m_idsInDB.AppendElements(keys);

  return NS_OK;
}

NS_IMETHODIMP
nsNNTPArticleList::AddArticleKey(nsMsgKey key) {
#ifdef DEBUG
  m_idsOnServer.AppendElement(key);
#endif

  if (m_dbIndex < m_idsInDB.Length()) {
    nsMsgKey idInDBToCheck = m_idsInDB[m_dbIndex];
    // if there are keys in the database that aren't in the newsgroup
    // on the server, remove them. We probably shouldn't do this if
    // we have a copy of the article offline.
    // We'll add to m_idsDeleted for now and remove the id later
    while (idInDBToCheck < key) {
      m_idsDeleted.AppendElement(idInDBToCheck);
      if (m_dbIndex >= m_idsInDB.Length()) break;
      idInDBToCheck = m_idsInDB[++m_dbIndex];
    }
    if (idInDBToCheck == key) m_dbIndex++;
  }
  return NS_OK;
}

NS_IMETHODIMP
nsNNTPArticleList::FinishAddingArticleKeys() {
  // if the last n messages in the group are cancelled, they won't have gotten
  // removed so we have to go and remove them now.
  if (m_dbIndex < m_idsInDB.Length())
    m_idsDeleted.AppendElements(&m_idsInDB[m_dbIndex],
                                m_idsInDB.Length() - m_dbIndex);

  if (m_idsDeleted.Length()) m_newsFolder->RemoveMessages(m_idsDeleted);

#ifdef DEBUG
  // make sure none of the deleted turned up on the idsOnServer list
  for (uint32_t i = 0; i < m_idsDeleted.Length(); i++) {
    NS_ASSERTION(!m_idsOnServer.Contains((nsMsgKey)m_idsDeleted[i]),
                 "a deleted turned up on the idsOnServer list");
  }
#endif
  return NS_OK;
}