2 Gwenview: an image viewer
3 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "sorteddirmodel.moc"
21 #include <config-gwenview.h>
27 #include <kdatetime.h>
28 #include <kdirlister.h>
31 #include <lib/archiveutils.h>
32 #include <lib/timeutils.h>
33 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
34 #include <kdirmodel.h>
36 #include "abstractsemanticinfobackend.h"
37 #include "semanticinfodirmodel.h"
42 AbstractSortedDirModelFilter::AbstractSortedDirModelFilter(SortedDirModel
* model
)
47 mModel
->addFilter(this);
51 AbstractSortedDirModelFilter::~AbstractSortedDirModelFilter()
54 mModel
->removeFilter(this);
59 struct SortedDirModelPrivate
{
60 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
61 KDirModel
* mSourceModel
;
63 SemanticInfoDirModel
* mSourceModel
;
65 QStringList mBlackListedExtensions
;
66 QStringList mMimeExcludeFilter
;
67 QList
<AbstractSortedDirModelFilter
*> mFilters
;
68 QTimer mDelayedApplyFiltersTimer
;
72 SortedDirModel::SortedDirModel(QObject
* parent
)
73 : KDirSortFilterProxyModel(parent
)
74 , d(new SortedDirModelPrivate
)
76 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
77 d
->mSourceModel
= new KDirModel(this);
79 d
->mSourceModel
= new SemanticInfoDirModel(this);
81 setSourceModel(d
->mSourceModel
);
82 d
->mDelayedApplyFiltersTimer
.setInterval(0);
83 d
->mDelayedApplyFiltersTimer
.setSingleShot(true);
84 connect(&d
->mDelayedApplyFiltersTimer
, SIGNAL(timeout()), SLOT(doApplyFilters()));
88 SortedDirModel::~SortedDirModel() {
93 void SortedDirModel::addFilter(AbstractSortedDirModelFilter
* filter
) {
94 d
->mFilters
<< filter
;
99 void SortedDirModel::removeFilter(AbstractSortedDirModelFilter
* filter
) {
100 d
->mFilters
.removeAll(filter
);
105 KDirLister
* SortedDirModel::dirLister() {
106 return d
->mSourceModel
->dirLister();
110 void SortedDirModel::reload() {
111 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
112 d
->mSourceModel
->clearSemanticInfoCache();
114 dirLister()->updateDirectory(dirLister()->url());
118 void SortedDirModel::setBlackListedExtensions(const QStringList
& list
) {
119 d
->mBlackListedExtensions
= list
;
123 KFileItem
SortedDirModel::itemForIndex(const QModelIndex
& index
) const {
124 if (!index
.isValid()) {
128 QModelIndex sourceIndex
= mapToSource(index
);
129 return d
->mSourceModel
->itemForIndex(sourceIndex
);
133 KFileItem
SortedDirModel::itemForSourceIndex(const QModelIndex
& sourceIndex
) const {
134 if (!sourceIndex
.isValid()) {
137 return d
->mSourceModel
->itemForIndex(sourceIndex
);
141 QModelIndex
SortedDirModel::indexForItem(const KFileItem
& item
) const {
143 return QModelIndex();
146 QModelIndex sourceIndex
= d
->mSourceModel
->indexForItem(item
);
147 return mapFromSource(sourceIndex
);
151 QModelIndex
SortedDirModel::indexForUrl(const KUrl
& url
) const {
152 if (!url
.isValid()) {
153 return QModelIndex();
155 QModelIndex sourceIndex
= d
->mSourceModel
->indexForUrl(url
);
156 return mapFromSource(sourceIndex
);
160 void SortedDirModel::setMimeExcludeFilter(const QStringList
&mimeList
) {
161 if (d
->mMimeExcludeFilter
== mimeList
) {
164 d
->mMimeExcludeFilter
= mimeList
;
169 bool SortedDirModel::filterAcceptsRow(int row
, const QModelIndex
& parent
) const {
170 QModelIndex index
= d
->mSourceModel
->index(row
, 0, parent
);
171 KFileItem fileItem
= d
->mSourceModel
->itemForIndex(index
);
173 QString extension
= fileItem
.name().section('.', -1).toLower();
174 if (d
->mBlackListedExtensions
.contains(extension
)) {
178 if (!d
->mMimeExcludeFilter
.isEmpty()) {
179 if (d
->mMimeExcludeFilter
.contains(fileItem
.mimetype())) {
183 if (!ArchiveUtils::fileItemIsDirOrArchive(fileItem
)) {
184 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
185 if (!d
->mSourceModel
->semanticInfoAvailableForIndex(index
)) {
186 Q_FOREACH(const AbstractSortedDirModelFilter
* filter
, d
->mFilters
) {
187 // Make sure we have semanticinfo, otherwise retrieve it and
188 // return false, we will be called again later when it is
190 if (filter
->needsSemanticInfo()) {
191 d
->mSourceModel
->retrieveSemanticInfoForIndex(index
);
198 Q_FOREACH(const AbstractSortedDirModelFilter
* filter
, d
->mFilters
) {
199 if (!filter
->acceptsIndex(index
)) {
204 return KDirSortFilterProxyModel::filterAcceptsRow(row
, parent
);
208 AbstractSemanticInfoBackEnd
* SortedDirModel::semanticInfoBackEnd() const {
209 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
212 return d
->mSourceModel
->semanticInfoBackEnd();
217 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
218 SemanticInfo
SortedDirModel::semanticInfoForSourceIndex(const QModelIndex
& sourceIndex
) const {
219 return d
->mSourceModel
->semanticInfoForIndex(sourceIndex
);
224 void SortedDirModel::applyFilters() {
225 d
->mDelayedApplyFiltersTimer
.start();
229 void SortedDirModel::doApplyFilters() {
230 QSortFilterProxyModel::invalidateFilter();
234 bool SortedDirModel::lessThan(const QModelIndex
& left
, const QModelIndex
& right
) const {
235 if (sortRole() != KDirModel::ModifiedTime
) {
236 return KDirSortFilterProxyModel::lessThan(left
, right
);
239 const KDateTime leftDate
= TimeUtils::dateTimeForFileItem(itemForSourceIndex(left
));
240 const KDateTime rightDate
= TimeUtils::dateTimeForFileItem(itemForSourceIndex(right
));
242 return leftDate
< rightDate
;