KIO

mkpathjob.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Stephan Kulow <[email protected]>
3  2000-2009 David Faure <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "mkpathjob.h"
22 #include "job_p.h"
23 #include "mkdirjob.h"
24 #include "../pathhelpers_p.h"
25 
26 #include <QTimer>
27 #include <QFileInfo>
28 
29 using namespace KIO;
30 
31 class KIO::MkpathJobPrivate : public KIO::JobPrivate
32 {
33 public:
34  MkpathJobPrivate(const QUrl &url, const QUrl &baseUrl, JobFlags flags)
35  : JobPrivate(),
36  m_url(url),
37 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
38  m_pathComponents(url.path().split(QLatin1Char('/'), QString::SkipEmptyParts)),
39 #else
40  m_pathComponents(url.path().split(QLatin1Char('/'), Qt::SkipEmptyParts)),
41 #endif
42  m_pathIterator(),
43  m_flags(flags)
44  {
45 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
46  const QStringList basePathComponents = baseUrl.path().split(QLatin1Char('/'), QString::SkipEmptyParts);
47 #else
48  const QStringList basePathComponents = baseUrl.path().split(QLatin1Char('/'), Qt::SkipEmptyParts);
49 #endif
50  m_url.setPath(QStringLiteral("/"));
51  int i = 0;
52  for (; i < basePathComponents.count() && i < m_pathComponents.count(); ++i) {
53  const QString pathComponent = m_pathComponents.at(i);
54  if (pathComponent == basePathComponents.at(i)) {
55  m_url.setPath(concatPaths(m_url.path(), pathComponent));
56  } else {
57  break;
58  }
59  }
60  if (i > 0) {
61  m_pathComponents.erase(m_pathComponents.begin(), m_pathComponents.begin() + i);
62  }
63 
64  // fast path for local files using QFileInfo::isDir
65  if (m_url.isLocalFile()) {
66  i = 0;
67  for (; i < m_pathComponents.count(); ++i) {
68  const QString localFile = m_url.toLocalFile();
69  QString testDir;
70  if (localFile == QLatin1String("/")) {
71  testDir = localFile + m_pathComponents.at(i);
72  } else {
73  testDir = localFile + QLatin1Char('/') + m_pathComponents.at(i);
74  }
75  if (QFileInfo(testDir).isDir()) {
76  m_url.setPath(testDir);
77  } else {
78  break;
79  }
80  }
81  if (i > 0) {
82  m_pathComponents.erase(m_pathComponents.begin(), m_pathComponents.begin() + i);
83  }
84  }
85 
86  m_pathIterator = m_pathComponents.constBegin();
87  }
88 
89  QUrl m_url;
90  QUrl m_baseUrl;
91  QStringList m_pathComponents;
92  QStringList::const_iterator m_pathIterator;
93  const JobFlags m_flags;
94  Q_DECLARE_PUBLIC(MkpathJob)
95 
96  void slotStart();
97 
98  static inline MkpathJob *newJob(const QUrl &url, const QUrl &baseUrl, JobFlags flags)
99  {
100  MkpathJob *job = new MkpathJob(*new MkpathJobPrivate(url, baseUrl, flags));
102  if (!(flags & HideProgressInfo)) {
104  }
105  if (!(flags & NoPrivilegeExecution)) {
106  job->d_func()->m_privilegeExecutionEnabled = true;
107  job->d_func()->m_operationType = MkDir;
108  }
109  return job;
110  }
111 
112 };
113 
114 MkpathJob::MkpathJob(MkpathJobPrivate &dd)
115  : Job(dd)
116 {
117  QTimer::singleShot(0, this, SLOT(slotStart()));
118 }
119 
120 MkpathJob::~MkpathJob()
121 {
122 }
123 
124 void MkpathJobPrivate::slotStart()
125 {
126  Q_Q(MkpathJob);
127 
128  if (m_pathIterator == m_pathComponents.constBegin()) { // first time: emit total
129  q->setTotalAmount(KJob::Directories, m_pathComponents.count());
130  }
131 
132  if (m_pathIterator != m_pathComponents.constEnd()) {
133  m_url.setPath(concatPaths(m_url.path(), *m_pathIterator));
134  KIO::Job* job = KIO::mkdir(m_url);
135  job->setParentJob(q);
136  q->addSubjob(job);
137  q->setProcessedAmount(KJob::Directories, q->processedAmount(KJob::Directories) + 1);
138  } else {
139  q->emitResult();
140  }
141 }
142 
143 void MkpathJob::slotResult(KJob *job)
144 {
145  Q_D(MkpathJob);
146  if (job->error() && job->error() != KIO::ERR_DIR_ALREADY_EXIST) {
147  KIO::Job::slotResult(job); // will set the error and emit result(this)
148  return;
149  }
150  removeSubjob(job);
151 
152  emit directoryCreated(d->m_url);
153 
154  // Move on to next one
155  ++d->m_pathIterator;
156  emitPercent(d->m_pathIterator - d->m_pathComponents.constBegin(), d->m_pathComponents.count());
157  d->slotStart();
158 }
159 
160 MkpathJob * KIO::mkpath(const QUrl &url, const QUrl &baseUrl, KIO::JobFlags flags)
161 {
162  return MkpathJobPrivate::newJob(url, baseUrl, flags);
163 }
164 
165 #include "moc_mkpathjob.cpp"
void setUiDelegate(KJobUiDelegate *delegate)
KIOCORE_EXPORT MkpathJob * mkpath(const QUrl &url, const QUrl &baseUrl=QUrl(), JobFlags flags=DefaultFlags)
Creates a directory, creating parent directories as needed.
Definition: mkpathjob.cpp:160
A namespace for KIO globals.
Definition: authinfo.h:34
Hide progress information dialog, i.e.
Definition: job_base.h:287
const T & at(int i) const const
KIOCORE_EXPORT MkdirJob * mkdir(const QUrl &url, int permissions=-1)
Creates a single directory.
Definition: mkdirjob.cpp:120
void setPath(const QString &path, QUrl::ParsingMode mode)
int count(const T &value) const const
When set, notifies the slave that application/job does not want privilege execution.
Definition: job_base.h:312
virtual void slotResult(KJob *job)
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there&#39;s one, to create a delegate and return it...
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString path(QUrl::ComponentFormattingOptions options) const const
KIOCORE_EXPORT KJobTrackerInterface * getJobTracker()
Returns the job tracker to be used by all KIO jobs (in which HideProgressInfo is not set) ...
Definition: jobtracker.cpp:25
SkipEmptyParts
A KIO job that creates a directory, after creating all parent directories necessary for this...
Definition: mkpathjob.h:42
if(recurs()&&!first)
void setParentJob(Job *parentJob)
Set the parent Job.
Definition: job.cpp:214
const QChar at(int position) const const
The base class for all jobs.
Definition: job_base.h:57
bool removeSubjob(KJob *job) override
Mark a sub job as being done.
Definition: job.cpp:98
virtual void registerJob(KJob *job)
QList::const_iterator constEnd() const const
void emitPercent(qulonglong processedAmount, qulonglong totalAmount)
QList::const_iterator constBegin() const const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:58:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.