]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kdirectorycontentscounterworker.cpp
e9c954ed9ce32183716bf3f978f36a1ff4780a1b
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
21 #include "kdirectorycontentscounterworker.h"
23 // Required includes for subItemsCount():
28 #include <qplatformdefs.h>
31 KDirectoryContentsCounterWorker::KDirectoryContentsCounterWorker(QObject
* parent
) :
34 qRegisterMetaType
<KDirectoryContentsCounterWorker::Options
>();
37 int KDirectoryContentsCounterWorker::subItemsCount(const QString
& path
, Options options
)
39 const bool countHiddenFiles
= options
& CountHiddenFiles
;
40 const bool countDirectoriesOnly
= options
& CountDirectoriesOnly
;
44 QDir::Filters filters
= QDir::NoDotAndDotDot
| QDir::System
;
45 if (countHiddenFiles
) {
46 filters
|= QDir::Hidden
;
48 if (countDirectoriesOnly
) {
49 filters
|= QDir::Dirs
;
51 filters
|= QDir::AllEntries
;
53 return dir
.entryList(filters
).count();
55 // Taken from kio/src/widgets/kdirmodel.cpp
56 // Copyright (C) 2006 David Faure <faure@kde.org>
59 auto dir
= QT_OPENDIR(QFile::encodeName(path
));
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
69 if (dirEntry
->d_name
[1] == '.' && dirEntry
->d_name
[2] == '\0') {
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
;
92 void KDirectoryContentsCounterWorker::countDirectoryContents(const QString
& path
, Options options
)
94 emit
result(path
, subItemsCount(path
, options
));