KIO

namefinderjob.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
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
17class KIO::NameFinderJobPrivate
18{
19public:
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
40KIO::NameFinderJob::NameFinderJob(const QUrl &baseUrl, const QString &name, QObject *parent)
41 : KCompositeJob(parent)
42 , d(new NameFinderJobPrivate(baseUrl, name, this))
43{
44}
45
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
62void 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::stat(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
77void 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}
112
113#include "moc_namefinderjob.cpp"
NameFinderJob finds a valid "New Folder" name.
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...
~NameFinderJob() override
Destructor.
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.
void start() override
Starts the job.
QString finalName() const
Call this to get the suggested new folder/file name.
QUrl finalUrl() const
Call this to get the full target URL (basically the baseUrl() + "/" + finalName()).
A KIO job that retrieves information about a file or directory.
void result(KJob *job)
KCOREADDONS_EXPORT QString makeSuggestedName(const QString &oldName)
KIOCORE_EXPORT StatJob * stat(const QUrl &url, JobFlags flags=DefaultFlags)
Find all details for one file or directory.
Definition statjob.cpp:203
@ HideProgressInfo
Hide progress information dialog, i.e. don't show a GUI.
Definition job_base.h:251
@ StatNoDetails
No field returned, useful to check if a file exists.
Definition global.h:253
QString name(StandardAction id)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
StripTrailingSlash
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.