File indexing completed on 2024-04-28 03:51:40
0001 /* 0002 This file is part of the Nepomuk KDE project. 0003 SPDX-FileCopyrightText: 2011 Sebastian Trueg <trueg@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #ifndef FILEINDEXERCONFIGUTILS_H 0009 #define FILEINDEXERCONFIGUTILS_H 0010 0011 #include <KConfig> 0012 #include <KConfigGroup> 0013 0014 #include <memory> 0015 #include <QDir> 0016 #include <QTextStream> 0017 #include <QTemporaryDir> 0018 #include <QStandardPaths> 0019 0020 #ifdef Q_OS_WIN 0021 #include <windows.h> 0022 #endif 0023 0024 namespace Baloo 0025 { 0026 namespace Test 0027 { 0028 void writeIndexerConfig(const QStringList& includeFolders, 0029 const QStringList& excludeFolders, 0030 const QStringList& excludeFilters = QStringList(), 0031 bool indexHidden = false) 0032 { 0033 QStandardPaths::setTestModeEnabled(true); 0034 KConfig fileIndexerConfig(QStringLiteral("baloofilerc")); 0035 fileIndexerConfig.group(QStringLiteral("General")).writePathEntry("folders", includeFolders); 0036 fileIndexerConfig.group(QStringLiteral("General")).writePathEntry("exclude folders", excludeFolders); 0037 fileIndexerConfig.group(QStringLiteral("General")).writeEntry("exclude filters", excludeFilters); 0038 fileIndexerConfig.group(QStringLiteral("General")).writeEntry("index hidden folders", indexHidden); 0039 fileIndexerConfig.sync(); 0040 } 0041 0042 std::unique_ptr<QTemporaryDir> createTmpFolders(const QStringList& folders) 0043 { 0044 auto tmpDir = std::make_unique<QTemporaryDir>(); 0045 // If the temporary directory is in a hidden folder, then the tests will fail, 0046 // so we use /tmp/ instead. 0047 // TODO: Find a better solution 0048 if (QFileInfo(tmpDir->path()).isHidden()) { 0049 tmpDir = std::make_unique<QTemporaryDir>(QStringLiteral("/tmp/")); 0050 } 0051 for (const QString & f : folders) { 0052 QDir dir(tmpDir->path()); 0053 const auto lst = f.split(QLatin1Char('/'), Qt::SkipEmptyParts); 0054 for (const QString & sf : lst) { 0055 if (!dir.exists(sf)) { 0056 dir.mkdir(sf); 0057 } 0058 #ifdef Q_OS_WIN 0059 if(sf.startsWith(QLatin1Char('.'))) { 0060 if(!SetFileAttributesW(reinterpret_cast<const WCHAR*>((dir.path() + "/" + sf).utf16()), FILE_ATTRIBUTE_HIDDEN)) { 0061 qWarning("failed to set 'hidden' attribute!"); 0062 } 0063 } 0064 #endif 0065 dir.cd(sf); 0066 } 0067 } 0068 return tmpDir; 0069 } 0070 0071 std::unique_ptr<QTemporaryDir> createTmpFilesAndFolders(const QStringList& list) 0072 { 0073 auto tmpDir = std::make_unique<QTemporaryDir>(); 0074 // If the temporary directory is in a hidden folder, then the tests will fail, 0075 // so we use /tmp/ instead. 0076 // TODO: Find a better solution 0077 if (QFileInfo(tmpDir->path()).isHidden()) { 0078 tmpDir = std::make_unique<QTemporaryDir>(QStringLiteral("/tmp/")); 0079 } 0080 for (const QString& f : list) { 0081 if (f.endsWith(QLatin1Char('/'))) { 0082 QDir dir(tmpDir->path()); 0083 const auto lst = f.split(QLatin1Char('/'), Qt::SkipEmptyParts); 0084 for (const QString & sf : lst) { 0085 if (!dir.exists(sf)) { 0086 dir.mkdir(sf); 0087 } 0088 #ifdef Q_OS_WIN 0089 if(sf.startsWith(QLatin1Char('.'))) { 0090 if(!SetFileAttributesW(reinterpret_cast<const WCHAR*>((dir.path() + "/" + sf).utf16()), FILE_ATTRIBUTE_HIDDEN)) { 0091 qWarning("failed to set 'hidden' attribute!"); 0092 } 0093 } 0094 #endif 0095 dir.cd(sf); 0096 } 0097 } 0098 else { 0099 QFile file(tmpDir->path() + QLatin1Char('/') + f); 0100 file.open(QIODevice::WriteOnly); 0101 0102 QTextStream stream(&file); 0103 stream << "test"; 0104 } 0105 } 0106 return tmpDir; 0107 } 0108 // 0109 // Trying to put all cases into one folder tree: 0110 // |- indexedRootDir 0111 // |- indexedSubDir 0112 // |- indexedSubSubDir 0113 // |- excludedSubSubDir 0114 // |- .hiddenSubSubDir 0115 // |- ignoredSubFolderToIndexedHidden 0116 // |- indexedSubFolderToIndexedHidden 0117 // |- excludedSubDir 0118 // |- indexedSubDirToExcluded 0119 // |- .indexedHiddenSubDirToExcluded 0120 // |- .hiddenSubDir 0121 // |- .indexedHiddenSubDir 0122 // |- ignoredRootDir 0123 // |- excludedRootDir 0124 // 0125 const QString indexedRootDir = QStringLiteral("d1"); 0126 const QString indexedSubDir = QStringLiteral("d1/sd1"); 0127 const QString indexedSubSubDir = QStringLiteral("d1/sd1/ssd1"); 0128 const QString excludedSubSubDir = QStringLiteral("d1/sd1/ssd2"); 0129 const QString hiddenSubSubDir = QStringLiteral("d1/sd1/.ssd3"); 0130 const QString ignoredSubFolderToIndexedHidden = QStringLiteral("d1/sd1/.ssd3/isfh1"); 0131 const QString indexedSubFolderToIndexedHidden = QStringLiteral("d1/sd1/.ssd3/isfh2"); 0132 const QString excludedSubDir = QStringLiteral("d1/sd2"); 0133 const QString indexedSubDirToExcluded = QStringLiteral("d1/sd2/isde1"); 0134 const QString indexedHiddenSubDirToExcluded = QStringLiteral("d1/sd2/.isde2"); 0135 const QString hiddenSubDir = QStringLiteral("d1/.sd3"); 0136 const QString indexedHiddenSubDir = QStringLiteral("d1/.sd4"); 0137 const QString ignoredRootDir = QStringLiteral("d2"); 0138 const QString excludedRootDir = QStringLiteral("d3"); 0139 } 0140 } 0141 0142 #endif // FILEINDEXERCONFIGUTILS_H