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

akonadi

  • akonadi
  • kmime
removeduplicatescommand.cpp
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
3  Copyright (c) 2010 Andras Mantia <andras@kdab.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #include "removeduplicatescommand_p.h"
22 #include "util_p.h"
23 
24 #include "akonadi/itemfetchjob.h"
25 #include "akonadi/itemfetchscope.h"
26 #include "akonadi/itemdeletejob.h"
27 #include "kmime/kmime_message.h"
28 
29 RemoveDuplicatesCommand::RemoveDuplicatesCommand( const QAbstractItemModel* model, const Akonadi::Collection::List& folders, QObject* parent ) :
30  CommandBase( parent )
31 {
32  mModel = model;
33  mFolders = folders;
34  mJobCount = mFolders.size();
35 }
36 
37 void RemoveDuplicatesCommand::execute()
38 {
39  if ( mJobCount <= 0 ) {
40  emitResult( OK );
41  return;
42  }
43  fetchItem();
44 }
45 
46 void RemoveDuplicatesCommand::fetchItem()
47 {
48  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[ mJobCount - 1] , parent() );
49  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
50  job->fetchScope().fetchFullPayload();
51  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
52 }
53 
54 void RemoveDuplicatesCommand::slotFetchDone( KJob* job )
55 {
56  mJobCount--;
57  if ( job->error() ) {
58  // handle errors
59  Util::showJobError(job);
60  emitResult( Failed );
61  return;
62  }
63  Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
64  Q_ASSERT( fjob );
65  Akonadi::Item::List items = fjob->items();
66 
67  //find duplicate mails with the same messageid
68  //if duplicates are found, check the content as well to be sure they are the same
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 ) {
74  Akonadi::Item item = items[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 );
78  //TODO: Maybe do some more check in case of idStr.isEmpty()
79  //like when the first message's body is different from the 2nd,
80  //but the 2nd is the same as the 3rd, etc.
81  //if ( !idStr.isEmpty() )
82  {
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 );
91  } else {
92  messageIds[ idStr ] = i;
93  }
94  }
95  }
96  }
97 
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] );
103  }
104  }
105  if ( mJobCount > 0 ) {
106  fetchItem();
107  } else {
108  if ( mDuplicateItems.isEmpty() )
109  {
110  emitResult( OK );
111  return;
112  }
113  else
114  {
115  Akonadi::ItemDeleteJob *delCmd = new Akonadi::ItemDeleteJob( mDuplicateItems, parent() );
116  connect( delCmd, SIGNAL(result(KJob*)), this, SLOT(slotDeleteItemJobDone(KJob*)) );
117  }
118  }
119 }
120 
121 void RemoveDuplicatesCommand::slotDeleteItemJobDone(KJob* job)
122 {
123  if ( job->error() ) {
124  // handle errors
125  Util::showJobError(job);
126  emitResult( Failed );
127  return;
128  }
129  emitResult( OK );
130 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:07 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 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