MailImporter

filterlnotes.cpp
1/*
2 filterlnotes.cpp - Lotus Notes Structured Text mail import
3
4 SPDX-FileCopyrightText: 2005 Robert Rockers <kconfigure@rockerssoft.com>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9#include "filterlnotes.h"
10
11#include <KLocalizedString>
12
13#include <QFileDialog>
14#include <QTemporaryFile>
15
16using namespace MailImporter;
17class MailImporter::FilterLNotesPrivate
18{
19public:
20 /** the working directory */
21 QDir dir;
22 /** which file (of d->totalFiles) is now in the work? */
23 int currentFile = 1;
24 /** total number of files that get imported */
25 int totalFiles = 0;
26};
27
28/** Default constructor. */
30 : Filter(i18n("Import Lotus Notes Emails"),
31 QStringLiteral("Robert Rockers"),
32 i18n("<p><b>Lotus Notes Structured Text mail import filter</b></p>"
33 "<p>This filter will import Structure Text files from an exported Lotus Notes email "
34 "client into KMail. Use this filter if you want to import mails from Lotus or other "
35 "mailers that use Lotus Notes' Structured Text format.</p>"
36 "<p><b>Note:</b> Since it is possible to recreate the folder structure, the imported "
37 "messages will be stored in subfolders named by the files they came from under: "
38 "\"LNotes-Import\" in your local folder.</p>"))
39 , d(new MailImporter::FilterLNotesPrivate)
40{
41}
42
43/** Destructor. */
45
46/**
47 * Recursive import of The Bat! maildir.
48 * @param info Information storage for the operation.
49 */
51{
52 const QStringList filenames = QFileDialog::getOpenFileNames(filterInfo()->parentWidget(), QString(), QDir::homePath(), i18n("All Files (*)"));
53 if (filenames.isEmpty()) {
54 filterInfo()->alert(i18n("No files selected."));
55 return;
56 }
57
58 d->currentFile = 1;
59 d->totalFiles = filenames.count();
60 filterInfo()->setOverall(0);
61
62 // See filter_mbox.cxx for better reference.
63 QStringList::ConstIterator end = filenames.constEnd();
64 for (QStringList::ConstIterator filename = filenames.constBegin(); filename != end; ++filename) {
65 ++d->currentFile;
66 filterInfo()->addInfoLogEntry(i18n("Importing emails from %1", *filename));
67 ImportLNotes(*filename);
68 filterInfo()->setOverall(100 * d->currentFile / d->totalFiles);
69 if (filterInfo()->shouldTerminate()) {
70 break;
71 }
72 }
73}
74
75/**
76 * Import the files within a Folder.
77 * @param file The name of the file to import.
78 */
79void FilterLNotes::ImportLNotes(const QString &file)
80{
81 // See Filter_pmail.cxx for better reference
82
83 // Format of a Lotus Notes 5 Structured Text Document w form feed
84 // Each email begins with a custom Header Principal:
85 // The Message ends with a 0c character
86
87 // open the message
88 QFile f(file);
89
90 if (!f.open(QIODevice::ReadOnly)) {
91 filterInfo()->alert(i18n("Unable to open %1, skipping", file));
92 } else {
93 char ch = 0;
94 int state = 0;
95 int n = 0;
96 QTemporaryFile *tempfile = nullptr;
97
98 // Get folder name
99 QFileInfo filenameInfo(file);
100 QString folder(i18nc("Define folder name where we import lotus note mails", "LNotes-Import") + QLatin1Char('/') + filenameInfo.completeBaseName());
101 filterInfo()->setTo(folder);
102
103 // State machine to read the data in. The fgetc usage is probably terribly slow ...
104 while (f.getChar(&ch)) {
105 switch (state) {
106 // new message state
107 case 0:
108 // open temp output file
109 state = 1;
110 filterInfo()->setCurrent(i18n("Message %1", n++));
111 if (filterInfo()->shouldTerminate()) {
112 return;
113 }
114
115 tempfile = new QTemporaryFile;
116 tempfile->setAutoRemove(false);
117 tempfile->open();
118 // fall through
119 [[fallthrough]];
120 // inside a message state
121 case 1:
122 if (ch == 0x0c) {
123 // close file, send it
124 tempfile->close();
125
126 if (!importMessage(folder, tempfile->fileName(), filterInfo()->removeDupMessage())) {
127 filterInfo()->addErrorLogEntry(i18n("Could not import %1", tempfile->fileName()));
128 }
129
130 tempfile->setAutoRemove(true);
131 state = 0;
132
133 int currentPercentage = (int)(((float)f.pos() / filenameInfo.size()) * 100);
134 filterInfo()->setCurrent(currentPercentage);
135 if (filterInfo()->shouldTerminate()) {
136 return;
137 }
138
139 break;
140 }
141 if (ch == 0x0d) {
142 break;
143 }
144 tempfile->putChar(ch);
145 break;
146 }
147 }
148
149 if (tempfile) {
150 tempfile->close();
151 }
152
153 // did Folder end without 0x1a at the end?
154 if (state != 0) {
155 Q_ASSERT(tempfile);
156
157 if (!importMessage(folder, tempfile->fileName(), filterInfo()->removeDupMessage())) {
158 filterInfo()->addErrorLogEntry(i18n("Could not import %1", tempfile->fileName()));
159 }
160 }
161 if (tempfile) {
162 tempfile->setAutoRemove(true);
163 delete tempfile;
164 }
165
166 f.close();
167 }
168}
void import() override
Standard import filter... starting line for our import.
~FilterLNotes() override
Destructor.
FilterLNotes()
Default constructor.
The Filter class.
Definition filters.h:29
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString homePath()
virtual void close() override
QStringList getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
bool putChar(char c)
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
bool isEmpty() const const
virtual QString fileName() const const override
void setAutoRemove(bool b)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:54:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.