icedoveimportdata.cpp 4.5 KB
Newer Older
1
/*
2
   Copyright (C) 2012-2017 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18 19 20 21
*/

#include "icedoveimportdata.h"
#include "importfilterinfogui.h"
Laurent Montel's avatar
Laurent Montel committed
22 23
#include "../thunderbird/thunderbirdsettings.h"
#include "../thunderbird/thunderbirdaddressbook.h"
24 25 26

#include "mailimporter/filtericedove.h"
#include "mailimporter/filterinfo.h"
Laurent Montel's avatar
Laurent Montel committed
27
#include "MailCommon/FilterImporterExporter"
28 29
#include "importwizard.h"

Laurent Montel's avatar
Laurent Montel committed
30
#include <KLocalizedString>
31
#include <kpluginfactory.h>
32 33 34 35
#include <KConfig>

#include <QDir>

36
IcedoveImportData::IcedoveImportData(QObject *parent, const QList<QVariant> &)
37
    : AbstractImporter(parent)
38 39 40 41 42 43 44 45 46 47 48
{
    mPath = MailImporter::FilterIcedove::defaultSettingsPath();
}

IcedoveImportData::~IcedoveImportData()
{
}

QString IcedoveImportData::defaultProfile()
{
    if (mDefaultProfile.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
49
        mDefaultProfile = MailImporter::FilterThunderbird::defaultProfile(mPath, mImportWizard);
50 51 52 53 54 55
    }
    return mDefaultProfile;
}

bool IcedoveImportData::foundMailer() const
{
56 57
    QDir directory(mPath);
    if (directory.exists()) {
58
        return true;
59
    }
60 61 62 63 64
    return false;
}

bool IcedoveImportData::importAddressBook()
{
65 66
    const QDir addressbookDir(mPath + defaultProfile());
    ThunderBirdAddressBook account(addressbookDir, mImportWizard);
67
    account.importAddressBook();
68 69 70 71 72
    return true;
}

QString IcedoveImportData::name() const
{
Laurent Montel's avatar
Laurent Montel committed
73
    return QStringLiteral("Icedove");
74 75 76 77 78
}

bool IcedoveImportData::importSettings()
{
    const QString accountFile = mPath + defaultProfile() + QLatin1String("/prefs.js");
79
    if (QFileInfo::exists(accountFile)) {
80
        ThunderbirdSettings account(accountFile, mImportWizard);
81
        account.importSettings();
82 83 84 85 86 87 88 89 90 91
    } else {
        addImportSettingsInfo(i18n("Thunderbird settings not found."));
    }
    return true;
}

bool IcedoveImportData::importMails()
{
    //* This should be usually ~/.icedove/xxxx.default/Mail/Local Folders/
    MailImporter::FilterThunderbird thunderbird;
Laurent Montel's avatar
Laurent Montel committed
92 93
    initializeFilter(thunderbird);
    thunderbird.filterInfo()->setStatusMessage(i18n("Import in progress"));
94 95
    const QString mailsPath = mPath + defaultProfile() + QLatin1String("/Mail/Local Folders/");
    QDir directory(mailsPath);
96
    if (directory.exists()) {
97
        thunderbird.importMails(mailsPath);
98
    } else {
99
        thunderbird.import();
100
    }
Laurent Montel's avatar
Laurent Montel committed
101
    thunderbird.filterInfo()->setStatusMessage(i18n("Import finished"));
102 103 104 105 106 107 108 109
    return true;
}

bool IcedoveImportData::importFilters()
{
    const QString path(mPath + defaultProfile());
    QDir dir(path);
    bool filtersAdded = false;
110 111
    const QStringList subDir = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
    if (subDir.isEmpty()) {
112
        return true;
113
    }
114

115
    for (const QString &mailPath : subDir) {
116 117
        const QString subMailPath(path + QLatin1Char('/') + mailPath);
        QDir dirMail(subMailPath);
118
        const QStringList subDirMail = dirMail.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
119
        bool foundFilterFile = false;
120
        for (const QString &file : subDirMail) {
121
            const QString filterFile(subMailPath + QLatin1Char('/') + file + QLatin1String("/msgFilterRules.dat"));
122
            if (QFileInfo::exists(filterFile)) {
123
                foundFilterFile = true;
124
                const bool added = addFilters(filterFile, MailCommon::FilterImporterExporter::ThunderBirdFilter);
125 126 127 128 129
                if (!filtersAdded && added) {
                    filtersAdded = true;
                }
            }
        }
130
        if (!foundFilterFile) {
131
            return true;
132
        }
133 134 135 136 137 138 139
    }
    return filtersAdded;
}

AbstractImporter::TypeSupportedOptions IcedoveImportData::supportedOption()
{
    TypeSupportedOptions options;
140 141 142 143
    options |= AbstractImporter::Mails;
    options |= AbstractImporter::Filters;
    options |= AbstractImporter::Settings;
    options |= AbstractImporter::AddressBooks;
144 145
    return options;
}