/* This file is part of the Nepomuk KDE project. SPDX-FileCopyrightText: 2011 Sebastian Trueg SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #ifndef FILEINDEXERCONFIGUTILS_H #define FILEINDEXERCONFIGUTILS_H #include #include #include #include #include #include #ifdef Q_OS_WIN #include #endif namespace Baloo { namespace Test { void writeIndexerConfig(const QStringList& includeFolders, const QStringList& excludeFolders, const QStringList& excludeFilters = QStringList(), bool indexHidden = false) { QStandardPaths::setTestModeEnabled(true); KConfig fileIndexerConfig(QStringLiteral("baloofilerc")); fileIndexerConfig.group(QStringLiteral("General")).writePathEntry("folders", includeFolders); fileIndexerConfig.group(QStringLiteral("General")).writePathEntry("exclude folders", excludeFolders); fileIndexerConfig.group(QStringLiteral("General")).writeEntry("exclude filters", excludeFilters); fileIndexerConfig.group(QStringLiteral("General")).writeEntry("index hidden folders", indexHidden); fileIndexerConfig.sync(); } std::unique_ptr createTmpFilesAndFolders(const QStringList& list) { auto tmpDir = std::make_unique(); // If the temporary directory is in a hidden folder, then the tests will fail, // so we use /tmp/ instead. // TODO: Find a better solution if (QFileInfo(tmpDir->path()).isHidden()) { tmpDir = std::make_unique(QStringLiteral("/tmp/")); } for (const QString& f : list) { if (f.endsWith(QLatin1Char('/'))) { QDir dir(tmpDir->path()); const auto lst = f.split(QLatin1Char('/'), Qt::SkipEmptyParts); for (const QString & sf : lst) { if (!dir.exists(sf)) { dir.mkdir(sf); } #ifdef Q_OS_WIN if(sf.startsWith(QLatin1Char('.'))) { if(!SetFileAttributesW(reinterpret_cast((dir.path() + "/" + sf).utf16()), FILE_ATTRIBUTE_HIDDEN)) { qWarning("failed to set 'hidden' attribute!"); } } #endif dir.cd(sf); } } else { QFile file(tmpDir->path() + QLatin1Char('/') + f); if (!file.open(QIODevice::WriteOnly)) { return nullptr; } file.write("test"); } } return tmpDir; } } // namespace Test } // namespace Baloo #endif // FILEINDEXERCONFIGUTILS_H