summaryrefslogtreecommitdiffstats
path: root/mailnews/base/prefs/content/am-server-advanced.js
blob: 9232f525ae0c0ab6aceba5cd21c0a5ccc069add3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource://gre/modules/Services.jsm");

// pull stuff out of window.arguments
var gServerSettings = window.arguments[0];

var gFirstDeferredAccount;
// initialize the controls with the "gServerSettings" argument

var gControls;
function getControls()
{
  if (!gControls)
    gControls = document.getElementsByAttribute("amsa_persist", "true");
  return gControls;
}

function getLocalFoldersAccount()
{
  return MailServices.accounts
    .FindAccountForServer(MailServices.accounts.localFoldersServer);
}

function onLoad()
{
  var prettyName = gServerSettings.serverPrettyName;

  if (prettyName)
    document.getElementById("serverPrettyName").value = 
      document.getElementById("bundle_prefs")
              .getFormattedString("forAccount", [prettyName]);

  if (gServerSettings.serverType == "imap")
  {
    document.getElementById("pop3Panel").hidden = true;
  }
  else if (gServerSettings.serverType == "pop3")
  {
    document.getElementById("imapPanel").hidden = true;
    let radioGroup = document.getElementById("folderStorage");

    gFirstDeferredAccount = gServerSettings.deferredToAccount;
    let folderPopup = document.getElementById("deferredServerPopup");

    // The current account should not be shown in the folder picker
    // of the "other account" option.
    folderPopup._teardown();
    folderPopup.setAttribute("excludeServers",
                             gServerSettings.account.incomingServer.key);
    folderPopup._ensureInitialized();

    if (gFirstDeferredAccount.length)
    {
      // The current account is deferred.
      let account = MailServices.accounts.getAccount(gFirstDeferredAccount);
      radioGroup.value = "otherAccount";
      folderPopup.selectFolder(account.incomingServer.rootFolder);
    }
    else
    {
      // Current account is not deferred.
      radioGroup.value = "currentAccount";
      // If there are no suitable accounts to defer to, then the menulist is
      // disabled by the picker with an appropriate message.
      folderPopup.selectFolder();
      if (gServerSettings.account.incomingServer.isDeferredTo) {
        // Some other account already defers to this account
        // therefore this one can't be deferred further.
        radioGroup.disabled = true;
      }
    }

    let picker = document.getElementById("deferredServerFolderPicker");
    picker.disabled = radioGroup.selectedIndex != 1;
  }

  var controls = getControls();

  for (var i = 0; i < controls.length; i++)
  {
    var slot = controls[i].id;
    if (slot in gServerSettings)
    {
      if (controls[i].localName == "checkbox")
        controls[i].checked = gServerSettings[slot];
      else
        controls[i].value = gServerSettings[slot];
    }
  }
}

function onOk()
{
  // Handle account deferral settings for POP3 accounts.
  if (gServerSettings.serverType == "pop3")
  {
    var radioGroup = document.getElementById("folderStorage");
    var gPrefsBundle = document.getElementById("bundle_prefs");
    let picker = document.getElementById("deferredServerFolderPicker");

    // This account wasn't previously deferred, but is now deferred.
    if (radioGroup.value != "currentAccount" && !gFirstDeferredAccount.length)
    {
      // If the user hasn't selected a folder, keep the default.
      if (!picker.selectedItem)
        return true;

      var confirmDeferAccount =
        gPrefsBundle.getString("confirmDeferAccountWarning");

      var confirmTitle = gPrefsBundle.getString("confirmDeferAccountTitle");

      if (!Services.prompt.confirm(window, confirmTitle, confirmDeferAccount))
        return false;
    }
    switch (radioGroup.value)
    {
      case "currentAccount":
        gServerSettings['deferredToAccount'] = "";
        break;
      case "otherAccount":
        let server = picker.selectedItem._folder.server;
        let account = MailServices.accounts.FindAccountForServer(server);
        gServerSettings['deferredToAccount'] = account.key;
        break;
    }
  }

  // Save the controls back to the "gServerSettings" array.
  var controls = getControls();
  for (var i = 0; i < controls.length; i++)
  {
    var slot = controls[i].id;
    if (slot in gServerSettings)
    {
      if (controls[i].localName == "checkbox")
        gServerSettings[slot] = controls[i].checked;
      else
        gServerSettings[slot] = controls[i].value;
    }
  }

  return true;
}


// Set radio element choices and picker states
function updateInboxAccount(enablePicker)
{
  document.getElementById("deferredServerFolderPicker").disabled = !enablePicker;
  document.getElementById("deferGetNewMail").disabled = !enablePicker;
}