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
|
/* -*- 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/. */
/*
* The offline manager service - manages going online and offline, and synchronization
*/
#include "msgCore.h"
#include "netCore.h"
#include "nsMsgOfflineManager.h"
#include "nsIServiceManager.h"
#include "nsMsgBaseCID.h"
#include "nsIImapService.h"
#include "nsMsgImapCID.h"
#include "nsIMsgSendLater.h"
#include "nsIMsgAccountManager.h"
#include "nsMsgCompCID.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#include "nsMsgNewsCID.h"
#include "nsINntpService.h"
#include "nsIMsgStatusFeedback.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/Services.h"
#include "nsIArray.h"
#include "nsArrayUtils.h"
static NS_DEFINE_CID(kMsgSendLaterCID, NS_MSGSENDLATER_CID);
NS_IMPL_ISUPPORTS(nsMsgOfflineManager,
nsIMsgOfflineManager,
nsIMsgSendLaterListener,
nsIObserver,
nsISupportsWeakReference,
nsIUrlListener)
nsMsgOfflineManager::nsMsgOfflineManager() :
m_inProgress (false),
m_sendUnsentMessages(false),
m_downloadNews(false),
m_downloadMail(false),
m_playbackOfflineImapOps(false),
m_goOfflineWhenDone(false),
m_curState(eNoState),
m_curOperation(eNoOp)
{
}
nsMsgOfflineManager::~nsMsgOfflineManager()
{
}
/* attribute nsIMsgWindow window; */
NS_IMETHODIMP nsMsgOfflineManager::GetWindow(nsIMsgWindow * *aWindow)
{
NS_ENSURE_ARG(aWindow);
*aWindow = m_window;
NS_IF_ADDREF(*aWindow);
return NS_OK;
}
NS_IMETHODIMP nsMsgOfflineManager::SetWindow(nsIMsgWindow * aWindow)
{
m_window = aWindow;
if (m_window)
m_window->GetStatusFeedback(getter_AddRefs(m_statusFeedback));
else
m_statusFeedback = nullptr;
return NS_OK;
}
/* attribute boolean inProgress; */
NS_IMETHODIMP nsMsgOfflineManager::GetInProgress(bool *aInProgress)
{
NS_ENSURE_ARG(aInProgress);
*aInProgress = m_inProgress;
return NS_OK;
}
NS_IMETHODIMP nsMsgOfflineManager::SetInProgress(bool aInProgress)
{
m_inProgress = aInProgress;
return NS_OK;
}
nsresult nsMsgOfflineManager::StopRunning(nsresult exitStatus)
{
m_inProgress = false;
return exitStatus;
}
nsresult nsMsgOfflineManager::AdvanceToNextState(nsresult exitStatus)
{
// NS_BINDING_ABORTED is used for the user pressing stop, which
// should cause us to abort the offline process. Other errors
// should allow us to continue.
if (exitStatus == NS_BINDING_ABORTED)
{
return StopRunning(exitStatus);
}
if (m_curOperation == eGoingOnline)
{
switch (m_curState)
{
case eNoState:
m_curState = eSendingUnsent;
if (m_sendUnsentMessages)
{
SendUnsentMessages();
}
else
AdvanceToNextState(NS_OK);
break;
case eSendingUnsent:
m_curState = eSynchronizingOfflineImapChanges;
if (m_playbackOfflineImapOps)
return SynchronizeOfflineImapChanges();
else
AdvanceToNextState(NS_OK); // recurse to next state.
break;
case eSynchronizingOfflineImapChanges:
m_curState = eDone;
return StopRunning(exitStatus);
default:
NS_ASSERTION(false, "unhandled current state when going online");
}
}
else if (m_curOperation == eDownloadingForOffline)
{
switch (m_curState)
{
case eNoState:
m_curState = eDownloadingNews;
if (m_downloadNews)
DownloadOfflineNewsgroups();
else
AdvanceToNextState(NS_OK);
break;
case eSendingUnsent:
if (m_goOfflineWhenDone)
{
SetOnlineState(false);
}
break;
case eDownloadingNews:
m_curState = eDownloadingMail;
if (m_downloadMail)
DownloadMail();
else
AdvanceToNextState(NS_OK);
break;
case eDownloadingMail:
m_curState = eSendingUnsent;
if (m_sendUnsentMessages)
SendUnsentMessages();
else
AdvanceToNextState(NS_OK);
break;
default:
NS_ASSERTION(false, "unhandled current state when downloading for offline");
}
}
return NS_OK;
}
nsresult nsMsgOfflineManager::SynchronizeOfflineImapChanges()
{
nsresult rv = NS_OK;
nsCOMPtr<nsIImapService> imapService = do_GetService(NS_IMAPSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return imapService->PlaybackAllOfflineOperations(m_window, this, getter_AddRefs(mOfflineImapSync));
}
nsresult nsMsgOfflineManager::SendUnsentMessages()
{
nsresult rv;
nsCOMPtr<nsIMsgSendLater> pMsgSendLater(do_GetService(kMsgSendLaterCID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// now we have to iterate over the identities, finding the *unique* unsent messages folder
// for each one, determine if they have unsent messages, and if so, add them to the list
// of identities to send unsent messages from.
// However, I think there's only ever one unsent messages folder at the moment,
// so I think we'll go with that for now.
nsCOMPtr<nsIArray> identities;
if (NS_SUCCEEDED(rv) && accountManager)
{
rv = accountManager->GetAllIdentities(getter_AddRefs(identities));
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr <nsIMsgIdentity> identityToUse;
uint32_t numIndentities;
identities->GetLength(&numIndentities);
for (uint32_t i = 0; i < numIndentities; i++)
{
nsCOMPtr<nsIMsgIdentity> thisIdentity(do_QueryElementAt(identities, i, &rv));
if (NS_SUCCEEDED(rv) && thisIdentity)
{
nsCOMPtr <nsIMsgFolder> outboxFolder;
pMsgSendLater->GetUnsentMessagesFolder(thisIdentity, getter_AddRefs(outboxFolder));
if (outboxFolder)
{
int32_t numMessages;
outboxFolder->GetTotalMessages(false, &numMessages);
if (numMessages > 0)
{
identityToUse = thisIdentity;
break;
}
}
}
}
if (identityToUse)
{
#ifdef MOZ_SUITE
if (m_statusFeedback)
pMsgSendLater->SetStatusFeedback(m_statusFeedback);
#endif
pMsgSendLater->AddListener(this);
rv = pMsgSendLater->SendUnsentMessages(identityToUse);
ShowStatus("sendingUnsent");
// if we succeeded, return - we'll run the next operation when the
// send finishes. Otherwise, advance to the next state.
if (NS_SUCCEEDED(rv))
return rv;
}
return AdvanceToNextState(rv);
}
#define MESSENGER_STRING_URL "chrome://messenger/locale/messenger.properties"
nsresult nsMsgOfflineManager::ShowStatus(const char *statusMsgName)
{
if (!mStringBundle)
{
nsCOMPtr<nsIStringBundleService> sBundleService =
mozilla::services::GetStringBundleService();
NS_ENSURE_TRUE(sBundleService, NS_ERROR_UNEXPECTED);
sBundleService->CreateBundle(MESSENGER_STRING_URL, getter_AddRefs(mStringBundle));
return NS_OK;
}
nsString statusString;
nsresult res = mStringBundle->GetStringFromName(NS_ConvertASCIItoUTF16(statusMsgName).get(),
getter_Copies(statusString));
if (NS_SUCCEEDED(res) && m_statusFeedback)
m_statusFeedback->ShowStatusString(statusString);
return res;
}
nsresult nsMsgOfflineManager::DownloadOfflineNewsgroups()
{
nsresult rv;
ShowStatus("downloadingNewsgroups");
nsCOMPtr<nsINntpService> nntpService(do_GetService(NS_NNTPSERVICE_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv) && nntpService)
rv = nntpService->DownloadNewsgroupsForOffline(m_window, this);
if (NS_FAILED(rv))
return AdvanceToNextState(rv);
return rv;
}
nsresult nsMsgOfflineManager::DownloadMail()
{
nsresult rv = NS_OK;
ShowStatus("downloadingMail");
nsCOMPtr<nsIImapService> imapService = do_GetService(NS_IMAPSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
return imapService->DownloadAllOffineImapFolders(m_window, this);
// ### we should do get new mail on pop servers, and download imap messages for offline use.
}
/* void goOnline (in boolean sendUnsentMessages, in boolean playbackOfflineImapOperations, in nsIMsgWindow aMsgWindow); */
NS_IMETHODIMP nsMsgOfflineManager::GoOnline(bool sendUnsentMessages, bool playbackOfflineImapOperations, nsIMsgWindow *aMsgWindow)
{
m_sendUnsentMessages = sendUnsentMessages;
m_playbackOfflineImapOps = playbackOfflineImapOperations;
m_curOperation = eGoingOnline;
m_curState = eNoState;
SetWindow(aMsgWindow);
SetOnlineState(true);
if (!m_sendUnsentMessages && !playbackOfflineImapOperations)
return NS_OK;
else
AdvanceToNextState(NS_OK);
return NS_OK;
}
/* void synchronizeForOffline (in boolean downloadNews, in boolean downloadMail, in boolean sendUnsentMessages, in boolean goOfflineWhenDone, in nsIMsgWindow aMsgWindow); */
NS_IMETHODIMP nsMsgOfflineManager::SynchronizeForOffline(bool downloadNews, bool downloadMail, bool sendUnsentMessages, bool goOfflineWhenDone, nsIMsgWindow *aMsgWindow)
{
m_curOperation = eDownloadingForOffline;
m_downloadNews = downloadNews;
m_downloadMail = downloadMail;
m_sendUnsentMessages = sendUnsentMessages;
SetWindow(aMsgWindow);
m_goOfflineWhenDone = goOfflineWhenDone;
m_curState = eNoState;
if (!downloadNews && !downloadMail && !sendUnsentMessages)
{
if (goOfflineWhenDone)
return SetOnlineState(false);
}
else
return AdvanceToNextState(NS_OK);
return NS_OK;
}
nsresult nsMsgOfflineManager::SetOnlineState(bool online)
{
nsCOMPtr<nsIIOService> netService =
mozilla::services::GetIOService();
NS_ENSURE_TRUE(netService, NS_ERROR_UNEXPECTED);
return netService->SetOffline(!online);
}
// nsIUrlListener methods
NS_IMETHODIMP
nsMsgOfflineManager::OnStartRunningUrl(nsIURI * aUrl)
{
return NS_OK;
}
NS_IMETHODIMP
nsMsgOfflineManager::OnStopRunningUrl(nsIURI * aUrl, nsresult aExitCode)
{
mOfflineImapSync = nullptr;
AdvanceToNextState(aExitCode);
return NS_OK;
}
NS_IMETHODIMP nsMsgOfflineManager::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
{
return NS_OK;
}
// nsIMsgSendLaterListener implementation
NS_IMETHODIMP
nsMsgOfflineManager::OnStartSending(uint32_t aTotalMessageCount)
{
return NS_OK;
}
NS_IMETHODIMP
nsMsgOfflineManager::OnMessageStartSending(uint32_t aCurrentMessage,
uint32_t aTotalMessageCount,
nsIMsgDBHdr *aMessageHeader,
nsIMsgIdentity *aIdentity)
{
return NS_OK;
}
NS_IMETHODIMP
nsMsgOfflineManager::OnMessageSendProgress(uint32_t aCurrentMessage,
uint32_t aTotalMessageCount,
uint32_t aMessageSendPercent,
uint32_t aMessageCopyPercent)
{
if (m_statusFeedback && aTotalMessageCount)
return m_statusFeedback->ShowProgress((100 * aCurrentMessage) /
aTotalMessageCount);
return NS_OK;
}
NS_IMETHODIMP
nsMsgOfflineManager::OnMessageSendError(uint32_t aCurrentMessage,
nsIMsgDBHdr *aMessageHeader,
nsresult aStatus,
const char16_t *aMsg)
{
return NS_OK;
}
NS_IMETHODIMP
nsMsgOfflineManager::OnStopSending(nsresult aStatus,
const char16_t *aMsg, uint32_t aTotalTried,
uint32_t aSuccessful)
{
#ifdef NS_DEBUG
if (NS_SUCCEEDED(aStatus))
printf("SendLaterListener::OnStopSending: Tried to send %d messages. %d successful.\n",
aTotalTried, aSuccessful);
#endif
return AdvanceToNextState(aStatus);
}
|