!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 * Scott MacGregor <mscott@mozilla.org>
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 const nsIPromptService = Components.interfaces.nsIPromptService;
41 var smtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"].getService(Components.interfaces.nsISmtpService);
42
43 var gSmtpServerListWindow =
44 {
45 mBundle: null,
46 mServerList: null,
47 mAddButton: null,
48 mEditButton: null,
49 mDeleteButton: null,
50 mSetDefaultServerButton: null,
51
onLoad
52 onLoad: function()
53 {
54 parent.onPanelLoaded('am-smtp.xul');
55
56 this.mBundle = document.getElementById("bundle_messenger");
57 this.mServerList = document.getElementById("smtpList");
58 this.mAddButton = document.getElementById("addButton");
59 this.mEditButton = document.getElementById("editButton");
60 this.mDeleteButton = document.getElementById("deleteButton");
61 this.mSetDefaultServerButton = document.getElementById("setDefaultButton");
62
63 this.refreshServerList("", false);
64 },
65
onSelectionChanged
66 onSelectionChanged: function(aEvent)
67 {
68 if (this.mServerList.selectedItems.length <= 0)
69 return;
70
71 var server = this.getSelectedServer();
72 this.updateButtons(server);
73 this.updateServerInfoBox(server);
74 },
75
onDeleteServer
76 onDeleteServer: function (aEvent)
77 {
78 var server = this.getSelectedServer();
79 if (server)
80 {
81 // confirm deletion
82 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(nsIPromptService);
83 var cancel = promptService.confirmEx(window, this.mBundle.getString('smtpServers-confirmServerDeletionTitle'),
84 this.mBundle.getFormattedString('smtpServers-confirmServerDeletion', [server.hostname], 1),
85 nsIPromptService.STD_YES_NO_BUTTONS,
86 null, null, null, null, { });
87
88 if (!cancel)
89 {
90 smtpService.deleteSmtpServer(server);
91 parent.replaceWithDefaultSmtpServer(server.key);
92 this.refreshServerList("", true);
93 }
94 }
95 },
96
onAddServer
97 onAddServer: function (aEvent)
98 {
99 this.openServerEditor(null);
100 },
101
onEditServer
102 onEditServer: function (aEvent)
103 {
104 if (this.mServerList.selectedItems.length <= 0)
105 return;
106 this.openServerEditor(this.getSelectedServer());
107 },
108
onSetDefaultServer
109 onSetDefaultServer: function(aEvent)
110 {
111 if (this.mServerList.selectedItems.length <= 0)
112 return;
113
114 smtpService.defaultServer = this.getSelectedServer();
115 this.refreshServerList(smtpService.defaultServer.key, true);
116 },
117
updateButtons
118 updateButtons: function(aServer)
119 {
120 // can't delete default server
121 if (smtpService.defaultServer == aServer)
122 {
123 this.mSetDefaultServerButton.setAttribute("disabled", "true");
124 this.mDeleteButton.setAttribute("disabled", "true");
125 }
126 else
127 {
128 this.mSetDefaultServerButton.removeAttribute("disabled");
129 this.mDeleteButton.removeAttribute("disabled");
130 }
131 },
132
updateServerInfoBox
133 updateServerInfoBox: function(aServer)
134 {
135 var noneSelected = this.mBundle.getString("smtpServerList-NotSpecified");
136
137 document.getElementById('nameValue').value = aServer.hostname;
138 document.getElementById('descriptionValue').value = aServer.description || noneSelected;
139 document.getElementById('portValue').value = aServer.port;
140 document.getElementById('userNameValue').value = aServer.username || noneSelected;
141 document.getElementById('useSecureConnectionValue').value = this.mBundle.getString("smtpServer-SecureConnection-Type_" +
142 aServer.trySSL);
143 document.getElementById('useSecureAuthenticationValue').value = this.mBundle.getString("smtpServer-SecureAuthentication-Type-" +
144 aServer.useSecAuth);
145 },
146
refreshServerList
147 refreshServerList: function(aServerKeyToSelect, aFocusList)
148 {
149 // remove all children
150 while (this.mServerList.hasChildNodes())
151 this.mServerList.removeChild(this.mServerList.lastChild);
152
153 var defaultServer = smtpService.defaultServer;
154 this.fillSmtpServers(this.mServerList, smtpService.smtpServers, defaultServer);
155
156 if (aServerKeyToSelect)
157 this.mServerList.selectItem(this.mServerList.getElementsByAttribute("key", aServerKeyToSelect)[0]);
158 else // select the default server
159 this.mServerList.selectItem(this.mServerList.getElementsByAttribute("default", "true")[0]);
160
161 if (aFocusList)
162 this.mServerList.focus();
163 },
164
fillSmtpServers
165 fillSmtpServers: function(aListBox, aServers, aDefaultServer)
166 {
167 if (!aListBox || !aServers)
168 return;
169
170 while (aServers.hasMoreElements())
171 {
172 var server = aServers.getNext();
173
174 if (server instanceof Components.interfaces.nsISmtpServer)
175 {
176 var isDefault = (aDefaultServer.key == server.key);
177
178 var listitem = this.createSmtpListItem(server, isDefault);
179 aListBox.appendChild(listitem);
180 }
181 }
182 },
183
createSmtpListItem
184 createSmtpListItem: function(aServer, aIsDefault)
185 {
186 var listitem = document.createElement("listitem");
187 var serverName = "";
188
189 if (aServer.description)
190 serverName = aServer.description + ' - ';
191 else if (aServer.username)
192 serverName = aServer.username + ' - ';
193
194 serverName += aServer.hostname;
195
196 if (aIsDefault)
197 {
198 serverName += " " + this.mBundle.getString("defaultServerTag");
199 listitem.setAttribute("default", "true");
200 }
201
202 listitem.setAttribute("label", serverName);
203 listitem.setAttribute("key", aServer.key);
204 listitem.setAttribute("class", "smtpServerListItem");
205
206 // give it some unique id
207 listitem.id = "smtpServer." + aServer.key;
208 return listitem;
209 },
210
openServerEditor
211 openServerEditor: function(aServer)
212 {
213 var args = {server: aServer,
214 result: false,
215 addSmtpServer: ""};
216
217 window.openDialog("chrome://messenger/content/SmtpServerEdit.xul",
218 "smtpEdit", "chrome,titlebar,modal,centerscreen", args);
219
220 // now re-select the server which was just added
221 if (args.result)
222 this.refreshServerList(aServer ? aServer.key : args.addSmtpServer, true);
223
224 return args.result;
225 },
226
getSelectedServer
227 getSelectedServer: function()
228 {
229 var serverKey = this.mServerList.selectedItems[0].getAttribute("key");
230 return smtpService.getServerByKey(serverKey);
231 }
232 };