• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <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  }
83  return 4;
84 }
85 
86 QVariant CollectionStatisticsModel::data( const QModelIndex & index, int role ) const
87 {
88  Q_D( const CollectionStatisticsModel );
89  if ( !index.isValid() ) {
90  return QVariant();
91  }
92 
93  Collection col = collectionForId( CollectionModel::data( index, CollectionIdRole ).toLongLong() );
94  if ( !col.isValid() ) {
95  return QVariant();
96  }
97  CollectionStatistics statistics = col.statistics();
98 
99  qint64 total = statistics.count();
100  qint64 unread = statistics.unreadCount();
101  qint64 size = statistics.size();
102  qint64 totalRecursive = d->countRecursive( col.id(),
103  CollectionStatisticsModelPrivate::Total );
104  qint64 unreadRecursive = d->countRecursive( col.id(),
105  CollectionStatisticsModelPrivate::Unread );
106  qint64 sizeRecursive = d->countRecursive( col.id(),
107  CollectionStatisticsModelPrivate::Size );
108 
109  if ( role == TotalRole ) {
110  return total;
111  } else if ( role == UnreadRole ) {
112  return unread;
113  } else if ( role == SizeRole ) {
114  return size;
115  } else if ( role == RecursiveUnreadRole ) {
116  return unreadRecursive;
117  } else if ( role == RecursiveTotalRole ) {
118  return totalRecursive;
119  } else if ( role == RecursiveSizeRole ) {
120  return sizeRecursive;
121  } else if ( role == StatisticsRole ) {
122  QVariant var;
123  var.setValue( statistics );
124  return var;
125  } else if ( role == RecursiveStatisticsRole ) {
126  QVariant var;
127  var.setValue( statistics ); //FIXME:(tmg) returns a recursive statistic object here
128  return var;
129  }
130 
131  if ( role == Qt::DisplayRole &&
132  ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) {
133 
134  qint64 value = -1;
135  switch ( index.column() ) {
136  case 1 : value = unread; break;
137  case 2 : value = total; break;
138  case 3 : value = size; break;
139  }
140  if ( value < 0 ) {
141  return QString();
142  } else if ( value == 0 ) {
143  return QLatin1String( "-" );
144  } else if ( index.column() == 3 ) {
145  return KGlobal::locale()->formatByteSize( value );
146  } else {
147  return QString::number( value );
148  }
149  }
150 
151  if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) {
152  return Qt::AlignRight;
153  }
154 
155  return CollectionModel::data( index, role );
156 }
157 
158 QVariant CollectionStatisticsModel::headerData( int section, Qt::Orientation orientation, int role ) const
159 {
160  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
161  switch ( section ) {
162  case 1: return i18nc( "@title:column, number of unread messages", "Unread" );
163  case 2: return i18nc( "@title:column, total number of messages", "Total" );
164  case 3: return i18nc( "@title:column, total size (in bytes) of the collection", "Size" );
165  }
166  }
167 
168  return CollectionModel::headerData( section, orientation, role );
169 }
170 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:33 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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