!import
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Communicator client code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
24 * Henrik Gemal <mozilla@gemal.dk>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 // be real hacky with document.getElementById until document.controls works
41 // with the new XUL widgets
42
43 var gSmtpUsername;
44 var gSmtpDescription;
45 var gSmtpUsernameLabel;
46 var gSmtpHostname;
47 var gSmtpPort;
48 var gSmtpUseUsername;
49 var gSmtpAuthMethod;
50 var gSmtpTrySSL;
51 var gSmtpPrefBranch;
52 var gPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
53 var gSmtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"].getService(Components.interfaces.nsISmtpService);
54 var gSavedUsername="";
55 var gPort;
56 var gDefaultPort;
57
initSmtpSettings
58 function initSmtpSettings(server) {
59
60 gSmtpUsername = document.getElementById("smtp.username");
61 gSmtpDescription = document.getElementById("smtp.description");
62 gSmtpUsernameLabel = document.getElementById("smtpusernamelabel");
63 gSmtpHostname = document.getElementById("smtp.hostname");
64 gSmtpPort = document.getElementById("smtp.port");
65 gSmtpUseUsername = document.getElementById("smtp.useUsername");
66 gSmtpAuthMethod = document.getElementById("smtp.authMethod");
67 gSmtpTrySSL = document.getElementById("smtp.trySSL");
68 gSmtpUseSecAuth = document.getElementById("smtp.useSecAuth");
69 gDefaultPort = document.getElementById("smtp.defaultPort");
70 gPort = document.getElementById("smtp.port");
71
72 if (server) {
73 gSmtpHostname.value = server.hostname;
74 gSmtpDescription.value = server.description;
75 gSmtpPort.value = server.port ? server.port : "";
76 gSmtpUsername.value = server.username;
77 gSmtpAuthMethod.setAttribute("value", server.authMethod);
78 gSmtpTrySSL.value = (server.trySSL < 4) ? server.trySSL : 1;
79 gSmtpUseSecAuth.checked = server.useSecAuth == "1";
80 } else {
81 gSmtpAuthMethod.setAttribute("value", "1");
82 gSmtpTrySSL.value = 1;
83 }
84
85 gSmtpUseUsername.checked = (gSmtpAuthMethod.getAttribute("value") == "1");
86
87 //dump("gSmtpAuthMethod = <" + gSmtpAuthMethod.localName + ">\n");
88 //dump("gSmtpAuthMethod.value = " + gSmtpAuthMethod.getAttribute("value") + "\n");
89
90 onUseUsername(gSmtpUseUsername, false);
91 selectProtocol(1);
92 if (gSmtpService.defaultServer)
93 onLockPreference();
94 }
95
96 // Disables xul elements that have associated preferences locked.
onLockPreference
97 function onLockPreference()
98 {
99 var defaultSmtpServerKey = gPrefBranch.getCharPref("mail.smtp.defaultserver");
100 var finalPrefString = "mail.smtpserver." + defaultSmtpServerKey + ".";
101
102 var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
103
104 var allPrefElements = {
105 hostname: gSmtpHostname,
106 description: gSmtpDescription,
107 port: gSmtpPort,
108 use_username: gSmtpUseUsername,
109 try_ssl: gSmtpTrySSL
110 };
111
112 gSmtpPrefBranch = prefService.getBranch(finalPrefString);
113 disableIfLocked( allPrefElements );
114 }
115
116 // Does the work of disabling an element given the array which contains xul id/prefstring pairs.
117 // Also saves the id/locked state in an array so that other areas of the code can avoid
118 // stomping on the disabled state indiscriminately.
disableIfLocked
119 function disableIfLocked( prefstrArray )
120 {
121 for (var prefstring in prefstrArray)
122 if (gSmtpPrefBranch.prefIsLocked(prefstring))
123 prefstrArray[prefstring].disabled = true;
124 }
125
saveSmtpSettings
126 function saveSmtpSettings(server)
127 {
128
129 if (gSmtpUseUsername.checked)
130 gSmtpAuthMethod.setAttribute("value", "1");
131 else
132 gSmtpAuthMethod.setAttribute("value", "0");
133
134 //dump("Saving to " + server + "\n");
135 if (server) {
136 server.hostname = gSmtpHostname.value;
137 server.description = gSmtpDescription.value;
138 server.port = gSmtpPort.value;
139 server.authMethod = (gSmtpUseUsername.checked ? 1 : 0);
140 //dump("Saved authmethod = " + server.authMethod +
141 // " but checked = " + gSmtpUseUsername.checked + "\n");
142 server.username = gSmtpUsername.value;
143 server.trySSL = gSmtpTrySSL.value;
144 server.useSecAuth = gSmtpUseSecAuth.checked;
145 }
146 }
147
onUseUsername
148 function onUseUsername(checkbox, dofocus)
149 {
150 if (checkbox.checked) {
151 // not only do we enable the elements when the check box is checked,
152 // but we also make sure that it's not disabled (ie locked) as well.
153 if (!checkbox.disabled) {
154 gSmtpUsername.removeAttribute("disabled");
155 gSmtpUsernameLabel.removeAttribute("disabled");
156 gSmtpUseSecAuth.removeAttribute("disabled");
157 }
158 if (dofocus)
159 gSmtpUsername.focus();
160 if (gSavedUsername && gSavedUsername != "")
161 gSmtpUsername.value = gSavedUsername;
162 } else {
163 gSavedUsername = gSmtpUsername.value;
164 gSmtpUsername.value = "";
165 gSmtpUsername.setAttribute("disabled", "true");
166 gSmtpUsernameLabel.setAttribute("disabled", "true");
167 gSmtpUseSecAuth.setAttribute("disabled", "true");
168 }
169 }
170
selectProtocol
171 function selectProtocol(init) {
172 var prevDefaultPort = gDefaultPort.value;
173
174 if (gSmtpTrySSL.value == 3) {
175 gDefaultPort.value = "465";
176 if(gPort.value == "" || (!init && gPort.value == "25" && prevDefaultPort != gDefaultPort.value))
177 gPort.value = gDefaultPort.value;
178 } else {
179 gDefaultPort.value = "25";
180 if(gPort.value == "" || (!init && gPort.value == "465" && prevDefaultPort != gDefaultPort.value))
181 gPort.value = gDefaultPort.value;
182 }
183 }