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#include <klocalizedstring.h>
30
31using namespace Akonadi;
32
33namespace Akonadi {
34
35class CollectionStatisticsModelPrivate : public CollectionModelPrivate
36{
37public:
38 enum CountType {
39 Total,
40 Unread,
41 Size
42 };
43 Q_DECLARE_PUBLIC(CollectionStatisticsModel)
44 CollectionStatisticsModelPrivate(CollectionStatisticsModel *parent)
45 : CollectionModelPrivate(parent)
46 {}
47
48 qint64 countRecursive(Collection::Id collection, CountType type) const;
49};
50
51}
52
53qint64 CollectionStatisticsModelPrivate::countRecursive(Collection::Id collection,
54 CountType type) const
55{
56 qint64 result = -1;
57 switch (type) {
58 case Unread:
59 result = collections.value(collection).statistics().unreadCount();
60 break;
61 case Total:
62 result = collections.value(collection).statistics().count();
63 break;
64 case Size:
65 result = collections.value(collection).statistics().size();
66 break;
67 default:
68 Q_ASSERT(false);
69 break;
70 }
71
72 const QVector<Collection::Id> children = childCollections.value(collection);
73 foreach (Collection::Id currentCollection, children) {
74 result += countRecursive(currentCollection, type);
75 }
76 return result;
77}
78
79CollectionStatisticsModel::CollectionStatisticsModel(QObject *parent)
80 : CollectionModel(new CollectionStatisticsModelPrivate(this), parent)
81{
82 fetchCollectionStatistics(true);
83}
84
85int CollectionStatisticsModel::columnCount(const QModelIndex &parent) const
86{
87 if (parent.isValid() && parent.column() != 0) {
88 return 0;
89 }
90 return 4;
91}
92
93QVariant CollectionStatisticsModel::data(const QModelIndex &index, int role) const
94{
95 Q_D(const CollectionStatisticsModel);
96 if (!index.isValid()) {
97 return QVariant();
98 }
99
100 Collection col = collectionForId(CollectionModel::data(index, CollectionIdRole).toLongLong());
101 if (!col.isValid()) {
102 return QVariant();
103 }
104 CollectionStatistics statistics = col.statistics();
105
106 qint64 total = statistics.count();
107 qint64 unread = statistics.unreadCount();
108 qint64 size = statistics.size();
109 qint64 totalRecursive = d->countRecursive(col.id(),
110 CollectionStatisticsModelPrivate::Total);
111 qint64 unreadRecursive = d->countRecursive(col.id(),
112 CollectionStatisticsModelPrivate::Unread);
113 qint64 sizeRecursive = d->countRecursive(col.id(),
114 CollectionStatisticsModelPrivate::Size);
115
116 if (role == TotalRole) {
117 return total;
118 } else if (role == UnreadRole) {
119 return unread;
120 } else if (role == SizeRole) {
121 return size;
122 } else if (role == RecursiveUnreadRole) {
123 return unreadRecursive;
124 } else if (role == RecursiveTotalRole) {
125 return totalRecursive;
126 } else if (role == RecursiveSizeRole) {
127 return sizeRecursive;
128 } else if (role == StatisticsRole) {
129 QVariant var;
130 var.setValue(statistics);
131 return var;
132 } else if (role == RecursiveStatisticsRole) {
133 QVariant var;
134 var.setValue(statistics); //FIXME:(tmg) returns a recursive statistic object here
135 return var;
136 }
137
138 if (role == Qt::DisplayRole &&
139 (index.column() == 1 || index.column() == 2 || index.column() == 3)) {
140
141 qint64 value = -1;
142 switch (index.column()) {
143 case 1:
144 value = unread;
145 break;
146 case 2:
147 value = total;
148 break;
149 case 3:
150 value = size;
151 break;
152 }
153 if (value < 0) {
154 return QString();
155 } else if (value == 0) {
156 return QLatin1String("-");
157 } else if (index.column() == 3) {
158 return KGlobal::locale()->formatByteSize(value);
159 } else {
160 return QString::number(value);
161 }
162 }
163
164 if (role == Qt::TextAlignmentRole && (index.column() == 1 || index.column() == 2 || index.column() == 3)) {
165 return Qt::AlignRight;
166 }
167
168 return CollectionModel::data(index, role);
169}
170
171QVariant CollectionStatisticsModel::headerData(int section, Qt::Orientation orientation, int role) const
172{
173 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
174 switch (section) {
175 case 1:
176 return i18nc("@title:column, number of unread messages", "Unread");
177 case 2:
178 return i18nc("@title:column, total number of messages", "Total");
179 case 3:
180 return i18nc("@title:column, total size (in bytes) of the collection", "Size");
181 }
182 }
183
184 return CollectionModel::headerData(section, orientation, role);
185}
186