File indexing completed on 2024-05-12 03:54:55

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 
0010 #include "kfileutils.h"
0011 
0012 #include <QDirIterator>
0013 #include <QFileInfo>
0014 #include <QMimeDatabase>
0015 #include <QRegularExpression>
0016 
0017 #include <set>
0018 
0019 QString KFileUtils::makeSuggestedName(const QString &oldName)
0020 {
0021     QString basename;
0022 
0023     // Extract the original file extension from the filename
0024     QMimeDatabase db;
0025     QString nameSuffix = db.suffixForFileName(oldName);
0026 
0027     if (oldName.lastIndexOf(QLatin1Char('.')) == 0) {
0028         basename = QStringLiteral(".");
0029         nameSuffix = oldName;
0030     } else if (nameSuffix.isEmpty()) {
0031         const int lastDot = oldName.lastIndexOf(QLatin1Char('.'));
0032         if (lastDot == -1) {
0033             basename = oldName;
0034         } else {
0035             basename = oldName.left(lastDot);
0036             nameSuffix = oldName.mid(lastDot);
0037         }
0038     } else {
0039         nameSuffix.prepend(QLatin1Char('.'));
0040         basename = oldName.left(oldName.length() - nameSuffix.length());
0041     }
0042 
0043     // check if (number) exists at the end of the oldName and increment that number
0044     const static QRegularExpression re(QStringLiteral("\\((\\d+)\\)"));
0045     QRegularExpressionMatch rmatch;
0046     if (oldName.lastIndexOf(re, -1, &rmatch) != -1) {
0047         const int currentNum = rmatch.captured(1).toInt();
0048         const QString number = QString::number(currentNum + 1);
0049         basename.replace(rmatch.capturedStart(1), rmatch.capturedLength(1), number);
0050     } else {
0051         // number does not exist, so just append " (1)" to filename
0052         basename += QLatin1String(" (1)");
0053     }
0054 
0055     return basename + nameSuffix;
0056 }
0057 
0058 QString KFileUtils::suggestName(const QUrl &baseURL, const QString &oldName)
0059 {
0060     QString suggestedName = makeSuggestedName(oldName);
0061 
0062     if (baseURL.isLocalFile()) {
0063         const QString basePath = baseURL.toLocalFile() + QLatin1Char('/');
0064         while (QFileInfo::exists(basePath + suggestedName)) {
0065             suggestedName = makeSuggestedName(suggestedName);
0066         }
0067     }
0068 
0069     return suggestedName;
0070 }
0071 
0072 QStringList KFileUtils::findAllUniqueFiles(const QStringList &dirs, const QStringList &nameFilters)
0073 {
0074     QStringList foundFilePaths;
0075     std::set<QString> foundFileNames;
0076     for (const QString &dir : dirs) {
0077         QDirIterator it(dir, nameFilters, QDir::Files);
0078         while (it.hasNext()) {
0079             it.next();
0080             const auto [iter, isFirstSeen] = foundFileNames.insert(it.fileName());
0081             if (isFirstSeen) {
0082                 foundFilePaths << it.filePath();
0083             }
0084         }
0085     }
0086     return foundFilePaths;
0087 }