File indexing completed on 2024-12-22 04:52:50

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "thunderbirdimportdata.h"
0008 
0009 #include "abstractdisplayinfo.h"
0010 #include "thunderbirdaddressbook.h"
0011 #include "thunderbirdsettings.h"
0012 
0013 #include <MailCommon/FilterImporterExporter>
0014 #include <MailImporter/FilterInfo>
0015 #include <MailImporter/FilterThunderbird>
0016 
0017 #include <KLocalizedString>
0018 #include <KPluginFactory>
0019 
0020 #include <QDir>
0021 
0022 K_PLUGIN_CLASS_WITH_JSON(ThunderbirdImportData, "thunderbirdimporter.json")
0023 
0024 ThunderbirdImportData::ThunderbirdImportData(QObject *parent, const QList<QVariant> &)
0025     : LibImportWizard::AbstractImporter(parent)
0026 {
0027     mPath = MailImporter::FilterThunderbird::defaultSettingsPath();
0028 }
0029 
0030 ThunderbirdImportData::~ThunderbirdImportData() = default;
0031 
0032 QString ThunderbirdImportData::defaultProfile()
0033 {
0034     if (mDefaultProfile.isEmpty()) {
0035         mDefaultProfile = MailImporter::FilterThunderbird::defaultProfile(mPath, mAbstractDisplayInfo->parentWidget());
0036     }
0037     return mDefaultProfile;
0038 }
0039 
0040 bool ThunderbirdImportData::foundMailer() const
0041 {
0042     QDir directory(mPath);
0043     if (directory.exists()) {
0044         return true;
0045     }
0046     return false;
0047 }
0048 
0049 bool ThunderbirdImportData::importAddressBook()
0050 {
0051     const QDir addressbookDir(mPath + defaultProfile());
0052     ThunderBirdAddressBook account(addressbookDir);
0053     account.setAbstractDisplayInfo(mAbstractDisplayInfo);
0054     account.importAddressBook();
0055     return true;
0056 }
0057 
0058 QString ThunderbirdImportData::name() const
0059 {
0060     return QStringLiteral("Thunderbird");
0061 }
0062 
0063 bool ThunderbirdImportData::importSettings()
0064 {
0065     const QString accountFile = mPath + defaultProfile() + QLatin1StringView("/prefs.js");
0066     if (QFileInfo::exists(accountFile)) {
0067         ThunderbirdSettings account(accountFile);
0068         account.setAbstractDisplayInfo(mAbstractDisplayInfo);
0069         account.importSettings();
0070     } else {
0071         addImportSettingsInfo(i18n("Thunderbird settings not found."));
0072     }
0073     return true;
0074 }
0075 
0076 bool ThunderbirdImportData::importMails()
0077 {
0078     //* This should be usually ~/.thunderbird/xxxx.default/Mail/Local Folders/
0079     MailImporter::FilterThunderbird thunderbird;
0080     initializeFilter(thunderbird);
0081     thunderbird.filterInfo()->setStatusMessage(i18n("Import in progress"));
0082     const QString mailsPath = mPath + defaultProfile() + QLatin1StringView("/Mail/Local Folders/");
0083     QDir directory(mailsPath);
0084     if (directory.exists()) {
0085         thunderbird.importMails(mailsPath);
0086     } else {
0087         thunderbird.import();
0088     }
0089     thunderbird.filterInfo()->setStatusMessage(i18n("Import finished"));
0090     return true;
0091 }
0092 
0093 bool ThunderbirdImportData::importFilters()
0094 {
0095     const QString path(mPath + defaultProfile());
0096     QDir dir(path);
0097     bool filtersAdded = false;
0098     const QStringList subDir = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
0099     if (subDir.isEmpty()) {
0100         return true;
0101     }
0102 
0103     for (const QString &mailPath : subDir) {
0104         const QString subMailPath(path + QLatin1Char('/') + mailPath);
0105         QDir dirMail(subMailPath);
0106         const QStringList subDirMail = dirMail.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
0107         bool foundFilterFile = false;
0108         for (const QString &file : subDirMail) {
0109             const QString filterFile(subMailPath + QLatin1Char('/') + file + QLatin1StringView("/msgFilterRules.dat"));
0110             if (QFileInfo::exists(filterFile)) {
0111                 foundFilterFile = true;
0112                 const bool added = addFilters(filterFile, MailCommon::FilterImporterExporter::ThunderBirdFilter);
0113                 if (!filtersAdded && added) {
0114                     filtersAdded = true;
0115                 }
0116             }
0117         }
0118         if (!foundFilterFile) {
0119             return true;
0120         }
0121     }
0122     return filtersAdded;
0123 }
0124 
0125 LibImportWizard::AbstractImporter::TypeSupportedOptions ThunderbirdImportData::supportedOption()
0126 {
0127     TypeSupportedOptions options;
0128     options |= LibImportWizard::AbstractImporter::Mails;
0129     options |= LibImportWizard::AbstractImporter::Filters;
0130     options |= LibImportWizard::AbstractImporter::Settings;
0131     options |= LibImportWizard::AbstractImporter::AddressBooks;
0132     return options;
0133 }
0134 
0135 #include "thunderbirdimportdata.moc"