1 /***************************************************************************
2 filter_mailapp.cxx - OS X Mail App import
4 copyright : (C) 2004 by Chris Howells
5 email : howells@kde.org
8 copyright : (C) 2003 by Laurence Anderson
9 email : l.d.anderson@warwick.ac.uk
11 ***************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 ***************************************************************************/
24 #include <kfiledialog.h>
25 #include <ktemporaryfile.h>
28 #include "filter_mailapp.h"
30 using namespace MailImporter
;
33 FilterMailApp::FilterMailApp() :
34 Filter( i18n("Import From OS X Mail"),
35 "Chris Howells<br /><br />Filter accelerated by Danny Kukawka )",
36 i18n("<p><b>OS X Mail Import Filter</b></p>"
37 "<p>This filter imports e-mails from the Mail client in Apple Mac OS X.</p>"))
41 FilterMailApp::~FilterMailApp()
47 void FilterMailApp::import()
49 const QString directory
= KFileDialog::getExistingDirectory( QDir::homePath(), filterInfo()->parent() );
50 importMails( directory
);
53 void FilterMailApp::importMails( const QString
&maildir
)
56 int overall_status
= 0;
57 bool first_msg
= true;
60 if ( mailDir().isEmpty() )
62 filterInfo()->alert(i18n("No files selected."));
66 filterInfo()->setOverall(0);
68 // kDebug() <<"starting by looking in directory" << directory;
69 traverseDirectory(mailDir());
71 QStringList::ConstIterator
end( mMboxFiles
.constEnd() );
72 for ( QStringList::ConstIterator filename
= mMboxFiles
.constBegin(); filename
!= end
; ++filename
, ++currentFile
) {
73 if ( filterInfo()->shouldTerminate() )
75 QFile
mbox( *filename
);
76 if (! mbox
.open( QIODevice::ReadOnly
) ) {
77 filterInfo()->alert( i18n("Unable to open %1, skipping", *filename
) );
79 QFileInfo
filenameInfo( *filename
);
80 kDebug() <<"importing filename" << *filename
;
81 QStringList name
= (*filename
).split('/', QString::SkipEmptyParts
);
82 QString
folderName(name
[name
.count() - 2]);
84 filterInfo()->setCurrent(0);
85 filterInfo()->addInfoLogEntry( i18n("Importing emails from %1...", *filename
) );
86 filterInfo()->setFrom( *filename
);
87 filterInfo()->setTo( folderName
);
89 QByteArray
input(MAX_LINE
,'\0');
92 while ( ! mbox
.atEnd() ) {
96 * Don't use QTextStream to read from mbox, better use QDataStream. QTextStream only
97 * support Unicode/Latin1/Locale. So you lost information from emails with
98 * charset!=Unicode/Latin1/Locale (e.g. KOI8-R) and Content-Transfer-Encoding != base64
99 * (e.g. 8Bit). It also not help to convert the QTextStream to Unicode. By this you
100 * get Unicode/UTF-email but KMail can't detect the correct charset.
105 tmp
.write( input
, l
);
106 l
= mbox
.readLine( input
.data(),MAX_LINE
); // read the first line, prevent "From "
107 tmp
.write( input
, l
);
109 while ( ! mbox
.atEnd() && (l
= mbox
.readLine(input
.data(),MAX_LINE
)) &&((separate
= input
.data()).left(5) != "From ")) {
110 tmp
.write( input
, l
);
115 /* comment by Danny Kukawka:
116 * addMessage() == old function, need more time and check for duplicates
117 * addMessage_fastImport == new function, faster and no check for duplicates
119 if(filterInfo()->removeDupMessage())
120 addMessage( folderName
, tmp
.fileName() );
122 addMessage_fastImport( folderName
, tmp
.fileName() );
124 int currentPercentage
= (int) ( ( (float) mbox
.pos() / filenameInfo
.size() ) * 100 );
125 filterInfo()->setCurrent( currentPercentage
);
126 if (currentFile
== 1)
127 overall_status
= (int)( currentPercentage
*((float)currentFile
/mMboxFiles
.count()));
129 overall_status
= (int)(((currentFile
-1)*(100.0/(float)mMboxFiles
.count()))+(currentPercentage
*(1.0/(float)mMboxFiles
.count())));
130 filterInfo()->setOverall( overall_status
);
131 if ( filterInfo()->shouldTerminate() ) break;
134 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", *filename
) );
135 if (countDuplicates() > 0) {
136 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported to folder %2 in KMail",
137 "%1 duplicate messages not imported to folder %2 in KMail", countDuplicates(), folderName
));
139 setCountDuplicates(0);
143 if (filterInfo()->shouldTerminate())
144 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
147 void FilterMailApp::traverseDirectory(const QString
&dirName
)
150 dir
.setFilter(QDir::Dirs
| QDir::Files
);
152 const QFileInfoList fileinfolist
= dir
.entryInfoList();
153 Q_FOREACH( const QFileInfo
&fi
, fileinfolist
) {
154 const QString
filename(fi
.fileName());
155 if (filename
== QLatin1String( "." ) || filename
== QLatin1String( ".." ) ) {
158 if (fi
.isDir() && fi
.isReadable()) {
159 traverseDirectory(fi
.filePath());
161 if (!fi
.isDir() & &filename
== QLatin1String( "mbox" )) {
162 kDebug() <<"adding the file" << fi
.filePath();
163 mMboxFiles
.append(fi
.filePath());