10#include "mailimporter_debug.h"
12#include <KLocalizedString>
15#include <QTemporaryFile>
17using namespace MailImporter;
19FilterMBox::FilterMBox()
20 :
Filter(
i18n(
"Import mbox Files (UNIX, Evolution)"),
21 i18n(
"Laurence Anderson <p>( Filter accelerated by Danny Kukawka )</p>"),
22 i18n(
"<p><b>mbox import filter</b></p>"
23 "<p>This filter will import mbox files into KMail. Use this filter "
24 "if you want to import mails from Ximian Evolution or other mailers "
25 "that use this traditional UNIX format.</p>"
26 "<p><b>Note:</b> Emails will be imported into folders named after the "
27 "file they came from, prefixed with MBOX-</p>"))
31FilterMBox::~FilterMBox()
35void FilterMBox::import()
39 importMails(filenames);
42void FilterMBox::importMails(
const QStringList &filenames)
45 filterInfo()->alert(
i18n(
"No files selected."));
49 int overall_status = 0;
50 bool first_msg =
true;
52 filterInfo()->setOverall(0);
56 QFile mbox(*filename);
58 filterInfo()->alert(
i18n(
"Unable to open %1, skipping", *filename));
61 QString folderName(QStringLiteral(
"MBOX-%1").arg(filenameInfo.completeBaseName()));
63 filterInfo()->setCurrent(0);
64 filterInfo()->addInfoLogEntry(
i18n(
"Importing emails from %1...", *filename));
66 filterInfo()->setFrom(*filename);
67 filterInfo()->setTo(folderName);
72 while (!mbox.atEnd()) {
87 if (!first_msg && ((separate = input.data()).left(5) !=
"From ")) {
88 tmp.
write(input.constData(), l);
91 l = mbox.readLine(input.data(), MAX_LINE);
93 if ((separate = input.data()).left(5) !=
"From ") {
94 tmp.
write(input.constData(), l);
97 while (!mbox.atEnd() && (l = mbox.readLine(input.data(), MAX_LINE)) && ((separate = input.data()).left(5) !=
"From ")) {
98 tmp.
write(input.constData(), l);
102 if (mbox.pos() == filepos) {
103 mbox.seek(mbox.size());
105 filepos = mbox.pos();
111 if (tmp.
size() > 0) {
112 if (!importMessage(folderName, tmp.
fileName(), filterInfo()->removeDupMessage())) {
113 filterInfo()->addErrorLogEntry(
i18n(
"Could not import %1", tmp.
fileName()));
116 qCWarning(MAILIMPORTER_LOG) <<
"Message size is 0 bytes, not importing it.";
119 int currentPercentage = (int)(((
float)mbox.pos() / filenameInfo.size()) * 100);
120 filterInfo()->setCurrent(currentPercentage);
121 if (currentFile == 1) {
122 overall_status = (int)(currentPercentage * ((
float)currentFile / filenames.
count()));
124 overall_status = (int)(((currentFile - 1) * (100.0 / (
float)filenames.
count())) + (currentPercentage * (1.0 / (
float)filenames.
count())));
126 filterInfo()->setOverall(overall_status);
128 if (filterInfo()->shouldTerminate()) {
133 filterInfo()->addInfoLogEntry(
i18n(
"Finished importing emails from %1", *filename));
134 if (countDuplicates() > 0) {
135 filterInfo()->addInfoLogEntry(
i18np(
"1 duplicate message not imported to folder %2 in KMail",
136 "%1 duplicate messages not imported to folder %2 in KMail",
140 if (filterInfo()->shouldTerminate()) {
141 filterInfo()->addInfoLogEntry(
i18n(
"Finished import, canceled by user."));
144 clearCountDuplicate();
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & end()
virtual qint64 size() const const override
QStringList getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
qint64 write(const QByteArray &data)
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
bool isEmpty() const const
virtual QString fileName() const const override