Remove KActionCollection::setComponentData: this does not work on a KActionCollection...
[kdepim.git] / kmailcvt / filter_kmail_maildir.cxx
blob9593396aca636b33066464cc98ffbb12b1ed31a3
1 /***************************************************************************
2 filter_kmail_maildir.cxx - Kmail maildir mail import
3 -------------------
4 begin : April 06 2005
5 copyright : (C) 2005 by Danny Kukawka
6 email : danny.kukawka@web.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
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. *
15 * *
16 ***************************************************************************/
18 #include "filter_kmail_maildir.hxx"
20 #include <klocale.h>
21 #include <kfiledialog.h>
23 /** Default constructor. */
24 FilterKMail_maildir::FilterKMail_maildir( void ) :
25 Filter( i18n( "Import KMail Maildirs and Folder Structure" ),
26 "Danny Kukawka",
27 i18n( "<p><b>KMail import filter</b></p>"
28 "<p>Select the base directory of the KMail mailfolder you want to import.</p>"
29 "<p><b>Note:</b> Never select your current local KMail maildir (usually "
30 "~/Mail or ~/.kde/share/apps/kmail/mail ): in this case, KMailCVT may become stuck "
31 "in a continuous loop. </p>"
32 "<p>This filter does not import KMail mailfolders with mbox files.</p>"
33 "<p>Since it is possible to recreate the folder structure, the folders "
34 "will be stored under: \"KMail-Import\" in your local folder.</p>" ) )
37 /** Destructor. */
38 FilterKMail_maildir::~FilterKMail_maildir( void )
42 /** Recursive import of KMail maildir. */
43 void FilterKMail_maildir::import( FilterInfo *info )
46 QString _homeDir = QDir::homePath();
48 KFileDialog *kfd;
49 kfd = new KFileDialog( _homeDir, "", 0 );
50 kfd->setMode( KFile::Directory | KFile::LocalOnly );
51 kfd->exec();
52 mailDir = kfd->selectedFile();
54 if ( mailDir.isEmpty() ) {
55 info->alert( i18n( "No directory selected." ) );
57 /**
58 * If the user only select homedir no import needed because
59 * there should be no files and we surely import wrong files.
61 else if ( mailDir == QDir::homePath() || mailDir == ( QDir::homePath() + '/' ) ) {
62 info->addLog( i18n( "No files found for import." ) );
63 } else {
64 info->setOverall(0);
66 /** Recursive import of the MailArchives */
67 QDir dir(mailDir);
68 const QStringList rootSubDirs = dir.entryList(QStringList("*"), QDir::Dirs | QDir::Hidden, QDir::Name);
69 int currentDir = 1, numSubDirs = rootSubDirs.size();
70 for(QStringList::ConstIterator filename = rootSubDirs.constBegin() ; filename != rootSubDirs.constEnd() ; ++filename, ++currentDir) {
71 if(info->shouldTerminate()) break;
72 if(!(*filename == "." || *filename == "..")) {
73 info->setCurrent(0);
74 importDirContents(info, dir.filePath(*filename));
75 info->setOverall((int) ((float) currentDir / numSubDirs * 100));
76 info->setCurrent(100);
81 info->addLog( i18n("Finished importing emails from %1", mailDir ));
82 if (count_duplicates > 0) {
83 info->addLog( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", count_duplicates));
85 if (info->shouldTerminate()) info->addLog( i18n("Finished import, canceled by user."));
86 count_duplicates = 0;
87 info->setCurrent(100);
88 info->setOverall(100);
89 delete kfd;
92 /**
93 * Import of a directory contents.
94 * @param info Information storage for the operation.
95 * @param dirName The name of the directory to import.
97 void FilterKMail_maildir::importDirContents( FilterInfo *info, const QString& dirName)
100 /** Here Import all archives in the current dir */
101 importFiles(info, dirName);
103 /** If there are subfolders, we import them one by one */
105 QDir subfolders(dirName);
106 const QStringList subDirs = subfolders.entryList(QStringList("*"), QDir::Dirs | QDir::Hidden, QDir::Name);
107 for(QStringList::ConstIterator filename = subDirs.constBegin() ; filename != subDirs.constEnd() ; ++filename) {
108 if(info->shouldTerminate()) return;
109 if(!(*filename == "." || *filename == "..")) {
110 importDirContents(info, subfolders.filePath(*filename));
116 * Import the files within a Folder.
117 * @param info Information storage for the operation.
118 * @param dirName The name of the directory to import.
120 void FilterKMail_maildir::importFiles( FilterInfo *info, const QString& dirName)
123 QDir dir(dirName);
124 QString _path;
125 bool generatedPath = false;
127 QDir importDir (dirName);
128 const QStringList files = importDir.entryList(QStringList("[^\\.]*"), QDir::Files, QDir::Name);
129 int currentFile = 1, numFiles = files.size();
130 for ( QStringList::ConstIterator mailFile = files.constBegin(); mailFile != files.constEnd(); ++mailFile, ++currentFile) {
131 if(info->shouldTerminate()) return;
132 QString temp_mailfile = *mailFile;
133 if (!(temp_mailfile.endsWith(QLatin1String(".index")) || temp_mailfile.endsWith(QLatin1String(".index.ids")) ||
134 temp_mailfile.endsWith(QLatin1String(".index.sorted")) || temp_mailfile.endsWith(QLatin1String(".uidcache")) )) {
135 if(!generatedPath) {
136 _path = "KMail-Import";
137 QString _tmp = dir.filePath(*mailFile);
138 _tmp = _tmp.remove( mailDir, Qt::CaseSensitive );
139 QStringList subFList = _tmp.split( '/', QString::SkipEmptyParts );
140 for ( QStringList::Iterator it = subFList.begin(); it != subFList.end(); ++it ) {
141 QString _cat = *it;
142 if(!(_cat == *mailFile)) {
143 if(_cat.startsWith('.') && _cat.endsWith(".directory")) {
144 _cat.remove(0,1);
145 _cat.remove((_cat.length() - 10), 10);
146 } else if (_cat.startsWith('.')) {
147 _cat = _cat.remove(0 , 1);
149 _path += '/' + _cat;
152 if(_path.endsWith("cur"))
153 _path.remove(_path.length() - 4 , 4);
154 QString _info = _path;
155 info->addLog(i18n("Import folder %1...", _info.remove(0,12)));
156 info->setFrom(_info);
157 info->setTo(_path);
158 generatedPath = true;
161 if(info->removeDupMsg) {
162 if(! addMessage( info, _path, dir.filePath(*mailFile) )) {
163 info->addLog( i18n("Could not import %1", *mailFile ) );
165 info->setCurrent((int) ((float) currentFile / numFiles * 100));
166 } else {
167 if(! addMessage_fastImport( info, _path, dir.filePath(*mailFile) )) {
168 info->addLog( i18n("Could not import %1", *mailFile ) );
170 info->setCurrent((int) ((float) currentFile / numFiles * 100));