!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: Java; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 var gPrefInt = null;
3 var gCurrentDirectory = null;
4 var gReplicationBundle = null;
5 var gReplicationService =
6 Components.classes["@mozilla.org/addressbook/ldap-replication-service;1"].
7 getService(Components.interfaces.nsIAbLDAPReplicationService);
8 var gReplicationCancelled = false;
9 var gProgressText;
10 var gProgressMeter;
11 var gDownloadInProgress = false;
12
13 const kDefaultMaxHits = 100;
14 const kDefaultLDAPPort = 389;
15 const kDefaultSecureLDAPPort = 636;
16 const kLDAPDirectory = 0; // defined in nsDirPrefs.h
17
18 var ldapOfflineObserver = {
observe
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
19 observe: function(subject, topic, state)
20 {
21 // sanity checks
22 if (topic != "network:offline-status-changed") return;
23 setDownloadOfflineOnlineState(state == "offline");
24 }
25 }
26
Startup
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
27 function Startup()
28 {
29 gPrefInt = Components.classes["@mozilla.org/preferences-service;1"]
30 .getService(Components.interfaces.nsIPrefBranch);
31 gReplicationBundle = document.getElementById("bundle_replication");
32
33 document.getElementById("download").label =
34 gReplicationBundle.getString("downloadButton");
35 document.getElementById("download").accessKey =
36 gReplicationBundle.getString("downloadButton.accesskey");
37
38 if ( "arguments" in window && window.arguments[0] ) {
39 gCurrentDirectory = window.arguments[0].selectedDirectory;
40 try {
41 fillSettings();
42 } catch (ex) {
43 dump("pref-directory-add.js:Startup(): fillSettings() exception: "
44 + ex + "\n");
45 }
46
47 // Only set up the download button for online/offline status toggling
48 // if the pref isn't locked to disable the button.
49 if (!gPrefInt.prefIsLocked(gCurrentDirectory.dirPrefId +
50 ".disable_button_download")) {
51 // Now connect to the offline/online observer
52 var observerService = Components.classes["@mozilla.org/observer-service;1"]
53 .getService(Components.interfaces.nsIObserverService);
54 observerService.addObserver(ldapOfflineObserver,
55 "network:offline-status-changed", false);
56
57 // Now set the initial offline/online state.
58 var ioService = Components.classes["@mozilla.org/network/io-service;1"]
59 .getService(Components.interfaces.nsIIOService);
60 // And update the state
61 setDownloadOfflineOnlineState(ioService.offline);
62 }
63 } else {
64 fillDefaultSettings();
65 // Don't add observer here as it doesn't make any sense.
66 }
67 }
68
onUnload
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
69 function onUnload()
70 {
71 if ("arguments" in window &&
72 window.arguments[0] &&
73 !gPrefInt.prefIsLocked(gCurrentDirectory.dirPrefId +
74 ".disable_button_download")) {
75 // Remove the observer that we put in on dialog startup
76 var observerService = Components.classes["@mozilla.org/observer-service;1"]
77 .getService(Components.interfaces.nsIObserverService);
78 observerService.removeObserver(ldapOfflineObserver,
79 "network:offline-status-changed");
80 }
81 }
82
83 var progressListener = {
onStateChange
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
84 onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
85 {
86 if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START) {
87 // start the spinning
88 gProgressMeter.setAttribute("mode", "undetermined");
89 gProgressText.value = gReplicationBundle.getString(aStatus ?
90 "replicationStarted" :
91 "changesStarted");
92 gDownloadInProgress = true;
93 document.getElementById("download").label =
94 gReplicationBundle.getString("cancelDownloadButton");
95 document.getElementById("download").accessKey =
96 gReplicationBundle.getString("cancelDownloadButton.accesskey");
97 }
98
99 if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
100 EndDownload(aStatus);
101 }
102 },
onProgressChange
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
103 onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
104 {
105 gProgressText.value = gReplicationBundle.getFormattedString("currentCount",
106 [aCurSelfProgress]);
107 },
onLocationChange
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
108 onLocationChange: function(aWebProgress, aRequest, aLocation)
109 {
110 },
onStatusChange
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
111 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
112 {
113 },
onSecurityChange
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
114 onSecurityChange: function(aWebProgress, aRequest, state)
115 {
116 },
QueryInterface
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
117 QueryInterface : function(iid)
118 {
119 if (iid.equals(Components.interfaces.nsIWebProgressListener) ||
120 iid.equals(Components.interfaces.nsISupportsWeakReference) ||
121 iid.equals(Components.interfaces.nsISupports))
122 return this;
123 throw Components.results.NS_NOINTERFACE;
124 }
125 };
126
DownloadNow
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
127 function DownloadNow()
128 {
129 if (!gDownloadInProgress) {
130 gProgressText = document.getElementById("replicationProgressText");
131 gProgressMeter = document.getElementById("replicationProgressMeter");
132
133 gProgressText.hidden = false;
134 gProgressMeter.hidden = false;
135 gReplicationCancelled = false;
136
137 try {
138 if (gCurrentDirectory instanceof Components.interfaces.nsIAbLDAPDirectory)
139 gReplicationService.startReplication(gCurrentDirectory,
140 progressListener);
141 else
142 EndDownload(false);
143 }
144 catch (ex) {
145 EndDownload(false);
146 }
147 } else {
148 gReplicationCancelled = true;
149 try {
150 gReplicationService.cancelReplication(gCurrentDirectory.dirPrefId);
151 }
152 catch (ex) {
153 // XXX todo
154 // perhaps replication hasn't started yet? This can happen if you hit cancel after attempting to replication when offline
155 dump("unexpected failure while cancelling. ex=" + ex + "\n");
156 }
157 }
158 }
159
EndDownload
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
160 function EndDownload(aStatus)
161 {
162 document.getElementById("download").label =
163 gReplicationBundle.getString("downloadButton");
164 document.getElementById("download").accessKey =
165 gReplicationBundle.getString("downloadButton.accesskey");
166
167 // stop the spinning
168 gProgressMeter.setAttribute("mode", "normal");
169 gProgressMeter.setAttribute("value", "100");
170 gProgressMeter.hidden = true;
171
172 gDownloadInProgress = false;
173 gProgressText.value =
174 gReplicationBundle.getString(aStatus ? "replicationSucceeded" :
175 gReplicationCancelled ? "replicationCancelled" :
176 "replicationFailed");
177 }
178
179 // fill the settings panel with the data from the preferences.
180 //
fillSettings
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
181 function fillSettings()
182 {
183 document.getElementById("description").value = gCurrentDirectory.dirName;
184
185 if (gCurrentDirectory instanceof Components.interfaces.nsIAbLDAPDirectory) {
186 var ldapUrl = gCurrentDirectory.lDAPURL;
187
188 document.getElementById("results").value = gCurrentDirectory.maxHits;
189 document.getElementById("login").value = gCurrentDirectory.authDn;
190 document.getElementById("hostname").value = ldapUrl.host;
191 document.getElementById("port").value = ldapUrl.port;
192 document.getElementById("basedn").value = ldapUrl.dn;
193 document.getElementById("search").value = ldapUrl.filter;
194
195 var sub = document.getElementById("sub");
196 switch(ldapUrl.scope) {
197 case Components.interfaces.nsILDAPURL.SCOPE_ONELEVEL:
198 sub.radioGroup.selectedItem = document.getElementById("one");
199 break;
200 default:
201 sub.radioGroup.selectedItem = sub;
202 break;
203 }
204
205 if (ldapUrl.options & ldapUrl.OPT_SECURE)
206 document.getElementById("secure").setAttribute("checked", "true");
207 }
208
209 // check if any of the preferences for this server are locked.
210 //If they are locked disable them
211 DisableUriFields(gCurrentDirectory.dirPrefId + ".uri");
212 DisableElementIfPrefIsLocked(gCurrentDirectory.dirPrefId + ".description", "description");
213 DisableElementIfPrefIsLocked(gCurrentDirectory.dirPrefId + ".disable_button_download", "download");
214 DisableElementIfPrefIsLocked(gCurrentDirectory.dirPrefId + ".maxHits", "results");
215 DisableElementIfPrefIsLocked(gCurrentDirectory.dirPrefId + ".auth.dn", "login");
216 }
217
DisableElementIfPrefIsLocked
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
218 function DisableElementIfPrefIsLocked(aPrefName, aElementId)
219 {
220 if (gPrefInt.prefIsLocked(aPrefName))
221 document.getElementById(aElementId).setAttribute('disabled', true);
222 }
223
224 // disables all the text fields corresponding to the .uri pref.
DisableUriFields
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
225 function DisableUriFields(aPrefName)
226 {
227 if (gPrefInt.prefIsLocked(aPrefName)) {
228 var lockedElements = document.getElementsByAttribute("disableiflocked", "true");
229 for (var i=0; i<lockedElements.length; i++)
230 lockedElements[i].setAttribute('disabled', 'true');
231 }
232 }
233
onSecure
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
234 function onSecure()
235 {
236 document.getElementById("port").value =
237 document.getElementById("secure").checked ? kDefaultSecureLDAPPort :
238 kDefaultLDAPPort;
239 }
240
fillDefaultSettings
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
241 function fillDefaultSettings()
242 {
243 document.getElementById("port").value = kDefaultLDAPPort;
244 document.getElementById("results").value = kDefaultMaxHits;
245 var sub = document.getElementById("sub");
246 sub.radioGroup.selectedItem = sub;
247
248 // Disable the download button and add some text indicating why.
249 document.getElementById("download").disabled = true;
250 document.getElementById("downloadWarningMsg").hidden = false;
251 document.getElementById("downloadWarningMsg").textContent = document.
252 getElementById("bundle_addressBook").
253 getString("abReplicationSaveSettings");
254 }
255
hasOnlyWhitespaces
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
256 function hasOnlyWhitespaces(string)
257 {
258 // get all the whitespace characters of string and assign them to str.
259 // string is not modified in this function
260 // returns true if string contains only whitespaces and/or tabs
261 var re = /[ \s]/g;
262 var str = string.match(re);
263 if (str && (str.length == string.length))
264 return true;
265 else
266 return false;
267 }
268
hasCharacters
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
269 function hasCharacters(number)
270 {
271 var re = /[0-9]/g;
272 var num = number.match(re);
273 if(num && (num.length == number.length))
274 return false;
275 else
276 return true;
277 }
278
onAccept
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
279 function onAccept()
280 {
281 var addressbook = Components.classes["@mozilla.org/abmanager;1"]
282 .getService(Components.interfaces.nsIAbManager);
283
284 try {
285 var pref_string_content = "";
286 var pref_string_title = "";
287
288 var ldapUrl = Components.classes["@mozilla.org/network/ldap-url;1"];
289 ldapUrl = ldapUrl.createInstance().
290 QueryInterface(Components.interfaces.nsILDAPURL);
291
292 var description = document.getElementById("description").value;
293 var hostname = document.getElementById("hostname").value;
294 var port = document.getElementById("port").value;
295 var secure = document.getElementById("secure");
296 var results = document.getElementById("results").value;
297 var errorValue = null;
298 if ((!description) || hasOnlyWhitespaces(description))
299 errorValue = "invalidName";
300 else if ((!hostname) || hasOnlyWhitespaces(hostname))
301 errorValue = "invalidHostname";
302 // XXX write isValidDn and call it on the dn string here?
303 else if (port && hasCharacters(port))
304 errorValue = "invalidPortNumber";
305 else if (results && hasCharacters(results))
306 errorValue = "invalidResults";
307 if (!errorValue) {
308 ldapUrl.host = hostname;
309 ldapUrl.dn = document.getElementById("basedn").value;
310 ldapUrl.filter = document.getElementById("search").value;
311 if (!port) {
312 if (secure.checked)
313 ldapUrl.port = kDefaultSecureLDAPPort;
314 else
315 ldapUrl.port = kDefaultLDAPPort;
316 } else {
317 ldapUrl.port = port;
318 }
319 if (document.getElementById("one").selected) {
320 ldapUrl.scope = Components.interfaces.nsILDAPURL.SCOPE_ONELEVEL;
321 } else {
322 ldapUrl.scope = Components.interfaces.nsILDAPURL.SCOPE_SUBTREE;
323 }
324 if (secure.checked)
325 ldapUrl.options |= ldapUrl.OPT_SECURE;
326
327 // check if we are modifying an existing directory or adding a new directory
328 if (gCurrentDirectory) {
329 gCurrentDirectory.dirName = description;
330 gCurrentDirectory.lDAPURL = ldapUrl;
331 window.opener.gNewServerString = gCurrentDirectory.dirPrefId;
332 }
333 else { // adding a new directory
334 window.opener.gNewServerString =
335 addressbook.newAddressBook(description, ldapUrl.spec, kLDAPDirectory);
336 }
337
338 // the rdf service
339 var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
340 .getService(Components.interfaces.nsIRDFService);
341
342 // XXX This is really annoying - both new/modify Address Book don't
343 // give us back the new directory we just created - so go find it from
344 // rdf so we can set a few final things up on it.
345 var theDirectory = RDF.GetResource("moz-abldapdirectory://" +
346 window.opener.gNewServerString)
347 .QueryInterface(Components.interfaces.nsIAbLDAPDirectory);
348
349 theDirectory.maxHits = results;
350 theDirectory.authDn = document.getElementById("login").value;
351
352 window.opener.gNewServer = description;
353 // set window.opener.gUpdate to true so that LDAP Directory Servers
354 // dialog gets updated
355 window.opener.gUpdate = true;
356 } else {
357 var addressBookBundle = document.getElementById("bundle_addressBook");
358
359 var promptService = Components.
360 classes["@mozilla.org/embedcomp/prompt-service;1"].
361 getService(Components.interfaces.nsIPromptService);
362
363 promptService.alert(window,
364 document.title,
365 addressBookBundle.getString(errorValue));
366 return false;
367 }
368 } catch (outer) {
369 dump("Internal error in pref-directory-add.js:onAccept() " + outer + "\n");
370 }
371 return true;
372 }
373
onCancel
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
374 function onCancel()
375 {
376 window.opener.gUpdate = false;
377 }
378
379
380 // called by Help button in platform overlay
doHelpButton
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
381 function doHelpButton()
382 {
383 openHelp("mail-ldap-properties");
384 }
385
386 // Sets the download button state for offline or online.
387 // This function should only be called for ldap edit dialogs.
setDownloadOfflineOnlineState
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
388 function setDownloadOfflineOnlineState(isOffline)
389 {
390 if (isOffline)
391 {
392 // Disable the download button and add some text indicating why.
393 document.getElementById("downloadWarningMsg").textContent = document.
394 getElementById("bundle_addressBook").
395 getString("abReplicationOfflineWarning");
396 }
397 document.getElementById("downloadWarningMsg").hidden = !isOffline;
398 document.getElementById("download").disabled = isOffline;
399 }