SVN_SILENT made messages (.desktop file)
[kdepim.git] / mailimporter / filter_evolution_v3.cpp
blob7f8be295f80bdc70824ce6277338f4ebed458cff
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 <KLocalizedString>
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>"))
37 /** Destructor. */
38 FilterEvolution_v3::~FilterEvolution_v3()
42 QString FilterEvolution_v3::defaultSettingsPath()
44 return QDir::homePath() + QLatin1String("/.local/share/evolution/mail/local/");
47 /** Recursive import of KMail maildir. */
48 void FilterEvolution_v3::import()
50 setCountDuplicates(0);
51 QString evolDir = defaultSettingsPath();
52 QDir d(evolDir);
53 if (!d.exists()) {
54 evolDir = QDir::homePath();
57 QPointer<KFileDialog> kfd = new KFileDialog(evolDir, "", 0);
58 kfd->setMode(KFile::Directory | KFile::LocalOnly);
59 if (kfd->exec()) {
60 const QString dir = kfd->selectedFile();
61 importMails(dir);
63 delete kfd;
67 void FilterEvolution_v3::processDirectory(const QString &path)
69 QDir dir(path);
70 const QStringList rootSubDirs = dir.entryList(QStringList("*"), QDir::Dirs | QDir::Hidden, QDir::Name);
71 QStringList::ConstIterator end = rootSubDirs.constEnd();
72 for (QStringList::ConstIterator filename = rootSubDirs.constBegin() ; filename != end ; ++filename) {
73 if (filterInfo()->shouldTerminate()) {
74 break;
76 if (!(*filename == QLatin1String(".") || *filename == QLatin1String(".."))) {
77 filterInfo()->setCurrent(0);
78 importDirContents(dir.filePath(*filename));
79 filterInfo()->setOverall((mTotalDir > 0) ? (int)((float) mImportDirDone / mTotalDir * 100) : 0);
80 filterInfo()->setCurrent(100);
86 void FilterEvolution_v3::importMails(const QString &maildir)
88 setMailDir(maildir);
89 if (mailDir().isEmpty()) {
90 filterInfo()->alert(i18n("No directory selected."));
91 return;
93 /**
94 * If the user only select homedir no import needed because
95 * there should be no files and we surely import wrong files.
97 else if (mailDir() == QDir::homePath() || mailDir() == (QDir::homePath() + QLatin1Char('/'))) {
98 filterInfo()->addErrorLogEntry(i18n("No files found for import."));
99 } else {
100 filterInfo()->setOverall(0);
101 mImportDirDone = 0;
103 /** Recursive import of the MailArchives */
104 QDir dir(mailDir());
105 mTotalDir = Filter::countDirectory(dir, true /*search hidden directory*/);
107 processDirectory(mailDir());
109 filterInfo()->addInfoLogEntry(i18n("Finished importing emails from %1", mailDir()));
111 if (countDuplicates() > 0) {
112 filterInfo()->addInfoLogEntry(i18np("1 duplicate message not imported", "%1 duplicate messages not imported", countDuplicates()));
115 if (filterInfo()->shouldTerminate()) {
116 filterInfo()->addInfoLogEntry(i18n("Finished import, canceled by user."));
119 filterInfo()->setCurrent(100);
120 filterInfo()->setOverall(100);
124 * Import of a directory contents.
125 * @param info Information storage for the operation.
126 * @param dirName The name of the directory to import.
128 void FilterEvolution_v3::importDirContents(const QString &dirName)
131 /** Here Import all archives in the current dir */
132 importFiles(dirName);
134 /** If there are subfolders, we import them one by one */
135 processDirectory(dirName);
139 * Import the files within a Folder.
140 * @param info Information storage for the operation.
141 * @param dirName The name of the directory to import.
143 void FilterEvolution_v3::importFiles(const QString &dirName)
146 QDir dir(dirName);
147 QString _path;
148 bool generatedPath = false;
150 QDir importDir(dirName);
151 const QStringList files = importDir.entryList(QStringList("[^\\.]*"), QDir::Files, QDir::Name);
152 int currentFile = 1, numFiles = files.size();
153 QStringList::ConstIterator filesEnd(files.constEnd());
155 for (QStringList::ConstIterator mailFile = files.constBegin(); mailFile != filesEnd; ++mailFile, ++currentFile) {
156 if (filterInfo()->shouldTerminate()) {
157 return;
159 QString temp_mailfile = *mailFile;
160 if (!(temp_mailfile.endsWith(QLatin1String(".db"))
161 || temp_mailfile.endsWith(QLatin1String(".cmeta"))
162 || temp_mailfile.endsWith(QLatin1String(".ev-summary"))
163 || temp_mailfile.endsWith(QLatin1String(".ibex.index"))
164 || temp_mailfile.endsWith(QLatin1String(".ibex.index.data")))) {
165 if (!generatedPath) {
166 _path = i18nc("define folder name where we import evolution mails", "Evolution-Import");
167 QString _tmp = dir.filePath(*mailFile);
168 _tmp = _tmp.remove(mailDir(), Qt::CaseSensitive);
169 QStringList subFList = _tmp.split(QLatin1Char('/'), QString::SkipEmptyParts);
170 QStringList::ConstIterator end(subFList.end());
171 for (QStringList::ConstIterator it = subFList.constBegin(); it != end; ++it) {
172 QString _cat = *it;
173 if (!(_cat == *mailFile)) {
174 if (_cat.startsWith(QLatin1Char('.'))) {
175 _cat = _cat.remove(0 , 1);
177 //Evolution store inbox as "."
178 if (_cat.startsWith(QLatin1Char('.'))) {
179 _cat = _cat.replace(0, 1, QLatin1String("Inbox/"));
182 _path += QLatin1Char('/') + _cat;
183 _path.replace(QLatin1Char('.'), QLatin1Char('/'));
186 if (_path.endsWith(QLatin1String("cur"))) {
187 _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));
202 } else {
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 FilterEvolution_v3::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);
231 return status;