1 /***************************************************************************
2 filter_kmail_maildir.cxx - Kmail maildir mail import
5 copyright : (C) 2005 by Danny Kukawka
6 email : danny.kukawka@web.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
17 /* Copyright (c) 2012 Montel Laurent <montel@kde.org> */
20 #include "filter_kmail_maildir.h"
23 #include <kfiledialog.h>
26 using namespace MailImporter
;
28 /** Default constructor. */
29 FilterKMail_maildir::FilterKMail_maildir() :
30 Filter( i18n( "Import KMail Maildirs and Folder Structure" ),
32 i18n( "<p><b>KMail import filter</b></p>"
33 "<p>Select the base directory of the KMail mailfolder you want to import.</p>"
34 "<p><b>Note:</b> Never select your current local KMail maildir (usually "
35 "~/Mail or ~/.kde/share/apps/kmail/mail ): in this case, KMailCVT may become stuck "
36 "in a continuous loop. </p>"
37 "<p>This filter does not import KMail mailfolders with mbox files.</p>"
38 "<p>Since it is possible to recreate the folder structure, the folders "
39 "will be stored under: \"KMail-Import\" in your local folder.</p>" ) )
44 FilterKMail_maildir::~FilterKMail_maildir()
48 /** Recursive import of KMail maildir. */
49 void FilterKMail_maildir::import()
51 setCountDuplicates(0);
52 const QString homeDir
= QDir::homePath();
54 QPointer
<KFileDialog
> kfd
= new KFileDialog( homeDir
, "", 0 );
55 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
57 const QString maildir
= kfd
->selectedFile();
58 importMails( maildir
);
64 void FilterKMail_maildir::processDirectory( const QString
&path
)
67 const QStringList rootSubDirs
= dir
.entryList(QStringList("*"), QDir::Dirs
| QDir::Hidden
, QDir::Name
);
68 QStringList::ConstIterator end
= rootSubDirs
.constEnd();
69 for (QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= end
; ++filename
) {
70 if(filterInfo()->shouldTerminate())
72 if(!(*filename
== QLatin1String( "." ) || *filename
== QLatin1String( ".." ))) {
73 filterInfo()->setCurrent(0);
74 importDirContents(dir
.filePath(*filename
));
75 filterInfo()->setOverall((int) ((float) mImportDirDone
/ mTotalDir
* 100));
76 filterInfo()->setCurrent(100);
82 void FilterKMail_maildir::importMails( const QString
&maildir
)
85 if ( mailDir().isEmpty() ) {
86 filterInfo()->alert( i18n( "No directory selected." ) );
90 * If the user only select homedir no import needed because
91 * there should be no files and we surely import wrong files.
93 else if ( mailDir() == QDir::homePath() || mailDir() == ( QDir::homePath() + '/' ) ) {
94 filterInfo()->addErrorLogEntry( i18n( "No files found for import." ) );
96 filterInfo()->setOverall(0);
99 /** Recursive import of the MailArchives */
101 mTotalDir
= Filter::countDirectory( dir
, true /*search hidden directory*/ );
102 processDirectory( mailDir());
104 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", mailDir() ));
105 if (countDuplicates() > 0) {
106 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
109 if (filterInfo()->shouldTerminate())
110 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
111 filterInfo()->setCurrent(100);
112 filterInfo()->setOverall(100);
116 * Import of a directory contents.
117 * @param info Information storage for the operation.
118 * @param dirName The name of the directory to import.
120 void FilterKMail_maildir::importDirContents( const QString
&dirName
)
123 /** Here Import all archives in the current dir */
124 importFiles(dirName
);
126 /** If there are subfolders, we import them one by one */
128 processDirectory( dirName
);
132 * Import the files within a Folder.
133 * @param info Information storage for the operation.
134 * @param dirName The name of the directory to import.
136 void FilterKMail_maildir::importFiles( const QString
&dirName
)
141 bool generatedPath
= false;
143 QDir
importDir (dirName
);
144 const QStringList files
= importDir
.entryList(QStringList("[^\\.]*"), QDir::Files
, QDir::Name
);
145 int currentFile
= 1, numFiles
= files
.size();
146 QStringList::ConstIterator
filesEnd( files
.constEnd() );
148 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= filesEnd
; ++mailFile
, ++currentFile
) {
149 if(filterInfo()->shouldTerminate()) {
153 QString temp_mailfile
= *mailFile
;
154 if (!(temp_mailfile
.endsWith(QLatin1String(".index")) || temp_mailfile
.endsWith(QLatin1String(".index.ids")) ||
155 temp_mailfile
.endsWith(QLatin1String(".index.sorted")) || temp_mailfile
.endsWith(QLatin1String(".uidcache")) )) {
157 _path
= "KMail-Import";
158 QString _tmp
= dir
.filePath(*mailFile
);
159 _tmp
= _tmp
.remove( mailDir(), Qt::CaseSensitive
);
160 const QStringList subFList
= _tmp
.split( QLatin1Char('/'), QString::SkipEmptyParts
);
161 QStringList::ConstIterator
end( subFList
.end() );
162 for ( QStringList::ConstIterator it
= subFList
.constBegin(); it
!= end
; ++it
) {
164 if(!(_cat
== *mailFile
)) {
165 if(_cat
.startsWith('.') && _cat
.endsWith(".directory")) {
167 _cat
.remove((_cat
.length() - 10), 10);
168 } else if (_cat
.startsWith('.')) {
169 _cat
= _cat
.remove(0 , 1);
174 if(_path
.endsWith("cur"))
175 _path
.remove(_path
.length() - 4 , 4);
176 QString _info
= _path
;
177 filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info
.remove(0,12)));
178 filterInfo()->setFrom(_info
);
179 filterInfo()->setTo(_path
);
180 generatedPath
= true;
183 if(filterInfo()->removeDupMessage()) {
184 if(! addMessage( _path
, dir
.filePath(*mailFile
) )) {
185 filterInfo()->addErrorLogEntry( i18n( "Could not import %1, duplicated message", *mailFile
) );
187 filterInfo()->setCurrent((int) ((float) currentFile
/ numFiles
* 100));
189 if(! addMessage_fastImport( _path
, dir
.filePath(*mailFile
) )) {
190 filterInfo()->addErrorLogEntry( i18n("Could not import %1", *mailFile
) );
192 filterInfo()->setCurrent((int) ((float) currentFile
/ numFiles
* 100));