!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 /*
40 * here's how this dialog works:
41 * The main dialog contains a tree on the left (accounttree) and a
42 * deck on the right. Each card in the deck on the right contains an
43 * IFRAME which loads a particular preference document (such as am-main.xul)
44 *
45 * when the user clicks on items in the tree on the right, two things have
46 * to be determined before the UI can be updated:
47 * - the relevant account
48 * - the relevant page
49 *
50 * when both of these are known, this is what happens:
51 * - every form element of the previous page is saved in the account value
52 * hashtable for the previous account
53 * - the card containing the relevant page is brought to the front
54 * - each form element in the page is filled in with an appropriate value
55 * from the current account's hashtable
56 * - in the IFRAME inside the page, if there is an onInit() method,
57 * it is called. The onInit method can further update this page based
58 * on values set in the previous step.
59 */
60
61 var gSmtpHostNameIsIllegal = false;
62 var accountArray;
63 var gGenericAttributeTypes;
64 var accounttree;
65
66 var currentServerId;
67 var currentPageId;
68
69 var pendingServerId;
70 var pendingPageId;
71 var gPrefsBundle;
72 var gBrandBundle;
73
74 // services used
75 var RDF;
76 var accountManager;
77 var smtpService;
78 var nsPrefBranch;
79 var gPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
80
81 // widgets
82 var duplicateButton;
83 var removeButton;
84 var addAccountButton;
85 var setDefaultButton;
86
87 // This sets an attribute in a xul element so that we can later
88 // know what value to substitute in a prefstring. Different
89 // preference types set different attributes. We get the value
90 // in the same way as the function getAccountValue() determines it.
updateElementWithKeys
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
91 function updateElementWithKeys(account, element, type) {
92 switch (type)
93 {
94 case "identity":
95 element["identitykey"] = account.defaultIdentity.key;
96 break;
97 case "pop3":
98 case "imap":
99 case "nntp":
100 case "server":
101 element["serverkey"] = account.incomingServer.key;
102 break;
103 case "smtp":
104 if (smtpService.defaultServer)
105 element["serverkey"] = smtpService.defaultServer.key;
106 break;
107 default:
108 // dump("unknown element type! "+type+"\n");
109 }
110 }
111
hideShowControls
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
112 function hideShowControls(serverType) {
113 var controls = document.getElementsByAttribute("hidefor", "*");
114 for (var controlNo = 0; controlNo < controls.length; controlNo++) {
115 var control = controls[controlNo];
116 var hideFor = control.getAttribute("hidefor");
117
118 // Hide unsupported server types using hideFor="servertype1,servertype2".
119 var hide = false;
120 var hideForTokens = hideFor.split(",");
121 for (var tokenNo = 0; tokenNo < hideForTokens.length; tokenNo++) {
122 if (hideForTokens[tokenNo] == serverType) {
123 hide = true;
124 break;
125 }
126 }
127
128 if (hide)
129 control.setAttribute("hidden", "true");
130 else
131 control.removeAttribute("hidden");
132 }
133 }
134
135 // called when the whole document loads
136 // perform initialization here
onLoad
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
137 function onLoad() {
138 gPrefsBundle = document.getElementById("bundle_prefs");
139 gBrandBundle = document.getElementById("bundle_brand");
140
141 var selectedServer;
142 var selectPage = null;
143 if ("arguments" in window && window.arguments[0]) {
144 selectedServer = window.arguments[0].server;
145 selectPage = window.arguments[0].selectPage;
146 }
147
148 accountArray = new Array;
149 gGenericAttributeTypes = new Array;
150
151 RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
152
153 accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
154
155 smtpService =
156 Components.classes["@mozilla.org/messengercompose/smtp;1"].getService(Components.interfaces.nsISmtpService);
157 accounttree = document.getElementById("accounttree");
158
159 var prefService = Components.classes["@mozilla.org/preferences-service;1"];
160 prefService = prefService.getService();
161 prefService=prefService.QueryInterface(Components.interfaces.nsIPrefService);
162 nsPrefBranch = prefService.getBranch(null);
163
164 addAccountButton = document.getElementById("addAccountButton");
165 duplicateButton = document.getElementById("duplicateButton");
166 removeButton = document.getElementById("removeButton");
167 setDefaultButton = document.getElementById("setDefaultButton");
168
169 setTimeout(selectServer, 0, selectedServer ? selectedServer.serverURI : "", selectPage);
170 }
171
selectServer
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
172 function selectServer(serverId, selectPage)
173 {
174 var selectedServer, selectedItem;
175
176 if (serverId)
177 selectedServer = document.getElementById(serverId);
178 if (!selectedServer)
179 selectedServer = getFirstAccount();
180
181 if (serverId && selectedServer && selectPage)
182 selectedItem = findSelectPage(selectedServer, selectPage);
183 if (!selectedItem)
184 selectedItem = selectedServer;
185
186 var index = accounttree.contentView.getIndexOfItem(selectedItem);
187 accounttree.view.selection.select(index);
188 accounttree.treeBoxObject.ensureRowIsVisible(index);
189
190 var lastItem = selectedServer.lastChild.lastChild;
191 if (lastItem.localName == "treeitem")
192 index = accounttree.contentView.getIndexOfItem(lastItem);
193
194 accounttree.treeBoxObject.ensureRowIsVisible(index);
195 }
196
findSelectPage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
197 function findSelectPage(selectServer, selectPage)
198 {
199 var children = selectServer.childNodes;
200 for (i=0; i < children.length; i++)
201 {
202 if (children[i].localName == "treechildren") {
203 var pageNodes = children[i].childNodes;
204 for (j=0; j < pageNodes.length; j++) {
205 if (pageNodes[j].localName == "treeitem") {
206 var page = pageNodes[j].getAttribute('PageTag');
207 if (page == selectPage) {
208 return pageNodes[j];
209 }
210 }
211 }
212 }
213 }
214 return null;
215 }
216
getFirstAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
217 function getFirstAccount()
218 {
219 var tree = document.getElementById("accounttree");
220 var firstItem = findFirstTreeItem(tree);
221
222 return firstItem;
223 }
224
findFirstTreeItem
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
225 function findFirstTreeItem(tree) {
226 var children = tree.childNodes;
227
228 var treechildren;
229 for (var i=0;i<children.length; i++) {
230 if (children[i].localName == "treechildren") {
231 treechildren = children[i];
232 break;
233 }
234 }
235
236 children = treechildren.childNodes;
237 for (i=0; i<children.length; i++) {
238 if (children[i].localName == "treeitem")
239 return children[i];
240 }
241 return null;
242 }
243
replaceWithDefaultSmtpServer
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
244 function replaceWithDefaultSmtpServer(deletedSmtpServerKey)
245 {
246 //First we replace the smtpserverkey in every identity
247
248 var allIdentities = accountManager.allIdentities;
249 var identitiesCount = allIdentities.Count();
250 for (var i = 0; i < identitiesCount; i++) {
251 var currentIdentity = allIdentities.QueryElementAt(i, Components.interfaces.nsIMsgIdentity);
252 if (currentIdentity.smtpServerKey == deletedSmtpServerKey)
253 currentIdentity.smtpServerKey = smtpService.defaultServer.key;
254 }
255
256 //When accounts have already been loaded in the panel then the first replacement will be
257 //overwritten when the accountvalues are written out from the pagedata.
258 //we get the loaded accounts and check to make sure that the account exists for the accountid
259 //and that it has a default identity associated with it (to exclude smtpservers and local folders)
260 //Then we check only for the identity[type] and smtpServerKey[slot]
261 //and replace that with the default smtpserverkey if necessary.
262
263 for (var accountid in accountArray) {
264 var account = getAccountFromServerId(accountid);
265 if(account && account.defaultIdentity) {
266 var accountValues = accountArray[accountid];
267 if (accountValues['identity']['smtpServerKey'] == deletedSmtpServerKey)
268 setAccountValue(accountValues,'identity', 'smtpServerKey', smtpService.defaultServer.key);
269 }
270 }
271 }
272
onAccept
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
273 function onAccept() {
274 // Check if user/host have been modified.
275 if (!checkUserServerChanges(true))
276 return false;
277
278 if (gSmtpHostNameIsIllegal) {
279 gSmtpHostNameIsIllegal = false;
280 return false;
281 }
282
283 onSave();
284 // hack hack - save the prefs file NOW in case we crash
285 try {
286 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
287 .getService(Components.interfaces.nsIPrefService);
288 prefs.savePrefFile(null);
289 } catch (ex) {
290 dump("Error saving prefs!\n");
291 }
292 return true;
293 }
294
295 // Check if the user and/or host names have been changed and
296 // if so check if the new names already exists for an account.
297 //
checkUserServerChanges
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
298 function checkUserServerChanges(showAlert) {
299
300 if (smtpService.defaultServer) {
301 try {
302 var smtpHostName = top.frames["contentFrame"].document.getElementById("smtp.hostname");
303 if (hostnameIsIllegal(smtpHostName.value)) {
304 var alertTitle = gBrandBundle.getString("brandShortName");
305 var alertMsg = gPrefsBundle.getString("enterValidHostname");
306
307 if (gPromptService)
308 gPromptService.alert(window, alertTitle, alertMsg);
309 else
310 window.alert(alertMsg);
311 gSmtpHostNameIsIllegal = true;
312 }
313 }
314 catch (ex) {}
315 }
316
317 var accountValues = getValueArrayFor(currentServerId);
318 if (!accountValues)
319 return true;
320 var pageElements = getPageFormElements();
321
322 if (pageElements == null) return true;
323
324 // Get the new username, hostname and type from the page
325 var newUser, newHost, newType, oldUser, oldHost;
326 var uIndx, hIndx;
327 for (var i=0; i<pageElements.length; i++) {
328 if (pageElements[i].id) {
329 var vals = pageElements[i].id.split(".");
330 if (vals.length >= 2) {
331 var type = vals[0];
332 var slot = vals[1];
333 //dump("In checkUserServerChanges() ***: accountValues[" + type + "][" + slot + "] = " + getFormElementValue(pageElements[i]) + "/" + accountValues[type][slot] + "\n");
334
335 // if this type doesn't exist (just removed) then return.
336 if (!(type in accountValues) || !accountValues[type]) return true;
337
338 if (slot == "realHostName") {
339 oldHost = accountValues[type][slot];
340 newHost = getFormElementValue(pageElements[i]);
341 hIndx = i;
342 }
343 else if (slot == "realUsername") {
344 oldUser = accountValues[type][slot];
345 newUser = getFormElementValue(pageElements[i]);
346 uIndx = i;
347 }
348 else if (slot == "type")
349 newType = getFormElementValue(pageElements[i]);
350 }
351 }
352 }
353
354 // There is no username defined for news so reset it.
355 if (newType == "nntp")
356 oldUser = newUser = "";
357
358 //dump("In checkUserServerChanges() *** Username = " + newUser + "/" + oldUser + "\n");
359 //dump("In checkUserServerChanges() *** Hostname = " + newHost + "/" + oldHost + "\n");
360 //dump("In checkUserServerChanges() *** Type = " + newType + "\n");
361
362 // If something is changed then check if the new user/host already exists.
363 if ( (oldUser != newUser) || (oldHost != newHost) ) {
364 var newServer = accountManager.findRealServer(newUser, newHost, newType, 0);
365 if (newServer) {
366 if (showAlert) {
367 var alertText = gPrefsBundle.getString("modifiedAccountExists");
368 window.alert(alertText);
369 }
370 // Restore the old values before return
371 if (newType != "nntp")
372 setFormElementValue(pageElements[uIndx], oldUser);
373 setFormElementValue(pageElements[hIndx], oldHost);
374 return false;
375 }
376
377 //dump("In checkUserServerChanges() Ah, Server not found !!!" + "\n");
378 // If username is changed remind users to change Your Name and Email Address.
379 // If serve name is changed and has defined filters then remind users to edit rules.
380 if (showAlert) {
381 var account = getAccountFromServerId(currentServerId);
382 var filterList;
383 if (account && (newType != "nntp")) {
384 var server = account.incomingServer;
385 filterList = server.getFilterList(null);
386 }
387 var userChangeText, serverChangeText;
388 if ( (oldHost != newHost) && (filterList != undefined) && filterList.filterCount )
389 serverChangeText = gPrefsBundle.getString("serverNameChanged");
390 if (oldUser != newUser)
391 userChangeText = gPrefsBundle.getString("userNameChanged");
392
393 if ( (serverChangeText != undefined) && (userChangeText != undefined) )
394 serverChangeText = serverChangeText + "\n\n" + userChangeText;
395 else
396 if (userChangeText != undefined)
397 serverChangeText = userChangeText;
398
399 if (serverChangeText != undefined)
400 window.alert(serverChangeText);
401 }
402 }
403 return true;
404 }
405
onSave
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
406 function onSave() {
407 if (pendingPageId) {
408 dump("ERROR: " + pendingPageId + " hasn't loaded yet! Not saving.\n");
409 return;
410 }
411
412 // make sure the current visible page is saved
413 savePage(currentServerId);
414
415 for (var accountid in accountArray) {
416 var account = getAccountFromServerId(accountid);
417 var accountValues = accountArray[accountid];
418
419 if (accountValues)
420 saveAccount(accountValues, account);
421 }
422 }
423
onAddAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
424 function onAddAccount() {
425 MsgAccountWizard();
426 }
427
ReloadSmtpPanel
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
428 function ReloadSmtpPanel()
429 {
430 var smtpUsername = top.frames["contentFrame"].document.getElementById("smtp.username");
431 var smtpHostname = top.frames["contentFrame"].document.getElementById("smtp.hostname");
432 var smtpPort = top.frames["contentFrame"].document.getElementById("smtp.port");
433 var smtpUseUsername = top.frames["contentFrame"].document.getElementById("smtp.useUsername");
434 var smtpAuthMethod = top.frames["contentFrame"].document.getElementById("smtp.authMethod");
435 var smtpTrySSL = top.frames["contentFrame"].document.getElementById("smtp.trySSL");
436
437 var defaultServer = smtpService.defaultServer;
438
439 smtpUsername.value = defaultServer.username;
440 smtpHostname.value = defaultServer.hostname;
441 smtpPort.value = defaultServer.port ? defaultServer.port : "";
442 smtpAuthMethod.setAttribute("value", defaultServer.authMethod);
443 if (smtpAuthMethod.getAttribute("value") == "1")
444 smtpUseUsername.checked = true;
445 var elements = smtpTrySSL.getElementsByAttribute("value", defaultServer.trySSL);
446 if (!elements.item(0))
447 elements = smtpTrySSL.getElementsByAttribute("value", "1");
448 smtpTrySSL.selectedItem = elements[0];
449 }
450
onDuplicateAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
451 function onDuplicateAccount() {
452 //dump("onDuplicateAccount\n");
453
454 if (duplicateButton.getAttribute("disabled") == "true") return;
455
456 var result = getServerIdAndPageIdFromTree(accounttree);
457 if (result) {
458 var canDuplicate = true;
459 var account = getAccountFromServerId(result.serverId);
460 if (account) {
461 var server = account.incomingServer;
462 var type = server.type;
463
464 var protocolinfo = Components.classes["@mozilla.org/messenger/protocol/info;1?type=" + type].getService(Components.interfaces.nsIMsgProtocolInfo);
465 canDuplicate = protocolinfo.canDuplicate;
466 }
467 else {
468 canDuplicate = false;
469 }
470
471 if (canDuplicate) {
472 try {
473 accountManager.duplicateAccount(account);
474 }
475 catch (ex) {
476 var alertText = gPrefsBundle.getString("failedDuplicateAccount");
477 window.alert(alertText);
478 }
479 }
480 }
481 }
482
onSetDefault
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
483 function onSetDefault(event) {
484 if (event.target.getAttribute("disabled") == "true") return;
485
486 var result = getServerIdAndPageIdFromTree(accounttree);
487 if (!result) return;
488
489 var account = getAccountFromServerId(result.serverId);
490 if (!account) return;
491
492 accountManager.defaultAccount = account;
493 setEnabled(setDefaultButton, false);
494 }
495
onRemoveAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
496 function onRemoveAccount(event) {
497 //dump("onRemoveAccount\n");
498
499 if (event.target.getAttribute("disabled") == "true") return;
500
501 var result = getServerIdAndPageIdFromTree(accounttree);
502 if (!result)
503 return;
504
505 if (result.serverId == "http://home.netscape.com/NC-rdf#PageTitleFakeAccount") {
506 nsPrefBranch.setBoolPref("mailnews.fakeaccount.show", false);
507
508 // select the default account
509 selectServer(null, null);
510 }
511
512 var account = getAccountFromServerId(result.serverId);
513 if (!account)
514 return;
515
516 var server = account.incomingServer;
517 var type = server.type;
518 var prettyName = server.prettyName;
519
520 var protocolinfo = Components.classes["@mozilla.org/messenger/protocol/info;1?type=" + type].getService(Components.interfaces.nsIMsgProtocolInfo);
521 var canDelete = protocolinfo.canDelete;
522 if (!canDelete) {
523 canDelete = server.canDelete;
524 }
525 if (!canDelete)
526 return;
527
528 var confirmRemoveAccount =
529 gPrefsBundle.getFormattedString("confirmRemoveAccount", [prettyName]);
530
531 var confirmTitle = gPrefsBundle.getString("confirmRemoveAccountTitle");
532
533 var promptService =
534 Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
535 getService(Components.interfaces.nsIPromptService);
536 if (!promptService ||
537 !promptService.confirm(window, confirmTitle, confirmRemoveAccount)) {
538 return;
539 }
540
541 try {
542 // clear cached data out of the account array
543 if (accountArray[result.serverId])
544 delete accountArray[result.serverId];
545 currentServerId = currentPageId = null;
546
547 accountManager.removeAccount(account);
548 selectServer(null, null);
549 }
550 catch (ex) {
551 dump("failure to remove account: " + ex + "\n");
552 var alertText = gPrefsBundle.getString("failedRemoveAccount");
553 window.alert(alertText);
554 }
555 }
556
saveAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
557 function saveAccount(accountValues, account)
558 {
559 var identity = null;
560 var server = null;
561
562 if (account) {
563 identity = account.defaultIdentity;
564 server = account.incomingServer;
565 }
566
567 for (var type in accountValues) {
568 var typeArray = accountValues[type];
569
570 for (var slot in typeArray) {
571 var dest;
572 try {
573 if (type == "identity")
574 dest = identity;
575 else if (type == "server")
576 dest = server;
577 else if (type == "pop3")
578 dest = server.QueryInterface(Components.interfaces.nsIPop3IncomingServer);
579 else if (type == "imap")
580 dest = server.QueryInterface(Components.interfaces.nsIImapIncomingServer);
581 else if (type == "none")
582 dest = server.QueryInterface(Components.interfaces.nsINoIncomingServer);
583 else if (type == "nntp")
584 dest = server.QueryInterface(Components.interfaces.nsINntpIncomingServer);
585 else if (type == "smtp")
586 dest = smtpService.defaultServer;
587
588 } catch (ex) {
589 // don't do anything, just means we don't support that
590 }
591 if (dest == undefined) continue;
592
593 if ((type in gGenericAttributeTypes) && (slot in gGenericAttributeTypes[type])) {
594 switch (gGenericAttributeTypes[type][slot]) {
595 case "int":
596 if (dest.getIntAttribute(slot) != typeArray[slot])
597 dest.setIntAttribute(slot, typeArray[slot]);
598 break;
599 case "wstring":
600 if (dest.getUnicharAttribute(slot) != typeArray[slot])
601 dest.setUnicharAttribute(slot, typeArray[slot]);
602 break;
603 case "string":
604 if (dest.getCharAttribute(slot) != typeArray[slot])
605 dest.setCharAttribute(slot, typeArray[slot]);
606 break;
607 case "bool":
608 // in some cases
609 // like for radiogroups of type boolean
610 // the value will be "false" instead of false
611 // we need to convert it.
612 if (typeArray[slot] == "false")
613 typeArray[slot] = false;
614 else if (typeArray[slot] == "true")
615 typeArray[slot] = true;
616
617 if (dest.getBoolAttribute(slot) != typeArray[slot])
618 dest.setBoolAttribute(slot, typeArray[slot]);
619 break;
620 default:
621 dump("unexpected preftype: " + preftype + "\n");
622 break;
623 }
624 }
625 else {
626 if (slot in dest && typeArray[slot] != undefined && dest[slot] != typeArray[slot]) {
627 try {
628 dest[slot] = typeArray[slot];
629 }
630 catch (ex) {
631 // hrm... need to handle special types here
632 }
633 }
634 }
635 }
636 }
637
638 // if we made account changes to the spam settings, we'll need to re-initialize
639 // our settings object
640 if (server && server.spamSettings)
641 server.spamSettings.initialize(server);
642 }
643
updateButtons
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
644 function updateButtons(tree,serverId) {
645 var canCreate = true;
646 var canDuplicate = true;
647 var canDelete = true;
648 var canSetDefault = true;
649
650 //dump("updateButtons\n");
651 //dump("serverId = " + serverId + "\n");
652 var account = getAccountFromServerId(serverId);
653 //dump("account = " + account + "\n");
654
655 if (account) {
656 var server = account.incomingServer;
657 var type = server.type;
658
659 if (account == accountManager.defaultAccount ||
660 !server.canBeDefaultServer ||
661 account.identities.Count() < 1)
662 canSetDefault = false;
663
664 //dump("servertype = " + type + "\n");
665
666 var protocolinfo = Components.classes["@mozilla.org/messenger/protocol/info;1?type=" + type].getService(Components.interfaces.nsIMsgProtocolInfo);
667 canDuplicate = protocolinfo.canDuplicate;
668 canDelete = protocolinfo.canDelete;
669 if (!canDelete) {
670 canDelete = server.canDelete;
671 }
672 }
673 else {
674 // HACK
675 // if account is null, we have either selected a SMTP server, or there is a problem
676 // either way, we don't want the user to be able to delete it or duplicate it
677
678 canSetDefault = false;
679 canDelete = (serverId == "http://home.netscape.com/NC-rdf#PageTitleFakeAccount");
680 canDuplicate = false;
681 }
682
683 if (tree.view.selection.count < 1)
684 canDuplicate = canSetDefault = canDelete = false;
685
686 // check for disabled preferences on the account buttons.
687 // Not currently handled by WSM or the main loop yet since these buttons aren't
688 // under the IFRAME
689 if (nsPrefBranch.prefIsLocked(addAccountButton.getAttribute("prefstring")))
690 canCreate = false;
691 if (nsPrefBranch.prefIsLocked(setDefaultButton.getAttribute("prefstring")))
692 canSetDefault = false;
693 if (nsPrefBranch.prefIsLocked(removeButton.getAttribute("prefstring")))
694 canDelete = false;
695
696 setEnabled(addAccountButton, canCreate);
697 setEnabled(duplicateButton, canDuplicate);
698 setEnabled(setDefaultButton, canSetDefault);
699 setEnabled(removeButton, canDelete);
700
701 }
702
setEnabled
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
703 function setEnabled(control, enabled)
704 {
705 if (!control) return;
706 if (enabled)
707 control.removeAttribute("disabled");
708 else
709 control.setAttribute("disabled", true);
710 }
711
712 //
713 // Called when someone clicks on an account. Figure out context by what they
714 // clicked on. This is also called when an account is removed. In this case,
715 // nothing is selected.
716 //
onAccountClick
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
717 function onAccountClick(tree)
718 {
719 var currentSelection = getServerIdAndPageIdFromTree(tree);
720
721 // Nothing is selected. This is the case when an account was removed.
722 // A call of selectServer(null,null) after the removal ensures that
723 // onAccountClick() is called again after that with a valid selection.
724 if (!currentSelection)
725 return;
726
727 if (showPage(currentSelection.serverId, currentSelection.pageId))
728 updateButtons(tree, currentSelection.serverId);
729 }
730
731 // show the page for the given server:
732 // - save the old values
733 // - start loading the new page
showPage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
734 function showPage(serverId, pageId)
735 {
736 if (pageId == currentPageId &&
737 serverId == currentServerId)
738 return false;
739
740 // check if user/host names have been changed
741 checkUserServerChanges(false);
742
743 if (gSmtpHostNameIsIllegal) {
744 gSmtpHostNameIsIllegal = false;
745 selectServer(currentServerId, currentPageId);
746 return false;
747 }
748
749 // save the previous page
750 savePage(currentServerId);
751
752 var changeServerId = (serverId != currentServerId);
753 // loading a complete different page
754 if (pageId != currentPageId) {
755
756 // prevent overwriting with bad stuff
757 currentServerId = currentPageId = null;
758
759 pendingServerId=serverId;
760 pendingPageId=pageId;
761 loadPage(pageId);
762 }
763
764 // same page, different server
765 else if (changeServerId) {
766 restorePage(pageId, serverId);
767 }
768 return changeServerId;
769 }
770
771 // page has loaded
onPanelLoaded
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
772 function onPanelLoaded(pageId) {
773 if (pageId != pendingPageId) {
774
775 // if we're reloading the current page, we'll assume the
776 // page has asked itself to be completely reloaded from
777 // the prefs. to do this, clear out the the old entry in
778 // the account data, and then restore theh page
779 if (pageId == currentPageId) {
780 clearAccountData(currentServerId, currentPageId);
781 restorePage(currentPageId, currentServerId);
782 }
783 } else {
784
785 restorePage(pendingPageId, pendingServerId);
786 }
787
788 // probably unnecessary, but useful for debugging
789 pendingServerId = null;
790 pendingPageId = null;
791 }
792
loadPage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
793 function loadPage(pageId)
794 {
795 var chromePackageName;
796 try
797 {
798 // we could compare against "main","server","copies","offline","addressing",
799 // "smtp","advanced", and "fakeaccount" first to save the work, but don't
800 // as some of these might be turned into extensions (for thunderbird)
801 chromePackageName = accountManager.getChromePackageName(pageId.split("am-")[1].split(".xul")[0]);
802 }
803 catch (ex)
804 {
805 chromePackageName = "messenger";
806 }
807 const LOAD_FLAGS_NONE = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
808 document.getElementById("contentFrame").webNavigation.loadURI("chrome://" + chromePackageName + "/content/" + pageId, LOAD_FLAGS_NONE, null, null, null);
809 }
810
811 //
812 // save the values of the widgets to the given server
813 //
savePage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
814 function savePage(serverId)
815 {
816 if (!serverId)
817 return;
818
819 // tell the page that it's about to save
820 if ("onSave" in top.frames["contentFrame"])
821 top.frames["contentFrame"].onSave();
822
823 var accountValues = getValueArrayFor(serverId);
824 if (!accountValues)
825 return;
826
827 var pageElements = getPageFormElements();
828 if (!pageElements)
829 return;
830
831 // store the value in the account
832 for (var i=0; i<pageElements.length; i++) {
833 if (pageElements[i].id) {
834 var vals = pageElements[i].id.split(".");
835 if (vals.length >= 2) {
836 var type = vals[0];
837 var slot = vals[1];
838
839 setAccountValue(accountValues,
840 type, slot,
841 getFormElementValue(pageElements[i]));
842 }
843 }
844 }
845 }
846
setAccountValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
847 function setAccountValue(accountValues, type, slot, value) {
848 if (!(type in accountValues))
849 accountValues[type] = new Array;
850
851 //dump("Form->Array: accountValues[" + type + "][" + slot + "] = " + value + "\n");
852
853 accountValues[type][slot] = value;
854 }
855
getAccountValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
856 function getAccountValue(account, accountValues, type, slot, preftype, isGeneric) {
857 if (!(type in accountValues))
858 accountValues[type] = new Array;
859
860 // fill in the slot from the account if necessary
861 if (!(slot in accountValues[type]) || accountValues[type][slot] == undefined) {
862 // dump("Array->Form: lazily reading in the " + slot + " from the " + type + "\n");
863 var server;
864 if (account)
865 server= account.incomingServer;
866 var source = null;
867 try {
868 if (type == "identity")
869 source = account.defaultIdentity;
870 else if (type == "server")
871 source = account.incomingServer;
872 else if (type == "pop3")
873 source = server.QueryInterface(Components.interfaces.nsIPop3IncomingServer);
874 else if (type == "imap")
875 source = server.QueryInterface(Components.interfaces.nsIImapIncomingServer);
876 else if (type == "none")
877 source = server.QueryInterface(Components.interfaces.nsINoIncomingServer);
878 else if (type == "nntp")
879 source = server.QueryInterface(Components.interfaces.nsINntpIncomingServer);
880 else if (type == "smtp")
881 source = smtpService.defaultServer;
882 } catch (ex) {
883 }
884
885 if (source) {
886 if (isGeneric) {
887 if (!(type in gGenericAttributeTypes))
888 gGenericAttributeTypes[type] = new Array;
889
890 // we need the preftype later, for setting when we save.
891 gGenericAttributeTypes[type][slot] = preftype;
892 switch (preftype) {
893 case "int":
894 accountValues[type][slot] = source.getIntAttribute(slot);
895 break;
896 case "wstring":
897 accountValues[type][slot] = source.getUnicharAttribute(slot);
898 break;
899 case "string":
900 accountValues[type][slot] = source.getCharAttribute(slot);
901 break;
902 case "bool":
903 accountValues[type][slot] = source.getBoolAttribute(slot);
904 break;
905 default:
906 dump("unexpected preftype: " + preftype + "\n");
907 break;
908 }
909 }
910 else if (slot in source) {
911 accountValues[type][slot] = source[slot];
912 } else {
913 accountValues[type][slot] = null;
914 }
915 }
916 else {
917 accountValues[type][slot] = null;
918 }
919 }
920 var value = accountValues[type][slot];
921 //dump("Array->Form: accountValues[" + type + "][" + slot + "] = " + value + "\n");
922 return value;
923 }
924
925 //
926 // restore the values of the widgets from the given server
927 //
restorePage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
928 function restorePage(pageId, serverId)
929 {
930 if (!serverId)
931 return;
932
933 var accountValues = getValueArrayFor(serverId);
934 if (!accountValues)
935 return;
936
937 var pageElements = getPageFormElements();
938 if (!pageElements)
939 return;
940
941 var account = getAccountFromServerId(serverId);
942
943 if ("onPreInit" in top.frames["contentFrame"])
944 top.frames["contentFrame"].onPreInit(account, accountValues);
945
946 // restore the value from the account
947 for (var i=0; i<pageElements.length; i++) {
948 if (pageElements[i].id) {
949 var vals = pageElements[i].id.split(".");
950 if (vals.length >= 2) {
951 var type = vals[0];
952 var slot = vals[1];
953 // buttons are lockable, but don't have any data so we skip that part.
954 // elements that do have data, we get the values at poke them in.
955 if (pageElements[i].localName != "button") {
956 var value = getAccountValue(account, accountValues, type, slot, pageElements[i].getAttribute("preftype"), (pageElements[i].getAttribute("genericattr") == "true"));
957 setFormElementValue(pageElements[i], value);
958 }
959 updateElementWithKeys(account,pageElements[i],type);
960 var isLocked = getAccountValueIsLocked(pageElements[i]);
961 setEnabled(pageElements[i],!isLocked);
962 }
963 }
964 }
965
966 // tell the page that new values have been loaded
967 if ("onInit" in top.frames["contentFrame"])
968 top.frames["contentFrame"].onInit(pageId, serverId);
969
970 // everything has succeeded, vervied by setting currentPageId
971 currentPageId = pageId;
972 currentServerId = serverId;
973
974 }
975
976 //
977 // gets the value of a widget
978 //
getFormElementValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
979 function getFormElementValue(formElement) {
980 try {
981 var type = formElement.localName;
982 if (type=="checkbox") {
983 if (formElement.getAttribute("reversed"))
984 return !formElement.checked;
985 return formElement.checked;
986 }
987 if (type == "textbox" &&
988 formElement.getAttribute("datatype") == "nsILocalFile") {
989 if (formElement.value) {
990 var localfile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
991
992 localfile.initWithPath(formElement.value);
993 return localfile;
994 }
995 return null;
996 }
997 if (type == "text") {
998 var val = formElement.getAttribute("value");
999 if (val) return val;
1000 return null;
1001 }
1002 if ("value" in formElement) {
1003 return formElement.value;
1004 }
1005 return null;
1006 }
1007 catch (ex) {
1008 dump("getFormElementValue failed, ex="+ex+"\n");
1009 }
1010 return null;
1011 }
1012
1013 //
1014 // sets the value of a widget
1015 //
setFormElementValue
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1016 function setFormElementValue(formElement, value) {
1017
1018 //formElement.value = formElement.defaultValue;
1019 // formElement.checked = formElement.defaultChecked;
1020 var type = formElement.localName;
1021 if (type == "checkbox") {
1022 if (value == undefined) {
1023 if ("defaultChecked" in formElement && formElement.defaultChecked)
1024 formElement.checked = formElement.defaultChecked;
1025 else
1026 formElement.checked = false;
1027 } else {
1028 if (formElement.getAttribute("reversed"))
1029 formElement.checked = !value;
1030 else
1031 formElement.checked = value;
1032 }
1033 }
1034
1035 else if (type == "radiogroup" || type =="menulist") {
1036
1037 var selectedItem;
1038 if (value == undefined) {
1039 if (type == "radiogroup")
1040 selectedItem = formElement.firstChild;
1041 else
1042 selectedItem = formElement.firstChild.firstChild;
1043 }
1044 else
1045 selectedItem = formElement.getElementsByAttribute("value", value)[0];
1046
1047 formElement.selectedItem = selectedItem;
1048 }
1049 // handle nsILocalFile
1050 else if (type == "textbox" &&
1051 formElement.getAttribute("datatype") == "nsILocalFile") {
1052 if (value) {
1053 var localfile = value.QueryInterface(Components.interfaces.nsILocalFile);
1054 try {
1055 formElement.value = localfile.path;
1056 } catch (ex) {
1057 dump("Still need to fix uninitialized nsIFile problem!\n");
1058 }
1059
1060 } else {
1061 if ("defaultValue" in formElement)
1062 formElement.value = formElement.defaultValue;
1063 else
1064 formElement.value = "";
1065 }
1066 }
1067
1068 else if (type == "text") {
1069 if (value == null || value == undefined)
1070 formElement.removeAttribute("value");
1071 else
1072 formElement.setAttribute("value",value);
1073 }
1074
1075 // let the form figure out what to do with it
1076 else {
1077 if (value == undefined) {
1078 if ("defaultValue" in formElement && formElement.defaultValue)
1079 formElement.value = formElement.defaultValue;
1080 }
1081 else
1082 formElement.value = value;
1083 }
1084 }
1085
1086 //
1087 // conversion routines - get data associated
1088 // with a given pageId, serverId, etc
1089 //
1090
1091 // helper routine for account manager panels to get the current account for the selected server
getCurrentAccount
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1092 function getCurrentAccount()
1093 {
1094 var result = getServerIdAndPageIdFromTree(accounttree);
1095 return getAccountFromServerId(result.serverId);
1096 }
1097
1098 //
1099 // get the account associated with this serverId
1100 //
getAccountFromServerId
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1101 function getAccountFromServerId(serverId) {
1102 // get the account by dipping into RDF and then into the acount manager
1103 var serverResource = RDF.GetResource(serverId);
1104 try {
1105 var serverFolder =
1106 serverResource.QueryInterface(Components.interfaces.nsIMsgFolder);
1107 var incomingServer = serverFolder.server;
1108 var account = accountManager.FindAccountForServer(incomingServer);
1109 return account;
1110 } catch (ex) {
1111 }
1112 return null;
1113 }
1114
1115 //
1116 // get the array of form elements for the given page
1117 //
getPageFormElements
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1118 function getPageFormElements() {
1119 try {
1120 if("getElementsByAttribute" in top.frames["contentFrame"].document) {
1121 var pageElements =
1122 top.frames["contentFrame"].document.getElementsByAttribute("wsm_persist", "true");
1123 return pageElements;
1124 }
1125 }
1126 catch (ex) {
1127 dump("getPageFormElements() failed: " + ex + "\n");
1128 }
1129 return null;
1130 }
1131
1132 //
1133 // get the value array for the given serverId
1134 //
getValueArrayFor
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1135 function getValueArrayFor(serverId) {
1136 if (serverId == undefined) serverId="global";
1137
1138 if (!(serverId in accountArray)) {
1139 accountArray[serverId] = new Array;
1140 }
1141
1142 return accountArray[serverId];
1143 }
1144
clearAccountData
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1145 function clearAccountData(serverId, pageId)
1146 {
1147 accountArray[serverId] = null;
1148 }
1149
getServerIdAndPageIdFromTree
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1150 function getServerIdAndPageIdFromTree(tree)
1151 {
1152 var serverId = null;
1153
1154 if (tree.view.selection.count < 1) return null;
1155 var node = tree.contentView.getItemAtIndex(tree.currentIndex);
1156
1157 // get the page to load
1158 // (stored in the PageTag attribute of this node)
1159 var pageId = node.getAttribute('PageTag');
1160
1161 // get the server's Id
1162 // (stored in the Id attribute of the server node)
1163 var servernode = node.parentNode.parentNode;
1164
1165 // for toplevel treeitems, we just use the current treeitem
1166 // dump("servernode is " + servernode + "\n");
1167 if (servernode.localName != "treeitem") {
1168 servernode = node;
1169 }
1170 return {"serverId": servernode.id, "pageId": pageId }
1171 }