File indexing completed on 2024-10-06 12:24:01
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org> 0004 SPDX-FileCopyrightText: 2005-2013 David Faure <faure@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-only 0007 */ 0008 0009 #ifndef KSYCOCAUTILS_P_H 0010 #define KSYCOCAUTILS_P_H 0011 0012 #include <QDir> 0013 #include <QFileInfo> 0014 #include <QString> 0015 0016 namespace KSycocaUtilsPrivate 0017 { 0018 // helper function for visitResourceDirectory 0019 template<typename Visitor> 0020 bool visitResourceDirectoryHelper(const QString &dirname, Visitor visitor) 0021 { 0022 QDir dir(dirname); 0023 const QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs, QDir::Unsorted); 0024 for (const QFileInfo &fi : list) { 0025 if (fi.isDir() && !fi.isSymLink() && !fi.isBundle()) { // same check as in vfolder_menu.cpp 0026 if (!visitor(fi)) { 0027 return false; 0028 } 0029 if (!visitResourceDirectoryHelper(fi.filePath(), visitor)) { 0030 return false; 0031 } 0032 } 0033 } 0034 return true; 0035 } 0036 0037 // visitor is a function/functor accepts QFileInfo as argument and returns bool 0038 // visitResourceDirectory will visit the resource directory in a depth-first way. 0039 // visitor can terminate the visit by returning false, and visitResourceDirectory 0040 // will also return false in this case, otherwise it will return true. 0041 template<typename Visitor> 0042 bool visitResourceDirectory(const QString &dirname, Visitor visitor) 0043 { 0044 QFileInfo info(dirname); 0045 if (!visitor(info)) { 0046 return false; 0047 } 0048 0049 // Recurse only for services and menus. 0050 // Apps and servicetypes don't need recursion, so save the directory listing. 0051 if (!dirname.contains(QLatin1String("/applications")) && !dirname.contains(QLatin1String("/kservicetypes5"))) { 0052 return visitResourceDirectoryHelper(dirname, visitor); 0053 } 0054 0055 return true; 0056 } 0057 0058 } 0059 0060 #endif /* KSYCOCAUTILS_P_H */