• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

collectionstatisticsmodel.cpp

00001 /*
00002     Copyright (c) 2006 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "collectionstatisticsmodel.h"
00021 
00022 #include "collection.h"
00023 #include "collectionmodel_p.h"
00024 #include "collectionstatistics.h"
00025 
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 
00029 #include <QtGui/QFont>
00030 
00031 using namespace Akonadi;
00032 
00033 namespace Akonadi {
00034 
00035 class CollectionStatisticsModelPrivate : public CollectionModelPrivate
00036 {
00037   public:
00038     enum CountType { Total, Unread };
00039     Q_DECLARE_PUBLIC( CollectionStatisticsModel )
00040     CollectionStatisticsModelPrivate( CollectionStatisticsModel *parent )
00041         : CollectionModelPrivate( parent )
00042     {}
00043 
00044     qint64 countRecursive( Collection::Id collection, CountType type ) const;
00045 };
00046 
00047 }
00048 
00049 qint64 CollectionStatisticsModelPrivate::countRecursive( Collection::Id collection,
00050                                                          CountType type ) const
00051 {
00052   qint64 result = -1;
00053   switch ( type ) {
00054     case Unread: result = collections.value( collection ).statistics().unreadCount();
00055                  break;
00056     case Total: result = collections.value( collection ).statistics().count();
00057                 break;
00058     default: Q_ASSERT( false );
00059              break;
00060   }
00061 
00062   QList<Collection::Id> children = childCollections.value( collection );
00063   foreach( Collection::Id currentCollection, children ) {
00064     result += countRecursive( currentCollection, type );
00065   }
00066   return result;
00067 }
00068 
00069 CollectionStatisticsModel::CollectionStatisticsModel( QObject * parent ) :
00070     CollectionModel( new CollectionStatisticsModelPrivate( this ), parent )
00071 {
00072   fetchCollectionStatistics( true );
00073 }
00074 
00075 int CollectionStatisticsModel::columnCount( const QModelIndex & parent ) const
00076 {
00077   if ( parent.isValid() && parent.column() != 0 )
00078     return 0;
00079   return 3;
00080 }
00081 
00082 QVariant CollectionStatisticsModel::data( const QModelIndex & index, int role ) const
00083 {
00084   Q_D( const CollectionStatisticsModel );
00085   if ( !index.isValid() )
00086     return QVariant();
00087 
00088   Collection col = collectionForId( CollectionModel::data( index, CollectionIdRole ).toLongLong() );
00089   if ( !col.isValid() )
00090     return QVariant();
00091   CollectionStatistics statistics = col.statistics();
00092 
00093   qint64 total = statistics.count();
00094   qint64 unread = statistics.unreadCount();
00095   qint64 totalRecursive = d->countRecursive( col.id(),
00096                                              CollectionStatisticsModelPrivate::Total );
00097   qint64 unreadRecursive = d->countRecursive( col.id(),
00098                                               CollectionStatisticsModelPrivate::Unread );
00099 
00100   if ( role == TotalRole )
00101     return total;
00102   else if ( role == UnreadRole )
00103     return unread;
00104   else if ( role == RecursiveUnreadRole )
00105     return unreadRecursive;
00106   else if ( role == RecursiveTotalRole )
00107     return totalRecursive;
00108   else if ( role == StatisticsRole ) {
00109     QVariant var;
00110     var.setValue( statistics );
00111     return var;
00112   } else if ( role == RecursiveStatisticsRole ) {
00113     QVariant var;
00114     var.setValue( statistics ); //FIXME:(tmg) returns a recursive statistic object here
00115     return var;
00116   }
00117 
00118   if ( role == Qt::DisplayRole &&
00119        ( index.column() == 1 || index.column() == 2 ) ) {
00120 
00121     qint64 value = -1;
00122     switch ( index.column() ) {
00123       case 1 : value = unread; break;
00124       case 2 : value = total; break;
00125     }
00126     if ( value < 0 )
00127       return QString();
00128     else if ( value == 0 )
00129       return QLatin1String( "-" );
00130     else
00131       return QString::number( value );
00132   }
00133 
00134   if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 ) )
00135     return Qt::AlignRight;
00136 
00137   return CollectionModel::data( index, role );
00138 }
00139 
00140 QVariant CollectionStatisticsModel::headerData( int section, Qt::Orientation orientation, int role ) const
00141 {
00142   if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
00143     switch ( section ) {
00144       case 1: return i18nc( "@title:column, number of unread messages", "Unread" );
00145       case 2: return i18nc( "@title:column, total number of messages", "Total" );
00146     }
00147 
00148   return CollectionModel::headerData( section, orientation, role );
00149 }
00150 
00151 #include "collectionstatisticsmodel.moc"

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
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
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