summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/prefs/content/amUtils.js
blob: b63f27b6c4334b87a7b1df1576767b019263c727 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* -*- Mode: C++; tab-width: 4; 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/. */

/* import-globals-from am-smtp.js */

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

function BrowseForLocalFolders() {
  const nsIFilePicker = Ci.nsIFilePicker;
  const nsIFile = Ci.nsIFile;

  var currentFolderTextBox = document.getElementById("server.localPath");
  var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);

  fp.init(
    window,
    document
      .getElementById("browseForLocalFolder")
      .getAttribute("filepickertitle"),
    nsIFilePicker.modeGetFolder
  );

  var currentFolder = Cc["@mozilla.org/file/local;1"].createInstance(nsIFile);
  try {
    currentFolder.initWithPath(currentFolderTextBox.value);
    fp.displayDirectory = currentFolder;
  } catch (e) {
    console.error(
      `Failed to set folder path from value=${currentFolderTextBox.value}\n`
    );
  }

  fp.open(rv => {
    if (rv != nsIFilePicker.returnOK || !fp.file) {
      return;
    }
    // Retrieve the selected folder.
    let selectedFolder = fp.file;

    // Check if the folder can be used for mail storage.
    if (!top.checkDirectoryIsUsable(selectedFolder)) {
      return;
    }

    currentFolderTextBox.value = selectedFolder.path;
    currentFolderTextBox.dispatchEvent(new CustomEvent("change"));
  });
}

/**
 * Return server/folder name formatted with server name if needed.
 *
 * @param {nsIMsgFolder} aTargetFolder - nsIMsgFolder to format name for
   @returns {string} THe formatted name.
 *   If target.isServer then only its name is returned.
 *   Otherwise return the name as "<foldername> on <servername>".
 */
function prettyFolderName(aTargetFolder) {
  if (aTargetFolder.isServer) {
    return aTargetFolder.prettyName;
  }

  return document
    .getElementById("bundle_messenger")
    .getFormattedString("verboseFolderFormat", [
      aTargetFolder.prettyName,
      aTargetFolder.server.prettyName,
    ]);
}

/**
 * Checks validity of junk target server name and folder.
 *
 * @param {string} aTargetURI - The URI specification to check.
 * @param {boolean} aIsServer - true if the URI specifies only a server
 *   (without folder)
 *
 * @returns {string} the value of aTargetURI if it is valid (usable), otherwise null
 */
function checkJunkTargetFolder(aTargetURI, aIsServer) {
  try {
    // Does the target account exist?
    let targetServer;
    if (aIsServer) {
      targetServer = MailUtils.getOrCreateFolder(aTargetURI + "/Junk").server;
    } else {
      targetServer = MailUtils.getExistingFolder(aTargetURI).server;
    }

    // If the target server has deferred storage, Junk can't be stored into it.
    if (targetServer.rootFolder != targetServer.rootMsgFolder) {
      return null;
    }
  } catch (e) {
    return null;
  }

  return aTargetURI;
}

/**
 * Finds a usable target for storing Junk mail.
 * If the passed in server URI is not usable, choose Local Folders.
 *
 * @param {string} aTargetURI - The URI of a server or folder to try first
 * @param {boolean} aIsServer - true if the URI specifies only a server (without folder)
 *
 * @returns {string} the server/folder URI of a usable target for storing Junk
 */
function chooseJunkTargetFolder(aTargetURI, aIsServer) {
  let server = null;

  if (aTargetURI) {
    server = MailUtils.getOrCreateFolder(aTargetURI).server;
    if (
      !server.canCreateFoldersOnServer ||
      !server.canSearchMessages ||
      server.rootFolder != server.rootMsgFolder
    ) {
      server = null;
    }
  }
  if (!server) {
    server = MailServices.accounts.localFoldersServer;
  }

  return server.serverURI + (!aIsServer ? "/Junk" : "");
}

/**
 * Fixes junk target folders if they point to an invalid/unusable (e.g. deferred)
 * folder/account. Only returns the new safe values. It is up to the caller
 * to push them to the proper elements/prefs.
 *
 * @param {string} aSpamActionTargetAccount - The value of the
 *   server.*.spamActionTargetAccount pref value (URI).
 * @param {string} aSpamActionTargetFolder - The value of the
 *   server.*.spamActionTargetFolder pref value (URI).
 * @param {string} aProposedTarget - The URI of a new target to try.
 * @param {integer} aMoveTargetModeValue - The value of the
 *   server.*.moveTargetMode pref value (0/1).
 * @param {nsISpamSettings} aServerSpamSettings - The nsISpamSettings object
 *    of any server (used just for the MOVE_TARGET_MODE_* constants).
 * @param {boolean} aMoveOnSpam - The server.*.moveOnSpam pref value).
 *
 * @returns {object[]} an array containing:
 *   newTargetAccount new safe junk target account
 *   newTargetAccount new safe junk target folder
 *   newMoveOnSpam    new moveOnSpam value
 */
function sanitizeJunkTargets(
  aSpamActionTargetAccount,
  aSpamActionTargetFolder,
  aProposedTarget,
  aMoveTargetModeValue,
  aServerSpamSettings,
  aMoveOnSpam
) {
  // Check if folder targets are valid.
  aSpamActionTargetAccount = checkJunkTargetFolder(
    aSpamActionTargetAccount,
    true
  );
  if (!aSpamActionTargetAccount) {
    // If aSpamActionTargetAccount is not valid,
    // reset to default behavior to NOT move junk messages...
    if (aMoveTargetModeValue == aServerSpamSettings.MOVE_TARGET_MODE_ACCOUNT) {
      aMoveOnSpam = false;
    }

    // ... and find a good default target.
    aSpamActionTargetAccount = chooseJunkTargetFolder(aProposedTarget, true);
  }

  aSpamActionTargetFolder = checkJunkTargetFolder(
    aSpamActionTargetFolder,
    false
  );
  if (!aSpamActionTargetFolder) {
    // If aSpamActionTargetFolder is not valid,
    // reset to default behavior to NOT move junk messages...
    if (aMoveTargetModeValue == aServerSpamSettings.MOVE_TARGET_MODE_FOLDER) {
      aMoveOnSpam = false;
    }

    // ... and find a good default target.
    aSpamActionTargetFolder = chooseJunkTargetFolder(aProposedTarget, false);
  }

  return [aSpamActionTargetAccount, aSpamActionTargetFolder, aMoveOnSpam];
}

/**
 * Opens Preferences (Options) dialog on the Advanced pane, General tab
 * so that the user sees where the global receipts settings can be found.
 *
 * @param {string} aTBPaneId - Thunderbird pref paneID to open.
 * @param {string} aTBScrollPaneTo - Thunderbird ID of the element to scroll into view.
 * @param {any} aTBOtherArgs - Other arguments to send to the pref tab.
 * @param {string} aSMPaneId - Seamonkey pref pane to open.
 */
function openPrefsFromAccountManager(
  aTBPaneId,
  aTBScrollPaneTo,
  aTBOtherArgs,
  aSMPaneId
) {
  let win =
    Services.wm.getMostRecentWindow("mail:3pane") ||
    Services.wm.getMostRecentWindow("mail:messageWindow") ||
    Services.wm.getMostRecentWindow("msgcompose");
  if (!win) {
    return;
  }

  // If openOptionsDialog() exists, we are in Thunderbird.
  if (typeof win.openOptionsDialog == "function") {
    win.openOptionsDialog(aTBPaneId, aTBScrollPaneTo, aTBOtherArgs);
  }
  // If goPreferences() exists, we are in Seamonkey.
  if (typeof win.goPreferences == "function") {
    win.goPreferences(aSMPaneId);
  }
}

/**
 * Check if the given account name already exists in any account.
 *
 * @param {string} aAccountName - The account name string to look for.
 * @param {string} [aAccountKey] - The key of an account that is skipped when
 *   searching the name. If unset, do not skip any account.
 */
function accountNameExists(aAccountName, aAccountKey) {
  for (let account of MailServices.accounts.accounts) {
    if (
      account.key != aAccountKey &&
      account.incomingServer &&
      aAccountName == account.incomingServer.prettyName
    ) {
      return true;
    }
  }

  return false;
}

/**
 * Open a dialog to edit properties of an SMTP server.
 *
 * @param {nsISmtpServer} aServer - The server to edit.
 * @returns {object} Object with result member to indicate whether 'OK'
 *   was clicked and addSmtpServer with key of newly created server.
 */
function editSMTPServer(aServer) {
  let args = { server: aServer, result: false, addSmtpServer: "" };

  let onCloseSMTPDialog = function () {
    if (args.result) {
      gSmtpServerListWindow.refreshServerList(aServer, true);
    }
  };

  parent.gSubDialog.open(
    "chrome://messenger/content/SmtpServerEdit.xhtml",
    { closingCallback: onCloseSMTPDialog },
    args
  );

  return args;
}