#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>
#ifdef HAVE_NEPOMUK
#include "private/knepomukrolesprovider.h"
- #include "private/knepomukresourcewatcher.h"
+ #include "private/nepomuk/resourcewatcher.h"
#endif
// Required includes for subItemsCount():
m_pendingInvisibleItems(),
m_previewJobs(),
m_changedItemsTimer(0),
- m_changedItems()
+ m_changedItems(),
+ m_dirWatcher(0),
+ m_watchedDirs()
#ifdef HAVE_NEPOMUK
, m_nepomukResourceWatcher(0),
m_nepomukUriItems()
#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)
void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList& list)
{
- if (m_enabledPlugins == list) {
+ if (m_enabledPlugins != list) {
m_enabledPlugins = list;
if (m_previewShown) {
updateAllPreviews();
{
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;
}
}
m_nepomukResourceWatcher->setResources(newResources);
+ if (newResources.isEmpty()) {
+ Q_ASSERT(m_nepomukUriItems.isEmpty());
+ m_nepomukResourceWatcher->stop();
+ }
}
}
#endif
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();
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;
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);
#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();
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)
}
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)));
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
}
#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();
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());
}
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
}
}
- // 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;
}
}