diff --git a/akonadi/collectionstatisticsmodel.cpp b/akonadi/collectionstatisticsmodel.cpp index d3729f58a..dfa45a3ce 100644 --- a/akonadi/collectionstatisticsmodel.cpp +++ b/akonadi/collectionstatisticsmodel.cpp @@ -1,151 +1,162 @@ /* Copyright (c) 2006 Volker Krause This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "collectionstatisticsmodel.h" #include "collection.h" #include "collectionmodel_p.h" #include "collectionstatistics.h" #include #include #include using namespace Akonadi; namespace Akonadi { class CollectionStatisticsModelPrivate : public CollectionModelPrivate { public: - enum CountType { Total, Unread }; + enum CountType { Total, Unread, Size }; Q_DECLARE_PUBLIC( CollectionStatisticsModel ) CollectionStatisticsModelPrivate( CollectionStatisticsModel *parent ) : CollectionModelPrivate( parent ) {} qint64 countRecursive( Collection::Id collection, CountType type ) const; }; } qint64 CollectionStatisticsModelPrivate::countRecursive( Collection::Id collection, CountType type ) const { qint64 result = -1; switch ( type ) { case Unread: result = collections.value( collection ).statistics().unreadCount(); break; case Total: result = collections.value( collection ).statistics().count(); break; + case Size: result = collections.value( collection ).statistics().size(); + break; default: Q_ASSERT( false ); break; } QList children = childCollections.value( collection ); foreach( Collection::Id currentCollection, children ) { result += countRecursive( currentCollection, type ); } return result; } CollectionStatisticsModel::CollectionStatisticsModel( QObject * parent ) : CollectionModel( new CollectionStatisticsModelPrivate( this ), parent ) { fetchCollectionStatistics( true ); } int CollectionStatisticsModel::columnCount( const QModelIndex & parent ) const { if ( parent.isValid() && parent.column() != 0 ) return 0; - return 3; + return 4; } QVariant CollectionStatisticsModel::data( const QModelIndex & index, int role ) const { Q_D( const CollectionStatisticsModel ); if ( !index.isValid() ) return QVariant(); Collection col = collectionForId( CollectionModel::data( index, CollectionIdRole ).toLongLong() ); if ( !col.isValid() ) return QVariant(); CollectionStatistics statistics = col.statistics(); qint64 total = statistics.count(); qint64 unread = statistics.unreadCount(); + qint64 size = statistics.size(); qint64 totalRecursive = d->countRecursive( col.id(), CollectionStatisticsModelPrivate::Total ); qint64 unreadRecursive = d->countRecursive( col.id(), CollectionStatisticsModelPrivate::Unread ); + qint64 sizeRecursive = d->countRecursive( col.id(), + CollectionStatisticsModelPrivate::Size ); if ( role == TotalRole ) return total; else if ( role == UnreadRole ) return unread; + else if ( role == SizeRole ) + return size; else if ( role == RecursiveUnreadRole ) return unreadRecursive; else if ( role == RecursiveTotalRole ) return totalRecursive; + else if ( role == RecursiveSizeRole ) + return sizeRecursive; else if ( role == StatisticsRole ) { QVariant var; var.setValue( statistics ); return var; } else if ( role == RecursiveStatisticsRole ) { QVariant var; var.setValue( statistics ); //FIXME:(tmg) returns a recursive statistic object here return var; } if ( role == Qt::DisplayRole && - ( index.column() == 1 || index.column() == 2 ) ) { + ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) { qint64 value = -1; switch ( index.column() ) { case 1 : value = unread; break; case 2 : value = total; break; + case 3 : value = size; break; } if ( value < 0 ) return QString(); else if ( value == 0 ) return QLatin1String( "-" ); else return QString::number( value ); } - if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 ) ) + if ( role == Qt::TextAlignmentRole && ( index.column() == 1 || index.column() == 2 || index.column() == 3 ) ) return Qt::AlignRight; return CollectionModel::data( index, role ); } QVariant CollectionStatisticsModel::headerData( int section, Qt::Orientation orientation, int role ) const { if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) switch ( section ) { case 1: return i18nc( "@title:column, number of unread messages", "Unread" ); case 2: return i18nc( "@title:column, total number of messages", "Total" ); + case 3: return i18nc( "@title:column, total size (in bytes) of the collection", "Size" ); } return CollectionModel::headerData( section, orientation, role ); } #include "collectionstatisticsmodel.moc" diff --git a/akonadi/collectionstatisticsmodel.h b/akonadi/collectionstatisticsmodel.h index b3f4ce408..2b0f7ff0c 100644 --- a/akonadi/collectionstatisticsmodel.h +++ b/akonadi/collectionstatisticsmodel.h @@ -1,87 +1,90 @@ /* Copyright (c) 2006 Volker Krause This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef AKONADI_COLLECTIONSTATISTICSMODEL_H #define AKONADI_COLLECTIONSTATISTICSMODEL_H #include "akonadi_export.h" #include namespace Akonadi { class CollectionStatisticsModelPrivate; /** * @short A model that provides statistics for collections. * * This model extends the CollectionModel by providing additional * information about the collections, e.g. the number of items - * in a collection or the number of read/unread items. + * in a collection, the number of read/unread items, or the total size + * of the collection. * * Example: * * @code * * QTreeView *view = new QTreeView( this ); * * Akonadi::CollectionStatisticsModel *model = new Akonadi::CollectionStatisticsModel( view ); * view->setModel( model ); * * @endcode * * @author Volker Krause */ class AKONADI_EXPORT CollectionStatisticsModel : public CollectionModel { Q_OBJECT public: /** * Describes the roles for the statistics collection model. */ enum Roles { UnreadRole = CollectionModel::UserRole + 1, ///< The number of unread items in this collection. TotalRole, ///< The number of items in this collection. StatisticsRole, ///< A statistics object of this collection. RecursiveUnreadRole, ///< The number of unread items in this collection and its children. RecursiveTotalRole, ///< The number of items in this collection and its children. RecursiveStatisticsRole, ///< A statistics object of this collection and its children. + SizeRole, ///< The total size of the items in this collection. + RecursiveSizeRole, ///< The total size of the items in this collection and its children. UserRole = CollectionModel::UserRole + 42 ///< Role for user extensions. }; /** * Creates a new collection statistics model. * @param parent The parent object. */ explicit CollectionStatisticsModel( QObject *parent = 0 ); virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const; virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const; virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; private: Q_DECLARE_PRIVATE( CollectionStatisticsModel ) }; } #endif