• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi/kmime

removeduplicatescommand.cpp
00001 /*
00002     Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
00003     Copyright (c) 2010 Andras Mantia <andras@kdab.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 #include "removeduplicatescommand_p.h"
00022 #include "util_p.h"
00023 
00024 #include "akonadi/itemfetchjob.h"
00025 #include "akonadi/itemfetchscope.h"
00026 #include "akonadi/itemdeletejob.h"
00027 #include "kmime/kmime_message.h"
00028 
00029 RemoveDuplicatesCommand::RemoveDuplicatesCommand( const QAbstractItemModel* model, const Akonadi::Collection::List& folders, QObject* parent ) :
00030   CommandBase( parent )
00031 {
00032   mModel = model;
00033   mFolders = folders;
00034   mJobCount = mFolders.size();
00035 }
00036 
00037 void RemoveDuplicatesCommand::execute()
00038 {
00039     if ( mJobCount <= 0 ) {
00040       emitResult( OK );
00041       return;
00042     }
00043     fetchItem();
00044 }
00045 
00046 void RemoveDuplicatesCommand::fetchItem()
00047 {
00048     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[ mJobCount - 1] , parent() );
00049     job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00050     job->fetchScope().fetchFullPayload();
00051     connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
00052 }
00053 
00054 void RemoveDuplicatesCommand::slotFetchDone( KJob* job )
00055 {
00056   mJobCount--;
00057   if ( job->error() ) {
00058     // handle errors
00059     Util::showJobError(job);
00060     emitResult( Failed );
00061     return;
00062   }
00063   Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
00064   Q_ASSERT( fjob );
00065   Akonadi::Item::List items = fjob->items();
00066 
00067   //find duplicate mails with the same messageid
00068   //if duplicates are found, check the content as well to be sure they are the same
00069   QMap<QByteArray, uint> messageIds;
00070   QMap<uint, QList<uint> > duplicates;
00071   QMap<uint, uint> bodyHashes;
00072   const int numberOfItems( items.size() );
00073   for ( int i = 0; i < numberOfItems; ++i ) {
00074     Akonadi::Item item = items[i];
00075     if ( item.hasPayload<KMime::Message::Ptr>() ) {
00076       KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
00077       QByteArray idStr = message->messageID()->as7BitString( false );
00078       //TODO: Maybe do some more check in case of idStr.isEmpty()
00079       //like when the first message's body is different from the 2nd,
00080       //but the 2nd is the same as the 3rd, etc.
00081       //if ( !idStr.isEmpty() )
00082       {
00083         if ( messageIds.contains( idStr ) ) {
00084           uint mainId = messageIds.value( idStr );
00085           if ( !bodyHashes.contains( mainId ) )
00086             bodyHashes[ mainId ] = qHash( items[mainId].payload<KMime::Message::Ptr>()->encodedContent() );
00087           uint hash = qHash( message->encodedContent() );
00088           kDebug() << idStr << bodyHashes[ mainId ] << hash;
00089           if ( bodyHashes[ mainId ] == hash )
00090             duplicates[ mainId ].append( i );
00091         } else {
00092           messageIds[ idStr ] = i;
00093         }
00094       }
00095     }
00096   }
00097 
00098   QMap<uint, QList<uint> >::ConstIterator end( duplicates.constEnd() );
00099   for(  QMap<uint, QList<uint> >::ConstIterator it = duplicates.constBegin(); it != end; ++it ) {
00100     QList<uint>::ConstIterator dupEnd( it.value().constEnd() );
00101     for (QList<uint>::ConstIterator dupIt = it.value().constBegin(); dupIt != dupEnd; ++dupIt ) {
00102       mDuplicateItems.append( items[*dupIt] );
00103     }
00104   }
00105   if ( mJobCount > 0 ) {
00106     fetchItem();
00107   } else {
00108     if ( mDuplicateItems.isEmpty() )
00109     {
00110       emitResult( OK );
00111       return;
00112     }
00113     else
00114     {
00115       Akonadi::ItemDeleteJob *delCmd = new Akonadi::ItemDeleteJob( mDuplicateItems, parent() );
00116       connect( delCmd, SIGNAL(result(KJob*)), this, SLOT(slotDeleteItemJobDone(KJob*)) );
00117     }
00118   }
00119 }
00120 
00121 void RemoveDuplicatesCommand::slotDeleteItemJobDone(KJob* job)
00122 {
00123   if ( job->error() ) {
00124     // handle errors
00125     Util::showJobError(job);
00126     emitResult( Failed );
00127     return;
00128   }
00129   emitResult( OK );
00130 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:40 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/kmime

Skip menu "akonadi/kmime"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal