summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/src/ThunderbirdImport.jsm
blob: 8263fa7098f55d0bb1a3eef6f556ac9b51192981 (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
/* 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/. */

const EXPORTED_SYMBOLS = ["ThunderbirdImport"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

const lazy = {};

XPCOMUtils.defineLazyGetter(
  lazy,
  "l10n",
  () => new Localization(["messenger/importDialog.ftl"], true)
);

/**
 * The importing process is managed by importDialog.js and nsImportMail.cpp.
 *
 * @implements {nsIImportMail}
 */
class ThunderbirdImportMail {
  QueryInterface = ChromeUtils.generateQI(["nsIImportMail"]);

  GetDefaultLocation(location, found, userVerify) {
    userVerify.value = true;
  }

  /**
   * Create a nsIImportMailboxDescriptor instance.
   *
   * @param {string} path - The mailbox path.
   * @param {string} name - The mailbox name.
   * @param {number} depth - The depth of the mailbox folder.
   * @returns {nsIImportMailboxDescriptor}
   */
  _createMailboxDescriptor(path, name, depth) {
    let importService = Cc[
      "@mozilla.org/import/import-service;1"
    ].createInstance(Ci.nsIImportService);
    let descriptor = importService.CreateNewMailboxDescriptor();
    descriptor.size = 100;
    descriptor.depth = depth;
    descriptor.SetDisplayName(name);
    descriptor.file.initWithPath(path);

    return descriptor;
  }

  /**
   * Recursively find mailboxes in a directory.
   *
   * @param {nsIFile} directory - The directory to find mailboxes.
   * @param {number} depth - The depth of the current directory.
   * @returns {nsIImportMailboxDescriptor[]} - All mailboxes found.
   */
  _collectMailboxesInDirectory(directory, depth) {
    let result = [];
    let name = directory.leafName;
    if (depth > 0 && !name.endsWith(".msf") && !name.endsWith(".dat")) {
      if (name.endsWith(".sbd")) {
        name = name.slice(0, name.lastIndexOf("."));
      }
      let descriptor = this._createMailboxDescriptor(
        directory.path,
        name,
        depth
      );
      result.push(descriptor);
    }
    if (directory.isDirectory()) {
      for (let entry of directory.directoryEntries) {
        if (
          (depth == 0 &&
            entry.leafName != "ImapMail" &&
            entry.leafName != "Mail") ||
          (depth == 1 && entry.leafName == "Feeds")
        ) {
          continue;
        }
        result.push(...this._collectMailboxesInDirectory(entry, depth + 1));
      }
    }
    return result;
  }

  findMailboxes(location) {
    return this._collectMailboxesInDirectory(location, 0);
  }

  ImportMailbox(source, dstFolder, errorLog, successLog, fatalError) {
    if (source.file.isFile()) {
      source.file.copyTo(
        dstFolder.filePath.parent,
        dstFolder.filePath.leafName
      );
      successLog.value = `Import ${source.file.leafName} succeeded.\n`;
    }
  }
}

/**
 * With this class, Thunderbird is shown as an option in the importDialog.xhtml.
 * Currently supports importing mail, see the GetImportInterface function.
 *
 * @implements {nsIImportModule}
 */
class ThunderbirdImport {
  QueryInterface = ChromeUtils.generateQI(["nsIImportModule"]);

  get name() {
    return lazy.l10n.formatValueSync("thunderbird-import-name");
  }

  get description() {
    return lazy.l10n.formatValueSync("thunderbird-import-description");
  }

  get supports() {
    return "mail";
  }

  get supportsUpgrade() {
    return false;
  }

  GetImportInterface(type) {
    if (type == "mail") {
      let importService = Cc[
        "@mozilla.org/import/import-service;1"
      ].createInstance(Ci.nsIImportService);
      let genericInterface = importService.CreateNewGenericMail();
      genericInterface.SetData("mailInterface", new ThunderbirdImportMail());
      let name = Cc["@mozilla.org/supports-string;1"].createInstance(
        Ci.nsISupportsString
      );
      name.data = "Thunderbird";
      genericInterface.SetData("name", name);
      return genericInterface;
    }
    return null;
  }
}