1 /***************************************************************************
2 filter_sylpheed.hxx - Sylpheed 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 ***************************************************************************/
18 #include "filter_sylpheed.hxx"
21 #include <kfiledialog.h>
24 /** Default constructor. */
25 FilterSylpheed::FilterSylpheed( void ) :
26 Filter( i18n( "Import Sylpheed Maildirs and Folder Structure" ),
28 i18n( "<p><b>Sylpheed import filter</b></p>"
29 "<p>Select the base directory of the Sylpheed mailfolder you want to import "
30 "(usually: ~/Mail ).</p>"
31 "<p>Since it is possible to recreate the folder structure, the folders "
32 "will be stored under: \"Sylpheed-Import\" in your local folder.</p>"
33 "<p>This filter also recreates the status of message, e.g. new or forwarded.</p>") )
37 FilterSylpheed::~FilterSylpheed( void )
41 /** Recursive import of Sylpheed maildir. */
42 void FilterSylpheed::import( FilterInfo
*info
)
45 QString _homeDir
= QDir::homePath();
48 kfd
= new KFileDialog( _homeDir
, "", 0 );
49 kfd
->setMode( KFile::Directory
| KFile::LocalOnly
);
51 mailDir
= kfd
->selectedFile();
53 if ( mailDir
.isEmpty() ) {
54 info
->alert( i18n( "No directory selected." ) );
57 * If the user only select homedir no import needed because
58 * there should be no files and we surely import wrong files.
60 else if ( mailDir
== QDir::homePath() || mailDir
== ( QDir::homePath() + '/' ) ) {
61 info
->addLog( i18n( "No files found for import." ) );
65 /** Recursive import of the MailFolders */
67 const QStringList rootSubDirs
= dir
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
68 int currentDir
= 1, numSubDirs
= rootSubDirs
.size();
69 for(QStringList::ConstIterator filename
= rootSubDirs
.constBegin() ; filename
!= rootSubDirs
.constEnd() ; ++filename
, ++currentDir
) {
70 if(info
->shouldTerminate()) break;
71 importDirContents(info
, dir
.filePath(*filename
));
72 info
->setOverall((int) ((float) currentDir
/ numSubDirs
* 100));
76 info
->addLog( i18n("Finished importing emails from %1", mailDir
));
77 if (count_duplicates
> 0) {
78 info
->addLog( i18np("1 duplicate message not imported", "%1 duplicate messages not imported", count_duplicates
));
80 if (info
->shouldTerminate()) info
->addLog( i18n("Finished import, canceled by user."));
82 info
->setCurrent(100);
83 info
->setOverall(100);
88 * Import of a directory contents.
89 * @param info Information storage for the operation.
90 * @param dirName The name of the directory to import.
92 void FilterSylpheed::importDirContents( FilterInfo
*info
, const QString
& dirName
)
94 if(info
->shouldTerminate()) return;
96 /** Here Import all archives in the current dir */
97 importFiles(info
, dirName
);
99 /** If there are subfolders, we import them one by one */
100 QDir
subfolders(dirName
);
101 const QStringList subDirs
= subfolders
.entryList(QStringList("[^\\.]*"), QDir::Dirs
, QDir::Name
);
102 for(QStringList::ConstIterator filename
= subDirs
.constBegin() ; filename
!= subDirs
.constEnd() ; ++filename
) {
103 if(info
->shouldTerminate()) return;
104 importDirContents(info
, subfolders
.filePath(*filename
));
110 * Import the files within a Folder.
111 * @param info Information storage for the operation.
112 * @param dirName The name of the directory to import.
114 void FilterSylpheed::importFiles( FilterInfo
*info
, const QString
& dirName
)
118 bool generatedPath
= false;
120 QHash
<QString
,unsigned long> msgflags
;
122 QDir
importDir (dirName
);
123 const QStringList files
= importDir
.entryList(QStringList("[^\\.]*"), QDir::Files
, QDir::Name
);
124 int currentFile
= 1, numFiles
= files
.size();
126 readMarkFile(info
, dir
.filePath(".sylpheed_mark"), msgflags
);
128 for ( QStringList::ConstIterator mailFile
= files
.constBegin(); mailFile
!= files
.constEnd(); ++mailFile
, ++currentFile
) {
129 if(info
->shouldTerminate()) return;
130 QString _mfile
= *mailFile
;
131 if (!(_mfile
.endsWith(QLatin1String(".sylpheed_cache")) || _mfile
.endsWith(QLatin1String(".sylpheed_mark"))
132 || _mfile
.endsWith(QLatin1String(".mh_sequences")) )) {
134 _path
= "Sylpheed-Import/";
135 QString _tmp
= dir
.filePath(*mailFile
);
136 _tmp
= _tmp
.remove(_tmp
.length() - _mfile
.length() -1, _mfile
.length()+1);
137 _path
+= _tmp
.remove( mailDir
, Qt::CaseSensitive
);
138 QString _info
= _path
;
139 info
->addLog(i18n("Import folder %1...", _info
.remove(0,15)));
141 info
->setFrom(_info
);
143 generatedPath
= true;
147 if (msgflags
[_mfile
])
148 flags
= msgFlagsToString((msgflags
[_mfile
]));
150 if(info
->removeDupMsg
) {
151 if(! addMessage( info
, _path
, dir
.filePath(*mailFile
), flags
)) {
152 info
->addLog( i18n("Could not import %1", *mailFile
) );
154 info
->setCurrent((int) ((float) currentFile
/ numFiles
* 100));
156 if(! addMessage_fastImport( info
, _path
, dir
.filePath(*mailFile
), flags
)) {
157 info
->addLog( i18n("Could not import %1", *mailFile
) );
159 info
->setCurrent((int) ((float) currentFile
/ numFiles
* 100));
166 void FilterSylpheed::readMarkFile( FilterInfo
*info
, const QString
&path
, QHash
<QString
,unsigned long> &dict
)
168 /* Each sylpheed mail directory contains a .sylpheed_mark file which
169 * contains all the flags for each messages. The layout of this file
170 * is documented in the source code of sylpheed: in procmsg.h for
171 * the flag bits, and procmsg.c.
173 * Note that the mark file stores 32 bit unsigned integers in the
174 * platform's native "endianness".
176 * The mark file starts with a 32 bit unsigned integer with a version
177 * number. It is then followed by pairs of 32 bit unsigned integers,
178 * the first one with the message file name (which is a number),
179 * and the second one with the actual message flags */
184 if (!file
.open(QIODevice::ReadOnly
))
187 QDataStream
stream(&file
);
189 if (Q_BYTE_ORDER
== Q_LITTLE_ENDIAN
)
190 stream
.setByteOrder(QDataStream::LittleEndian
);
194 /* Read version; if the value is reasonably too big, we're looking
195 * at a file created on another platform. I don't have any test
196 * marks/folders, so just ignoring this case */
198 if (in
> (quint32
) 0xffff)
201 while (!stream
.atEnd()) {
202 if(info
->shouldTerminate()){
210 dict
.insert(s
, flags
);
214 QString
FilterSylpheed::msgFlagsToString(unsigned long flags
)
218 /* see sylpheed's procmsg.h */
219 if (flags
& 1UL) status
+= 'N';
220 if (flags
& 2UL) status
+= 'U';
221 if ((flags
& 3UL) == 0UL) status
+= 'R';
222 if (flags
& 8UL) status
+= 'D';
223 if (flags
& 16UL) status
+= 'A';
224 if (flags
& 32UL) status
+= 'F';