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.hxx"
30 FilterMailApp::FilterMailApp() :
31 Filter( i18n("Import From OS X Mail"),
32 "Chris Howells<br /><br />Filter accelerated by Danny Kukawka )",
33 i18n("<p><b>OS X Mail Import Filter</b></p>"
34 "<p>This filter imports e-mails from the Mail client in Apple Mac OS X.</p>"))
37 FilterMailApp::~FilterMailApp()
41 void FilterMailApp::import(FilterInfo
*info
)
44 int overall_status
= 0;
45 bool first_msg
= true;
47 const QString directory
= KFileDialog::getExistingDirectory( QDir::homePath(), info
->parent() );
50 // kDebug() <<"starting by looking in directory" << directory;
51 traverseDirectory(directory
);
53 for ( QStringList::ConstIterator filename
= mMboxFiles
.constBegin(); filename
!= mMboxFiles
.constEnd(); ++filename
, ++currentFile
) {
54 if ( info
->shouldTerminate() ) break;
55 QFile
mbox( *filename
);
56 if (! mbox
.open( QIODevice::ReadOnly
) ) {
57 info
->alert( i18n("Unable to open %1, skipping", *filename
) );
59 QFileInfo
filenameInfo( *filename
);
60 kDebug() <<"importing filename" << *filename
;
61 QStringList name
= (*filename
).split('/', QString::SkipEmptyParts
);
62 QString
folderName(name
[name
.count() - 2]);
65 info
->addLog( i18n("Importing emails from %1...", *filename
) );
66 info
->setFrom( *filename
);
67 info
->setTo( folderName
);
69 QByteArray
input(MAX_LINE
,'\0');
72 while ( ! mbox
.atEnd() ) {
76 * Don't use QTextStream to read from mbox, better use QDataStream. QTextStream only
77 * support Unicode/Latin1/Locale. So you lost information from emails with
78 * charset!=Unicode/Latin1/Locale (e.g. KOI8-R) and Content-Transfer-Encoding != base64
79 * (e.g. 8Bit). It also not help to convert the QTextStream to Unicode. By this you
80 * get Unicode/UTF-email but KMail can't detect the correct charset.
85 tmp
.write( input
, l
);
86 l
= mbox
.readLine( input
.data(),MAX_LINE
); // read the first line, prevent "From "
87 tmp
.write( input
, l
);
89 while ( ! mbox
.atEnd() && (l
= mbox
.readLine(input
.data(),MAX_LINE
)) && ((separate
= input
.data()).left(5) != "From ")) {
90 tmp
.write( input
, l
);
95 /* comment by Danny Kukawka:
96 * addMessage() == old function, need more time and check for duplicates
97 * addMessage_fastImport == new function, faster and no check for duplicates
99 if(info
->removeDupMsg
)
100 addMessage( info
, folderName
, tmp
.fileName() );
102 addMessage_fastImport( info
, folderName
, tmp
.fileName() );
104 int currentPercentage
= (int) ( ( (float) mbox
.pos() / filenameInfo
.size() ) * 100 );
105 info
->setCurrent( currentPercentage
);
106 if (currentFile
== 1)
107 overall_status
= (int)( currentPercentage
*((float)currentFile
/mMboxFiles
.count()));
109 overall_status
= (int)(((currentFile
-1)*(100.0/(float)mMboxFiles
.count()))+(currentPercentage
*(1.0/(float)mMboxFiles
.count())));
110 info
->setOverall( overall_status
);
111 if ( info
->shouldTerminate() ) break;
114 info
->addLog( i18n("Finished importing emails from %1", *filename
) );
115 if (count_duplicates
> 0) {
116 info
->addLog( i18np("1 duplicate message not imported to folder %2 in KMail",
117 "%1 duplicate messages not imported to folder %2 in KMail", count_duplicates
, folderName
));
119 count_duplicates
= 0;
123 if (info
->shouldTerminate()) info
->addLog( i18n("Finished import, canceled by user."));
126 void FilterMailApp::traverseDirectory(const QString
&dirName
)
129 dir
.setFilter(QDir::Dirs
| QDir::Files
);
131 const QFileInfoList fileinfolist
= dir
.entryInfoList();
132 Q_FOREACH( const QFileInfo
&fi
, fileinfolist
) {
133 if (fi
.fileName() == "." || fi
.fileName() == "..") {
136 if (fi
.isDir() && fi
.isReadable()) {
137 traverseDirectory(fi
.filePath());
139 if (!fi
.isDir() && fi
.fileName() == "mbox") {
140 kDebug() <<"adding the file" << fi
.filePath();
141 mMboxFiles
.append(fi
.filePath());