1 /***************************************************************************
2 filter_thebat.hxx - 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 ***************************************************************************/
18 #include "filter_thebat.hxx"
24 #include <kfiledialog.h>
25 #include <ktemporaryfile.h>
27 #include <kmessagebox.h>
29 /** Default constructor. */
30 FilterTheBat::FilterTheBat( void ) :
31 Filter( i18n( "Import The Bat! Mails and Folder Structure" ),
33 i18n( "<p><b>The Bat! import filter</b></p>"
34 "<p>Select the base directory of the \'The Bat!\' local mailfolder you "
36 "<p><b>Note:</b> This filter imports the *.tbb-files from \'The Bat!\' "
37 "local folder, e.g. from POP accounts, and not from IMAP/DIMAP accounts.</p>"
38 "<p>Since it is possible to recreate the folder structure, the folders "
39 "will be stored under: \"TheBat-Import\" in your local account.</p>" ) )
43 FilterTheBat::~FilterTheBat( void )
47 /** Recursive import of The Bat! maildir. */
48 void FilterTheBat::import( FilterInfo
*info
)
50 QString _homeDir
= QDir::homePath();
53 kfd
= new KFileDialog( _homeDir
, "", 0 );
54 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
56 mailDir
= kfd
->selectedFile();
58 if ( mailDir
.isEmpty() ) {
59 info
->alert( i18n( "No directory selected." ) );
62 * If the user only select homedir no import needed because
63 * there should be no files and we surely import wrong files.
65 else if ( mailDir
== QDir::homePath() || mailDir
== ( QDir::homePath() + '/' ) ) {
66 info
->addLog( i18n( "No files found for import." ) );
70 /** Recursive import of the MailFolders */
72 const QStringList rootSubDirs
= dir
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
73 int currentDir
= 1, numSubDirs
= rootSubDirs
.size();
74 for(QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= rootSubDirs
.constEnd() ; ++filename
, ++currentDir
) {
75 importDirContents(info
, dir
.filePath(*filename
));
76 info
->setOverall((int) ((float) currentDir
/ numSubDirs
* 100));
77 if(info
->shouldTerminate()) break;
81 info
->addLog( i18n("Finished importing emails from %1", mailDir
));
82 if (count_duplicates
> 0) {
83 info
->addLog( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", count_duplicates
));
85 if (info
->shouldTerminate()) info
->addLog( i18n("Finished import, canceled by user."));
88 info
->setCurrent(100);
89 info
->setOverall(100);
94 * Import of a directory contents.
95 * @param info Information storage for the operation.
96 * @param dirName The name of the directory to import.
98 void FilterTheBat::importDirContents( FilterInfo
*info
, const QString
& dirName
)
100 if(info
->shouldTerminate()) return;
102 /** Here Import all archives in the current dir */
104 QDir
importDir (dirName
);
105 const QStringList files
= importDir
.entryList(QStringList("*.[tT][bB][bB]"), QDir::Files
, QDir::Name
);
106 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= files
.constEnd(); ++mailFile
) {
107 QString temp_mailfile
= *mailFile
;
108 importFiles(info
, (dirName
+ '/' + temp_mailfile
));
109 if(info
->shouldTerminate()) return;
112 /** If there are subfolders, we import them one by one */
113 QDir
subfolders(dirName
);
114 const QStringList subDirs
= subfolders
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
115 for(QStringList::ConstIterator filename
= subDirs
.constBegin() ; filename
!= subDirs
.constEnd() ; ++filename
) {
116 importDirContents(info
, subfolders
.filePath(*filename
));
117 if(info
->shouldTerminate()) return;
122 * Import the files within a Folder.
123 * @param info Information storage for the operation.
124 * @param dirName The name of the directory to import.
126 void FilterTheBat::importFiles( FilterInfo
*info
, const QString
& FileName
)
129 // Format of a tbb-file from The Bat! 3.x
130 // ----------------------------------------
131 // First comes a header of 3K (3128 byte/ 0x00000c38), which we can forget.
132 // The byte 3129 is the first character of the first message.
134 // The end of a message is marked trough "! p 0" and 43 following characters.
135 // (within: "_UB", blanks and some other chars.) Together are 48 characters as
137 // ----------------------------------------
140 QByteArray
input(50,'\0');
141 QRegExp
regexp("!.p.0");
148 if (!tbb
.open(QIODevice::ReadOnly
)) {
149 info
->alert(i18n("Unable to open %1, skipping", FileName
));
151 // BUILD the index of messages :
152 // We need this really ugly way, because read with tbb.readLine()
153 // does not work correct. Maybe in come in a continuous loop !!!
155 // if you use readLine() to read from a file with binary data
156 // QFile::at() and QFile::atEnd() return wrong value. So we
157 // never get QFile::atEnd() == true in some cases. This looks
158 // like a bug in Qt3 maybe fixed in Qt4.
160 while((l
= tbb
.read(input
.data(),50)) ) {
161 if(info
->shouldTerminate()) {
165 QString _tmp
= input
.data();
170 iFound
= _tmp
.count(regexp
);
172 iFound
= _tmp
.lastIndexOf("!");
173 if (iFound
>= 0 && ((l
-iFound
) < 5) ) {
175 tbb
.seek((_i
- iFound
));
179 endOfEmail
= (tbb
.pos() - l
+ _tmp
.indexOf(regexp
));
180 offsets
.append(endOfEmail
);
183 // info->addLog(i18n("--COUNTED: %1").arg(count));
185 // IMPORT the messages:
186 if(!offsets
.empty() || (offsets
.empty() && (tbb
.size() > 3128))) {
187 offsets
.append(tbb
.size());
192 QString _path
= "TheBat-Import/";
193 QString _tmp
= FileName
;
194 _tmp
= _tmp
.remove(_tmp
.length() - 13, 13);
195 _path
+= _tmp
.remove( mailDir
, Qt::CaseSensitive
);
196 QString _info
= _path
;
197 info
->addLog(i18n("Import folder %1...", _info
.remove(0,14)));
199 info
->setFrom("../" + _info
+ "/messages.tbb");
201 for(QList
<long>::Iterator it
= offsets
.begin() ; it
!= offsets
.end() ; ++it
) {
202 if(info
->shouldTerminate()) {
207 QByteArray
input(endPos
-lastPos
,'\0');
208 tbb
.read(input
.data(), endPos
-lastPos
);
212 tmp
.write( input
, endPos
-lastPos
);
215 //KMessageBox::warningContinueCancel(info->parent(), "");
216 if(info
->removeDupMsg
)
217 addMessage( info
, _path
, tmp
.fileName() );
219 addMessage_fastImport( info
, _path
, tmp
.fileName() );
221 lastPos
= endPos
+ 48;
223 info
->setCurrent( (int) ( ( (float) tbb
.pos() / tbb
.size() ) * 100 ));