KCoreAddons

kfileutils.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2000-2005 David Faure <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kfileutils.h"
10 
11 #include <QMimeDatabase>
12 #include <QFileInfo>
13 #include <QRegularExpression>
14 
15 QString KFileUtils::suggestName(const QUrl &baseURL, const QString &oldName)
16 {
17  QString basename;
18 
19  // Extract the original file extension from the filename
20  QMimeDatabase db;
21  QString nameSuffix = db.suffixForFileName(oldName);
22 
23  if (oldName.lastIndexOf(QLatin1Char('.')) == 0) {
24  basename = QStringLiteral(".");
25  nameSuffix = oldName;
26  } else if (nameSuffix.isEmpty()) {
27  const int lastDot = oldName.lastIndexOf(QLatin1Char('.'));
28  if (lastDot == -1) {
29  basename = oldName;
30  } else {
31  basename = oldName.left(lastDot);
32  nameSuffix = oldName.mid(lastDot);
33  }
34  } else {
35  nameSuffix.prepend(QLatin1Char('.'));
36  basename = oldName.left(oldName.length() - nameSuffix.length());
37  }
38 
39  // check if (number) exists at the end of the oldName and increment that number
40  const QRegularExpression re(QStringLiteral("\\((\\d+)\\)"));
42  oldName.lastIndexOf(re, -1, &rmatch);
43  if (rmatch.hasMatch()) {
44  const int currentNum = rmatch.captured(1).toInt();
45  const QString number = QString::number(currentNum + 1);
46  basename.replace(rmatch.capturedStart(1), rmatch.capturedLength(1), number);
47  } else {
48  // number does not exist, so just append " (1)" to filename
49  basename += QLatin1String(" (1)");
50  }
51  const QString suggestedName = basename + nameSuffix;
52 
53  // Check if suggested name already exists
54  bool exists = false;
55 
56  if (baseURL.isLocalFile()) {
57  exists = QFileInfo::exists(baseURL.toLocalFile() + QLatin1Char('/') + suggestedName);
58  }
59 
60  if (!exists) {
61  return suggestedName;
62  } else { // already exists -> recurse
63  return suggestName(baseURL, suggestedName);
64  }
65 }
QString captured(int nth) const const
QString suffixForFileName(const QString &fileName) const const
KCOREADDONS_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName)
Given a directory path and a filename (which usually exists already), this function returns a suggest...
Definition: kfileutils.cpp:15
int capturedStart(int nth) const const
QString & prepend(QChar ch)
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString number(int n, int base)
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool hasMatch() const const
QString toLocalFile() const const
bool exists() const const
QString & replace(int position, int n, QChar after)
QString mid(int position, int n) const const
int length() const const
QString left(int n) const const
int capturedLength(int nth) const const
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 6 2020 23:01:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.