• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kmimetyperesolver.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000, 2006 David Faure <faure@kde.org>
00003    Copyright (C) 2000 Rik Hemsley <rik@kde.org>
00004    Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kmimetyperesolver.h"
00022 #include <kdirmodel.h>
00023 #include <kfileitem.h>
00024 #include <kdirlister.h>
00025 #include <QAbstractItemView>
00026 #include <QScrollBar>
00027 #include <QTimer>
00028 
00029 class KMimeTypeResolverPrivate
00030 {
00031 public:
00032     KMimeTypeResolverPrivate()
00033         : m_delayForNonVisibleIcons(10), // TODO set me to 0 when image preview is enabled
00034           m_noVisibleIcon(false)
00035     {
00036         m_timer.setSingleShot(true);
00037     }
00038 
00039     void _k_slotRowsInserted(const QModelIndex&,int,int);
00040     void _k_slotViewportAdjusted();
00041     void _k_slotProcessMimeIcons();
00042 
00043     QModelIndex findVisibleIcon();
00044 
00045     QAbstractItemView* m_view;
00046     KDirModel* m_dirModel;
00047     int m_delayForNonVisibleIcons;
00048     QList<QPersistentModelIndex> m_pendingIndexes;
00049     QTimer m_timer;
00050     // Set to true when findVisibleIcon found no visible index in m_pendingIndexes.
00051     // This makes further calls to findVisibleIcon no-ops until this bool is reset to false.
00052     bool m_noVisibleIcon;
00053 };
00054 
00055 
00056 QModelIndex KMimeTypeResolverPrivate::findVisibleIcon()
00057 {
00058     if (m_noVisibleIcon)
00059         return QModelIndex();
00060 
00061     if (m_pendingIndexes.count() < 20) { // for few items, it's faster to not bother
00062         //kDebug() << "Few items, returning first one";
00063         return QModelIndex(m_pendingIndexes.first());
00064     }
00065 
00066     const QRect visibleArea = m_view->viewport()->rect();
00067     QList<QPersistentModelIndex>::const_iterator it = m_pendingIndexes.begin();
00068     const QList<QPersistentModelIndex>::const_iterator end = m_pendingIndexes.end();
00069     for ( ; it != end ; ++it ) {
00070         const QRect rect = m_view->visualRect(*it);
00071         if (rect.intersects(visibleArea)) {
00072             //kDebug() << "found item at " << rect << " in visibleArea " << visibleArea;
00073             return QModelIndex(*it);
00074         }
00075     }
00076 
00077     //kDebug() << "no more visible icon found";
00078     m_noVisibleIcon = true;
00079     return QModelIndex();
00080 }
00081 
00083 
00084 KMimeTypeResolver::KMimeTypeResolver(QAbstractItemView* view, KDirModel* model)
00085     : QObject(view), d(new KMimeTypeResolverPrivate)
00086 {
00087     d->m_view = view;
00088     d->m_dirModel = model;
00089     connect(d->m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
00090             this, SLOT(_k_slotRowsInserted(QModelIndex,int,int)));
00091     connect(&d->m_timer, SIGNAL(timeout()),
00092             this, SLOT(_k_slotProcessMimeIcons()));
00093     connect(d->m_view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
00094             this, SLOT(_k_slotViewportAdjusted()));
00095     connect(d->m_view->verticalScrollBar(), SIGNAL(valueChanged(int)),
00096             this, SLOT(_k_slotViewportAdjusted()));
00097 }
00098 
00099 KMimeTypeResolver::~KMimeTypeResolver()
00100 {
00101     delete d;
00102 }
00103 
00104 void KMimeTypeResolverPrivate::_k_slotProcessMimeIcons()
00105 {
00106     if (m_pendingIndexes.isEmpty()) {
00107         // Finished
00108         return;
00109     }
00110 
00111     int nextDelay = 0;
00112     bool isVisible = false;
00113     QModelIndex index = findVisibleIcon();
00114     if (index.isValid()) {
00115         // Found a visible item.
00116         const int numFound = m_pendingIndexes.removeAll(index);
00117         Q_ASSERT(numFound == 1);
00118         isVisible = true;
00119     } else {
00120         // No more visible items.
00121         // Do the unvisible ones, then, but with a bigger delay, if so configured
00122         index = m_pendingIndexes.takeFirst();
00123         nextDelay = m_delayForNonVisibleIcons;
00124     }
00125     KFileItem item = m_dirModel->itemForIndex(index);
00126     if (!item.isNull()) { // check that item still exists
00127         if (!item.isMimeTypeKnown()) { // check if someone did it meanwhile
00128             //kDebug() << "Determining mimetype for " << item.url();
00129             item.determineMimeType();
00130             if (isVisible) {
00131                 m_dirModel->itemChanged(index);
00132             }
00133         }
00134     }
00135     m_timer.start(nextDelay); // singleshot
00136 }
00137 
00138 void KMimeTypeResolverPrivate::_k_slotRowsInserted(const QModelIndex& parent, int first, int last)
00139 {
00140     KDirModel* model = m_dirModel;
00141     for (int row = first; row <= last; ++row) {
00142         QModelIndex idx = model->index(row, 0, parent);
00143         KFileItem item = model->itemForIndex(idx);
00144         if (!item.isMimeTypeKnown())
00145             m_pendingIndexes.append(idx);
00146         // TODO else if (item->isDir() && !item->isLocalFile() /*nor pseudo local...*/ &&
00147         // TODO   model->data(idx, ChildCountRole).toInt() == KDirModel::ChildCountUnknown)
00148         // TODO d->m_pendingIndexes.append(idx);
00149     }
00150     m_noVisibleIcon = false;
00151     m_timer.start(m_delayForNonVisibleIcons); // singleshot
00152 }
00153 
00154 void KMimeTypeResolverPrivate::_k_slotViewportAdjusted()
00155 {
00156     m_noVisibleIcon = false;
00157     m_timer.start(0);
00158 }
00159 
00160 #include "kmimetyperesolver.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal