summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/src/ImapFolderContentHandler.sys.mjs
blob: da33c08829a7962a25cb37f3c5a71e529028851e (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
/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

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

const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
  MailUtils: "resource:///modules/MailUtils.jsm",
});

/**
 * A service for handling content type x-application-imapfolder;
 * that is, opening IMAP folder URLs.
 *
 * Set mailnews.imap.jsmodule to true to use this module.
 *
 * @implements {nsIContentHandler}
 */
export class ImapFolderContentHandler {
  QueryInterface = ChromeUtils.generateQI(["nsIContentHandler"]);

  /**
   * @param contentType - The content type of request.
   * @param windowContest - Window context, used to get things like the current
   *   nsIDOMWindow for this request.
   * @param request - A request whose content type is already known.
   * @see {nsIContentHandler}
   */
  handleContent(contentType, windowContext, request) {
    if (contentType != "x-application-imapfolder") {
      throw Components.Exception(
        `Won't handle ${contentType}`,
        Cr.NS_ERROR_WONT_HANDLE_CONTENT
      );
    }
    request = request.QueryInterface(Ci.nsIChannel);

    let imapFolderURL = Services.io.unescapeString(
      request.URI.spec,
      Ci.nsINetUtil.ESCAPE_URL_PATH
    );

    if (Services.wm.getMostRecentWindow("mail:3pane")) {
      // Clicked IMAP folder URL in the window.
      let folder = MailServices.folderLookup.getFolderForURL(imapFolderURL);
      if (folder) {
        lazy.MailUtils.displayFolderIn3Pane(folder.URI);
      } else {
        folder =
          MailServices.folderLookup.getOrCreateFolderForURL(imapFolderURL);
        // TODO: ask and maybe subscribe, like
        // https://searchfox.org/comm-central/rev/1dd06be9d6c1178a34e6c28db03161e07e97d98c/mailnews/imap/src/nsImapService.cpp#2471-2534
        dump(`Maybe subscribe to folder ${folder.URI}\n`);
      }
    } else {
      // Got IMAP folder URL from command line (most likely).
      Cc["@mozilla.org/messenger/windowservice;1"]
        .getService(Ci.nsIMessengerWindowService)
        .openMessengerWindowWithUri("mail:3pane", imapFolderURL, -1);
    }
  }
}

ImapFolderContentHandler.prototype.classID = Components.ID(
  "{d927a82f-2d15-4972-ab88-6d84601aae68}"
);