summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/content/msgAccountCentral.js
blob: 7574c19da33b57f56084db3c9a28ac7739a48f1c (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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 { MailUtils } = ChromeUtils.import("resource:///modules/MailUtils.jsm");
var { UIDensity } = ChromeUtils.import("resource:///modules/UIDensity.jsm");
var { UIFontSize } = ChromeUtils.import("resource:///modules/UIFontSize.jsm");

var gSelectedServer = null;
var gSelectedFolder = null;

window.addEventListener("DOMContentLoaded", OnInit);

/**
 * Set up the whole page depending on the selected folder/account.
 * The folder is passed in via the document URL.
 */
function OnInit() {
  let el = document.getElementById("setupTitle");

  document.l10n.setAttributes(el, "setup-title", {
    accounts: MailServices.accounts.accounts.length,
  });

  // Selected folder URI is passed as folderURI argument in the query string.
  let folderURI = decodeURIComponent(
    document.location.search.replace("?folderURI=", "")
  );
  gSelectedFolder = folderURI ? MailUtils.getExistingFolder(folderURI) : null;
  gSelectedServer = gSelectedFolder ? gSelectedFolder.server : null;

  if (gSelectedServer) {
    // Display and collapse items presented to the user based on account type
    updateAccountCentralUI();
  } else {
    // If there is no gSelectedServer, we are in a brand new profile.
    document.getElementById("headerFirstRun").hidden = false;
    document.getElementById("headerExistingAccounts").hidden = true;
    document.getElementById("version").textContent = Services.appinfo.version;

    // Update the style of the account setup buttons and area.
    let accountSection = document.getElementById("accountSetupSection");
    for (let btn of accountSection.querySelectorAll(".btn-hub")) {
      btn.classList.remove("btn-inline");
    }
    accountSection.classList.remove("zebra");

    document.getElementById("accountFeaturesSection").hidden = true;
  }

  UIDensity.registerWindow(window);
  UIFontSize.registerWindow(window);
}

/**
 * Show items in the AccountCentral page depending on the capabilities
 * of the given server.
 */
function updateAccountCentralUI() {
  // Set the account name.
  document.getElementById("accountName").textContent =
    gSelectedServer.prettyName;

  // Update the account logo.
  document
    .getElementById("accountLogo")
    .setAttribute("type", gSelectedServer.type);

  let exceptions = [];
  let protocolInfo = null;
  try {
    protocolInfo = gSelectedServer.protocolInfo;
  } catch (e) {
    exceptions.push(e);
  }

  // Is this a RSS account?
  let isRssAccount = gSelectedServer?.type == "rss";

  // Is this an NNTP account?
  let isNNTPAccount = gSelectedServer?.type == "nntp";

  // Is this a Local Folders account?
  const isLocalFoldersAccount = gSelectedServer?.type == "none";

  document
    .getElementById("readButton")
    .toggleAttribute("hidden", !getReadMessagesFolder());

  // It can compose messages.
  let showComposeMsgLink = false;
  try {
    showComposeMsgLink = protocolInfo && protocolInfo.showComposeMsgLink;
    document
      .getElementById("composeButton")
      .toggleAttribute("hidden", !showComposeMsgLink);
  } catch (e) {
    exceptions.push(e);
  }

  // It can subscribe to a newsgroup.
  document
    .getElementById("nntpSubscriptionButton")
    .toggleAttribute("hidden", !isNNTPAccount);

  // It can subscribe to an RSS feed.
  document
    .getElementById("rssSubscriptionButton")
    .toggleAttribute("hidden", !isRssAccount);

  // It can search messages.
  let canSearchMessages = false;
  try {
    canSearchMessages = gSelectedServer && gSelectedServer.canSearchMessages;
    document
      .getElementById("searchButton")
      .toggleAttribute("hidden", !canSearchMessages);
  } catch (e) {
    exceptions.push(e);
  }

  // It can create filters.
  let canHaveFilters = false;
  try {
    canHaveFilters = gSelectedServer && gSelectedServer.canHaveFilters;
    document
      .getElementById("filterButton")
      .toggleAttribute("hidden", !canHaveFilters);
  } catch (e) {
    exceptions.push(e);
  }

  // It can have End-to-end Encryption.
  document
    .getElementById("e2eButton")
    .toggleAttribute(
      "hidden",
      isNNTPAccount || isRssAccount || isLocalFoldersAccount
    );

  // Check if we collected any exception.
  while (exceptions.length) {
    console.error(
      "Error in setting AccountCentral Items: " + exceptions.pop() + "\n"
    );
  }
}

/**
 * For the selected server, check for new messges and display first
 * suitable folder (genrally Inbox) for reading.
 */
function readMessages() {
  const folder = getReadMessagesFolder();
  top.MsgGetMessage([folder]);
  parent.displayFolder(folder);
}

/**
 * Find the folder Read Messages should use.
 *
 * @returns {?nsIMsgFolder} folder to use, if we have a suitable one.
 */
function getReadMessagesFolder() {
  const folder = MailUtils.getInboxFolder(gSelectedServer);
  if (folder) {
    return folder;
  }
  // For feeds and nntp, show the first non-trash folder. Don't use Outbox.
  return gSelectedServer.rootFolder.descendants.find(
    f =>
      !(f.flags & Ci.nsMsgFolderFlags.Trash) &&
      !(f.flags & Ci.nsMsgFolderFlags.Queue)
  );
}

/**
 * Open the AccountManager to view the settings for a given account.
 *
 * @param {string} selectPage - The xhtml file name for the viewing page,
 *   null for the account main page, other pages are 'am-server.xhtml',
 *   'am-copies.xhtml', 'am-offline.xhtml', 'am-addressing.xhtml',
 *   'am-smtp.xhtml'
 */
function viewSettings(selectPage) {
  window.browsingContext.topChromeWindow.MsgAccountManager(
    selectPage,
    gSelectedServer
  );
}

/**
 * Bring up the search interface for selected account.
 */
function searchMessages() {
  top.document
    .getElementById("tabmail")
    .currentAbout3Pane.commandController.doCommand("cmd_searchMessages");
}

/**
 * Open the filters window.
 */
function createMsgFilters() {
  window.browsingContext.topChromeWindow.MsgFilters(null, gSelectedFolder);
}

/**
 * Open the subscribe dialog.
 */
function subscribe() {
  if (!gSelectedServer) {
    return;
  }
  if (gSelectedServer.type == "rss") {
    window.browsingContext.topChromeWindow.openSubscriptionsDialog(
      gSelectedServer.rootFolder
    );
  } else {
    window.browsingContext.topChromeWindow.MsgSubscribe(gSelectedFolder);
  }
}

/**
 * Open the target's url on an external browser.
 *
 * @param {Event} event - The keypress or click event.
 */
function openLink(event) {
  event.preventDefault();
  Cc["@mozilla.org/uriloader/external-protocol-service;1"]
    .getService(Ci.nsIExternalProtocolService)
    .loadURI(Services.io.newURI(event.target.href));
}