SVN_SILENT made messages (.desktop file)
[kdepim.git] / mailimporter / filter_evolution_v3.cpp
blobb1702224075d8a743fcee384ee698e1dba45a0c6
1 /*
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 "filter_evolution_v3.h"
20 #include <klocale.h>
21 #include <kfiledialog.h>
22 #include <QPointer>
24 using namespace MailImporter;
26 /** Default constructor. */
27 FilterEvolution_v3::FilterEvolution_v3()
28 :Filter(i18n("Import Evolution 3.x Local Mails and Folder Structure"),
29 "Laurent Montel",
30 i18n("<p><b>Evolution 3.x import filter</b></p>"
31 "<p>Select the base directory of your local Evolution mailfolder (usually ~/.local/share/evolution/mail/local/).</p>"
32 "<p>Since it is possible to recreate the folder structure, the folders "
33 "will be stored under: \"Evolution-Import\".</p>") )
39 /** Destructor. */
40 FilterEvolution_v3::~FilterEvolution_v3()
44 QString FilterEvolution_v3::defaultSettingsPath()
46 return QDir::homePath() + QLatin1String( "/.local/share/evolution/mail/local/" );
50 /** Recursive import of KMail maildir. */
51 void FilterEvolution_v3::import()
53 setCountDuplicates(0);
54 QString evolDir = defaultSettingsPath();
55 QDir d( evolDir );
56 if ( !d.exists() ) {
57 evolDir = QDir::homePath();
60 QPointer<KFileDialog> kfd = new KFileDialog( evolDir, "", 0 );
61 kfd->setMode( KFile::Directory | KFile::LocalOnly );
62 if (kfd->exec()) {
63 const QString dir = kfd->selectedFile();
64 importMails( dir );
66 delete kfd;
70 void FilterEvolution_v3::processDirectory( const QString &path)
72 QDir dir(path);
73 const QStringList rootSubDirs = dir.entryList(QStringList("*"), QDir::Dirs | QDir::Hidden, QDir::Name);
74 QStringList::ConstIterator end = rootSubDirs.constEnd();
75 for (QStringList::ConstIterator filename = rootSubDirs.constBegin() ; filename != end ; ++filename) {
76 if(filterInfo()->shouldTerminate())
77 break;
78 if(!(*filename == QLatin1String( "." ) || *filename == QLatin1String( ".." ))) {
79 filterInfo()->setCurrent(0);
80 importDirContents(dir.filePath(*filename));
81 filterInfo()->setOverall( (mTotalDir > 0 ) ? (int) ((float) mImportDirDone / mTotalDir * 100) : 0);
82 filterInfo()->setCurrent(100);
88 void FilterEvolution_v3::importMails( const QString &maildir )
90 setMailDir(maildir);
91 if ( mailDir().isEmpty() ) {
92 filterInfo()->alert( i18n( "No directory selected." ) );
93 return;
95 /**
96 * If the user only select homedir no import needed because
97 * there should be no files and we surely import wrong files.
99 else if ( mailDir() == QDir::homePath() || mailDir() == ( QDir::homePath() + '/' ) ) {
100 filterInfo()->addErrorLogEntry( i18n( "No files found for import." ) );
101 } else {
102 filterInfo()->setOverall(0);
103 mImportDirDone = 0;
105 /** Recursive import of the MailArchives */
106 QDir dir(mailDir());
107 mTotalDir = Filter::countDirectory( dir, true /*search hidden directory*/ );
109 processDirectory( mailDir() );
111 filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", mailDir() ));
113 if (countDuplicates() > 0) {
114 filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
117 if (filterInfo()->shouldTerminate())
118 filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
120 filterInfo()->setCurrent(100);
121 filterInfo()->setOverall(100);
125 * Import of a directory contents.
126 * @param info Information storage for the operation.
127 * @param dirName The name of the directory to import.
129 void FilterEvolution_v3::importDirContents( const QString &dirName)
132 /** Here Import all archives in the current dir */
133 importFiles(dirName);
135 /** If there are subfolders, we import them one by one */
136 processDirectory( dirName );
140 * Import the files within a Folder.
141 * @param info Information storage for the operation.
142 * @param dirName The name of the directory to import.
144 void FilterEvolution_v3::importFiles( const QString &dirName)
147 QDir dir(dirName);
148 QString _path;
149 bool generatedPath = false;
151 QDir importDir (dirName);
152 const QStringList files = importDir.entryList(QStringList("[^\\.]*"), QDir::Files, QDir::Name);
153 int currentFile = 1, numFiles = files.size();
154 QStringList::ConstIterator filesEnd( files.constEnd() );
156 for ( QStringList::ConstIterator mailFile = files.constBegin(); mailFile != filesEnd; ++mailFile, ++currentFile) {
157 if(filterInfo()->shouldTerminate()) return;
158 QString temp_mailfile = *mailFile;
159 if (!( temp_mailfile.endsWith(QLatin1String(".db"))
160 || temp_mailfile.endsWith(QLatin1String(".cmeta"))
161 || temp_mailfile.endsWith(QLatin1String(".ev-summary"))
162 || temp_mailfile.endsWith(QLatin1String(".ibex.index"))
163 || temp_mailfile.endsWith(QLatin1String(".ibex.index.data")) ) ) {
164 if(!generatedPath) {
165 _path = i18nc("define folder name where we import evolution mails", "Evolution-Import" );
166 QString _tmp = dir.filePath(*mailFile);
167 _tmp = _tmp.remove( mailDir(), Qt::CaseSensitive );
168 QStringList subFList = _tmp.split( QLatin1Char( '/' ), QString::SkipEmptyParts );
169 QStringList::ConstIterator end( subFList.end() );
170 for ( QStringList::ConstIterator it = subFList.constBegin(); it != end; ++it ) {
171 QString _cat = *it;
172 if(!(_cat == *mailFile)) {
173 if (_cat.startsWith(QLatin1Char( '.' ))) {
174 _cat = _cat.remove(0 , 1);
176 //Evolution store inbox as "."
177 if ( _cat.startsWith(QLatin1Char( '.' ))) {
178 _cat = _cat.replace( 0, 1, QLatin1String( "Inbox/" ) );
181 _path += QLatin1Char( '/' ) + _cat;
182 _path.replace( QLatin1Char( '.' ), QLatin1Char( '/' ) );
185 if(_path.endsWith(QLatin1String( "cur" )))
186 _path.remove(_path.length() - 4 , 4);
187 QString _info = _path;
188 filterInfo()->addInfoLogEntry(i18n("Import folder %1...", _info));
189 filterInfo()->setFrom(_info);
190 filterInfo()->setTo(_path);
191 generatedPath = true;
193 Akonadi::MessageStatus status = statusFromFile( *mailFile );
195 if(filterInfo()->removeDupMessage()) {
196 if(! addMessage( _path, dir.filePath(*mailFile),status )) {
197 filterInfo()->addErrorLogEntry( i18n("Could not import %1", *mailFile ) );
199 filterInfo()->setCurrent((int) ((float) currentFile / numFiles * 100));
200 } else {
201 if(! addMessage_fastImport( _path, dir.filePath(*mailFile),status )) {
202 filterInfo()->addErrorLogEntry( i18n("Could not import %1", *mailFile ) );
204 filterInfo()->setCurrent((int) ((float) currentFile / numFiles * 100));
210 Akonadi::MessageStatus FilterEvolution_v3::statusFromFile( const QString &filename)
212 Akonadi::MessageStatus status;
213 const int statusIndex = filename.indexOf( ":2," );
214 if ( statusIndex != -1 ) {
215 const QString statusStr = filename.right( filename.length() - statusIndex -3 );
216 if ( statusStr.contains( QLatin1Char( 'S' ) ) ) {
217 status.setRead( true );
219 if ( statusStr.contains( QLatin1Char( 'F' ) ) ) {
222 if ( statusStr.contains( QLatin1Char( 'R' ) ) ) {
223 status.setReplied( true );
225 if ( statusStr.contains( QLatin1Char( 'P' ) ) ) {
226 status.setForwarded( true );
229 return status;