1 /***************************************************************************
2 filter_thebat.h - TheBat! mail import
5 copyright : (C) 2005 by Danny Kukawka
6 email : danny.kukawka@web.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
17 /* Copyright (c) 2012 Montel Laurent <montel@kde.org> */
19 #include "filter_thebat.h"
25 #include <kfiledialog.h>
26 #include <ktemporaryfile.h>
29 using namespace MailImporter
;
31 /** Default constructor. */
32 FilterTheBat::FilterTheBat() :
33 Filter( i18n( "Import The Bat! Mails and Folder Structure" ),
35 i18n( "<p><b>The Bat! import filter</b></p>"
36 "<p>Select the base directory of the \'The Bat!\' local mailfolder you "
38 "<p><b>Note:</b> This filter imports the *.tbb-files from \'The Bat!\' "
39 "local folder, e.g. from POP accounts, and not from IMAP/DIMAP accounts.</p>"
40 "<p>Since it is possible to recreate the folder structure, the folders "
41 "will be stored under: \"TheBat-Import\" in your local account.</p>" ) )
46 FilterTheBat::~FilterTheBat()
50 /** Recursive import of The Bat! maildir. */
51 void FilterTheBat::import()
53 const QString _homeDir
= QDir::homePath();
55 QPointer
<KFileDialog
> kfd
= new KFileDialog( _homeDir
, "", 0 );
56 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
58 const QString maildir
= kfd
->selectedFile();
59 importMails( maildir
);
64 void FilterTheBat::processDirectory( const QString
&path
)
67 const QStringList rootSubDirs
= dir
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
68 QStringList::ConstIterator end
= rootSubDirs
.constEnd();
69 for (QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= end
; ++filename
) {
70 if(filterInfo()->shouldTerminate())
72 importDirContents(dir
.filePath(*filename
));
73 filterInfo()->setOverall((int) ((float) mImportDirDone
/ mTotalDir
* 100));
78 void FilterTheBat::importMails( const QString
&maildir
)
81 if ( mailDir().isEmpty() ) {
82 filterInfo()->alert( i18n( "No directory selected." ) );
86 * If the user only select homedir no import needed because
87 * there should be no files and we surely import wrong files.
89 else if ( mailDir() == QDir::homePath() || mailDir() == ( QDir::homePath() + '/' ) ) {
90 filterInfo()->addErrorLogEntry( i18n( "No files found for import." ) );
92 filterInfo()->setOverall(0);
95 /** Recursive import of the MailFolders */
97 mTotalDir
= Filter::countDirectory( dir
, false );
99 processDirectory( mailDir() );
101 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", mailDir() ));
102 if (countDuplicates() > 0) {
103 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
106 if (filterInfo()->shouldTerminate())
107 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
109 setCountDuplicates(0);
110 filterInfo()->setCurrent(100);
111 filterInfo()->setOverall(100);
115 * Import of a directory contents.
116 * @param info Information storage for the operation.
117 * @param dirName The name of the directory to import.
119 void FilterTheBat::importDirContents(const QString
&dirName
)
121 if(filterInfo()->shouldTerminate()) return;
123 /** Here Import all archives in the current dir */
125 QDir
importDir (dirName
);
126 const QStringList files
= importDir
.entryList(QStringList("*.[tT][bB][bB]"), QDir::Files
, QDir::Name
);
127 QStringList::ConstIterator end
= files
.constEnd();
128 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= end
; ++mailFile
) {
129 QString temp_mailfile
= *mailFile
;
130 importFiles((dirName
+ '/' + temp_mailfile
));
131 if(filterInfo()->shouldTerminate())
135 /** If there are subfolders, we import them one by one */
136 processDirectory( dirName
);
140 * Import the files within a Folder.
141 * @param info Information storage for the operation.
142 * @param dirName The name of the directory to import.
144 void FilterTheBat::importFiles( const QString
&FileName
)
147 // Format of a tbb-file from The Bat! 3.x
148 // ----------------------------------------
149 // First comes a header of 3K (3128 byte/ 0x00000c38), which we can forget.
150 // The byte 3129 is the first character of the first message.
152 // The end of a message is marked trough "! p 0" and 43 following characters.
153 // (within: "_UB", blanks and some other chars.) Together are 48 characters as
155 // ----------------------------------------
158 QByteArray
input(50,'\0');
159 QRegExp
regexp("!.p.0");
166 if (!tbb
.open(QIODevice::ReadOnly
)) {
167 filterInfo()->alert(i18n("Unable to open %1, skipping", FileName
));
169 // BUILD the index of messages :
170 // We need this really ugly way, because read with tbb.readLine()
171 // does not work correct. Maybe in come in a continuous loop !!!
173 // if you use readLine() to read from a file with binary data
174 // QFile::at() and QFile::atEnd() return wrong value. So we
175 // never get QFile::atEnd() == true in some cases. This looks
176 // like a bug in Qt3 maybe fixed in Qt4.
178 while((l
= tbb
.read(input
.data(),50)) ) {
179 if(filterInfo()->shouldTerminate()) {
183 QString _tmp
= input
.data();
188 iFound
= _tmp
.count(regexp
);
190 iFound
= _tmp
.lastIndexOf("!");
191 if (iFound
>= 0 &&((l
-iFound
) < 5) ) {
193 tbb
.seek((_i
- iFound
));
197 endOfEmail
= (tbb
.pos() - l
+ _tmp
.indexOf(regexp
));
198 offsets
.append(endOfEmail
);
201 // filterInfo()->addInfoLogEntry(i18n("--COUNTED: %1").arg(count));
203 // IMPORT the messages:
204 if(!offsets
.empty() || (offsets
.empty() &&(tbb
.size() > 3128))) {
205 offsets
.append(tbb
.size());
210 QString _path
= i18nc("Define folder where we will import thebat mails", "TheBat-Import") + QLatin1Char('/');
211 QString _tmp
= FileName
;
212 _tmp
= _tmp
.remove(_tmp
.length() - 13, 13);
213 _path
+= _tmp
.remove( mailDir(), Qt::CaseSensitive
);
214 QString _info
= _path
;
215 filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info
.remove(0,14)));
216 filterInfo()->setTo(_path
);
217 filterInfo()->setFrom("../" + _info
+ "/messages.tbb");
219 QList
<long>::Iterator end
= offsets
.end();
220 for (QList
<long>::Iterator it
= offsets
.begin() ; it
!= end
; ++it
) {
221 if(filterInfo()->shouldTerminate()) {
226 QByteArray
input(endPos
-lastPos
,'\0');
227 tbb
.read(input
.data(), endPos
-lastPos
);
231 tmp
.write( input
, endPos
-lastPos
);
234 if(filterInfo()->removeDupMessage())
235 addMessage( _path
, tmp
.fileName() );
237 addMessage_fastImport( _path
, tmp
.fileName() );
239 lastPos
= endPos
+ 48;
241 filterInfo()->setCurrent( (int) ( ( (float) tbb
.pos() / tbb
.size() ) * 100 ));