KIO

namefinderjob.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2020 Ahmad Samir <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 
8 #include "namefinderjob.h"
9 
10 #include "../utils_p.h"
11 #include "kiocoredebug.h"
12 #include <KFileUtils>
13 #include <KIO/StatJob>
14 
15 #include <QUrl>
16 
17 class KIO::NameFinderJobPrivate
18 {
19 public:
20  explicit NameFinderJobPrivate(const QUrl &baseUrl, const QString &name, NameFinderJob *qq)
21  : m_baseUrl(baseUrl)
22  , m_name(name)
23  , m_statJob(nullptr)
24  , q(qq)
25  {
26  }
27 
28  QUrl m_baseUrl;
29  QString m_name;
30  QUrl m_finalUrl;
31  KIO::StatJob *m_statJob;
32  bool m_firstStat = true;
33 
34  KIO::NameFinderJob *const q;
35 
36  void statUrl();
37  void slotStatResult();
38 };
39 
40 KIO::NameFinderJob::NameFinderJob(const QUrl &baseUrl, const QString &name, QObject *parent)
41  : KCompositeJob(parent)
42  , d(new NameFinderJobPrivate(baseUrl, name, this))
43 {
44 }
45 
47 {
48 }
49 
51 {
52  if (!d->m_baseUrl.isValid() || d->m_baseUrl.scheme().isEmpty()) {
53  qCDebug(KIO_CORE) << "Malformed URL" << d->m_baseUrl;
54  setError(KIO::ERR_MALFORMED_URL);
55  emitResult();
56  return;
57  }
58 
59  d->statUrl();
60 }
61 
62 void KIO::NameFinderJobPrivate::statUrl()
63 {
64  m_finalUrl = m_baseUrl;
65  m_finalUrl.setPath(Utils::concatPaths(m_baseUrl.path(), m_name));
66 
67  m_statJob = KIO::statDetails(m_finalUrl,
68  KIO::StatJob::DestinationSide,
69  KIO::StatNoDetails, // Just checking if it exists
71 
72  QObject::connect(m_statJob, &KJob::result, q, [this]() {
73  slotStatResult();
74  });
75 }
76 
77 void KIO::NameFinderJobPrivate::slotStatResult()
78 {
79  // m_statJob will resolve the url to the most local one in the first run
80  if (m_firstStat) {
81  m_finalUrl = m_statJob->mostLocalUrl();
82  m_firstStat = false;
83  }
84 
85  // StripTrailingSlash so that fileName() doesn't return an empty string
86  m_finalUrl = m_finalUrl.adjusted(QUrl::StripTrailingSlash);
87  m_baseUrl = m_finalUrl.adjusted(QUrl::RemoveFilename);
88  m_name = m_finalUrl.fileName();
89 
90  if (m_statJob->error()) { // Doesn't exist, we're done
91  q->emitResult();
92  } else { // Exists, create a new name, then stat again
93  m_name = KFileUtils::makeSuggestedName(m_name);
94  statUrl();
95  }
96 }
97 
99 {
100  return d->m_finalUrl;
101 }
102 
104 {
105  return d->m_baseUrl;
106 }
107 
109 {
110  return d->m_name;
111 }
KCOREADDONS_EXPORT QString makeSuggestedName(const QString &oldName)
void result(KJob *job)
QUrl finalUrl() const
Call this to get the full target URL (basically the baseUrl() + "/" + finalName()).
NameFinderJob(const QUrl &baseUrl, const QString &name, QObject *parent)
Creates a NameFinderJob to get a "New Folder" (or "Text File.txt") name that doesn't already exist.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
StripTrailingSlash
@ StatNoDetails
No field returned, useful to check if a file exists.
Definition: global.h:367
NameFinderJob finds a valid "New Folder" name.
Definition: namefinderjob.h:69
~NameFinderJob() override
Destructor.
QString finalName() const
Call this to get the suggested new folder/file name.
QString name(StandardShortcut id)
KIOCORE_EXPORT StatJob * statDetails(const QUrl &url, KIO::StatJob::StatSide side, KIO::StatDetails details=KIO::StatDefaultDetails, JobFlags flags=DefaultFlags)
Find all details for one file or directory.
Definition: statjob.cpp:263
@ HideProgressInfo
Hide progress information dialog, i.e. don't show a GUI.
Definition: job_base.h:274
QUrl baseUrl() const
Call this to get the base URL (i.e. the URL of the folder where a new folder/file is going to be crea...
void start() override
Starts the job.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:00:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.