french -> French
[kdepim.git] / mailimporter / filter_thebat.cpp
blob34e34a1431a2842ee9d209741751bb43ba1c4a6e
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>
23 #include <QPointer>
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 const QString _homeDir = QDir::homePath();
55 QPointer<KFileDialog> kfd = new KFileDialog( _homeDir, "", 0 );
56 kfd->setMode( KFile::Directory | KFile::LocalOnly );
57 if (kfd->exec()) {
58 const QString maildir = kfd->selectedFile();
59 importMails( maildir );
61 delete kfd;
64 void FilterTheBat::processDirectory( const QString &path)
66 QDir dir(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())
71 break;
72 importDirContents(dir.filePath(*filename));
73 filterInfo()->setOverall((int) ((float) mImportDirDone / mTotalDir * 100));
74 mImportDirDone++;
78 void FilterTheBat::importMails( const QString &maildir )
80 setMailDir(maildir);
81 if ( mailDir().isEmpty() ) {
82 filterInfo()->alert( i18n( "No directory selected." ) );
83 return;
85 /**
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." ) );
91 } else {
92 filterInfo()->setOverall(0);
93 mImportDirDone = 0;
95 /** Recursive import of the MailFolders */
96 QDir dir(mailDir());
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 */
124 QDir dir(dirName);
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())
132 return;
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
154 // separator.
155 // ----------------------------------------
157 long l = 0;
158 QByteArray input(50,'\0');
159 QRegExp regexp("!.p.0");
160 QFile tbb(FileName);
161 int iFound = 0;
162 int count = 0;
163 long endOfEmail = 0;
164 QList<long> offsets;
166 if (!tbb.open(QIODevice::ReadOnly)) {
167 filterInfo()->alert(i18n("Unable to open %1, skipping", FileName));
168 } else {
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 !!!
172 // Reason:
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()) {
180 tbb.close();
181 return;
183 QString _tmp = input.data();
185 if (tbb.atEnd())
186 break;
188 iFound = _tmp.count(regexp);
189 if(!iFound) {
190 iFound = _tmp.lastIndexOf("!");
191 if (iFound >= 0 &&((l-iFound) < 5) ) {
192 int _i = tbb.pos();
193 tbb.seek((_i - iFound));
195 } else {
196 ++count;
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());
206 tbb.seek(3128);
207 long lastPos = 3128;
208 long endPos = 0;
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()) {
222 tbb.close();
223 return;
225 endPos = *it;
226 QByteArray input(endPos-lastPos,'\0');
227 tbb.read(input.data(), endPos-lastPos);
229 KTemporaryFile tmp;
230 tmp.open();
231 tmp.write( input, endPos-lastPos );
232 tmp.flush();
234 if(filterInfo()->removeDupMessage())
235 addMessage( _path, tmp.fileName() );
236 else
237 addMessage_fastImport( _path, tmp.fileName() );
239 lastPos = endPos + 48;
240 tbb.seek(lastPos);
241 filterInfo()->setCurrent( (int) ( ( (float) tbb.pos() / tbb.size() ) * 100 ));
246 tbb.close();