filterevolution_v3.cpp 8.34 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2012-2017 Montel Laurent <montel@kde.org>
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

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

  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; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "filterevolution_v3.h"

#include <KLocalizedString>
#include <QFileDialog>

using namespace MailImporter;

class MailImporter::FilterEvolution_v3Private
{
public:
    FilterEvolution_v3Private()
Laurent Montel's avatar
Laurent Montel committed
29 30
        : mImportDirDone(-1)
        , mTotalDir(-1)
31 32 33 34 35 36 37 38 39 40 41 42 43
    {
    }

    int mImportDirDone;
    int mTotalDir;
};
/** Default constructor. */
FilterEvolution_v3::FilterEvolution_v3()
    : Filter(i18n("Import Evolution 3.x Local Mails and Folder Structure"),
             QStringLiteral("Laurent Montel"),
             i18n("<p><b>Evolution 3.x import filter</b></p>"
                  "<p>Select the base directory of your local Evolution mailfolder (usually ~/.local/share/evolution/mail/local/).</p>"
                  "<p>Since it is possible to recreate the folder structure, the folders "
Laurent Montel's avatar
Laurent Montel committed
44 45
                  "will be stored under: \"Evolution-Import\".</p>"))
    , d(new MailImporter::FilterEvolution_v3Private)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
}

/** Destructor. */
FilterEvolution_v3::~FilterEvolution_v3()
{
    delete d;
}

QString FilterEvolution_v3::defaultSettingsPath()
{
    return QDir::homePath() + QLatin1String("/.local/share/evolution/mail/local/");
}

/** Recursive import of KMail maildir. */
void FilterEvolution_v3::import()
{
63
    clearCountDuplicate();
64 65 66 67 68 69
    QString evolDir = defaultSettingsPath();
    QDir d(evolDir);
    if (!d.exists()) {
        evolDir = QDir::homePath();
    }

Laurent Montel's avatar
Laurent Montel committed
70
    const QString dir = QFileDialog::getExistingDirectory(nullptr, QString(), evolDir);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    if (!dir.isEmpty()) {
        importMails(dir);
    }
}

void FilterEvolution_v3::processDirectory(const QString &path)
{
    QDir dir(path);
    const QStringList rootSubDirs = dir.entryList(QStringList(QStringLiteral("*")), QDir::Dirs | QDir::Hidden, QDir::Name);
    QStringList::ConstIterator end = rootSubDirs.constEnd();
    for (QStringList::ConstIterator filename = rootSubDirs.constBegin(); filename != end; ++filename) {
        if (filterInfo()->shouldTerminate()) {
            break;
        }
        if (!(*filename == QLatin1String(".") || *filename == QLatin1String(".."))) {
            filterInfo()->setCurrent(0);
            importDirContents(dir.filePath(*filename));
Laurent Montel's avatar
Laurent Montel committed
88
            filterInfo()->setOverall((d->mTotalDir > 0) ? (int)((float)d->mImportDirDone / d->mTotalDir * 100) : 0);
89 90 91 92 93 94 95
            filterInfo()->setCurrent(100);
        }
    }
}

void FilterEvolution_v3::importMails(const QString &maildir)
{
96
    if (maildir.isEmpty()) {
97 98 99
        filterInfo()->alert(i18n("No directory selected."));
        return;
    }
100
    setMailDir(maildir);
101 102 103 104
    /**
    * If the user only select homedir no import needed because
    * there should be no files and we surely import wrong files.
    */
105
    if (mailDir() == QDir::homePath() || mailDir() == (QDir::homePath() + QLatin1Char('/'))) {
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        filterInfo()->addErrorLogEntry(i18n("No files found for import."));
    } else {
        filterInfo()->setOverall(0);
        d->mImportDirDone = 0;

        /** Recursive import of the MailArchives */
        QDir dir(mailDir());
        d->mTotalDir = Filter::countDirectory(dir, true /*search hidden directory*/);

        processDirectory(mailDir());

        filterInfo()->addInfoLogEntry(i18n("Finished importing emails from %1", mailDir()));

        if (countDuplicates() > 0) {
            filterInfo()->addInfoLogEntry(i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
        }

        if (filterInfo()->shouldTerminate()) {
            filterInfo()->addInfoLogEntry(i18n("Finished import, canceled by user."));
        }
    }
    filterInfo()->setCurrent(100);
    filterInfo()->setOverall(100);
}

/**
 * Import of a directory contents.
 * @param info Information storage for the operation.
 * @param dirName The name of the directory to import.
 */
void FilterEvolution_v3::importDirContents(const QString &dirName)
{
    /** Here Import all archives in the current dir */
    importFiles(dirName);

    /** If there are subfolders, we import them one by one */
    processDirectory(dirName);
}

/**
 * Import the files within a Folder.
 * @param info Information storage for the operation.
 * @param dirName The name of the directory to import.
 */
void FilterEvolution_v3::importFiles(const QString &dirName)
{
    QDir dir(dirName);
    QString _path;
    bool generatedPath = false;

    QDir importDir(dirName);
    const QStringList files = importDir.entryList(QStringList(QStringLiteral("[^\\.]*")), QDir::Files, QDir::Name);
    int currentFile = 1, numFiles = files.size();
    QStringList::ConstIterator filesEnd(files.constEnd());

    for (QStringList::ConstIterator mailFile = files.constBegin(); mailFile != filesEnd; ++mailFile, ++currentFile) {
        if (filterInfo()->shouldTerminate()) {
            return;
        }
        QString temp_mailfile = *mailFile;
        if (!(temp_mailfile.endsWith(QLatin1String(".db"))
Laurent Montel's avatar
Laurent Montel committed
167 168 169 170
              || temp_mailfile.endsWith(QLatin1String(".cmeta"))
              || temp_mailfile.endsWith(QLatin1String(".ev-summary"))
              || temp_mailfile.endsWith(QLatin1String(".ibex.index"))
              || temp_mailfile.endsWith(QLatin1String(".ibex.index.data")))) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            if (!generatedPath) {
                _path = i18nc("define folder name where we import evolution mails", "Evolution-Import");
                QString _tmp = dir.filePath(*mailFile);
                _tmp = _tmp.remove(mailDir(), Qt::CaseSensitive);
                QStringList subFList = _tmp.split(QLatin1Char('/'), QString::SkipEmptyParts);
                QStringList::ConstIterator end(subFList.end());
                for (QStringList::ConstIterator it = subFList.constBegin(); it != end; ++it) {
                    QString _cat = *it;
                    if (!(_cat == *mailFile)) {
                        if (_cat.startsWith(QLatin1Char('.'))) {
                            _cat = _cat.remove(0, 1);
                        }
                        //Evolution store inbox as "."
                        if (_cat.startsWith(QLatin1Char('.'))) {
                            _cat = _cat.replace(0, 1, QStringLiteral("Inbox/"));
                        }

                        _path += QLatin1Char('/') + _cat;
                        _path.replace(QLatin1Char('.'), QLatin1Char('/'));
                    }
                }
                if (_path.endsWith(QLatin1String("cur"))) {
                    _path.remove(_path.length() - 4, 4);
                }
                QString _info = _path;
                filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info));
                filterInfo()->setFrom(_info);
                filterInfo()->setTo(_path);
                generatedPath = true;
            }
Laurent Montel's avatar
Laurent Montel committed
201
            MailImporter::MessageStatus status = statusFromFile(*mailFile);
202

Laurent Montel's avatar
Laurent Montel committed
203
            if (!importMessage(_path, dir.filePath(*mailFile), filterInfo()->removeDupMessage(), status)) {
Laurent Montel's avatar
Laurent Montel committed
204
                filterInfo()->addErrorLogEntry(i18n("Could not import %1", *mailFile));
205
            }
Laurent Montel's avatar
Laurent Montel committed
206
            filterInfo()->setCurrent((int)((float)currentFile / numFiles * 100));
207 208 209 210
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
211
MailImporter::MessageStatus FilterEvolution_v3::statusFromFile(const QString &filename)
212
{
Laurent Montel's avatar
Laurent Montel committed
213
    MailImporter::MessageStatus status;
214 215 216 217 218 219 220
    const int statusIndex = filename.indexOf(QStringLiteral(":2,"));
    if (statusIndex != -1) {
        const QString statusStr = filename.right(filename.length() - statusIndex - 3);
        if (statusStr.contains(QLatin1Char('S'))) {
            status.setRead(true);
        }
        if (statusStr.contains(QLatin1Char('F'))) {
221
            //TODO ?
222 223 224 225 226 227 228 229 230 231
        }
        if (statusStr.contains(QLatin1Char('R'))) {
            status.setReplied(true);
        }
        if (statusStr.contains(QLatin1Char('P'))) {
            status.setForwarded(true);
        }
    }
    return status;
}