]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodelrolesupdater.cpp
Prevent crashes caused by nested event loops run when renaming inline
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.cpp
index 942db2bbd9fe306d71795354cbc243b34f940bc4..57beb9d2f63637c4aa98ef4cef1379e24e6d2c04 100644 (file)
 #include <KConfig>
 #include <KConfigGroup>
 #include <KDebug>
+#include <KDirWatch>
 #include <KFileItem>
 #include <KGlobal>
+#include <KIO/JobUiDelegate>
 #include <KIO/PreviewJob>
 
 #include "private/kpixmapmodifier.h"
 
+#include <QApplication>
 #include <QPainter>
 #include <QPixmap>
 #include <QElapsedTimer>
@@ -37,7 +40,7 @@
 
 #ifdef HAVE_NEPOMUK
     #include "private/knepomukrolesprovider.h"
-    #include "private/knepomukresourcewatcher.h"
+    #include "private/nepomuk/resourcewatcher.h"
 #endif
 
 // Required includes for subItemsCount():
@@ -82,7 +85,9 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO
     m_pendingInvisibleItems(),
     m_previewJobs(),
     m_changedItemsTimer(0),
-    m_changedItems()
+    m_changedItems(),
+    m_dirWatcher(0),
+    m_watchedDirs()
   #ifdef HAVE_NEPOMUK
   , m_nepomukResourceWatcher(0),
     m_nepomukUriItems()
@@ -119,10 +124,16 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO
 #ifdef HAVE_NEPOMUK
     m_resolvableRoles += KNepomukRolesProvider::instance().roles();
 #endif
+
+    // When folders are expandable or the item-count is shown for folders, it is necessary
+    // to watch the number of items of the sub-folder to be able to react on changes.
+    m_dirWatcher = new KDirWatch(this);
+    connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
 }
 
 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
 {
+    resetPendingRoles();
 }
 
 void KFileItemModelRolesUpdater::setIconSize(const QSize& size)
@@ -204,7 +215,7 @@ bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
 
 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList& list)
 {
-    if (m_enabledPlugins == list) {
+    if (m_enabledPlugins != list) {
         m_enabledPlugins = list;
         if (m_previewShown) {
             updateAllPreviews();
@@ -316,11 +327,33 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRang
 {
     Q_UNUSED(itemRanges);
 
+    const bool allItemsRemoved = (m_model->count() == 0);
+
+    if (!m_watchedDirs.isEmpty()) {
+        // Don't let KDirWatch watch for removed items
+        if (allItemsRemoved) {
+            foreach (const QString& path, m_watchedDirs) {
+                m_dirWatcher->removeDir(path);
+            }
+            m_watchedDirs.clear();
+        } else {
+            QMutableSetIterator<QString> it(m_watchedDirs);
+            while (it.hasNext()) {
+                const QString& path = it.next();
+                if (m_model->index(KUrl(path)) < 0) {
+                    m_dirWatcher->removeDir(path);
+                    it.remove();
+                }
+            }
+        }
+    }
+
 #ifdef HAVE_NEPOMUK
     if (m_nepomukResourceWatcher) {
         // Don't let the ResourceWatcher watch for removed items
-        if (m_model->count() == 0) {
+        if (allItemsRemoved) {
             m_nepomukResourceWatcher->setResources(QList<Nepomuk::Resource>());
+            m_nepomukResourceWatcher->stop();
             m_nepomukUriItems.clear();
         } else {
             QList<Nepomuk::Resource> newResources;
@@ -335,6 +368,10 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRang
                 }
             }
             m_nepomukResourceWatcher->setResources(newResources);
+            if (newResources.isEmpty()) {
+                Q_ASSERT(m_nepomukUriItems.isEmpty());
+                m_nepomukResourceWatcher->stop();
+            }
         }
     }
 #endif
@@ -345,7 +382,7 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRang
         return;
     }
 
-    if (m_model->count() == 0) {
+    if (allItemsRemoved) {
         // Most probably a directory change is done. Clear all pending items
         // and also kill all ongoing preview-jobs.
         resetPendingRoles();
@@ -402,9 +439,22 @@ void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray& current,
 
 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPixmap& pixmap)
 {
+    const int oldNumberOfPendingItems = m_pendingVisibleItems.count() + m_pendingInvisibleItems.count();
+
     m_pendingVisibleItems.remove(item);
     m_pendingInvisibleItems.remove(item);
 
+    const int newNumberOfPendingItems = m_pendingVisibleItems.count() + m_pendingInvisibleItems.count();
+
+    if (oldNumberOfPendingItems == newNumberOfPendingItems) {
+        // 'item' could not be removed from either of the sets. It looks like
+        // we have hit bug 304986. Replace the items in the sets by the items
+        // in the model to work around the problem.
+        // NOTE: This workaround is not needed any more in KDE 4.10.
+        m_pendingVisibleItems = sortedItems(m_pendingVisibleItems).toSet();
+        m_pendingInvisibleItems = sortedItems(m_pendingInvisibleItems).toSet();
+    }
+
     const int index = m_model->index(item);
     if (index < 0) {
         return;
@@ -462,9 +512,22 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi
 
 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item)
 {
+    const int oldNumberOfPendingItems = m_pendingVisibleItems.count() + m_pendingInvisibleItems.count();
+
     m_pendingVisibleItems.remove(item);
     m_pendingInvisibleItems.remove(item);
 
+    const int newNumberOfPendingItems = m_pendingVisibleItems.count() + m_pendingInvisibleItems.count();
+
+    if (oldNumberOfPendingItems == newNumberOfPendingItems) {
+        // 'item' could not be removed from either of the sets. It looks like
+        // we have hit bug 304986. Replace the items in the sets by the items
+        // in the model to work around the problem.
+        // NOTE: This workaround is not needed any more in KDE 4.10.
+        m_pendingVisibleItems = sortedItems(m_pendingVisibleItems).toSet();
+        m_pendingInvisibleItems = sortedItems(m_pendingInvisibleItems).toSet();
+    }
+
     const bool clearPreviews = m_clearPreviews;
     m_clearPreviews = true;
     applyResolvedRoles(item, ResolveAll);
@@ -558,6 +621,13 @@ void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk::Resourc
 #ifdef HAVE_NEPOMUK
     const KUrl itemUrl = m_nepomukUriItems.value(resource.resourceUri());
     const KFileItem item = m_model->fileItem(itemUrl);
+
+    if (item.isNull()) {
+        // itemUrl is not in the model anymore, probably because
+        // the corresponding file has been deleted in the meantime.
+        return;
+    }
+
     QHash<QByteArray, QVariant> data = rolesData(item);
 
     const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance();
@@ -574,8 +644,33 @@ void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk::Resourc
     connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
             this,    SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
 #else
+#ifndef Q_CC_MSVC
     Q_UNUSED(resource);
 #endif
+#endif
+}
+
+void KFileItemModelRolesUpdater::slotDirWatchDirty(const QString& path)
+{
+    const bool getSizeRole = m_roles.contains("size");
+    const bool getIsExpandableRole = m_roles.contains("isExpandable");
+
+    if (getSizeRole || getIsExpandableRole) {
+        const int index = m_model->index(KUrl(path));
+        if (index >= 0) {
+            QHash<QByteArray, QVariant> data;
+
+            const int count = subItemsCount(path);
+            if (getSizeRole) {
+                data.insert("size", count);
+            }
+            if (getIsExpandableRole) {
+                data.insert("isExpandable", count > 0);
+            }
+
+            m_model->setData(index, data);
+        }
+    }
 }
 
 void KFileItemModelRolesUpdater::startUpdating(const KItemRangeList& itemRanges)
@@ -655,6 +750,9 @@ void KFileItemModelRolesUpdater::startPreviewJob(const KFileItemList& items)
     }
     KIO::PreviewJob* job = new KIO::PreviewJob(itemSubSet, cacheSize, &m_enabledPlugins);
     job->setIgnoreMaximumSize(items.first().isLocalFile());
+    if (job->ui()) {
+        job->ui()->setWindow(qApp->activeWindow());
+    }
 
     connect(job,  SIGNAL(gotPreview(KFileItem,QPixmap)),
             this, SLOT(slotGotPreview(KFileItem,QPixmap)));
@@ -958,6 +1056,11 @@ QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileIte
             if (getIsExpandableRole) {
                 data.insert("isExpandable", count > 0);
             }
+
+            if (!m_dirWatcher->contains(path)) {
+                m_dirWatcher->addDir(path);
+                m_watchedDirs.insert(path);
+            }
         } else if (getSizeRole) {
             data.insert("size", -1); // -1 indicates an unknown number of items
         }
@@ -972,7 +1075,7 @@ QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileIte
 #ifdef HAVE_NEPOMUK
     if (m_nepomukResourceWatcher) {
         const KNepomukRolesProvider& rolesProvider = KNepomukRolesProvider::instance();
-        Nepomuk::Resource resource(item.url());
+        Nepomuk::Resource resource(item.nepomukUri());
         QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(resource, m_roles));
         while (it.hasNext()) {
             it.next();
@@ -987,11 +1090,11 @@ QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileIte
             uri = resource.resourceUri();
         }
         if (!uri.isEmpty() && !m_nepomukUriItems.contains(uri)) {
-            // TODO: Calling stop()/start() is a workaround until
-            // ResourceWatcher has been fixed.
-            m_nepomukResourceWatcher->stop();
             m_nepomukResourceWatcher->addResource(resource);
-            m_nepomukResourceWatcher->start();
+
+            if (m_nepomukUriItems.isEmpty()) {
+                m_nepomukResourceWatcher->start();
+            }
 
             m_nepomukUriItems.insert(uri, item.url());
         }
@@ -1060,10 +1163,10 @@ int KFileItemModelRolesUpdater::subItemsCount(const QString& path) const
 
     int count = -1;
     DIR* dir = ::opendir(QFile::encodeName(path));
-    if (dir) {
+    if (dir) {  // krazy:exclude=syscalls
         count = 0;
         struct dirent *dirEntry = 0;
-        while ((dirEntry = ::readdir(dir))) { // krazy:exclude=syscalls
+        while ((dirEntry = ::readdir(dir))) {
             if (dirEntry->d_name[0] == '.') {
                 if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
                     // Skip "." or hidden files
@@ -1075,9 +1178,14 @@ int KFileItemModelRolesUpdater::subItemsCount(const QString& path) const
                 }
             }
 
-            // If only directories are counted, consider an unknown file type also
-            // as directory instead of trying to do an expensive stat() (see bug 292642).
-            if (!showFoldersOnly || dirEntry->d_type == DT_DIR || dirEntry->d_type == DT_UNKNOWN) {
+            // If only directories are counted, consider an unknown file type and links also
+            // as directory instead of trying to do an expensive stat()
+            // (see bugs 292642 and 299997).
+            const bool countEntry = !showFoldersOnly ||
+                                    dirEntry->d_type == DT_DIR ||
+                                    dirEntry->d_type == DT_LNK ||
+                                    dirEntry->d_type == DT_UNKNOWN;
+            if (countEntry) {
                 ++count;
             }
         }