icedoveimportdata.cpp 4.68 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
*/

#include "icedoveimportdata.h"
21

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

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

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> &)
Laurent Montel's avatar
Laurent Montel committed
37
    : LibImportWizard::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()) {
49
        mDefaultProfile = MailImporter::FilterThunderbird::defaultProfile(mPath, mAbstractDisplayInfo->parentWidget());
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
    const QDir addressbookDir(mPath + defaultProfile());
66 67
    ThunderBirdAddressBook account(addressbookDir);
    account.setAbstractDisplayInfo(mAbstractDisplayInfo);
68
    account.importAddressBook();
69 70 71 72 73
    return true;
}

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

bool IcedoveImportData::importSettings()
{
    const QString accountFile = mPath + defaultProfile() + QLatin1String("/prefs.js");
80
    if (QFileInfo::exists(accountFile)) {
81 82
        ThunderbirdSettings account(accountFile);
        account.setAbstractDisplayInfo(mAbstractDisplayInfo);
83
        account.importSettings();
84
    } else {
Laurent Montel's avatar
Laurent Montel committed
85
        addImportSettingsInfo(i18n("Icedove settings not found."));
86 87 88 89 90 91 92 93
    }
    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
94 95
    initializeFilter(thunderbird);
    thunderbird.filterInfo()->setStatusMessage(i18n("Import in progress"));
96 97
    const QString mailsPath = mPath + defaultProfile() + QLatin1String("/Mail/Local Folders/");
    QDir directory(mailsPath);
98
    if (directory.exists()) {
99
        thunderbird.importMails(mailsPath);
100
    } else {
101
        thunderbird.import();
102
    }
Laurent Montel's avatar
Laurent Montel committed
103
    thunderbird.filterInfo()->setStatusMessage(i18n("Import finished"));
104 105 106 107 108 109 110 111
    return true;
}

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

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

Laurent Montel's avatar
Laurent Montel committed
139
LibImportWizard::AbstractImporter::TypeSupportedOptions IcedoveImportData::supportedOption()
140 141
{
    TypeSupportedOptions options;
Laurent Montel's avatar
Laurent Montel committed
142 143 144 145
    options |= LibImportWizard::AbstractImporter::Mails;
    options |= LibImportWizard::AbstractImporter::Filters;
    options |= LibImportWizard::AbstractImporter::Settings;
    options |= LibImportWizard::AbstractImporter::AddressBooks;
146 147
    return options;
}