• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi

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 "movetotrashcommand_p.h"
00023 #include "util_p.h"
00024 
00025 #include "akonadi/itemfetchjob.h"
00026 #include "akonadi/itemfetchscope.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     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[ mJobCount - 1] , parent() );
00044     job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00045     job->fetchScope().fetchFullPayload();
00046     connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) );
00047 }
00048 
00049 void RemoveDuplicatesCommand::slotFetchDone( KJob* job )
00050 {
00051   mJobCount--;
00052   if ( job->error() ) {
00053     // handle errors
00054     Util::showJobError(job);
00055     emitResult( Failed );
00056     return;
00057   } 
00058   Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
00059   Q_ASSERT( fjob );
00060   Akonadi::Item::List items = fjob->items();
00061 
00062   //find duplicate mails with the same messageid
00063   //if duplicates are found, check the content as well to be sure they are the same
00064   QMap<QByteArray, uint> messageIds;
00065   QMap<uint, QList<uint> > duplicates;
00066   QMap<uint, uint> bodyHashes;
00067   for ( int i = 0; i < items.size(); ++i ) {
00068     Akonadi::Item item = items[i];
00069     if ( item.hasPayload<KMime::Message::Ptr>() ) {
00070       KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
00071       QByteArray idStr = message->messageID()->as7BitString( false );
00072       //TODO: Maybe do some more check in case of idStr.isEmpty()
00073       //like when the first message's body is different from the 2nd,
00074       //but the 2nd is the same as the 3rd, etc.
00075       //if ( !idStr.isEmpty() )
00076       {
00077         if ( messageIds.contains( idStr ) ) {
00078           uint mainId = messageIds.value( idStr );
00079           if ( !bodyHashes.contains( mainId ) )
00080             bodyHashes[ mainId ] = qHash( items[mainId].payload<KMime::Message::Ptr>()->encodedContent() );
00081           uint hash = qHash( message->encodedContent() );
00082           qDebug() << idStr << bodyHashes[ mainId ] << hash;
00083           if ( bodyHashes[ mainId ] == hash )
00084             duplicates[ mainId ].append( i );
00085         } else {
00086           messageIds[ idStr ] = i;
00087         }
00088       }
00089     }
00090   }
00091 
00092   for(  QMap<uint, QList<uint> >::iterator it = duplicates.begin(); it != duplicates.end(); ++it ) {    
00093     for (QList<uint>::iterator dupIt = it.value().begin(); dupIt != it.value().end(); ++dupIt ) {
00094       mDuplicateItems.append( items[*dupIt] );
00095     }
00096   }
00097 
00098   if ( mJobCount > 0 ) {
00099     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[ mJobCount - 1 ] , parent() );
00100     job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00101     job->fetchScope().fetchFullPayload();
00102     connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) );    
00103   } else {
00104     MoveToTrashCommand *trashCmd = new MoveToTrashCommand( mModel, mDuplicateItems, parent() );
00105     connect( trashCmd, SIGNAL(result(Result)), this, SLOT(emitResult(Result)) );
00106     trashCmd->execute();
00107   }
00108 }
00109 

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal