summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/nsMsgOfflineManager.cpp
blob: 7d6935fc890c3852bc186a1039689b9146044045 (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
/* -*- 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 "nsIImapService.h"
#include "nsIMsgSendLater.h"
#include "nsIMsgAccountManager.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#include "nsINntpService.h"
#include "nsIMsgStatusFeedback.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/Components.h"

#define NS_MSGSENDLATER_CID                         \
  { /* E15C83F1-1CF4-11d3-8EF0-00A024A7D144 */      \
    0xe15c83f1, 0x1cf4, 0x11d3, {                   \
      0x8e, 0xf0, 0x0, 0xa0, 0x24, 0xa7, 0xd1, 0x44 \
    }                                               \
  }
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);
  NS_IF_ADDREF(*aWindow = m_window);
  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("@mozilla.org/messenger/imapservice;1", &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("@mozilla.org/messenger/account-manager;1", &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.
  nsTArray<RefPtr<nsIMsgIdentity>> identities;

  if (NS_SUCCEEDED(rv) && accountManager) {
    rv = accountManager->GetAllIdentities(identities);
    NS_ENSURE_SUCCESS(rv, rv);
  }
  nsCOMPtr<nsIMsgIdentity> identityToUse;
  for (auto thisIdentity : identities) {
    if (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::components::StringBundle::Service();
    NS_ENSURE_TRUE(sBundleService, NS_ERROR_UNEXPECTED);
    sBundleService->CreateBundle(MESSENGER_STRING_URL,
                                 getter_AddRefs(mStringBundle));
    return NS_OK;
  }

  nsString statusString;
  nsresult res = mStringBundle->GetStringFromName(statusMsgName, 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("@mozilla.org/messenger/nntpservice;1", &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("@mozilla.org/messenger/imapservice;1", &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.
}

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;
}

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::components::IO::Service();
  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);
}