summaryrefslogtreecommitdiffstats
path: root/mailnews/base/prefs/content/removeAccount.js
blob: 350a0a0659952a2a3276f79c86b0d69a683d77ea (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
/* -*- 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");

var gServer;
var gDialog;

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

  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", Components.interfaces.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 Components.interfaces.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();
  window.sizeToContent();
}

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",
                                           "nsILocalFile", "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;
  }

  window.sizeToContent();
  gDialog.getButton("disclosure").disabled = true;
  gDialog.getButton("disclosure").blur();
}

function removeAccount() {
  let removeAccount = document.getElementById("removeAccount").checked;
  let removeData = document.getElementById("removeData").checked;
  try {
    // Remove the requested account data.
    if (removeAccount) {
      // Remove account
      // Need to save these before the account and its server is removed.
      let serverUri = gServer.type + "://" + gServer.hostName;
      let serverUsername = gServer.username;

      MailServices.accounts.removeAccount(window.arguments[0].account, removeData);
      window.arguments[0].result = true;

      // Remove password information.
      let logins = Services.logins.findLogins({}, serverUri, null, serverUri);
      for (let i = 0; i < logins.length; i++) {
        if (logins[i].username == serverUsername) {
          Services.logins.removeLogin(logins[i]);
          break;
        }
      }
    } else if (removeData) {
      // Remove files only.
      // TODO: bug 1302193
      window.arguments[0].result = false;
    }

    document.getElementById("status").selectedPanel =
      document.getElementById("success");
  } catch (ex) {
    document.getElementById("status").selectedPanel =
      document.getElementById("failure");
    Components.utils.reportError("Failure to remove account: " + ex);
    window.arguments[0].result = false;
  }
}

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

  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("infoPane").selectedIndex = 1;
  window.sizeToContent();

  removeAccount();

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