11#include "filtermailapp.h"
12#include "mailimporter_debug.h"
14#include <KLocalizedString>
17#include <QTemporaryFile>
19using namespace MailImporter;
21class MailImporter::FilterMailAppPrivate
24 QStringList mMboxFiles;
27FilterMailApp::FilterMailApp()
29 i18n(
"Chris Howells<br /><br />Filter accelerated by Danny Kukawka )"),
30 i18n(
"<p><b>OS X Mail Import Filter</b></p>"
31 "<p>This filter imports e-mails from the Mail client in Apple Mac OS X.</p>"))
32 , d(new MailImporter::FilterMailAppPrivate)
36FilterMailApp::~FilterMailApp() =
default;
38void FilterMailApp::import()
41 importMails(directory);
44void FilterMailApp::importMails(
const QString &maildir)
47 filterInfo()->alert(
i18n(
"No files selected."));
52 int overall_status = 0;
53 bool first_msg =
true;
55 filterInfo()->setOverall(0);
58 traverseDirectory(mailDir());
62 if (filterInfo()->shouldTerminate()) {
65 QFile mbox(*filename);
67 filterInfo()->alert(
i18n(
"Unable to open %1, skipping", *filename));
69 QFileInfo filenameInfo(*filename);
70 qCDebug(MAILIMPORTER_LOG) <<
"importing filename" << *filename;
72 QString folderName(name[name.count() - 2]);
74 filterInfo()->setCurrent(0);
75 filterInfo()->addInfoLogEntry(
i18n(
"Importing emails from %1...", *filename));
76 filterInfo()->setFrom(*filename);
77 filterInfo()->setTo(folderName);
79 QByteArray input(MAX_LINE,
'\0');
82 while (!mbox.atEnd()) {
95 tmp.
write(input.constData(), l);
97 l = mbox.readLine(input.data(), MAX_LINE);
98 tmp.
write(input.constData(), l);
100 while (!mbox.atEnd() && (l = mbox.readLine(input.data(), MAX_LINE)) && ((separate = input.data()).left(5) !=
"From ")) {
101 tmp.
write(input.constData(), l);
106 if (!importMessage(folderName, tmp.
fileName(), filterInfo()->removeDupMessage())) {
107 filterInfo()->addErrorLogEntry(
i18n(
"Could not import %1", tmp.
fileName()));
110 int currentPercentage = (int)(((
float)mbox.pos() / filenameInfo.size()) * 100);
111 filterInfo()->setCurrent(currentPercentage);
112 if (currentFile == 1) {
113 overall_status = (int)(currentPercentage * ((
float)currentFile / d->mMboxFiles.count()));
116 (int)(((currentFile - 1) * (100.0 / (
float)d->mMboxFiles.count())) + (currentPercentage * (1.0 / (
float)d->mMboxFiles.count())));
118 filterInfo()->setOverall(overall_status);
119 if (filterInfo()->shouldTerminate()) {
124 filterInfo()->addInfoLogEntry(
i18n(
"Finished importing emails from %1", *filename));
125 if (countDuplicates() > 0) {
126 filterInfo()->addInfoLogEntry(
i18np(
"1 duplicate message not imported to folder %2 in KMail",
127 "%1 duplicate messages not imported to folder %2 in KMail",
131 clearCountDuplicate();
135 if (filterInfo()->shouldTerminate()) {
136 filterInfo()->addInfoLogEntry(
i18n(
"Finished import, canceled by user."));
140void FilterMailApp::traverseDirectory(
const QString &dirName)
145 const QFileInfoList fileinfolist =
dir.entryInfoList();
146 for (
const QFileInfo &fi : fileinfolist) {
147 const QString filename(fi.fileName());
148 if (filename == QLatin1Char(
'.') || filename == QLatin1StringView(
"..")) {
151 if (fi.isDir() && fi.isReadable()) {
152 traverseDirectory(fi.filePath());
154 if (!fi.isDir() && (filename == QLatin1StringView(
"mbox"))) {
155 qCDebug(MAILIMPORTER_LOG) <<
"adding the file" << fi.filePath();
156 d->mMboxFiles.append(fi.filePath());
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString dir(const QString &fileClass)
const QList< QKeySequence > & end()
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, Options options)
qint64 write(const QByteArray &data)
bool isEmpty() const const
virtual QString fileName() const const override