File indexing completed on 2024-05-19 03:56:20
0001 /* 0002 This file is part of the KDE libraries 0003 0004 SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org> 0005 SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 #ifndef KFILEUTILS_H 0010 #define KFILEUTILS_H 0011 0012 #include "kcoreaddons_export.h" 0013 0014 #include <QString> 0015 #include <QUrl> 0016 0017 /** 0018 * @short A namespace for KFileUtils globals 0019 * 0020 */ 0021 namespace KFileUtils 0022 { 0023 /** 0024 * Given a directory path and a string representing a file or directory 0025 * (which usually exist already), this function returns a suggested name 0026 * for a file/directory that doesn't exist in @p baseURL. 0027 * 0028 * The suggested file name is of the form "foo (1)", "foo (2)" etc. 0029 * 0030 * For local URLs, this function will check if there is already a file/directory 0031 * with the new suggested name and will keep incrementing the number in the above 0032 * format until it finds one that doesn't exist. Note that this function uses a 0033 * blocking I/O call (using QFileInfo) to check the existence of the file/directory, 0034 * this could be problematic for network mounts (e.g. SMB, NFS) as these are treated 0035 * as local files by the upstream QFile code. An alternative is to use makeSuggestedName() 0036 * and use KIO to stat the new file/directory in an asynchronous way. 0037 * 0038 * @since 5.61 0039 */ 0040 KCOREADDONS_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName); 0041 0042 /** 0043 * Given a string, "foo", representing a file/directory (which usually exists already), 0044 * this function returns a suggested name for a file/directory in the form "foo (1)", 0045 * "foo (2)" etc. 0046 * 0047 * Unlike the suggestName() method, this function doesn't check if there is a file/directory 0048 * with the newly suggested name; the idea being that this responsibility falls on 0049 * the caller, e.g. one can use KIO::stat() to check asynchronously whether the new 0050 * name already exists (in its parent directory) or not. 0051 * 0052 * @since 5.76 0053 */ 0054 KCOREADDONS_EXPORT QString makeSuggestedName(const QString &oldName); 0055 0056 /** 0057 * Locates all files matching the @p nameFilters in the given @p dirs 0058 * The returned list does not contain duplicate file names. 0059 * In case there are multiple files the one which comes first in the dirs list is returned. 0060 * For example: 0061 * @code 0062 QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("krunner/dbusplugins"), QStandardPaths::LocateDirectory); 0063 KFileUtils::findAllUniqueFiles(dirs, QStringList{QStringLiteral("*.desktop")}); 0064 * @endcode 0065 * @param location standard location for the dir 0066 * @param dir directory in which the files are located 0067 * @param nameFilters filters that get passed to the QDirIterator that is used internally to 0068 * iterate over the files in each dir in the list 0069 * @return list of absolute file paths 0070 * @since 5.85 0071 */ 0072 KCOREADDONS_EXPORT QStringList findAllUniqueFiles(const QStringList &dirs, const QStringList &nameFilters = {}); 0073 } 0074 #endif