summaryrefslogtreecommitdiffstats
path: root/mailnews/db/msgdb/src/nsNewsDatabase.cpp
blob: 3fb3dbb2baaed6ff1805a85d48ad16f884d26ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* -*- Mode: C++; tab-width: 4; 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"
#include "nsIMsgDBView.h"
#include "nsIMsgThread.h"
#include "nsNewsDatabase.h"
#include "nsMsgKeySet.h"
#include "nsCOMPtr.h"
#include "prlog.h"

#if defined(DEBUG_sspitzer_) || defined(DEBUG_seth_)
#define DEBUG_NEWS_DATABASE 1
#endif

nsNewsDatabase::nsNewsDatabase()
{
  m_readSet = nullptr;
}

nsNewsDatabase::~nsNewsDatabase()
{
}

NS_IMPL_ADDREF_INHERITED(nsNewsDatabase, nsMsgDatabase)
NS_IMPL_RELEASE_INHERITED(nsNewsDatabase, nsMsgDatabase)

NS_IMETHODIMP nsNewsDatabase::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
  if (!aInstancePtr) return NS_ERROR_NULL_POINTER;
  *aInstancePtr = nullptr;

  if (aIID.Equals(NS_GET_IID(nsINewsDatabase)))
  {
    *aInstancePtr = static_cast<nsINewsDatabase *>(this);
  }

  if(*aInstancePtr)
  {
    AddRef();
    return NS_OK;
  }

  return nsMsgDatabase::QueryInterface(aIID, aInstancePtr);
}

nsresult nsNewsDatabase::Close(bool forceCommit)
{
  return nsMsgDatabase::Close(forceCommit);
}

nsresult nsNewsDatabase::ForceClosed()
{
  return nsMsgDatabase::ForceClosed();
}

nsresult nsNewsDatabase::Commit(nsMsgDBCommit commitType)
{
  if (m_dbFolderInfo && m_readSet)
  {
    // let's write out our idea of the read set so we can compare it with that of
    // the .rc file next time we start up.
    nsCString readSet;
    m_readSet->Output(getter_Copies(readSet));
    m_dbFolderInfo->SetCharProperty("readSet", readSet);
  }
  return nsMsgDatabase::Commit(commitType);
}


uint32_t nsNewsDatabase::GetCurVersion()
{
  return kMsgDBVersion;
}

NS_IMETHODIMP nsNewsDatabase::IsRead(nsMsgKey key, bool *pRead)
{
  NS_ASSERTION(pRead, "null out param in IsRead");
  if (!pRead) return NS_ERROR_NULL_POINTER;

  if (!m_readSet) return NS_ERROR_FAILURE;

  *pRead = m_readSet->IsMember(key);
  return NS_OK;
}

nsresult nsNewsDatabase::IsHeaderRead(nsIMsgDBHdr *msgHdr, bool *pRead)
{
    nsresult rv;
    nsMsgKey messageKey;

    if (!msgHdr || !pRead) return NS_ERROR_NULL_POINTER;

    rv = msgHdr->GetMessageKey(&messageKey);
    if (NS_FAILED(rv)) return rv;

    rv = IsRead(messageKey,pRead);
    return rv;
}

// return highest article number we've seen.
NS_IMETHODIMP nsNewsDatabase::GetHighWaterArticleNum(nsMsgKey *key)
{
  NS_ASSERTION(m_dbFolderInfo, "null db folder info");
  if (!m_dbFolderInfo)
    return NS_ERROR_FAILURE;
  return m_dbFolderInfo->GetHighWater(key);
}

// return the key of the first article number we know about.
// Since the iterator iterates in id order, we can just grab the
// messagekey of the first header it returns.
// ### dmb
// This will not deal with the situation where we get holes in
// the headers we know about. Need to figure out how and when
// to solve that. This could happen if a transfer is interrupted.
// Do we need to keep track of known arts permanently?
NS_IMETHODIMP nsNewsDatabase::GetLowWaterArticleNum(nsMsgKey *key)
{
  nsresult  rv;
  nsMsgHdr  *pHeader;

  nsCOMPtr<nsISimpleEnumerator> hdrs;
  rv = EnumerateMessages(getter_AddRefs(hdrs));
  if (NS_FAILED(rv))
    return rv;

  rv = hdrs->GetNext((nsISupports**)&pHeader);
  NS_ASSERTION(NS_SUCCEEDED(rv), "nsMsgDBEnumerator broken");
  if (NS_FAILED(rv))
    return rv;

  return pHeader->GetMessageKey(key);
}

nsresult  nsNewsDatabase::ExpireUpTo(nsMsgKey expireKey)
{
  return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult		nsNewsDatabase::ExpireRange(nsMsgKey startRange, nsMsgKey endRange)
{
  return NS_ERROR_NOT_IMPLEMENTED;
}


NS_IMETHODIMP nsNewsDatabase::GetReadSet(nsMsgKeySet **pSet)
{
    if (!pSet) return NS_ERROR_NULL_POINTER;
    *pSet = m_readSet;
    return NS_OK;
}

NS_IMETHODIMP nsNewsDatabase::SetReadSet(nsMsgKeySet *pSet)
{
  m_readSet = pSet;

  if (m_readSet)
  {
    // compare this read set with the one in the db folder info.
    // If not equivalent, sync with this one.
    nsCString dbReadSet;
    if (m_dbFolderInfo)
      m_dbFolderInfo->GetCharProperty("readSet", dbReadSet);
    nsCString newsrcReadSet;
    m_readSet->Output(getter_Copies(newsrcReadSet));
    if (!dbReadSet.Equals(newsrcReadSet))
      SyncWithReadSet();
  }
  return NS_OK;
}


bool nsNewsDatabase::SetHdrReadFlag(nsIMsgDBHdr *msgHdr, bool bRead)
{
    nsresult rv;
    bool isRead;
    rv = IsHeaderRead(msgHdr, &isRead);

    if (isRead == bRead)
    {
      // give the base class a chance to update m_flags.
      nsMsgDatabase::SetHdrReadFlag(msgHdr, bRead);
      return false;
    }
    else {
      nsMsgKey messageKey;

      // give the base class a chance to update m_flags.
      nsMsgDatabase::SetHdrReadFlag(msgHdr, bRead);
      rv = msgHdr->GetMessageKey(&messageKey);
      if (NS_FAILED(rv)) return false;

      NS_ASSERTION(m_readSet, "m_readSet is null");
      if (!m_readSet) return false;

      if (!bRead) {
#ifdef DEBUG_NEWS_DATABASE
        printf("remove %d from the set\n",messageKey);
#endif

        m_readSet->Remove(messageKey);

        rv = NotifyReadChanged(nullptr);
        if (NS_FAILED(rv)) return false;
      }
      else {
#ifdef DEBUG_NEWS_DATABASE
        printf("add %d to the set\n",messageKey);
#endif

        if (m_readSet->Add(messageKey) < 0) return false;

        rv = NotifyReadChanged(nullptr);
        if (NS_FAILED(rv)) return false;
      }
    }
    return true;
}

NS_IMETHODIMP nsNewsDatabase::MarkAllRead(uint32_t *aNumMarked,
                                          nsMsgKey **aThoseMarked)
{
  nsMsgKey lowWater = nsMsgKey_None, highWater;
  nsCString knownArts;
  if (m_dbFolderInfo)
  {
    m_dbFolderInfo->GetKnownArtsSet(getter_Copies(knownArts));
    nsMsgKeySet *knownKeys = nsMsgKeySet::Create(knownArts.get());
    if (knownKeys)
      lowWater = knownKeys->GetFirstMember();

    delete knownKeys;
  }
  if (lowWater == nsMsgKey_None)
    GetLowWaterArticleNum(&lowWater);
  GetHighWaterArticleNum(&highWater);
  if (lowWater > 2)
    m_readSet->AddRange(1, lowWater - 1);
  nsresult err = nsMsgDatabase::MarkAllRead(aNumMarked, aThoseMarked);
  if (NS_SUCCEEDED(err) && 1 <= highWater)
    m_readSet->AddRange(1, highWater); // mark everything read in newsrc.

  return err;
}

nsresult nsNewsDatabase::SyncWithReadSet()
{

  // The code below attempts to update the underlying nsMsgDatabase's idea
  // of read/unread flags to match the read set in the .newsrc file. It should
  // only be called when they don't match, e.g., we crashed after committing the
  // db but before writing out the .newsrc
  nsCOMPtr <nsISimpleEnumerator> hdrs;
  nsresult rv = EnumerateMessages(getter_AddRefs(hdrs));
  NS_ENSURE_SUCCESS(rv, rv);

  bool hasMore = false, readInNewsrc, isReadInDB, changed = false;
  int32_t numMessages = 0, numUnreadMessages = 0;
  nsMsgKey messageKey;
  nsCOMPtr <nsIMsgThread> threadHdr;

  // Scan all messages in DB
  while (NS_SUCCEEDED(rv = hdrs->HasMoreElements(&hasMore)) && hasMore)
  {
      nsCOMPtr<nsISupports> supports;
      rv = hdrs->GetNext(getter_AddRefs(supports));
      NS_ENSURE_SUCCESS(rv, rv);

      nsCOMPtr <nsIMsgDBHdr> header = do_QueryInterface(supports, &rv);
      NS_ENSURE_SUCCESS(rv, rv);

      rv = nsMsgDatabase::IsHeaderRead(header, &isReadInDB);
      NS_ENSURE_SUCCESS(rv, rv);

      header->GetMessageKey(&messageKey);
      IsRead(messageKey,&readInNewsrc);

      numMessages++;
      if (!readInNewsrc)
        numUnreadMessages++;

      // If DB and readSet disagree on Read/Unread, fix DB
      if (readInNewsrc!=isReadInDB)
      {
        MarkHdrRead(header, readInNewsrc, nullptr);
        changed = true;
      }
  }

  // Update FolderInfo Counters
  int32_t oldMessages, oldUnreadMessages;
  rv = m_dbFolderInfo->GetNumMessages(&oldMessages);
  if (NS_SUCCEEDED(rv) && oldMessages!=numMessages)
  {
      changed = true;
      m_dbFolderInfo->ChangeNumMessages(numMessages-oldMessages);
  }
  rv = m_dbFolderInfo->GetNumUnreadMessages(&oldUnreadMessages);
  if (NS_SUCCEEDED(rv) && oldUnreadMessages!=numUnreadMessages)
  {
      changed = true;
      m_dbFolderInfo->ChangeNumUnreadMessages(numUnreadMessages-oldUnreadMessages);
  }

  if (changed)
      Commit(nsMsgDBCommitType::kLargeCommit);

  return rv;
}

nsresult nsNewsDatabase::AdjustExpungedBytesOnDelete(nsIMsgDBHdr *msgHdr)
{
  uint32_t msgFlags;
  msgHdr->GetFlags(&msgFlags);
  if (msgFlags & nsMsgMessageFlags::Offline && m_dbFolderInfo)
  {
    uint32_t size = 0;
    (void)msgHdr->GetOfflineMessageSize(&size);
    return m_dbFolderInfo->ChangeExpungedBytes (size);
  }
  return NS_OK;
}

NS_IMETHODIMP
nsNewsDatabase::GetDefaultViewFlags(nsMsgViewFlagsTypeValue *aDefaultViewFlags)
{
  NS_ENSURE_ARG_POINTER(aDefaultViewFlags);
  GetIntPref("mailnews.default_news_view_flags", aDefaultViewFlags);
  if (*aDefaultViewFlags < nsMsgViewFlagsType::kNone ||
      *aDefaultViewFlags > (nsMsgViewFlagsType::kThreadedDisplay |
                            nsMsgViewFlagsType::kShowIgnored |
                            nsMsgViewFlagsType::kUnreadOnly |
                            nsMsgViewFlagsType::kExpandAll |
                            nsMsgViewFlagsType::kGroupBySort))
    *aDefaultViewFlags = nsMsgViewFlagsType::kThreadedDisplay;
  return NS_OK;
}

NS_IMETHODIMP
nsNewsDatabase::GetDefaultSortType(nsMsgViewSortTypeValue *aDefaultSortType)
{
  NS_ENSURE_ARG_POINTER(aDefaultSortType);
  GetIntPref("mailnews.default_news_sort_type", aDefaultSortType);
  if (*aDefaultSortType < nsMsgViewSortType::byDate ||
      *aDefaultSortType > nsMsgViewSortType::byAccount)
    *aDefaultSortType = nsMsgViewSortType::byThread;
  return NS_OK;
}

NS_IMETHODIMP
nsNewsDatabase::GetDefaultSortOrder(nsMsgViewSortOrderValue *aDefaultSortOrder)
{
  NS_ENSURE_ARG_POINTER(aDefaultSortOrder);
  GetIntPref("mailnews.default_news_sort_order", aDefaultSortOrder);
  if (*aDefaultSortOrder != nsMsgViewSortOrder::descending)
    *aDefaultSortOrder = nsMsgViewSortOrder::ascending;
  return NS_OK;
}