]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounterworker.cpp
Add clang-format and format code as in Frameworks
[dolphin.git] / src / kitemviews / private / kdirectorycontentscounterworker.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "kdirectorycontentscounterworker.h"
9
10 // Required includes for subItemsCount():
11 #ifdef Q_OS_WIN
12 #include <QDir>
13 #else
14 #include <QFile>
15 #include <qplatformdefs.h>
16 #endif
17
18 #include "dolphin_detailsmodesettings.h"
19
20 KDirectoryContentsCounterWorker::KDirectoryContentsCounterWorker(QObject *parent)
21 : QObject(parent)
22 {
23 qRegisterMetaType<KDirectoryContentsCounterWorker::Options>();
24 }
25
26 #ifndef Q_OS_WIN
27 KDirectoryContentsCounterWorker::CountResult
28 walkDir(const QString &dirPath, const bool countHiddenFiles, const bool countDirectoriesOnly, const uint allowedRecursiveLevel)
29 {
30 int count = -1;
31 long size = -1;
32 auto dir = QT_OPENDIR(QFile::encodeName(dirPath));
33 if (dir) {
34 count = 0;
35 size = 0;
36 QT_DIRENT *dirEntry;
37 QT_STATBUF buf;
38
39 while ((dirEntry = QT_READDIR(dir))) {
40 if (dirEntry->d_name[0] == '.') {
41 if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
42 // Skip "." or hidden files
43 continue;
44 }
45 if (dirEntry->d_name[1] == '.' && dirEntry->d_name[2] == '\0') {
46 // Skip ".."
47 continue;
48 }
49 }
50
51 // If only directories are counted, consider an unknown file type and links also
52 // as directory instead of trying to do an expensive stat()
53 // (see bugs 292642 and 299997).
54 const bool countEntry = !countDirectoriesOnly || dirEntry->d_type == DT_DIR || dirEntry->d_type == DT_LNK || dirEntry->d_type == DT_UNKNOWN;
55 if (countEntry) {
56 ++count;
57 }
58
59 if (allowedRecursiveLevel > 0) {
60 QString nameBuf = QStringLiteral("%1/%2").arg(dirPath, dirEntry->d_name);
61
62 if (dirEntry->d_type == DT_REG) {
63 if (QT_STAT(nameBuf.toLocal8Bit(), &buf) == 0) {
64 size += buf.st_size;
65 }
66 }
67 if (dirEntry->d_type == DT_DIR) {
68 // recursion for dirs
69 auto subdirResult = walkDir(nameBuf, countHiddenFiles, countDirectoriesOnly, allowedRecursiveLevel - 1);
70 if (subdirResult.size > 0) {
71 size += subdirResult.size;
72 }
73 }
74 }
75 }
76 QT_CLOSEDIR(dir);
77 }
78 return KDirectoryContentsCounterWorker::CountResult{count, size};
79 }
80 #endif
81
82 KDirectoryContentsCounterWorker::CountResult KDirectoryContentsCounterWorker::subItemsCount(const QString &path, Options options)
83 {
84 const bool countHiddenFiles = options & CountHiddenFiles;
85 const bool countDirectoriesOnly = options & CountDirectoriesOnly;
86
87 #ifdef Q_OS_WIN
88 QDir dir(path);
89 QDir::Filters filters = QDir::NoDotAndDotDot | QDir::System;
90 if (countHiddenFiles) {
91 filters |= QDir::Hidden;
92 }
93 if (countDirectoriesOnly) {
94 filters |= QDir::Dirs;
95 } else {
96 filters |= QDir::AllEntries;
97 }
98 return {static_cast<int>(dir.entryList(filters).count()), 0};
99 #else
100
101 const uint maxRecursiveLevel = DetailsModeSettings::directorySizeCount() ? 1 : DetailsModeSettings::recursiveDirectorySizeLimit();
102
103 auto res = walkDir(path, countHiddenFiles, countDirectoriesOnly, maxRecursiveLevel);
104
105 return res;
106 #endif
107 }
108
109 void KDirectoryContentsCounterWorker::countDirectoryContents(const QString &path, Options options)
110 {
111 auto res = subItemsCount(path, options);
112 Q_EMIT result(path, res.count, res.size);
113 }