summaryrefslogtreecommitdiffstats
path: root/comm/suite/components/pref/content/pref-proxies.js
blob: 5120c3f5d0762c9481d91979f5526e764850c86f (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
/* 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/. */

const kNoProxy = 0;
const kManualProxy = 1;
const kAutoConfigProxy = 2;
const kObsoleteProxy = 3;
const kAutoDiscoverProxy = 4;
const kSystemProxy = 5;

var gInstantApply;
var gHTTP;
var gHTTPPort;
var gSSL;
var gSSLPort;
var gFTP;
var gFTPPort;
var gAutoURL;
var gProxyType;
var gShareSettings;

// Only used by main prefwindow
function Startup()
{
  InitCommonGlobals();
  gAutoURL = document.getElementById("network.proxy.autoconfig_url");
  gProxyType = document.getElementById("network.proxy.type");

  // Check for system proxy settings class and unhide UI if present
  if ("@mozilla.org/system-proxy-settings;1" in Cc)
    document.getElementById("systemPref").hidden = false;

  // Calculate a sane default for network.proxy.share_proxy_settings.
  if (gShareSettings.value == null)
    gShareSettings.value = DefaultForShareSettingsPref();

  // The pref value 3 (kObsoleteProxy) for network.proxy.type is unused to
  // maintain backwards compatibility. Treat 3 (kObsoleteProxy) equally to
  // 0 (kNoProxy). See bug 115720.
  if (gProxyType.value == kObsoleteProxy)
    gProxyType.value = kNoProxy;

  DoEnabling();
}

// Only used by child prefwindow
function AdvancedInit()
{
  InitCommonGlobals();
  DoProxyCopy(gShareSettings.value);
}

function InitCommonGlobals()
{
  gInstantApply = document.documentElement.instantApply;
  gHTTP = document.getElementById("network.proxy.http");
  gHTTPPort = document.getElementById("network.proxy.http_port");
  gSSL = document.getElementById("network.proxy.ssl");
  gSSLPort = document.getElementById("network.proxy.ssl_port");
  gFTP = document.getElementById("network.proxy.ftp");
  gFTPPort = document.getElementById("network.proxy.ftp_port");
  gShareSettings = document.getElementById("network.proxy.share_proxy_settings");
}

// Returns true if all protocol specific proxies and all their
// ports are set to the same value, false otherwise.
function DefaultForShareSettingsPref()
{
  return gHTTP.value == gSSL.value &&
         gHTTP.value == gFTP.value &&
         gHTTPPort.value == gSSLPort.value &&
         gHTTPPort.value == gFTPPort.value;
}

function DoEnabling()
{
  // convenience arrays
  var manual = ["networkProxyHTTP", "networkProxyHTTP_Port",
                "networkProxyNone", "advancedButton"];
  var auto = ["networkProxyAutoconfigURL", "autoReload"];

  switch (gProxyType.value)
  {
    case kNoProxy:
    case kAutoDiscoverProxy:
    case kSystemProxy:
      Disable(manual);
      Disable(auto);
      break;
    case kManualProxy:
      Disable(auto);
      if (!gProxyType.locked)
        EnableUnlockedElements(manual, true);
      break;
    case kAutoConfigProxy:
    default:
      Disable(manual);
      if (!gProxyType.locked)
      {
        EnableElementById("networkProxyAutoconfigURL", true, false);
        EnableUnlockedButton(gAutoURL);
      }
      break;
  }
}

function Disable(aElementIds)
{
  for (var i = 0; i < aElementIds.length; i++)
    document.getElementById(aElementIds[i]).setAttribute("disabled", "true");
}

function EnableUnlockedElements(aElementIds, aEnable)
{
  for (var i = 0; i < aElementIds.length; i++)
    EnableElementById(aElementIds[i], aEnable, false);
}

function EnableUnlockedButton(aElement)
{
  var enable = gInstantApply ||
               (aElement.valueFromPreferences == aElement.value);
  EnableElementById("autoReload", enable, false);
}

function ReloadPAC() {
  // This reloads the PAC URL stored in preferences.
  // When not in instant apply mode, the button that calls this gets
  // disabled if the preference and what is showing in the UI differ.
  Cc["@mozilla.org/network/protocol-proxy-service;1"]
    .getService().reloadPAC();
}

function FixProxyURL(aURL)
{
  try
  {
    aURL.value =
      Services.uriFixup.createFixupURI(aURL.value,
                                       Ci.nsIURIFixup.FIXUP_FLAG_NONE).spec;
  }
  catch (e) {}

  if (!gInstantApply)
    EnableUnlockedButton(aURL);
}

function OpenAdvancedDialog()
{
  document.documentElement.openSubDialog("chrome://communicator/content/pref/pref-proxies-advanced.xul",
                                         "AdvancedProxyPreferences", null);
}

function DoProxyCopy(aChecked)
{
  DoProxyHostCopy(gHTTP.value);
  DoProxyPortCopy(gHTTPPort.value);
  var nonshare = ["networkProxySSL", "networkProxySSL_Port",
                  "networkProxyFTP", "networkProxyFTP_Port"];
  EnableUnlockedElements(nonshare, !aChecked);
}

function DoProxyHostCopy(aValue)
{
  if (!gShareSettings.value)
    return;

  gSSL.value = aValue;
  gFTP.value = aValue;
}

function DoProxyPortCopy(aValue)
{
  if (!gShareSettings.value)
    return;

  gSSLPort.value = aValue;
  gFTPPort.value = aValue;
}

function UpdateProxies()
{
  var noProxiesPref = document.getElementById("network.proxy.no_proxies_on");

  noProxiesPref.value = noProxiesPref.value.replace(/[;, \n]+/g, ", ")
                                           .replace(/^, |, $/g, "");
}