summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/mail-offline.js
blob: 13024b874aaebf0b20d2a30e9fedd4b7055f9be1 (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
/* -*- Mode: Javascript; 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/. */

/* globals msgWindow */ // From mailWindow.js

var MailOfflineMgr = {
  offlineManager: null,
  offlineBundle: null,

  init() {
    Services.obs.addObserver(this, "network:offline-status-changed");

    this.offlineManager = Cc[
      "@mozilla.org/messenger/offline-manager;1"
    ].getService(Ci.nsIMsgOfflineManager);
    this.offlineBundle = Services.strings.createBundle(
      "chrome://messenger/locale/offline.properties"
    );

    // initialize our offline state UI
    this.updateOfflineUI(!this.isOnline());
  },

  uninit() {
    Services.obs.removeObserver(this, "network:offline-status-changed");
  },

  /**
   * @returns true if we are online
   */
  isOnline() {
    return !Services.io.offline;
  },

  /**
   * Toggles the online / offline state, initiated by the user. Depending on user settings
   * we may prompt the user to send unsent messages when going online or to download messages for
   * offline use when going offline.
   */
  toggleOfflineStatus() {
    // the offline manager(goOnline and synchronizeForOffline) actually does the dirty work of
    // changing the offline state with the networking service.
    if (!this.isOnline()) {
      // We do the go online stuff in our listener for the online state change.
      Services.io.offline = false;
      // resume managing offline status now that we are going back online.
      Services.io.manageOfflineStatus =
        Services.prefs.getBoolPref("offline.autoDetect");
    } else {
      // going offline
      // Stop automatic management of the offline status since the user has
      // decided to go offline.
      Services.io.manageOfflineStatus = false;
      var prefDownloadMessages = Services.prefs.getIntPref(
        "offline.download.download_messages"
      );
      // 0 == Ask, 1 == Always Download, 2 == Never Download
      var downloadForOfflineUse =
        (prefDownloadMessages == 0 &&
          this.confirmDownloadMessagesForOfflineUse()) ||
        prefDownloadMessages == 1;
      this.offlineManager.synchronizeForOffline(
        downloadForOfflineUse,
        downloadForOfflineUse,
        false,
        true,
        msgWindow
      );
    }
  },

  observe(aSubject, aTopic, aState) {
    if (aTopic == "network:offline-status-changed") {
      this.mailOfflineStateChanged(aState == "offline");
    }
  },

  /**
   * @returns true if there are unsent messages
   */
  haveUnsentMessages() {
    return Cc["@mozilla.org/messengercompose/sendlater;1"]
      .getService(Ci.nsIMsgSendLater)
      .hasUnsentMessages();
  },

  /**
   * open the offline panel in the account manager for the currently loaded
   * account.
   */
  openOfflineAccountSettings() {
    window.parent.MsgAccountManager("am-offline.xhtml");
  },

  /**
   * Prompt the user about going online to send unsent messages, and then send them
   * if appropriate. Puts the app back into online mode.
   *
   * @param aMsgWindow the msg window to be used when going online
   */
  goOnlineToSendMessages(aMsgWindow) {
    let goOnlineToSendMsgs = Services.prompt.confirm(
      window,
      this.offlineBundle.GetStringFromName("sendMessagesOfflineWindowTitle1"),
      this.offlineBundle.GetStringFromName("sendMessagesOfflineLabel1")
    );

    if (goOnlineToSendMsgs) {
      this.offlineManager.goOnline(
        true /* send unsent messages*/,
        false,
        aMsgWindow
      );
    }
  },

  /**
   * Prompts the user to confirm sending of unsent messages. This is different from
   * goOnlineToSendMessages which involves going online to send unsent messages.
   *
   * @returns true if the user wants to send unsent messages
   */
  confirmSendUnsentMessages() {
    let alwaysAsk = { value: true };
    let sendUnsentMessages =
      Services.prompt.confirmEx(
        window,
        this.offlineBundle.GetStringFromName("sendMessagesWindowTitle1"),
        this.offlineBundle.GetStringFromName("sendMessagesLabel2"),
        Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 +
          Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_1,
        this.offlineBundle.GetStringFromName("sendMessagesNow2"),
        this.offlineBundle.GetStringFromName("processMessagesLater2"),
        null,
        this.offlineBundle.GetStringFromName("sendMessagesCheckboxLabel1"),
        alwaysAsk
      ) == 0;

    // if the user changed the ask me setting then update the global pref based on their yes / no answer
    if (!alwaysAsk.value) {
      Services.prefs.setIntPref(
        "offline.send.unsent_messages",
        sendUnsentMessages ? 1 : 2
      );
    }

    return sendUnsentMessages;
  },

  /**
   * Should we send unsent messages? Based on the value of
   * offline.send.unsent_messages, this method may prompt the user.
   *
   * @returns true if we should send unsent messages
   */
  shouldSendUnsentMessages() {
    var sendUnsentWhenGoingOnlinePref = Services.prefs.getIntPref(
      "offline.send.unsent_messages"
    );
    if (sendUnsentWhenGoingOnlinePref == 2) {
      // never send
      return false;
    } else if (this.haveUnsentMessages()) {
      // if we we have unsent messages, then honor the offline.send.unsent_messages pref.
      if (
        (sendUnsentWhenGoingOnlinePref == 0 &&
          this.confirmSendUnsentMessages()) ||
        sendUnsentWhenGoingOnlinePref == 1
      ) {
        return true;
      }
    }
    return false;
  },

  /**
   * Prompts the user to download messages for offline use before going offline.
   * May update the value of offline.download.download_messages
   *
   * @returns true if the user wants to download messages for offline use.
   */
  confirmDownloadMessagesForOfflineUse() {
    let alwaysAsk = { value: true };
    let downloadMessages =
      Services.prompt.confirmEx(
        window,
        this.offlineBundle.GetStringFromName("downloadMessagesWindowTitle1"),
        this.offlineBundle.GetStringFromName("downloadMessagesLabel1"),
        Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0 +
          Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_1,
        this.offlineBundle.GetStringFromName("downloadMessagesNow2"),
        this.offlineBundle.GetStringFromName("processMessagesLater2"),
        null,
        this.offlineBundle.GetStringFromName("downloadMessagesCheckboxLabel1"),
        alwaysAsk
      ) == 0;

    // if the user changed the ask me setting then update the global pref based on their yes / no answer
    if (!alwaysAsk.value) {
      Services.prefs.setIntPref(
        "offline.download.download_messages",
        downloadMessages ? 1 : 2
      );
    }
    return downloadMessages;
  },

  /**
   *  Get New Mail When Offline
   *  Prompts the user about going online in order to download new messages.
   *  Based on the response, will move us back to online mode.
   *
   * @returns true if the user confirms going online.
   */
  getNewMail() {
    let goOnline = Services.prompt.confirm(
      window,
      this.offlineBundle.GetStringFromName("getMessagesOfflineWindowTitle1"),
      this.offlineBundle.GetStringFromName("getMessagesOfflineLabel1")
    );

    if (goOnline) {
      this.offlineManager.goOnline(
        this.shouldSendUnsentMessages(),
        false /* playbackOfflineImapOperations */,
        msgWindow
      );
    }
    return goOnline;
  },

  /**
   * Private helper method to update the state of the Offline menu item
   * and the offline status bar indicator
   */
  updateOfflineUI(aIsOffline) {
    document
      .getElementById("goOfflineMenuItem")
      .setAttribute("checked", aIsOffline);
    var statusBarPanel = document.getElementById("offline-status");
    if (aIsOffline) {
      statusBarPanel.setAttribute("offline", "true");
      statusBarPanel.setAttribute(
        "tooltiptext",
        this.offlineBundle.GetStringFromName("offlineTooltip")
      );
    } else {
      statusBarPanel.removeAttribute("offline");
      statusBarPanel.setAttribute(
        "tooltiptext",
        this.offlineBundle.GetStringFromName("onlineTooltip")
      );
    }
  },

  /**
   * private helper method called whenever we detect a change to the offline state
   */
  mailOfflineStateChanged(aGoingOffline) {
    this.updateOfflineUI(aGoingOffline);
    if (!aGoingOffline) {
      let prefSendUnsentMessages = Services.prefs.getIntPref(
        "offline.send.unsent_messages"
      );
      // 0 == Ask, 1 == Always Send, 2 == Never Send
      let sendUnsentMessages =
        (prefSendUnsentMessages == 0 &&
          this.haveUnsentMessages() &&
          this.confirmSendUnsentMessages()) ||
        prefSendUnsentMessages == 1;
      this.offlineManager.goOnline(sendUnsentMessages, true, msgWindow);
    }
  },
};