SVN_SILENT made messages (.desktop file)
[kdepim.git] / mailimporter / filter_lnotes.cpp
blobe103676e1d6e2c8534ef48f77bcab96d071fd41b
1 /***************************************************************************
2 filter_lnotes.cxx - Lotus Notes Structured Text mail import
3 -------------------
4 begin : Wed Feb 16, 2005
5 copyright : (C) 2005 by Robert Rockers
6 email : kconfigure@rockerssoft.com
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
19 #include <klocale.h>
20 #include <kfiledialog.h>
21 #include <ktemporaryfile.h>
22 #include <kdebug.h>
23 #include <QFileInfo>
25 #include "filter_lnotes.h"
26 using namespace MailImporter;
28 /** Default constructor. */
29 FilterLNotes::FilterLNotes() :
30 Filter( i18n("Import Lotus Notes Emails"),
31 "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>"))
40 currentFile = 1;
41 totalFiles = 0;
44 /** Destructor. */
45 FilterLNotes::~FilterLNotes()
49 /**
50 * Recursive import of The Bat! maildir.
51 * @param info Information storage for the operation.
53 void FilterLNotes::import() {
55 const QStringList filenames = KFileDialog::getOpenFileNames( QDir::homePath(), "*|" + i18n("All Files (*)"),
56 filterInfo()->parent() );
57 if (filenames.isEmpty()) {
58 filterInfo()->alert(i18n("No files selected."));
59 return;
62 currentFile = 1;
63 totalFiles = 0;
65 totalFiles = filenames.count();
66 filterInfo()->setOverall(0);
68 // See filter_mbox.cxx for better reference.
69 QStringList::ConstIterator end = filenames.constEnd();
70 for ( QStringList::ConstIterator filename = filenames.constBegin(); filename != end; ++filename ) {
72 ++currentFile;
73 filterInfo()->addInfoLogEntry( i18n("Importing emails from %1", *filename) );
74 ImportLNotes( *filename );
75 filterInfo()->setOverall( 100 * currentFile / totalFiles );
76 if ( filterInfo()->shouldTerminate() )
77 break;
81 /**
82 * Import the files within a Folder.
83 * @param file The name of the file to import.
85 void FilterLNotes::ImportLNotes(const QString &file) {
87 // See Filter_pmail.cxx for better reference
89 // Format of a Lotus Notes 5 Structured Text Document w form feed
90 // Each email begins with a custom Header Principal:
91 // The Message ends with a 0c character
93 // open the message
94 QFile f(file);
96 if (! f.open( QIODevice::ReadOnly ) ) {
97 filterInfo()->alert( i18n("Unable to open %1, skipping", file ) );
98 } else {
100 char ch = 0;
101 int state = 0;
102 int n = 0;
103 KTemporaryFile *tempfile = 0;
105 // Get folder name
106 QFileInfo filenameInfo( file );
107 QString folder(i18nc("Define folder name where we import lotus note mails", "LNotes-Import") +QLatin1Char('/') + filenameInfo.completeBaseName());
108 filterInfo()->setTo(folder);
110 // State machine to read the data in. The fgetc usage is probably terribly slow ...
111 while (f.getChar(&ch)) {
112 switch (state) {
113 // new message state
114 case 0:
115 // open temp output file
116 state = 1;
117 filterInfo()->setCurrent(i18n("Message %1", n++));
118 if ( filterInfo()->shouldTerminate() )
119 return;
121 tempfile = new KTemporaryFile;
122 tempfile->setAutoRemove(false);
123 tempfile->open();
124 // fall through
126 // inside a message state
127 case 1:
128 if (ch == 0x0c) {
129 // close file, send it
130 tempfile->close();
132 if(filterInfo()->removeDupMessage())
133 addMessage( folder, tempfile->fileName() );
134 else
135 addMessage_fastImport( folder, tempfile->fileName() );
137 tempfile->setAutoRemove(true);
138 state = 0;
140 int currentPercentage = (int) ( ( (float) f.pos() / filenameInfo.size() ) * 100 );
141 filterInfo()->setCurrent( currentPercentage );
142 if ( filterInfo()->shouldTerminate() )
143 return;
145 break;
147 if (ch == 0x0d) {
148 break;
150 tempfile->putChar(ch);
151 break;
155 if (tempfile)
156 tempfile->close();
158 // did Folder end without 0x1a at the end?
159 if (state != 0) {
160 Q_ASSERT(tempfile);
162 if(filterInfo()->removeDupMessage())
163 addMessage( folder, tempfile->fileName() );
164 else
165 addMessage_fastImport( folder, tempfile->fileName() );
167 if (tempfile) {
168 tempfile->setAutoRemove(true);
169 delete tempfile;
172 f.close();