!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: C++; 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, released
16 * March 31, 1998.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998-1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 var gPrefs;
40 var gAddButton;
41 var gOKButton;
42 var gRemoveButton;
43 var gHeaderInputElement;
44 var gArrayHdrs;
45 var gHdrsList;
46 var gContainer;
47 var gFilterBundle=null;
48 var gCustomBundle=null;
49
onLoad
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
50 function onLoad()
51 {
52 gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
53 var hdrs;
54 try
55 {
56 hdrs = gPrefs.getCharPref("mailnews.customHeaders");
57 }
58 catch(ex)
59 {
60 hdrs =null;
61 }
62 gHeaderInputElement = document.getElementById("headerInput");
63 gHeaderInputElement.focus();
64
65 gHdrsList = document.getElementById("headerList");
66 gArrayHdrs = new Array();
67 gAddButton = document.getElementById("addButton");
68 gRemoveButton = document.getElementById("removeButton");
69 gOKButton = document.getElementById("ok");
70
71 initializeDialog(hdrs);
72
73 doSetOKCancel(onOk, null);
74
75 updateAddButton(true);
76 updateRemoveButton();
77
78 moveToAlertPosition();
79 }
80
initializeDialog
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
81 function initializeDialog(hdrs)
82 {
83 if (hdrs)
84 {
85 hdrs = hdrs.replace(/\s+/g,''); //remove white spaces before splitting
86 gArrayHdrs = hdrs.split(":");
87 for (var i = 0; i< gArrayHdrs.length; i++)
88 if (!gArrayHdrs[i])
89 gArrayHdrs.splice(i,1); //remove any null elements
90 initializeRows();
91 }
92 }
93
initializeRows
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
94 function initializeRows()
95 {
96 for (var i = 0; i< gArrayHdrs.length; i++)
97 addRow(TrimString(gArrayHdrs[i]));
98 }
99
onTextInput
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
100 function onTextInput()
101 {
102 // enable the add button if the user has started to type text
103 updateAddButton( (gHeaderInputElement.value == "") );
104 }
105
enterKeyPressed
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
106 function enterKeyPressed()
107 {
108 // if the add button is currently the default action then add the text
109 if (gHeaderInputElement.value != "" && !gAddButton.disabled)
110 {
111 onAddHeader();
112 }
113 else
114 {
115 // otherwise, the default action for the dialog is the OK button
116 if (! gOKButton.disabled)
117 doOKButton();
118 }
119 }
120
onOk
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
121 function onOk()
122 {
123 if (gArrayHdrs.length)
124 {
125 var hdrs;
126 if (gArrayHdrs.length == 1)
127 hdrs = gArrayHdrs;
128 else
129 hdrs = gArrayHdrs.join(": ");
130 gPrefs.setCharPref("mailnews.customHeaders", hdrs);
131 // flush prefs to disk, in case we crash, to avoid dataloss and problems with filters that use the custom headers
132 var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
133 prefService.savePrefFile(null);
134 }
135 else
136 {
137 try
138 {
139 gPrefs.clearUserPref("mailnews.customHeaders"); //clear the pref, no custom headers
140 }
141 catch(ex) {} //will throw an exception if there is no "mailnews.customHeaders" in prefs.js
142 }
143 window.close();
144 }
145
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
146 function customHeaderOverflow()
147 {
148 var nsMsgSearchAttrib = Components.interfaces.nsMsgSearchAttrib;
149 if (gArrayHdrs.length >= (nsMsgSearchAttrib.kNumMsgSearchAttributes - nsMsgSearchAttrib.OtherHeader - 1))
150 {
151 if (!gFilterBundle)
152 gFilterBundle = document.getElementById("bundle_filter");
153
154 var alertText = gFilterBundle.getString("customHeaderOverflow");
155 window.alert(alertText);
156 return true;
157 }
158 return false;
159 }
160
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
161 function onAddHeader()
162 {
163 var newHdr = TrimString(gHeaderInputElement.value);
164
165 if (!isRFC2822Header(newHdr)) // if user entered an invalid rfc822 header field name, bail out.
166 {
167 if (!gCustomBundle)
168 gCustomBundle = document.getElementById("bundle_custom");
169
170 var alertText = gCustomBundle.getString("colonInHeaderName");
171 window.alert(alertText);
172 return;
173 }
174
175 gHeaderInputElement.value = "";
176 if (!newHdr || customHeaderOverflow())
177 return;
178 if (!duplicateHdrExists(newHdr))
179 {
180 gArrayHdrs[gArrayHdrs.length] = newHdr;
181 var newItem = addRow(newHdr);
182 gHdrsList.selectItem (newItem); // make sure the new entry is selected in the tree
183 // now disable the add button
184 updateAddButton(true);
185 gHeaderInputElement.focus(); // refocus the input field for the next custom header
186 }
187 }
188
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
189 function isRFC2822Header(hdr)
190 {
191 var charCode;
192 for (var i=0; i< hdr.length; i++)
193 {
194 charCode = hdr.charCodeAt(i);
195 //58 is for colon and 33 and 126 are us-ascii bounds that should be used for header field name, as per rfc2822
196
197 if (charCode < 33 || charCode == 58 || charCode > 126)
198 return false;
199 }
200 return true;
201 }
202
duplicateHdrExists
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
203 function duplicateHdrExists(hdr)
204 {
205 for (var i=0;i<gArrayHdrs.length; i++)
206 {
207 if (gArrayHdrs[i] == hdr)
208 return true;
209 }
210 return false;
211 }
212
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
213 function onRemoveHeader()
214 {
215 var listitem = gHdrsList.selectedItems[0]
216 if (!listitem) return;
217 gHdrsList.removeChild(listitem);
218 var selectedHdr = GetListItemAttributeStr(listitem);
219 var j=0;
220 for (var i=0;i<gArrayHdrs.length; i++)
221 {
222 if (gArrayHdrs[i] == selectedHdr)
223 {
224 gArrayHdrs.splice(i,1);
225 break;
226 }
227 }
228 }
229
GetListItemAttributeStr
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
230 function GetListItemAttributeStr(listitem)
231 {
232 if (listitem)
233 return TrimString(listitem.getAttribute("label"));
234
235 return "";
236 }
237
addRow
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
238 function addRow(newHdr)
239 {
240 var listitem = document.createElement("listitem");
241 listitem.setAttribute("label", newHdr);
242 gHdrsList.appendChild(listitem);
243 return listitem;
244 }
245
updateAddButton
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
246 function updateAddButton(aDisable)
247 {
248 // only update the button if we absolutely have to...
249 if (aDisable != gAddButton.disabled)
250 {
251 gAddButton.disabled = aDisable;
252 if (aDisable)
253 {
254 gOKButton.setAttribute('default', true);
255 gAddButton.removeAttribute('default');
256 }
257 else
258 {
259 gOKButton.removeAttribute('default');
260 gAddButton.setAttribute('default', true);
261 }
262 }
263 }
264
updateRemoveButton
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
265 function updateRemoveButton()
266 {
267 var headerSelected = (gHdrsList.selectedItems.length > 0);
268 gRemoveButton.disabled = !headerSelected;
269 }
270
271 //Remove whitespace from both ends of a string
TrimString
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
272 function TrimString(string)
273 {
274 if (!string) return "";
275 return string.replace(/(^\s+)|(\s+$)/g, '')
276 }