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 QString _homeDir
= QDir::homePath();
55 KFileDialog
*kfd
= new KFileDialog( _homeDir
, "", 0 );
56 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
58 const QString maildir
= kfd
->selectedFile();
60 importMails( maildir
);
63 void FilterTheBat::processDirectory( const QString
& path
)
66 const QStringList rootSubDirs
= dir
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
67 QStringList::ConstIterator end
= rootSubDirs
.constEnd();
68 for(QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= end
; ++filename
) {
69 if(filterInfo()->shouldTerminate())
71 importDirContents(dir
.filePath(*filename
));
72 filterInfo()->setOverall((int) ((float) mImportDirDone
/ mTotalDir
* 100));
77 void FilterTheBat::importMails( const QString
& maildir
)
80 if ( mailDir().isEmpty() ) {
81 filterInfo()->alert( i18n( "No directory selected." ) );
85 * If the user only select homedir no import needed because
86 * there should be no files and we surely import wrong files.
88 else if ( mailDir() == QDir::homePath() || mailDir() == ( QDir::homePath() + '/' ) ) {
89 filterInfo()->addErrorLogEntry( i18n( "No files found for import." ) );
91 filterInfo()->setOverall(0);
94 /** Recursive import of the MailFolders */
96 mTotalDir
= Filter::countDirectory( dir
, false );
98 processDirectory( mailDir() );
100 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", mailDir() ));
101 if (countDuplicates() > 0) {
102 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
105 if (filterInfo()->shouldTerminate())
106 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
108 setCountDuplicates(0);
109 filterInfo()->setCurrent(100);
110 filterInfo()->setOverall(100);
114 * Import of a directory contents.
115 * @param info Information storage for the operation.
116 * @param dirName The name of the directory to import.
118 void FilterTheBat::importDirContents(const QString
& dirName
)
120 if(filterInfo()->shouldTerminate()) return;
122 /** Here Import all archives in the current dir */
124 QDir
importDir (dirName
);
125 const QStringList files
= importDir
.entryList(QStringList("*.[tT][bB][bB]"), QDir::Files
, QDir::Name
);
126 QStringList::ConstIterator end
= files
.constEnd();
127 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= end
; ++mailFile
) {
128 QString temp_mailfile
= *mailFile
;
129 importFiles((dirName
+ '/' + temp_mailfile
));
130 if(filterInfo()->shouldTerminate())
134 /** If there are subfolders, we import them one by one */
135 processDirectory( dirName
);
139 * Import the files within a Folder.
140 * @param info Information storage for the operation.
141 * @param dirName The name of the directory to import.
143 void FilterTheBat::importFiles( const QString
& FileName
)
146 // Format of a tbb-file from The Bat! 3.x
147 // ----------------------------------------
148 // First comes a header of 3K (3128 byte/ 0x00000c38), which we can forget.
149 // The byte 3129 is the first character of the first message.
151 // The end of a message is marked trough "! p 0" and 43 following characters.
152 // (within: "_UB", blanks and some other chars.) Together are 48 characters as
154 // ----------------------------------------
157 QByteArray
input(50,'\0');
158 QRegExp
regexp("!.p.0");
165 if (!tbb
.open(QIODevice::ReadOnly
)) {
166 filterInfo()->alert(i18n("Unable to open %1, skipping", FileName
));
168 // BUILD the index of messages :
169 // We need this really ugly way, because read with tbb.readLine()
170 // does not work correct. Maybe in come in a continuous loop !!!
172 // if you use readLine() to read from a file with binary data
173 // QFile::at() and QFile::atEnd() return wrong value. So we
174 // never get QFile::atEnd() == true in some cases. This looks
175 // like a bug in Qt3 maybe fixed in Qt4.
177 while((l
= tbb
.read(input
.data(),50)) ) {
178 if(filterInfo()->shouldTerminate()) {
182 QString _tmp
= input
.data();
187 iFound
= _tmp
.count(regexp
);
189 iFound
= _tmp
.lastIndexOf("!");
190 if (iFound
>= 0 && ((l
-iFound
) < 5) ) {
192 tbb
.seek((_i
- iFound
));
196 endOfEmail
= (tbb
.pos() - l
+ _tmp
.indexOf(regexp
));
197 offsets
.append(endOfEmail
);
200 // filterInfo()->addInfoLogEntry(i18n("--COUNTED: %1").arg(count));
202 // IMPORT the messages:
203 if(!offsets
.empty() || (offsets
.empty() && (tbb
.size() > 3128))) {
204 offsets
.append(tbb
.size());
209 QString _path
= i18nc("Define folder where we will import thebat mails", "TheBat-Import") + QLatin1Char('/');
210 QString _tmp
= FileName
;
211 _tmp
= _tmp
.remove(_tmp
.length() - 13, 13);
212 _path
+= _tmp
.remove( mailDir(), Qt::CaseSensitive
);
213 QString _info
= _path
;
214 filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info
.remove(0,14)));
215 filterInfo()->setTo(_path
);
216 filterInfo()->setFrom("../" + _info
+ "/messages.tbb");
218 QList
<long>::Iterator end
= offsets
.end();
219 for(QList
<long>::Iterator it
= offsets
.begin() ; it
!= end
; ++it
) {
220 if(filterInfo()->shouldTerminate()) {
225 QByteArray
input(endPos
-lastPos
,'\0');
226 tbb
.read(input
.data(), endPos
-lastPos
);
230 tmp
.write( input
, endPos
-lastPos
);
233 if(filterInfo()->removeDupMessage())
234 addMessage( _path
, tmp
.fileName() );
236 addMessage_fastImport( _path
, tmp
.fileName() );
238 lastPos
= endPos
+ 48;
240 filterInfo()->setCurrent( (int) ( ( (float) tbb
.pos() / tbb
.size() ) * 100 ));