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

akonadi

  • akonadi
collectionstatisticsmodel.cpp
1 /*
2  Copyright (c) 2006 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectionstatisticsmodel.h"
21 
22 #include "collection.h"
23 #include "collectionmodel_p.h"
24 #include "collectionstatistics.h"
25 
26 #include <kdebug.h>
27 #include <KGlobal>
28 #include <klocale.h>
29 
30 #include <QtGui/QFont>
31 
32 using namespace Akonadi;
33 
34 namespace Akonadi {
35 
36 class CollectionStatisticsModelPrivate : public CollectionModelPrivate
37 {
38  public:
39  enum CountType { Total, Unread, Size };
40  Q_DECLARE_PUBLIC( CollectionStatisticsModel )
41  CollectionStatisticsModelPrivate( CollectionStatisticsModel *parent )
42  : CollectionModelPrivate( parent )
43  {}
44 
45  qint64 countRecursive( Collection::Id collection, CountType type ) const;
46 };
47 
48 }
49 
50 qint64 CollectionStatisticsModelPrivate::countRecursive( Collection::Id collection,
51  CountType type ) const
52 {
53  qint64 result = -1;
54  switch ( type ) {
55  case Unread: result = collections.value( collection ).statistics().unreadCount();
56  break;
57  case Total: result = collections.value( collection ).statistics().count();
58  break;
59  case Size: result = collections.value( collection ).statistics().size();
60  break;
61  default: Q_ASSERT( false );
62  break;
63  }
64 
65  const QVector<Collection::Id> children = childCollections.value( collection );
66  foreach ( Collection::Id currentCollection, children ) {
67  result += countRecursive( currentCollection, type );
68  }
69  return result;
70 }
71 
72 CollectionStatisticsModel::CollectionStatisticsModel( QObject * parent ) :
73  CollectionModel( new CollectionStatisticsModelPrivate( this ), parent )
74 {
75  fetchCollectionStatistics( true );
76 }
77 
78 int CollectionStatisticsModel::columnCount( const QModelIndex & parent ) const
79 {
80  if ( parent.isValid() && parent.column() != 0 )
81  return 0;
82  return 4;
83 }
84 
85 QVariant CollectionStatisticsModel::data( const QModelIndex & index, int role ) const
86 {
87  Q_D( const CollectionStatisticsModel );
88  if ( !index.isValid() )
89  return QVariant();
90 
91  Collection col = collectionForId( CollectionModel::data( index, CollectionIdRole ).toLongLong() );
92  if ( !col.isValid() )
93  return QVariant();
94  CollectionStatistics statistics = col.statistics();
95 
96  qint64 total = statistics.count();
97  qint64 unread = statistics.unreadCount();
98  qint64 size = statistics.size();
99  qint64 totalRecursive = d->countRecursive( col.id(),
100  CollectionStatisticsModelPrivate::Total );
101  qint64 unreadRecursive = d->countRecursive( col.id(),
102  CollectionStatisticsModelPrivate::Unread );
103  qint64 sizeRecursive = d->countRecursive( col.id(),
104  CollectionStatisticsModelPrivate::Size );
105 
106  if ( role == TotalRole )
107  return total;
108  else if ( role == UnreadRole )
109  return unread;
110  else if ( role == SizeRole )
111  return size;
112  else if ( role == RecursiveUnreadRole )
113  return unreadRecursive;
114  else if ( role == RecursiveTotalRole )
115  return totalRecursive;
116  else if ( role == RecursiveSizeRole )
117  return sizeRecursive;
118  else if ( role == StatisticsRole ) {
119  QVariant var;
120  var.setValue( statistics );
121  return var;
122  } else if ( role == RecursiveStatisticsRole ) {
123  QVariant var;
124  var.setValue( statistics ); //FIXME:(tmg) returns a recursive statistic object here
125  return var;
126  }
127 
128  if ( role == Qt::DisplayRole &&
129  ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) {
130 
131  qint64 value = -1;
132  switch ( index.column() ) {
133  case 1 : value = unread; break;
134  case 2 : value = total; break;
135  case 3 : value = size; break;
136  }
137  if ( value < 0 )
138  return QString();
139  else if ( value == 0 )
140  return QLatin1String( "-" );
141  else if ( index.column() == 3 )
142  return KGlobal::locale()->formatByteSize( value );
143  else
144  return QString::number( value );
145  }
146 
147  if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) )
148  return Qt::AlignRight;
149 
150  return CollectionModel::data( index, role );
151 }
152 
153 QVariant CollectionStatisticsModel::headerData( int section, Qt::Orientation orientation, int role ) const
154 {
155  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
156  switch ( section ) {
157  case 1: return i18nc( "@title:column, number of unread messages", "Unread" );
158  case 2: return i18nc( "@title:column, total number of messages", "Total" );
159  case 3: return i18nc( "@title:column, total size (in bytes) of the collection", "Size" );
160  }
161 
162  return CollectionModel::headerData( section, orientation, role );
163 }
164 
165 #include "collectionstatisticsmodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:04 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.4 API Reference

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