summaryrefslogtreecommitdiffstats
path: root/mailnews/extensions/offline-startup/js/offlineStartup.js
blob: 56584465dc3b2d368818dc1b0aa98083dc6d7e53 (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
/* -*- Mode: Java; 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/. */

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

var kDebug              = false;
var kOfflineStartupPref = "offline.startup_state";
var kRememberLastState  = 0;
var kAskForOnlineState  = 1;
var kAlwaysOnline       = 2;
var kAlwaysOffline      = 3;
var kAutomatic          = 4;
var   gStartingUp         = true;
var   gOfflineStartupMode; //0 = remember last state, 1 = ask me, 2 == online, 3 == offline, 4 = automatic


////////////////////////////////////////////////////////////////////////
//
//   nsOfflineStartup : nsIObserver
//
//   Check if the user has set the pref to be prompted for
//   online/offline startup mode. If so, prompt the user. Also,
//   check if the user wants to remember their offline state
//   the next time they start up.
//   If the user shutdown offline, and is now starting up in online
//   mode, we will set the boolean pref "mailnews.playback_offline" to true.
//
////////////////////////////////////////////////////////////////////////

var nsOfflineStartup =
{
  onProfileStartup: function()
  {
    debug("onProfileStartup");

    if (gStartingUp)
    {
      gStartingUp = false;
      // if checked, the "work offline" checkbox overrides
      if (Services.io.offline && !Services.io.manageOfflineStatus)
      {
        debug("already offline!");
        return;
      }
    }

    var manageOfflineStatus = Services.prefs.getBoolPref("offline.autoDetect");
    gOfflineStartupMode = Services.prefs.getIntPref(kOfflineStartupPref);
    let wasOffline = !Services.prefs.getBoolPref("network.online");

    if (gOfflineStartupMode == kAutomatic)
    {
      // Offline state should be managed automatically
      // so do nothing specific at startup.
    }
    else if (gOfflineStartupMode == kAlwaysOffline)
    {
      Services.io.manageOfflineStatus = false;
      Services.io.offline = true;
    }
    else if (gOfflineStartupMode == kAlwaysOnline)
    {
      Services.io.manageOfflineStatus = manageOfflineStatus;
      if (wasOffline)
        Services.prefs.setBoolPref("mailnews.playback_offline", true);
      // If we're managing the offline status, don't force online here... it may
      // be the network really is offline.
      if (!manageOfflineStatus)
        Services.io.offline = false;
    }
    else if (gOfflineStartupMode == kRememberLastState)
    {
      Services.io.manageOfflineStatus = manageOfflineStatus && !wasOffline;
      // If we are meant to be online, and managing the offline status
      // then don't force it - it may be the network really is offline.
      if (!manageOfflineStatus || wasOffline)
        Services.io.offline = wasOffline;
    }
    else if (gOfflineStartupMode == kAskForOnlineState)
    {
      var bundle = Services.strings.createBundle("chrome://messenger/locale/offlineStartup.properties");
      var title = bundle.GetStringFromName("title");
      var desc = bundle.GetStringFromName("desc");
      var button0Text = bundle.GetStringFromName("workOnline");
      var button1Text = bundle.GetStringFromName("workOffline");
      var checkVal = {value:0};

      var result = Services.prompt.confirmEx(null, title, desc,
        (Services.prompt.BUTTON_POS_0 * Services.prompt.BUTTON_TITLE_IS_STRING) +
        (Services.prompt.BUTTON_POS_1 * Services.prompt.BUTTON_TITLE_IS_STRING),
        button0Text, button1Text, null, null, checkVal);
      debug ("result = " + result + "\n");
      Services.io.manageOfflineStatus = manageOfflineStatus && result != 1;
      Services.io.offline = result == 1;
      if (result != 1 && wasOffline)
        Services.prefs.setBoolPref("mailnews.playback_offline", true);
    }
  },

  observe: function(aSubject, aTopic, aData)
  {
    debug("observe: " + aTopic);

    if (aTopic == "profile-change-net-teardown")
    {
      debug("remembering offline state");
      Services.prefs.setBoolPref("network.online", !Services.io.offline);
    }
    else if (aTopic == "app-startup")
    {
      Services.obs.addObserver(this, "profile-after-change", false);
      Services.obs.addObserver(this, "profile-change-net-teardown", false);
    }
    else if (aTopic == "profile-after-change")
    {
      this.onProfileStartup();
    }
  },


  QueryInterface: function(aIID)
  {
    if (aIID.equals(Components.interfaces.nsIObserver) ||
        aIID.equals(Components.interfaces.nsISupports))
      return this;

    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}

function nsOfflineStartupModule()
{
}

nsOfflineStartupModule.prototype =
{
  classID: Components.ID("3028a3c8-2165-42a4-b878-398da5d32736"),
  _xpcom_factory:
  {
    createInstance: function(aOuter, aIID)
    {
      if (aOuter != null)
        throw Components.results.NS_ERROR_NO_AGGREGATION;

      // return the singleton
      return nsOfflineStartup.QueryInterface(aIID);
    },

    lockFactory: function(aLock)
    {
      // quieten warnings
    }
  }
};

////////////////////////////////////////////////////////////////////////
//
//   Debug helper
//
////////////////////////////////////////////////////////////////////////
if (!kDebug)
  debug = function(m) {};
else
  debug = function(m) {dump("\t *** nsOfflineStartup: " + m + "\n");};

var components = [nsOfflineStartupModule];
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);