french -> French
[kdepim.git] / mailimporter / filter_mailapp.cpp
blob349d5d24556e271b6b3d84c1999c717da61b3901
1 /***************************************************************************
2 filter_mailapp.cxx - OS X Mail App import
3 -------------------
4 copyright : (C) 2004 by Chris Howells
5 email : howells@kde.org
7 Derived from code by:
8 copyright : (C) 2003 by Laurence Anderson
9 email : l.d.anderson@warwick.ac.uk
11 ***************************************************************************/
13 /***************************************************************************
14 * *
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. *
19 * *
20 ***************************************************************************/
23 #include <klocale.h>
24 #include <kfiledialog.h>
25 #include <ktemporaryfile.h>
26 #include <kdebug.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 )
55 int currentFile = 1;
56 int overall_status = 0;
57 bool first_msg = true;
59 setMailDir(maildir);
60 if ( mailDir().isEmpty() )
62 filterInfo()->alert(i18n("No files selected."));
63 return;
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() )
74 break;
75 QFile mbox( *filename );
76 if (! mbox.open( QIODevice::ReadOnly ) ) {
77 filterInfo()->alert( i18n("Unable to open %1, skipping", *filename ) );
78 } else {
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');
90 long l = 0;
92 while ( ! mbox.atEnd() ) {
93 KTemporaryFile tmp;
94 tmp.open();
95 /* comment by Danny:
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.
102 QByteArray separate;
104 if(!first_msg)
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 );
112 tmp.flush();
113 first_msg = false;
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() );
121 else
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()));
128 else
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);
140 mbox.close();
143 if (filterInfo()->shouldTerminate())
144 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
147 void FilterMailApp::traverseDirectory(const QString &dirName)
149 QDir dir(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( ".." ) ) {
156 continue;
158 if (fi.isDir() && fi.isReadable()) {
159 traverseDirectory(fi.filePath());
160 } else {
161 if (!fi.isDir() && (filename == QLatin1String( "mbox" ))) {
162 kDebug() <<"adding the file" << fi.filePath();
163 mMboxFiles.append(fi.filePath());