KCoreAddons

kfileutils.cpp
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org>
5 SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "kfileutils.h"
11
12#include <QDirIterator>
13#include <QFileInfo>
14#include <QMimeDatabase>
15#include <QRegularExpression>
16
17#include <set>
18
20{
21 QString basename;
22
23 // Extract the original file extension from the filename
25 QString nameSuffix = db.suffixForFileName(oldName);
26
27 if (oldName.lastIndexOf(QLatin1Char('.')) == 0) {
28 basename = QStringLiteral(".");
29 nameSuffix = oldName;
30 } else if (nameSuffix.isEmpty()) {
31 const int lastDot = oldName.lastIndexOf(QLatin1Char('.'));
32 if (lastDot == -1) {
33 basename = oldName;
34 } else {
35 basename = oldName.left(lastDot);
36 nameSuffix = oldName.mid(lastDot);
37 }
38 } else {
39 nameSuffix.prepend(QLatin1Char('.'));
40 basename = oldName.left(oldName.length() - nameSuffix.length());
41 }
42
43 // check if (number) exists at the end of the oldName and increment that number
44 const static QRegularExpression re(QStringLiteral("\\((\\d+)\\)"));
46 if (oldName.lastIndexOf(re, -1, &rmatch) != -1) {
47 const int currentNum = rmatch.captured(1).toInt();
48 const QString number = QString::number(currentNum + 1);
49 basename.replace(rmatch.capturedStart(1), rmatch.capturedLength(1), number);
50 } else {
51 // number does not exist, so just append " (1)" to filename
52 basename += QLatin1String(" (1)");
53 }
54
55 return basename + nameSuffix;
56}
57
58QString KFileUtils::suggestName(const QUrl &baseURL, const QString &oldName)
59{
60 QString suggestedName = makeSuggestedName(oldName);
61
62 if (baseURL.isLocalFile()) {
63 const QString basePath = baseURL.toLocalFile() + QLatin1Char('/');
64 while (QFileInfo::exists(basePath + suggestedName)) {
65 suggestedName = makeSuggestedName(suggestedName);
66 }
67 }
68
69 return suggestedName;
70}
71
73{
74 QStringList foundFilePaths;
75 std::set<QString> foundFileNames;
76 for (const QString &dir : dirs) {
77 QDirIterator it(dir, nameFilters, QDir::Files);
78 while (it.hasNext()) {
79 it.next();
80 const auto [iter, isFirstSeen] = foundFileNames.insert(it.fileName());
81 if (isFirstSeen) {
82 foundFilePaths << it.filePath();
83 }
84 }
85 }
86 return foundFilePaths;
87}
KCOREADDONS_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName)
Given a directory path and a string representing a file or directory (which usually exist already),...
KCOREADDONS_EXPORT QStringList findAllUniqueFiles(const QStringList &dirs, const QStringList &nameFilters={})
Locates all files matching the nameFilters in the given dirs The returned list does not contain dupli...
KCOREADDONS_EXPORT QString makeSuggestedName(const QString &oldName)
Given a string, "foo", representing a file/directory (which usually exists already),...
QString fileName() const const
QString filePath() const const
bool hasNext() const const
QString next()
bool exists() const const
QString suffixForFileName(const QString &fileName) const const
QString captured(int nth) const const
int capturedLength(int nth) const const
int capturedStart(int nth) const const
QString & insert(int position, QChar ch)
bool isEmpty() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString left(int n) const const
int length() const const
QString mid(int position, int n) const const
QString number(int n, int base)
QString & prepend(QChar ch)
QString & replace(int position, int n, QChar after)
int toInt(bool *ok, int base) const const
bool isLocalFile() const const
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:38:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.