+ if (ContentDisplaySettings::directorySizeMode() == ContentDisplaySettings::EnumDirectorySizeMode::None || !item.isLocalFile()) {
+ return;
+ }
+
+ if (ContentDisplaySettings::directorySizeMode() == ContentDisplaySettings::EnumDirectorySizeMode::ContentCount || item.isSlow()) {
+ // fastpath no recursion necessary
+
+ auto data = m_model->data(index);
+ if (data.value("size") == -2) {
+ // means job already started
+ return;
+ }
+
+ auto url = item.url();
+ if (!item.localPath().isEmpty()) {
+ // optimization for desktop:/, trash:/
+ url = QUrl::fromLocalFile(item.localPath());
+ }
+
+ data.insert("size", -2); // invalid size, -1 means size unknown
+
+ disconnect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ m_model->setData(index, data);
+ connect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+
+ auto listJob = KIO::listDir(url, KIO::HideProgressInfo);
+ QObject::connect(listJob, &KIO::ListJob::entries, this, [this, item](const KJob * /*job*/, const KIO::UDSEntryList &list) {
+ int index = m_model->index(item);
+ if (index < 0) {
+ return;
+ }
+ auto data = m_model->data(index);
+ int origCount = data.value("count").toInt();
+ int entryCount = origCount;
+
+ for (const KIO::UDSEntry &entry : list) {
+ const auto name = entry.stringValue(KIO::UDSEntry::UDS_NAME);
+
+ if (name == QStringLiteral("..") || name == QStringLiteral(".")) {
+ continue;
+ }
+ if (!m_model->showHiddenFiles() && name.startsWith(QLatin1Char('.'))) {
+ continue;
+ }
+ if (m_model->showDirectoriesOnly() && !entry.isDir()) {
+ continue;
+ }
+ ++entryCount;
+ }
+
+ QHash<QByteArray, QVariant> newData;
+ QVariant expandable = data.value("isExpandable");
+ if (expandable.isNull() || expandable.toBool() != (entryCount > 0)) {
+ // if expandable has changed
+ newData.insert("isExpandable", entryCount > 0);
+ }
+
+ if (origCount != entryCount) {
+ // count has changed
+ newData.insert("count", entryCount);
+ }
+
+ if (!newData.isEmpty()) {
+ disconnect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ m_model->setData(index, newData);
+ connect(m_model, &KFileItemModel::itemsChanged, this, &KFileItemModelRolesUpdater::slotItemsChanged);
+ }
+ });
+ return;
+ }
+