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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "nsIMsgHdr.h"
#include "nsLocalUndoTxn.h"
#include "nsImapCore.h"
#include "nsMsgImapCID.h"
#include "nsIImapService.h"
#include "nsIUrlListener.h"
#include "nsIMsgLocalMailFolder.h"
#include "nsIMsgMailSession.h"
#include "nsIMsgFolderNotificationService.h"
#include "nsThreadUtils.h"
#include "nsIMsgDatabase.h"
#include "nsIMutableArray.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsMsgUtils.h"
NS_IMPL_ISUPPORTS_INHERITED(nsLocalMoveCopyMsgTxn, nsMsgTxn, nsIFolderListener)
nsLocalMoveCopyMsgTxn::nsLocalMoveCopyMsgTxn() : m_srcIsImap4(false),
m_canUndelete(false)
{
}
nsLocalMoveCopyMsgTxn::~nsLocalMoveCopyMsgTxn()
{
}
nsresult
nsLocalMoveCopyMsgTxn::Init(nsIMsgFolder* srcFolder, nsIMsgFolder* dstFolder,
bool isMove)
{
nsresult rv;
rv = SetSrcFolder(srcFolder);
NS_ENSURE_SUCCESS(rv, rv);
rv = SetDstFolder(dstFolder);
NS_ENSURE_SUCCESS(rv, rv);
m_isMove = isMove;
mUndoFolderListener = nullptr;
nsCString protocolType;
rv = srcFolder->GetURI(protocolType);
protocolType.SetLength(protocolType.FindChar(':'));
if (MsgLowerCaseEqualsLiteral(protocolType, "imap"))
m_srcIsImap4 = true;
return nsMsgTxn::Init();
}
nsresult
nsLocalMoveCopyMsgTxn::GetSrcIsImap(bool *isImap)
{
*isImap = m_srcIsImap4;
return NS_OK;
}
nsresult
nsLocalMoveCopyMsgTxn::SetSrcFolder(nsIMsgFolder* srcFolder)
{
nsresult rv = NS_ERROR_NULL_POINTER;
if (srcFolder)
m_srcFolder = do_GetWeakReference(srcFolder, &rv);
return rv;
}
nsresult
nsLocalMoveCopyMsgTxn::SetDstFolder(nsIMsgFolder* dstFolder)
{
nsresult rv = NS_ERROR_NULL_POINTER;
if (dstFolder)
m_dstFolder = do_GetWeakReference(dstFolder, &rv);
return rv;
}
nsresult
nsLocalMoveCopyMsgTxn::AddSrcKey(nsMsgKey aKey)
{
m_srcKeyArray.AppendElement(aKey);
return NS_OK;
}
nsresult
nsLocalMoveCopyMsgTxn::AddSrcStatusOffset(uint32_t aStatusOffset)
{
m_srcStatusOffsetArray.AppendElement(aStatusOffset);
return NS_OK;
}
nsresult
nsLocalMoveCopyMsgTxn::AddDstKey(nsMsgKey aKey)
{
m_dstKeyArray.AppendElement(aKey);
return NS_OK;
}
nsresult
nsLocalMoveCopyMsgTxn::AddDstMsgSize(uint32_t msgSize)
{
m_dstSizeArray.AppendElement(msgSize);
return NS_OK;
}
nsresult
nsLocalMoveCopyMsgTxn::UndoImapDeleteFlag(nsIMsgFolder* folder,
nsTArray<nsMsgKey>& keyArray,
bool deleteFlag)
{
nsresult rv = NS_ERROR_FAILURE;
if (m_srcIsImap4)
{
nsCOMPtr<nsIImapService> imapService = do_GetService(NS_IMAPSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIUrlListener> urlListener;
nsCString msgIds;
uint32_t i, count = keyArray.Length();
urlListener = do_QueryInterface(folder, &rv);
for (i=0; i < count; i++)
{
if (!msgIds.IsEmpty())
msgIds.Append(',');
msgIds.AppendInt((int32_t) keyArray[i]);
}
// This is to make sure that we are in the selected state
// when executing the imap url; we don't want to load the
// folder so use lite select to do the trick
rv = imapService->LiteSelectFolder(folder,
urlListener, nullptr, nullptr);
if (!deleteFlag)
rv =imapService->AddMessageFlags(folder,
urlListener, nullptr,
msgIds,
kImapMsgDeletedFlag,
true);
else
rv = imapService->SubtractMessageFlags(folder,
urlListener, nullptr,
msgIds,
kImapMsgDeletedFlag,
true);
if (NS_SUCCEEDED(rv) && m_msgWindow)
folder->UpdateFolder(m_msgWindow);
rv = NS_OK; // always return NS_OK to indicate that the src is imap
}
else
rv = NS_ERROR_FAILURE;
return rv;
}
NS_IMETHODIMP
nsLocalMoveCopyMsgTxn::UndoTransaction()
{
nsresult rv;
nsCOMPtr<nsIMsgDatabase> dstDB;
nsCOMPtr<nsIMsgFolder> dstFolder = do_QueryReferent(m_dstFolder, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgLocalMailFolder> dstlocalMailFolder = do_QueryReferent(m_dstFolder, &rv);
NS_ENSURE_SUCCESS(rv, rv);
dstlocalMailFolder->GetDatabaseWOReparse(getter_AddRefs(dstDB));
if (!dstDB)
{
// This will listen for the db reparse finishing, and the corresponding
// FolderLoadedNotification. When it gets that, it will then call
// UndoTransactionInternal.
mUndoFolderListener = new nsLocalUndoFolderListener(this, dstFolder);
if (!mUndoFolderListener)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(mUndoFolderListener);
nsCOMPtr<nsIMsgMailSession> mailSession =
do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
rv = mailSession->AddFolderListener(mUndoFolderListener, nsIFolderListener::event);
NS_ENSURE_SUCCESS(rv,rv);
rv = dstFolder->GetMsgDatabase(getter_AddRefs(dstDB));
NS_ENSURE_SUCCESS(rv,rv);
}
else
rv = UndoTransactionInternal();
return rv;
}
nsresult
nsLocalMoveCopyMsgTxn::UndoTransactionInternal()
{
nsresult rv = NS_ERROR_FAILURE;
if (mUndoFolderListener)
{
nsCOMPtr<nsIMsgMailSession> mailSession =
do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
rv = mailSession->RemoveFolderListener(mUndoFolderListener);
NS_ENSURE_SUCCESS(rv,rv);
NS_RELEASE(mUndoFolderListener);
mUndoFolderListener = nullptr;
}
nsCOMPtr<nsIMsgDatabase> srcDB;
nsCOMPtr<nsIMsgDatabase> dstDB;
nsCOMPtr<nsIMsgFolder> srcFolder = do_QueryReferent(m_srcFolder, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIMsgFolder> dstFolder = do_QueryReferent(m_dstFolder, &rv);
NS_ENSURE_SUCCESS(rv,rv);
rv = srcFolder->GetMsgDatabase(getter_AddRefs(srcDB));
if(NS_FAILED(rv)) return rv;
rv = dstFolder->GetMsgDatabase(getter_AddRefs(dstDB));
if (NS_FAILED(rv)) return rv;
uint32_t count = m_srcKeyArray.Length();
uint32_t i;
nsCOMPtr<nsIMsgDBHdr> oldHdr;
nsCOMPtr<nsIMsgDBHdr> newHdr;
// protect against a bogus undo txn without any source keys
// see bug #179856 for details
NS_ASSERTION(count, "no source keys");
if (!count)
return NS_ERROR_UNEXPECTED;
if (m_isMove)
{
if (m_srcIsImap4)
{
bool deleteFlag = true; //message has been deleted -we are trying to undo it
CheckForToggleDelete(srcFolder, m_srcKeyArray[0], &deleteFlag); //there could have been a toggle.
rv = UndoImapDeleteFlag(srcFolder, m_srcKeyArray, deleteFlag);
}
else if (m_canUndelete)
{
nsCOMPtr<nsIMutableArray> srcMessages =
do_CreateInstance(NS_ARRAY_CONTRACTID);
nsCOMPtr<nsIMutableArray> dstMessages =
do_CreateInstance(NS_ARRAY_CONTRACTID);
srcDB->StartBatch();
for (i = 0; i < count; i++)
{
rv = dstDB->GetMsgHdrForKey(m_dstKeyArray[i],
getter_AddRefs(oldHdr));
NS_ASSERTION(oldHdr, "fatal ... cannot get old msg header\n");
if (NS_SUCCEEDED(rv) && oldHdr)
{
rv = srcDB->CopyHdrFromExistingHdr(m_srcKeyArray[i],
oldHdr, true,
getter_AddRefs(newHdr));
NS_ASSERTION(newHdr,
"fatal ... cannot create new msg header\n");
if (NS_SUCCEEDED(rv) && newHdr)
{
newHdr->SetStatusOffset(m_srcStatusOffsetArray[i]);
srcDB->UndoDelete(newHdr);
srcMessages->AppendElement(newHdr, false);
// (we want to keep these two lists in sync)
dstMessages->AppendElement(oldHdr, false);
}
}
}
srcDB->EndBatch();
nsCOMPtr<nsIMsgFolderNotificationService>
notifier(do_GetService(NS_MSGNOTIFICATIONSERVICE_CONTRACTID));
if (notifier)
{
// Remember that we're actually moving things back from the destination
// to the source!
notifier->NotifyMsgsMoveCopyCompleted(true, dstMessages,
srcFolder, srcMessages);
}
nsCOMPtr <nsIMsgLocalMailFolder> localFolder = do_QueryInterface(srcFolder);
if (localFolder)
localFolder->MarkMsgsOnPop3Server(srcMessages, POP3_NONE /*deleteMsgs*/);
}
else // undoing a move means moving the messages back.
{
nsCOMPtr<nsIMutableArray> dstMessages =
do_CreateInstance(NS_ARRAY_CONTRACTID);
m_numHdrsCopied = 0;
m_srcKeyArray.Clear();
for (i = 0; i < count; i++)
{
// GetMsgHdrForKey is not a test for whether the key exists, so check.
bool hasKey = false;
dstDB->ContainsKey(m_dstKeyArray[i], &hasKey);
nsCOMPtr<nsIMsgDBHdr> dstHdr;
if (hasKey)
dstDB->GetMsgHdrForKey(m_dstKeyArray[i], getter_AddRefs(dstHdr));
if (dstHdr)
{
nsCString messageId;
dstHdr->GetMessageId(getter_Copies(messageId));
dstMessages->AppendElement(dstHdr, false);
m_copiedMsgIds.AppendElement(messageId);
}
else
{
NS_WARNING("Cannot get old msg header");
}
}
if (m_copiedMsgIds.Length())
{
srcFolder->AddFolderListener(this);
m_undoing = true;
return srcFolder->CopyMessages(dstFolder, dstMessages,
true, nullptr, nullptr, false,
false);
}
else
{
// Nothing to do, probably because original messages were deleted.
NS_WARNING("Undo did not find any messages to move");
}
}
srcDB->SetSummaryValid(true);
}
dstDB->DeleteMessages(m_dstKeyArray.Length(), m_dstKeyArray.Elements(), nullptr);
dstDB->SetSummaryValid(true);
return rv;
}
NS_IMETHODIMP
nsLocalMoveCopyMsgTxn::RedoTransaction()
{
nsresult rv;
nsCOMPtr<nsIMsgDatabase> srcDB;
nsCOMPtr<nsIMsgDatabase> dstDB;
nsCOMPtr<nsIMsgFolder> srcFolder = do_QueryReferent(m_srcFolder, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCOMPtr<nsIMsgFolder> dstFolder = do_QueryReferent(m_dstFolder, &rv);
NS_ENSURE_SUCCESS(rv,rv);
rv = srcFolder->GetMsgDatabase(getter_AddRefs(srcDB));
if(NS_FAILED(rv)) return rv;
rv = dstFolder->GetMsgDatabase(getter_AddRefs(dstDB));
if (NS_FAILED(rv)) return rv;
uint32_t count = m_srcKeyArray.Length();
uint32_t i;
nsCOMPtr<nsIMsgDBHdr> oldHdr;
nsCOMPtr<nsIMsgDBHdr> newHdr;
nsCOMPtr<nsIMutableArray> srcMessages = do_CreateInstance(NS_ARRAY_CONTRACTID);
nsCOMPtr <nsISupports> msgSupports;
for (i=0; i<count; i++)
{
rv = srcDB->GetMsgHdrForKey(m_srcKeyArray[i],
getter_AddRefs(oldHdr));
NS_ASSERTION(oldHdr, "fatal ... cannot get old msg header\n");
if (NS_SUCCEEDED(rv) && oldHdr)
{
msgSupports =do_QueryInterface(oldHdr);
srcMessages->AppendElement(msgSupports, false);
if (m_canUndelete)
{
rv = dstDB->CopyHdrFromExistingHdr(m_dstKeyArray[i],
oldHdr, true,
getter_AddRefs(newHdr));
NS_ASSERTION(newHdr, "fatal ... cannot get new msg header\n");
if (NS_SUCCEEDED(rv) && newHdr)
{
if (i < m_dstSizeArray.Length())
rv = newHdr->SetMessageSize(m_dstSizeArray[i]);
dstDB->UndoDelete(newHdr);
}
}
}
}
dstDB->SetSummaryValid(true);
if (m_isMove)
{
if (m_srcIsImap4)
{
// protect against a bogus undo txn without any source keys
// see bug #179856 for details
NS_ASSERTION(!m_srcKeyArray.IsEmpty(), "no source keys");
if (m_srcKeyArray.IsEmpty())
return NS_ERROR_UNEXPECTED;
bool deleteFlag = false; //message is un-deleted- we are trying to redo
CheckForToggleDelete(srcFolder, m_srcKeyArray[0], &deleteFlag); // there could have been a toggle
rv = UndoImapDeleteFlag(srcFolder, m_srcKeyArray, deleteFlag);
}
else if (m_canUndelete)
{
nsCOMPtr <nsIMsgLocalMailFolder> localFolder = do_QueryInterface(srcFolder);
if (localFolder)
localFolder->MarkMsgsOnPop3Server(srcMessages, POP3_DELETE /*deleteMsgs*/);
rv = srcDB->DeleteMessages(m_srcKeyArray.Length(), m_srcKeyArray.Elements(), nullptr);
srcDB->SetSummaryValid(true);
}
else
{
nsCOMPtr<nsIMsgDBHdr> srcHdr;
m_numHdrsCopied = 0;
m_dstKeyArray.Clear();
for (i = 0; i < count; i++)
{
srcDB->GetMsgHdrForKey(m_srcKeyArray[i], getter_AddRefs(srcHdr));
NS_ASSERTION(srcHdr, "fatal ... cannot get old msg header\n");
if (srcHdr)
{
nsCString messageId;
srcHdr->GetMessageId(getter_Copies(messageId));
m_copiedMsgIds.AppendElement(messageId);
}
}
dstFolder->AddFolderListener(this);
m_undoing = false;
return dstFolder->CopyMessages(srcFolder, srcMessages, true, nullptr,
nullptr, false, false);
}
}
return rv;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemAdded(nsIMsgFolder *parentItem, nsISupports *item)
{
nsCOMPtr<nsIMsgDBHdr> msgHdr(do_QueryInterface(item));
if (msgHdr)
{
nsresult rv;
nsCOMPtr<nsIMsgFolder> folder = do_QueryReferent(m_undoing ? m_srcFolder :
m_dstFolder, &rv);
NS_ENSURE_SUCCESS(rv,rv);
nsCString messageId;
msgHdr->GetMessageId(getter_Copies(messageId));
if (m_copiedMsgIds.Contains(messageId))
{
nsMsgKey msgKey;
msgHdr->GetMessageKey(&msgKey);
if (m_undoing)
m_srcKeyArray.AppendElement(msgKey);
else
m_dstKeyArray.AppendElement(msgKey);
if (++m_numHdrsCopied == m_copiedMsgIds.Length())
{
folder->RemoveFolderListener(this);
m_copiedMsgIds.Clear();
}
}
}
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemRemoved(nsIMsgFolder *parentItem, nsISupports *item)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemPropertyChanged(nsIMsgFolder *item, nsIAtom *property, const char *oldValue, const char *newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemIntPropertyChanged(nsIMsgFolder *item, nsIAtom *property, int64_t oldValue, int64_t newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemBoolPropertyChanged(nsIMsgFolder *item, nsIAtom *property, bool oldValue, bool newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemUnicharPropertyChanged(nsIMsgFolder *item, nsIAtom *property, const char16_t *oldValue, const char16_t *newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemPropertyFlagChanged(nsIMsgDBHdr *item, nsIAtom *property, uint32_t oldFlag, uint32_t newFlag)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalMoveCopyMsgTxn::OnItemEvent(nsIMsgFolder *aItem, nsIAtom *aEvent)
{
return NS_OK;
}
NS_IMPL_ISUPPORTS(nsLocalUndoFolderListener, nsIFolderListener)
nsLocalUndoFolderListener::nsLocalUndoFolderListener(nsLocalMoveCopyMsgTxn *aTxn, nsIMsgFolder *aFolder)
{
mTxn = aTxn;
mFolder = aFolder;
}
nsLocalUndoFolderListener::~nsLocalUndoFolderListener()
{
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemAdded(nsIMsgFolder *parentItem, nsISupports *item)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemRemoved(nsIMsgFolder *parentItem, nsISupports *item)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemPropertyChanged(nsIMsgFolder *item, nsIAtom *property, const char *oldValue, const char *newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemIntPropertyChanged(nsIMsgFolder *item, nsIAtom *property, int64_t oldValue, int64_t newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemBoolPropertyChanged(nsIMsgFolder *item, nsIAtom *property, bool oldValue, bool newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemUnicharPropertyChanged(nsIMsgFolder *item, nsIAtom *property, const char16_t *oldValue, const char16_t *newValue)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemPropertyFlagChanged(nsIMsgDBHdr *item, nsIAtom *property, uint32_t oldFlag, uint32_t newFlag)
{
return NS_OK;
}
NS_IMETHODIMP nsLocalUndoFolderListener::OnItemEvent(nsIMsgFolder *aItem, nsIAtom *aEvent)
{
if (mTxn && mFolder && aItem == mFolder) {
bool isEqual = false;
aEvent->ScriptableEquals(NS_LITERAL_STRING("FolderLoaded"), &isEqual);
if (isEqual)
return mTxn->UndoTransactionInternal();
}
return NS_ERROR_FAILURE;
}
|