Better wording
[kdepim.git] / mailimporter / filter_thebat.cpp
blob3281e5e5fc513928da2542c00b2855e8a6934d85
1 /***************************************************************************
2 filter_thebat.h - TheBat! mail import
3 -------------------
4 begin : April 07 2005
5 copyright : (C) 2005 by Danny Kukawka
6 email : danny.kukawka@web.de
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 ***************************************************************************/
17 /* Copyright (c) 2012 Montel Laurent <montel@kde.org> */
19 #include "filter_thebat.h"
22 #include <QRegExp>
24 #include <klocale.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" ),
34 "Danny Kukawka",
35 i18n( "<p><b>The Bat! import filter</b></p>"
36 "<p>Select the base directory of the \'The Bat!\' local mailfolder you "
37 "want to import.</p>"
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>" ) )
45 /** Destructor. */
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 );
57 kfd->exec();
58 const QString maildir = kfd->selectedFile();
59 delete kfd;
60 importMails( maildir );
63 void FilterTheBat::processDirectory( const QString& path)
65 QDir dir(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())
70 break;
71 importDirContents(dir.filePath(*filename));
72 filterInfo()->setOverall((int) ((float) mImportDirDone / mTotalDir * 100));
73 mImportDirDone++;
77 void FilterTheBat::importMails( const QString & maildir )
79 setMailDir(maildir);
80 if ( mailDir().isEmpty() ) {
81 filterInfo()->alert( i18n( "No directory selected." ) );
82 return;
84 /**
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." ) );
90 } else {
91 filterInfo()->setOverall(0);
92 mImportDirDone = 0;
94 /** Recursive import of the MailFolders */
95 QDir dir(mailDir());
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 */
123 QDir dir(dirName);
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())
131 return;
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
153 // separator.
154 // ----------------------------------------
156 long l = 0;
157 QByteArray input(50,'\0');
158 QRegExp regexp("!.p.0");
159 QFile tbb(FileName);
160 int iFound = 0;
161 int count = 0;
162 long endOfEmail = 0;
163 QList<long> offsets;
165 if (!tbb.open(QIODevice::ReadOnly)) {
166 filterInfo()->alert(i18n("Unable to open %1, skipping", FileName));
167 } else {
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 !!!
171 // Reason:
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()) {
179 tbb.close();
180 return;
182 QString _tmp = input.data();
184 if (tbb.atEnd())
185 break;
187 iFound = _tmp.count(regexp);
188 if(!iFound) {
189 iFound = _tmp.lastIndexOf("!");
190 if (iFound >= 0 && ((l-iFound) < 5) ) {
191 int _i = tbb.pos();
192 tbb.seek((_i - iFound));
194 } else {
195 ++count;
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());
205 tbb.seek(3128);
206 long lastPos = 3128;
207 long endPos = 0;
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()) {
221 tbb.close();
222 return;
224 endPos = *it;
225 QByteArray input(endPos-lastPos,'\0');
226 tbb.read(input.data(), endPos-lastPos);
228 KTemporaryFile tmp;
229 tmp.open();
230 tmp.write( input, endPos-lastPos );
231 tmp.flush();
233 if(filterInfo()->removeDupMessage())
234 addMessage( _path, tmp.fileName() );
235 else
236 addMessage_fastImport( _path, tmp.fileName() );
238 lastPos = endPos + 48;
239 tbb.seek(lastPos);
240 filterInfo()->setCurrent( (int) ( ( (float) tbb.pos() / tbb.size() ) * 100 ));
245 tbb.close();