diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mailnews/import/src/ThunderbirdImport.jsm | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comm/mailnews/import/src/ThunderbirdImport.jsm')
-rw-r--r-- | comm/mailnews/import/src/ThunderbirdImport.jsm | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/comm/mailnews/import/src/ThunderbirdImport.jsm b/comm/mailnews/import/src/ThunderbirdImport.jsm new file mode 100644 index 0000000000..8263fa7098 --- /dev/null +++ b/comm/mailnews/import/src/ThunderbirdImport.jsm @@ -0,0 +1,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; + } +} |