~ubuntu-branches/ubuntu/gutsy/kde4libs/gutsy

« back to all changes in this revision

Viewing changes to kio/kio/kmimetyperesolver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-02-21 11:00:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070221110012-6kw8khr9knv6lmg1
Tags: 3.80.3-0ubuntu1
New upstream unstable release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "kmimetyperesolver.h"
 
22
#include <kdirmodel.h>
 
23
#include <kfileitem.h>
 
24
#include <kdirlister.h>
 
25
#include <QAbstractItemView>
 
26
#include <QScrollBar>
 
27
#include <QTimer>
 
28
 
 
29
class KMimeTypeResolverPrivate
 
30
{
 
31
public:
 
32
    KMimeTypeResolverPrivate()
 
33
        : m_delayForNonVisibleIcons(10), // TODO set me to 0 when image preview is enabled
 
34
          m_noVisibleIcon(false)
 
35
    {
 
36
        m_timer.setSingleShot(true);
 
37
    }
 
38
 
 
39
    QModelIndex findVisibleIcon();
 
40
 
 
41
    QAbstractItemView* m_view;
 
42
    KDirModel* m_dirModel;
 
43
    int m_delayForNonVisibleIcons;
 
44
    QList<QPersistentModelIndex> m_pendingIndexes;
 
45
    QTimer m_timer;
 
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.
 
48
    bool m_noVisibleIcon;
 
49
};
 
50
 
 
51
 
 
52
QModelIndex KMimeTypeResolverPrivate::findVisibleIcon()
 
53
{
 
54
    if (m_noVisibleIcon)
 
55
        return QModelIndex();
 
56
 
 
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();
 
60
    }
 
61
 
 
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;
 
69
            return *it;
 
70
        }
 
71
    }
 
72
 
 
73
    kDebug() << k_funcinfo << "no more visible icon found" << endl;
 
74
    m_noVisibleIcon = true;
 
75
    return QModelIndex();
 
76
}
 
77
 
 
78
////
 
79
 
 
80
KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, KDirModel* model)
 
81
    : QObject(view), d(new KMimeTypeResolverPrivate)
 
82
{
 
83
    d->m_view = view;
 
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()));
 
93
}
 
94
 
 
95
KMimeTypeResolver::~KMimeTypeResolver()
 
96
{
 
97
    delete d;
 
98
}
 
99
 
 
100
void KMimeTypeResolver::slotProcessMimeIcons()
 
101
{
 
102
    if (d->m_pendingIndexes.isEmpty()) {
 
103
        // Finished
 
104
        return;
 
105
    }
 
106
 
 
107
    int nextDelay = 0;
 
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);
 
113
    }
 
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;
 
119
    }
 
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);
 
126
        }
 
127
    }
 
128
    d->m_timer.start(nextDelay); // singleshot
 
129
}
 
130
 
 
131
void KMimeTypeResolver::slotRowsInserted(const QModelIndex& parent, int first, int last)
 
132
{
 
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);
 
142
    }
 
143
    d->m_noVisibleIcon = false;
 
144
    d->m_timer.start(d->m_delayForNonVisibleIcons); // singleshot
 
145
}
 
146
 
 
147
void KMimeTypeResolver::slotViewportAdjusted()
 
148
{
 
149
    d->m_noVisibleIcon = false;
 
150
    d->m_timer.start(0);
 
151
}
 
152
 
 
153
#include "kmimetyperesolver.moc"