1
/* This file is part of the KDE libraries
2
Copyright (C) 2000, 2006 David Faure <faure@kde.org>
3
Copyright (C) 2000 Rik Hemsley <rik@kde.org>
4
Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License version 2 as published by the Free Software Foundation.
10
This library 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 GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#include "kmimetyperesolver.h"
22
#include <kdirmodel.h>
23
#include <kfileitem.h>
24
#include <kdirlister.h>
25
#include <QAbstractItemView>
29
class KMimeTypeResolverPrivate
32
KMimeTypeResolverPrivate()
33
: m_delayForNonVisibleIcons(10), // TODO set me to 0 when image preview is enabled
34
m_noVisibleIcon(false)
36
m_timer.setSingleShot(true);
39
QModelIndex findVisibleIcon();
41
QAbstractItemView* m_view;
42
KDirModel* m_dirModel;
43
int m_delayForNonVisibleIcons;
44
QList<QPersistentModelIndex> m_pendingIndexes;
46
// Set to true when findVisibleIcon found no visible index in m_pendingIndexes.
47
// This makes further calls to findVisibleIcon no-ops until this bool is reset to false.
52
QModelIndex KMimeTypeResolverPrivate::findVisibleIcon()
57
if (m_pendingIndexes.count() < 20) { // for few items, it's faster to not bother
58
kDebug() << k_funcinfo << "Few items, returning first one" << endl;
59
return m_pendingIndexes.first();
62
const QRect visibleArea = m_view->viewport()->rect();
63
QList<QPersistentModelIndex>::const_iterator it = m_pendingIndexes.begin();
64
const QList<QPersistentModelIndex>::const_iterator end = m_pendingIndexes.end();
65
for ( ; it != end ; ++it ) {
66
const QRect rect = m_view->visualRect(*it);
67
if (rect.intersects(visibleArea)) {
68
kDebug() << k_funcinfo << "found item at " << rect << " in visibleArea " << visibleArea << endl;
73
kDebug() << k_funcinfo << "no more visible icon found" << endl;
74
m_noVisibleIcon = true;
80
KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, KDirModel* model)
81
: QObject(view), d(new KMimeTypeResolverPrivate)
84
d->m_dirModel = model;
85
connect(d->m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
86
this, SLOT(slotRowsInserted(QModelIndex,int,int)));
87
connect(&d->m_timer, SIGNAL(timeout()),
88
this, SLOT(slotProcessMimeIcons()));
89
connect(d->m_view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
90
this, SLOT(slotViewportAdjusted()));
91
connect(d->m_view->verticalScrollBar(), SIGNAL(valueChanged(int)),
92
this, SLOT(slotViewportAdjusted()));
95
KMimeTypeResolver::~KMimeTypeResolver()
100
void KMimeTypeResolver::slotProcessMimeIcons()
102
if (d->m_pendingIndexes.isEmpty()) {
108
QModelIndex index = d->findVisibleIcon();
109
if (index.isValid()) {
110
// Found a visible item.
111
const int numFound = d->m_pendingIndexes.removeAll(index);
112
Q_ASSERT(numFound == 1);
114
if (!index.isValid()) {
115
// No more visible items.
116
// Do the unvisible ones, then, but with a bigger delay, if so configured
117
index = d->m_pendingIndexes.takeFirst();
118
nextDelay = d->m_delayForNonVisibleIcons;
120
KFileItem* item = d->m_dirModel->itemForIndex(index);
121
if (item) { // check that item still exists
122
if (!item->isMimeTypeKnown()) { // check if someone did it meanwhile
123
kDebug() << k_funcinfo << "Determining mimetype for " << item->url() << endl;
124
item->determineMimeType();
125
d->m_dirModel->itemChanged(index);
128
d->m_timer.start(nextDelay); // singleshot
131
void KMimeTypeResolver::slotRowsInserted(const QModelIndex& parent, int first, int last)
133
KDirModel* model = d->m_dirModel;
134
for (int row = first; row <= last; ++row) {
135
QModelIndex idx = model->index(row, 0, parent);
136
KFileItem* item = model->itemForIndex(idx);
137
if (!item->isMimeTypeKnown())
138
d->m_pendingIndexes.append(idx);
139
// TODO else if (item->isDir() && !item->isLocalFile() /*nor pseudo local...*/ &&
140
// TODO model->data(idx, ChildCountRole).toInt() == KDirModel::ChildCountUnknown)
141
// TODO d->m_pendingIndexes.append(idx);
143
d->m_noVisibleIcon = false;
144
d->m_timer.start(d->m_delayForNonVisibleIcons); // singleshot
147
void KMimeTypeResolver::slotViewportAdjusted()
149
d->m_noVisibleIcon = false;
153
#include "kmimetyperesolver.moc"