]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounterworker.cpp
[Details mode] Allow to fill the column size of directories with actual size
[dolphin.git] / src / kitemviews / private / kdirectorycontentscounterworker.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "kdirectorycontentscounterworker.h"
22
23 // Required includes for subItemsCount():
24 #ifdef Q_OS_WIN
25 #include <QDir>
26 #else
27 #include <QFile>
28 #include <qplatformdefs.h>
29 #endif
30
31 #include "dolphin_detailsmodesettings.h"
32
33 KDirectoryContentsCounterWorker::KDirectoryContentsCounterWorker(QObject* parent) :
34 QObject(parent)
35 {
36 qRegisterMetaType<KDirectoryContentsCounterWorker::Options>();
37 }
38
39 KDirectoryContentsCounterWorker::CountResult walkDir(const QString &dirPath,
40 const bool countHiddenFiles,
41 const bool countDirectoriesOnly,
42 QT_DIRENT *dirEntry,
43 const uint allowedRecursiveLevel)
44 {
45 int count = -1;
46 long size = -1;
47 auto dir = QT_OPENDIR(QFile::encodeName(dirPath));
48 if (dir) {
49 count = 0;
50 QT_STATBUF buf;
51
52 while ((dirEntry = QT_READDIR(dir))) {
53 if (dirEntry->d_name[0] == '.') {
54 if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
55 // Skip "." or hidden files
56 continue;
57 }
58 if (dirEntry->d_name[1] == '.' && dirEntry->d_name[2] == '\0') {
59 // Skip ".."
60 continue;
61 }
62 }
63
64 // If only directories are counted, consider an unknown file type and links also
65 // as directory instead of trying to do an expensive stat()
66 // (see bugs 292642 and 299997).
67 const bool countEntry = !countDirectoriesOnly ||
68 dirEntry->d_type == DT_DIR ||
69 dirEntry->d_type == DT_LNK ||
70 dirEntry->d_type == DT_UNKNOWN;
71 if (countEntry) {
72 ++count;
73 }
74
75 if (allowedRecursiveLevel > 0) {
76
77 bool linkFound = false;
78 QString nameBuf = QStringLiteral("%1/%2").arg(dirPath, dirEntry->d_name);
79
80 if (dirEntry->d_type == DT_REG || dirEntry->d_type == DT_LNK) {
81 if (QT_STAT(nameBuf.toLocal8Bit(), &buf) == 0) {
82 if (S_ISDIR(buf.st_mode)) {
83 // was a dir link, recurse
84 linkFound = true;
85 }
86 size += buf.st_size;
87 }
88 }
89 if (dirEntry->d_type == DT_DIR || linkFound) {
90 // recursion for dirs and dir links
91 size += walkDir(nameBuf, countHiddenFiles, countDirectoriesOnly, dirEntry, allowedRecursiveLevel - 1).size;
92 }
93 }
94 }
95 QT_CLOSEDIR(dir);
96 }
97 return KDirectoryContentsCounterWorker::CountResult{count, size};
98 }
99
100 KDirectoryContentsCounterWorker::CountResult KDirectoryContentsCounterWorker::subItemsCount(const QString& path, Options options)
101 {
102 const bool countHiddenFiles = options & CountHiddenFiles;
103 const bool countDirectoriesOnly = options & CountDirectoriesOnly;
104
105 #ifdef Q_OS_WIN
106 QDir dir(path);
107 QDir::Filters filters = QDir::NoDotAndDotDot | QDir::System;
108 if (countHiddenFiles) {
109 filters |= QDir::Hidden;
110 }
111 if (countDirectoriesOnly) {
112 filters |= QDir::Dirs;
113 } else {
114 filters |= QDir::AllEntries;
115 }
116 return {dir.entryList(filters).count(), 0};
117 #else
118
119 const uint maxRecursiveLevel = DetailsModeSettings::directorySizeCount() ? 1 : DetailsModeSettings::recursiveDirectorySizeLimit();
120
121 QT_DIRENT *dirEntry = nullptr;
122
123 auto res = walkDir(QFile::encodeName(path), countHiddenFiles, countDirectoriesOnly, dirEntry, maxRecursiveLevel);
124
125 return res;
126 #endif
127 }
128
129 void KDirectoryContentsCounterWorker::countDirectoryContents(const QString& path, Options options)
130 {
131 auto res = subItemsCount(path, options);
132 emit result(path, res.count, res.size);
133 }