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 <KGlobal> 00028 #include <klocale.h> 00029 00030 #include <QtGui/QFont> 00031 00032 using namespace Akonadi; 00033 00034 namespace Akonadi { 00035 00036 class CollectionStatisticsModelPrivate : public CollectionModelPrivate 00037 { 00038 public: 00039 enum CountType { Total, Unread, Size }; 00040 Q_DECLARE_PUBLIC( CollectionStatisticsModel ) 00041 CollectionStatisticsModelPrivate( CollectionStatisticsModel *parent ) 00042 : CollectionModelPrivate( parent ) 00043 {} 00044 00045 qint64 countRecursive( Collection::Id collection, CountType type ) const; 00046 }; 00047 00048 } 00049 00050 qint64 CollectionStatisticsModelPrivate::countRecursive( Collection::Id collection, 00051 CountType type ) const 00052 { 00053 qint64 result = -1; 00054 switch ( type ) { 00055 case Unread: result = collections.value( collection ).statistics().unreadCount(); 00056 break; 00057 case Total: result = collections.value( collection ).statistics().count(); 00058 break; 00059 case Size: result = collections.value( collection ).statistics().size(); 00060 break; 00061 default: Q_ASSERT( false ); 00062 break; 00063 } 00064 00065 const QVector<Collection::Id> children = childCollections.value( collection ); 00066 foreach ( Collection::Id currentCollection, children ) { 00067 result += countRecursive( currentCollection, type ); 00068 } 00069 return result; 00070 } 00071 00072 CollectionStatisticsModel::CollectionStatisticsModel( QObject * parent ) : 00073 CollectionModel( new CollectionStatisticsModelPrivate( this ), parent ) 00074 { 00075 fetchCollectionStatistics( true ); 00076 } 00077 00078 int CollectionStatisticsModel::columnCount( const QModelIndex & parent ) const 00079 { 00080 if ( parent.isValid() && parent.column() != 0 ) 00081 return 0; 00082 return 4; 00083 } 00084 00085 QVariant CollectionStatisticsModel::data( const QModelIndex & index, int role ) const 00086 { 00087 Q_D( const CollectionStatisticsModel ); 00088 if ( !index.isValid() ) 00089 return QVariant(); 00090 00091 Collection col = collectionForId( CollectionModel::data( index, CollectionIdRole ).toLongLong() ); 00092 if ( !col.isValid() ) 00093 return QVariant(); 00094 CollectionStatistics statistics = col.statistics(); 00095 00096 qint64 total = statistics.count(); 00097 qint64 unread = statistics.unreadCount(); 00098 qint64 size = statistics.size(); 00099 qint64 totalRecursive = d->countRecursive( col.id(), 00100 CollectionStatisticsModelPrivate::Total ); 00101 qint64 unreadRecursive = d->countRecursive( col.id(), 00102 CollectionStatisticsModelPrivate::Unread ); 00103 qint64 sizeRecursive = d->countRecursive( col.id(), 00104 CollectionStatisticsModelPrivate::Size ); 00105 00106 if ( role == TotalRole ) 00107 return total; 00108 else if ( role == UnreadRole ) 00109 return unread; 00110 else if ( role == SizeRole ) 00111 return size; 00112 else if ( role == RecursiveUnreadRole ) 00113 return unreadRecursive; 00114 else if ( role == RecursiveTotalRole ) 00115 return totalRecursive; 00116 else if ( role == RecursiveSizeRole ) 00117 return sizeRecursive; 00118 else if ( role == StatisticsRole ) { 00119 QVariant var; 00120 var.setValue( statistics ); 00121 return var; 00122 } else if ( role == RecursiveStatisticsRole ) { 00123 QVariant var; 00124 var.setValue( statistics ); //FIXME:(tmg) returns a recursive statistic object here 00125 return var; 00126 } 00127 00128 if ( role == Qt::DisplayRole && 00129 ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) { 00130 00131 qint64 value = -1; 00132 switch ( index.column() ) { 00133 case 1 : value = unread; break; 00134 case 2 : value = total; break; 00135 case 3 : value = size; break; 00136 } 00137 if ( value < 0 ) 00138 return QString(); 00139 else if ( value == 0 ) 00140 return QLatin1String( "-" ); 00141 else if ( index.column() == 3 ) 00142 return KGlobal::locale()->formatByteSize( value ); 00143 else 00144 return QString::number( value ); 00145 } 00146 00147 if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) 00148 return Qt::AlignRight; 00149 00150 return CollectionModel::data( index, role ); 00151 } 00152 00153 QVariant CollectionStatisticsModel::headerData( int section, Qt::Orientation orientation, int role ) const 00154 { 00155 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) 00156 switch ( section ) { 00157 case 1: return i18nc( "@title:column, number of unread messages", "Unread" ); 00158 case 2: return i18nc( "@title:column, total number of messages", "Total" ); 00159 case 3: return i18nc( "@title:column, total size (in bytes) of the collection", "Size" ); 00160 } 00161 00162 return CollectionModel::headerData( section, orientation, role ); 00163 } 00164 00165 #include "collectionstatisticsmodel.moc"