]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounterworker.cpp
e9c954ed9ce32183716bf3f978f36a1ff4780a1b
[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 KDirectoryContentsCounterWorker::KDirectoryContentsCounterWorker(QObject* parent) :
32 QObject(parent)
33 {
34 qRegisterMetaType<KDirectoryContentsCounterWorker::Options>();
35 }
36
37 int KDirectoryContentsCounterWorker::subItemsCount(const QString& path, Options options)
38 {
39 const bool countHiddenFiles = options & CountHiddenFiles;
40 const bool countDirectoriesOnly = options & CountDirectoriesOnly;
41
42 #ifdef Q_OS_WIN
43 QDir dir(path);
44 QDir::Filters filters = QDir::NoDotAndDotDot | QDir::System;
45 if (countHiddenFiles) {
46 filters |= QDir::Hidden;
47 }
48 if (countDirectoriesOnly) {
49 filters |= QDir::Dirs;
50 } else {
51 filters |= QDir::AllEntries;
52 }
53 return dir.entryList(filters).count();
54 #else
55 // Taken from kio/src/widgets/kdirmodel.cpp
56 // Copyright (C) 2006 David Faure <faure@kde.org>
57
58 int count = -1;
59 auto dir = QT_OPENDIR(QFile::encodeName(path));
60 if (dir) {
61 count = 0;
62 QT_DIRENT *dirEntry = nullptr;
63 while ((dirEntry = QT_READDIR(dir))) {
64 if (dirEntry->d_name[0] == '.') {
65 if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
66 // Skip "." or hidden files
67 continue;
68 }
69 if (dirEntry->d_name[1] == '.' && dirEntry->d_name[2] == '\0') {
70 // Skip ".."
71 continue;
72 }
73 }
74
75 // If only directories are counted, consider an unknown file type and links also
76 // as directory instead of trying to do an expensive stat()
77 // (see bugs 292642 and 299997).
78 const bool countEntry = !countDirectoriesOnly ||
79 dirEntry->d_type == DT_DIR ||
80 dirEntry->d_type == DT_LNK ||
81 dirEntry->d_type == DT_UNKNOWN;
82 if (countEntry) {
83 ++count;
84 }
85 }
86 QT_CLOSEDIR(dir);
87 }
88 return count;
89 #endif
90 }
91
92 void KDirectoryContentsCounterWorker::countDirectoryContents(const QString& path, Options options)
93 {
94 emit result(path, subItemsCount(path, options));
95 }