summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/prefs/content/removeAccount.js
blob: 724e45e1390808decc0bbf83bc75880dec771bfd (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
158
159
160
161
162
163
164
165
166
167
168
/* 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/. */

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gServer;
var gDialog;

window.addEventListener("DOMContentLoaded", onLoad);

document.addEventListener("dialogdisclosure", showInfo);
document.addEventListener("dialogaccept", onAccept);
document.subDialogSetDefaultFocus = isInitialFocus => {
  gDialog.getButton("cancel").focus();
  delete document.subDialogSetDefaultFocus;
};

function onLoad(event) {
  gServer = window.arguments[0].account.incomingServer;
  gDialog = document.querySelector("dialog");

  let bundle = document.getElementById("bundle_removeAccount");
  let removeQuestion = bundle.getFormattedString("removeQuestion", [
    gServer.prettyName,
  ]);
  document.getElementById("accountName").textContent = removeQuestion;

  // Allow to remove account data if it has a local storage.
  let localDirectory = gServer.localPath;
  if (localDirectory && localDirectory.exists()) {
    localDirectory.normalize();

    // Do not allow removal if localPath is outside of profile folder.
    let profilePath = Services.dirsvc.get("ProfD", Ci.nsIFile);
    profilePath.normalize();

    // TODO: bug 77652, decide what to do for deferred accounts.
    // And inform the user if the account localPath is outside the profile.
    if (
      gServer.isDeferredTo ||
      (gServer instanceof Ci.nsIPop3IncomingServer &&
        gServer.deferredToAccount) ||
      !profilePath.contains(localDirectory)
    ) {
      document.getElementById("removeData").disabled = true;
    }
  } else {
    document.getElementById("removeDataPossibility").collapsed = true;
  }

  if (gServer.type == "im") {
    let dataCheckbox = document.getElementById("removeData");
    dataCheckbox.label = dataCheckbox.getAttribute("labelChat");
    dataCheckbox.accessKey = dataCheckbox.getAttribute("accesskeyChat");
  }

  enableRemove();
}

function enableRemove() {
  gDialog.getButton("accept").disabled =
    !document.getElementById("removeAccount").checked &&
    !document.getElementById("removeData").checked;
}

/**
 * Show the local directory.
 */
function openLocalDirectory() {
  let nsLocalFile = Components.Constructor(
    "@mozilla.org/file/local;1",
    "nsIFile",
    "initWithPath"
  );
  let localDir = gServer.localPath.path;
  try {
    new nsLocalFile(localDir).reveal();
  } catch (e) {
    // Reveal may fail e.g. on Linux, then just show the path as a string.
    document.getElementById("localDirectory").value = localDir;
    document.getElementById("localDirectory").collapsed = false;
  }
}

function showInfo() {
  let descs = document.querySelectorAll("vbox.indent");
  for (let desc of descs) {
    desc.collapsed = false;
  }

  // TODO: bug 1238271, this should use showFor attributes if possible.
  if (gServer.type == "imap" || gServer.type == "nntp") {
    document.getElementById("serverAccount").collapsed = false;
  } else if (gServer.type == "im") {
    document.getElementById("chatAccount").collapsed = false;
  } else {
    document.getElementById("localAccount").collapsed = false;
  }

  parent.gSubDialog._topDialog.resizeDialog();
  gDialog.getButton("disclosure").disabled = true;
  gDialog.getButton("disclosure").blur();
}

function removeAccount() {
  let removeAccount = document.getElementById("removeAccount").checked;
  let removeData = document.getElementById("removeData").checked;
  let account = window.arguments[0].account;
  try {
    // Remove the requested account data.
    if (removeAccount) {
      try {
        // Remove password information first.
        account.incomingServer.forgetPassword();
      } catch (e) {
        /* It is OK if this fails. */
      }
      // Remove account
      MailServices.accounts.removeAccount(account, removeData);
      account = null;
      delete window.arguments[0].account;
      gServer = null;
      window.arguments[0].result = true;
    } else if (removeData) {
      // Remove files only.
      // TODO: bug 1302193
      window.arguments[0].result = false;
    }

    document.getElementById("success").hidden = false;
  } catch (ex) {
    document.getElementById("failure").hidden = false;
    console.error("Failure to remove account: " + ex);
    window.arguments[0].result = false;
  }
  document.getElementById("progress").hidden = true;
}

function onAccept(event) {
  // If Cancel is disabled, we already tried to remove the account
  // and can only close the dialog.
  if (gDialog.getButton("cancel").disabled) {
    return;
  }

  gDialog.getButton("accept").disabled = true;
  gDialog.getButton("cancel").disabled = true;
  gDialog.getButton("disclosure").disabled = true;

  // Change the "Remove" to an "OK" button by clearing the custom label.
  gDialog.removeAttribute("buttonlabelaccept");
  gDialog.removeAttribute("buttonaccesskeyaccept");
  gDialog.getButton("accept").removeAttribute("label");
  gDialog.getButton("accept").removeAttribute("accesskey");
  gDialog.buttons = "accept";

  document.getElementById("removeAccountSection").hidden = true;
  document.getElementById("confirmationSection").hidden = false;
  window.sizeToContent();

  removeAccount();

  gDialog.getButton("accept").disabled = false;
  event.preventDefault();
}