21 #include "removeduplicatescommand_p.h"
24 #include "akonadi/itemfetchjob.h"
25 #include "akonadi/itemfetchscope.h"
26 #include "akonadi/itemdeletejob.h"
27 #include "kmime/kmime_message.h"
29 RemoveDuplicatesCommand::RemoveDuplicatesCommand(
const QAbstractItemModel* model,
const Akonadi::Collection::List& folders, QObject* parent ) :
34 mJobCount = mFolders.size();
37 void RemoveDuplicatesCommand::execute()
39 if ( mJobCount <= 0 ) {
46 void RemoveDuplicatesCommand::fetchItem()
51 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFetchDone(KJob*)) );
54 void RemoveDuplicatesCommand::slotFetchDone( KJob* job )
59 Util::showJobError(job);
69 QMap<QByteArray, uint> messageIds;
70 QMap<uint, QList<uint> > duplicates;
71 QMap<uint, uint> bodyHashes;
72 const int numberOfItems( items.size() );
73 for (
int i = 0; i < numberOfItems; ++i ) {
75 if ( item.
hasPayload<KMime::Message::Ptr>() ) {
76 KMime::Message::Ptr message = item.
payload<KMime::Message::Ptr>();
77 QByteArray idStr = message->messageID()->as7BitString(
false );
83 if ( messageIds.contains( idStr ) ) {
84 uint mainId = messageIds.value( idStr );
85 if ( !bodyHashes.contains( mainId ) )
86 bodyHashes[ mainId ] = qHash( items[mainId].payload<KMime::Message::Ptr>()->encodedContent() );
87 uint hash = qHash( message->encodedContent() );
88 kDebug() << idStr << bodyHashes[ mainId ] << hash;
89 if ( bodyHashes[ mainId ] == hash )
90 duplicates[ mainId ].append( i );
92 messageIds[ idStr ] = i;
98 QMap<uint, QList<uint> >::ConstIterator end( duplicates.constEnd() );
99 for( QMap<uint, QList<uint> >::ConstIterator it = duplicates.constBegin(); it != end; ++it ) {
100 QList<uint>::ConstIterator dupEnd( it.value().constEnd() );
101 for (QList<uint>::ConstIterator dupIt = it.value().constBegin(); dupIt != dupEnd; ++dupIt ) {
102 mDuplicateItems.append( items[*dupIt] );
105 if ( mJobCount > 0 ) {
108 if ( mDuplicateItems.isEmpty() )
116 connect( delCmd, SIGNAL(result(KJob*)),
this, SLOT(slotDeleteItemJobDone(KJob*)) );
121 void RemoveDuplicatesCommand::slotDeleteItemJobDone(KJob* job)
123 if ( job->error() ) {
125 Util::showJobError(job);
126 emitResult( Failed );