2 Copyright (c) 2012 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "filterbalsa.h"
21 #include <kfiledialog.h>
25 using namespace MailImporter
;
27 /** Default constructor. */
28 FilterBalsa::FilterBalsa()
29 :Filter(i18n("Import Balsa Local Mails and Folder Structure"),
31 i18n("<p><b>Balsa import filter</b></p>"
32 "<p>Select the base directory of your local Balsa mailfolder (usually ~/mail/).</p>"
33 "<p>Since it is possible to recreate the folder structure, the folders "
34 "will be stored under: \"Balsa-Import\".</p>") )
39 FilterBalsa::~FilterBalsa()
43 QString
FilterBalsa::defaultSettingsPath()
45 return QDir::homePath() + QLatin1String("/.balsa/");
48 QString
FilterBalsa::localMailDirPath()
50 return QDir::homePath() + QLatin1String( "/mail/" );
53 /** Recursive import of KMail maildir. */
54 void FilterBalsa::import()
56 setCountDuplicates(0);
57 QString balsaDir
= localMailDirPath();
60 balsaDir
= QDir::homePath();
63 QPointer
<KFileDialog
> kfd
= new KFileDialog( balsaDir
, "", 0 );
64 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
66 const QString dir
= kfd
->selectedFile();
72 void FilterBalsa::processDirectory( const QString
&path
)
75 const QStringList rootSubDirs
= dir
.entryList(QStringList("*"), QDir::Dirs
| QDir::Hidden
, QDir::Name
);
76 QStringList::ConstIterator end
= rootSubDirs
.constEnd();
77 for (QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= end
; ++filename
) {
78 if(filterInfo()->shouldTerminate())
80 if(!(*filename
== QLatin1String( "." ) || *filename
== QLatin1String( ".." ))) {
81 filterInfo()->setCurrent(0);
82 importDirContents(dir
.filePath(*filename
));
83 filterInfo()->setOverall((int) ((float) mImportDirDone
/ mTotalDir
* 100));
84 filterInfo()->setCurrent(100);
90 void FilterBalsa::importMails( const QString
&maildir
)
93 if ( mailDir().isEmpty() ) {
94 filterInfo()->alert( i18n( "No directory selected." ) );
98 * If the user only select homedir no import needed because
99 * there should be no files and we surely import wrong files.
101 else if ( mailDir() == QDir::homePath() || mailDir() == ( QDir::homePath() + '/' ) ) {
102 filterInfo()->addErrorLogEntry( i18n( "No files found for import." ) );
104 filterInfo()->setOverall(0);
107 /** Recursive import of the MailArchives */
109 mTotalDir
= Filter::countDirectory( dir
, true /*search hidden directory*/ );
111 processDirectory( mailDir() );
113 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", mailDir() ));
115 if (countDuplicates() > 0) {
116 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
119 if (filterInfo()->shouldTerminate())
120 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
122 filterInfo()->setCurrent(100);
123 filterInfo()->setOverall(100);
127 * Import of a directory contents.
128 * @param info Information storage for the operation.
129 * @param dirName The name of the directory to import.
131 void FilterBalsa::importDirContents( const QString
&dirName
)
134 /** Here Import all archives in the current dir */
135 importFiles(dirName
);
137 /** If there are subfolders, we import them one by one */
138 processDirectory( dirName
);
142 * Import the files within a Folder.
143 * @param info Information storage for the operation.
144 * @param dirName The name of the directory to import.
146 void FilterBalsa::importFiles( const QString
&dirName
)
151 bool generatedPath
= false;
153 QDir
importDir (dirName
);
154 const QStringList files
= importDir
.entryList(QStringList("[^\\.]*"), QDir::Files
, QDir::Name
);
155 int currentFile
= 1, numFiles
= files
.size();
156 QStringList::ConstIterator
filesEnd( files
.constEnd() );
158 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= filesEnd
; ++mailFile
, ++currentFile
) {
159 if(filterInfo()->shouldTerminate()) return;
160 QString temp_mailfile
= *mailFile
;
161 if (!( temp_mailfile
.endsWith(QLatin1String(".db"))
162 || temp_mailfile
.endsWith(QLatin1String(".cmeta"))
163 || temp_mailfile
.endsWith(QLatin1String(".ev-summary"))
164 || temp_mailfile
.endsWith(QLatin1String(".ibex.index"))
165 || temp_mailfile
.endsWith(QLatin1String(".ibex.index.data")) ) ) {
167 _path
= i18nc("define folder name where we import evolution mails", "Evolution-Import" );
168 QString _tmp
= dir
.filePath(*mailFile
);
169 _tmp
= _tmp
.remove( mailDir(), Qt::CaseSensitive
);
170 QStringList subFList
= _tmp
.split( QLatin1Char( '/' ), QString::SkipEmptyParts
);
171 QStringList::ConstIterator
end( subFList
.end() );
172 for ( QStringList::ConstIterator it
= subFList
.constBegin(); it
!= end
; ++it
) {
174 if(!(_cat
== *mailFile
)) {
175 if (_cat
.startsWith(QLatin1Char( '.' ))) {
176 _cat
= _cat
.remove(0 , 1);
178 //Evolution store inbox as "."
179 if ( _cat
.startsWith(QLatin1Char( '.' ))) {
180 _cat
= _cat
.replace( 0, 1, QLatin1String( "Inbox/" ) );
183 _path
+= QLatin1Char( '/' ) + _cat
;
184 _path
.replace( QLatin1Char( '.' ), QLatin1Char( '/' ) );
187 if(_path
.endsWith(QLatin1String( "cur" )))
188 _path
.remove(_path
.length() - 4 , 4);
189 QString _info
= _path
;
190 filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info
));
191 filterInfo()->setFrom(_info
);
192 filterInfo()->setTo(_path
);
193 generatedPath
= true;
195 Akonadi::MessageStatus status
= statusFromFile( *mailFile
);
197 if(filterInfo()->removeDupMessage()) {
198 if(! addMessage( _path
, dir
.filePath(*mailFile
),status
)) {
199 filterInfo()->addErrorLogEntry( i18n("Could not import %1", *mailFile
) );
201 filterInfo()->setCurrent((int) ((float) currentFile
/ numFiles
* 100));
203 if(! addMessage_fastImport( _path
, dir
.filePath(*mailFile
),status
)) {
204 filterInfo()->addErrorLogEntry( i18n("Could not import %1", *mailFile
) );
206 filterInfo()->setCurrent((int) ((float) currentFile
/ numFiles
* 100));
212 Akonadi::MessageStatus
FilterBalsa::statusFromFile( const QString
&filename
)
214 Akonadi::MessageStatus status
;
215 const int statusIndex
= filename
.indexOf( ":2," );
216 if ( statusIndex
!= -1 ) {
217 const QString statusStr
= filename
.right( filename
.length() - statusIndex
-3 );
218 if ( statusStr
.contains( QLatin1Char( 'S' ) ) ) {
219 status
.setRead( true );
221 if ( statusStr
.contains( QLatin1Char( 'F' ) ) ) {
224 if ( statusStr
.contains( QLatin1Char( 'R' ) ) ) {
225 status
.setReplied( true );
227 if ( statusStr
.contains( QLatin1Char( 'P' ) ) ) {
228 status
.setForwarded( true );