• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.4 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
copyjob.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright 2000 Stephan Kulow <coolo@kde.org>
3  Copyright 2000-2006 David Faure <faure@kde.org>
4  Copyright 2000 Waldo Bastian <bastian@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "copyjob.h"
23 #include <errno.h>
24 #include "kdirlister.h"
25 #include "kfileitem.h"
26 #include "deletejob.h"
27 
28 #include <klocale.h>
29 #include <kdesktopfile.h>
30 #include <kdebug.h>
31 #include <kde_file.h>
32 
33 #include "slave.h"
34 #include "scheduler.h"
35 #include "kdirwatch.h"
36 #include "kprotocolmanager.h"
37 
38 #include "jobuidelegate.h"
39 
40 #include <kdirnotify.h>
41 #include <ktemporaryfile.h>
42 
43 #ifdef Q_OS_UNIX
44 #include <utime.h>
45 #endif
46 #include <assert.h>
47 
48 #include <QtCore/QTimer>
49 #include <QtCore/QFile>
50 #include <sys/stat.h> // mode_t
51 #include <QPointer>
52 
53 #include "job_p.h"
54 #include <kdiskfreespaceinfo.h>
55 #include <kfilesystemtype_p.h>
56 
57 using namespace KIO;
58 
59 //this will update the report dialog with 5 Hz, I think this is fast enough, aleXXX
60 #define REPORT_TIMEOUT 200
61 
62 enum DestinationState {
63  DEST_NOT_STATED,
64  DEST_IS_DIR,
65  DEST_IS_FILE,
66  DEST_DOESNT_EXIST
67 };
68 
85 enum CopyJobState {
86  STATE_STATING,
87  STATE_RENAMING,
88  STATE_LISTING,
89  STATE_CREATING_DIRS,
90  STATE_CONFLICT_CREATING_DIRS,
91  STATE_COPYING_FILES,
92  STATE_CONFLICT_COPYING_FILES,
93  STATE_DELETING_DIRS,
94  STATE_SETTING_DIR_ATTRIBUTES
95 };
96 
98 class KIO::CopyJobPrivate: public KIO::JobPrivate
99 {
100 public:
101  CopyJobPrivate(const KUrl::List& src, const KUrl& dest,
102  CopyJob::CopyMode mode, bool asMethod)
103  : m_globalDest(dest)
104  , m_globalDestinationState(DEST_NOT_STATED)
105  , m_defaultPermissions(false)
106  , m_bURLDirty(false)
107  , m_mode(mode)
108  , m_asMethod(asMethod)
109  , destinationState(DEST_NOT_STATED)
110  , state(STATE_STATING)
111  , m_freeSpace(-1)
112  , m_totalSize(0)
113  , m_processedSize(0)
114  , m_fileProcessedSize(0)
115  , m_processedFiles(0)
116  , m_processedDirs(0)
117  , m_srcList(src)
118  , m_currentStatSrc(m_srcList.constBegin())
119  , m_bCurrentOperationIsLink(false)
120  , m_bSingleFileCopy(false)
121  , m_bOnlyRenames(mode==CopyJob::Move)
122  , m_dest(dest)
123  , m_bAutoRenameFiles(false)
124  , m_bAutoRenameDirs(false)
125  , m_bAutoSkipFiles( false )
126  , m_bAutoSkipDirs( false )
127  , m_bOverwriteAllFiles( false )
128  , m_bOverwriteAllDirs( false )
129  , m_conflictError(0)
130  , m_reportTimer(0)
131  {
132  }
133 
134  // This is the dest URL that was initially given to CopyJob
135  // It is copied into m_dest, which can be changed for a given src URL
136  // (when using the RENAME dialog in slotResult),
137  // and which will be reset for the next src URL.
138  KUrl m_globalDest;
139  // The state info about that global dest
140  DestinationState m_globalDestinationState;
141  // See setDefaultPermissions
142  bool m_defaultPermissions;
143  // Whether URLs changed (and need to be emitted by the next slotReport call)
144  bool m_bURLDirty;
145  // Used after copying all the files into the dirs, to set mtime (TODO: and permissions?)
146  // after the copy is done
147  QLinkedList<CopyInfo> m_directoriesCopied;
148  QLinkedList<CopyInfo>::const_iterator m_directoriesCopiedIterator;
149 
150  CopyJob::CopyMode m_mode;
151  bool m_asMethod;
152  DestinationState destinationState;
153  CopyJobState state;
154 
155  KIO::filesize_t m_freeSpace;
156 
157  KIO::filesize_t m_totalSize;
158  KIO::filesize_t m_processedSize;
159  KIO::filesize_t m_fileProcessedSize;
160  int m_processedFiles;
161  int m_processedDirs;
162  QList<CopyInfo> files;
163  QList<CopyInfo> dirs;
164  KUrl::List dirsToRemove;
165  KUrl::List m_srcList;
166  KUrl::List m_successSrcList; // Entries in m_srcList that have successfully been moved
167  KUrl::List::const_iterator m_currentStatSrc;
168  bool m_bCurrentSrcIsDir;
169  bool m_bCurrentOperationIsLink;
170  bool m_bSingleFileCopy;
171  bool m_bOnlyRenames;
172  KUrl m_dest;
173  KUrl m_currentDest; // set during listing, used by slotEntries
174  //
175  QStringList m_skipList;
176  QSet<QString> m_overwriteList;
177  bool m_bAutoRenameFiles;
178  bool m_bAutoRenameDirs;
179  bool m_bAutoSkipFiles;
180  bool m_bAutoSkipDirs;
181  bool m_bOverwriteAllFiles;
182  bool m_bOverwriteAllDirs;
183  int m_conflictError;
184 
185  QTimer *m_reportTimer;
186 
187  // The current src url being stat'ed or copied
188  // During the stat phase, this is initially equal to *m_currentStatSrc but it can be resolved to a local file equivalent (#188903).
189  KUrl m_currentSrcURL;
190  KUrl m_currentDestURL;
191 
192  QSet<QString> m_parentDirs;
193 
194  void statCurrentSrc();
195  void statNextSrc();
196 
197  // Those aren't slots but submethods for slotResult.
198  void slotResultStating( KJob * job );
199  void startListing( const KUrl & src );
200  void slotResultCreatingDirs( KJob * job );
201  void slotResultConflictCreatingDirs( KJob * job );
202  void createNextDir();
203  void slotResultCopyingFiles( KJob * job );
204  void slotResultConflictCopyingFiles( KJob * job );
205 // KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, bool overwrite );
206  KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags );
207  void copyNextFile();
208  void slotResultDeletingDirs( KJob * job );
209  void deleteNextDir();
210  void sourceStated(const UDSEntry& entry, const KUrl& sourceUrl);
211  void skip(const KUrl & sourceURL, bool isDir);
212  void slotResultRenaming( KJob * job );
213  void slotResultSettingDirAttributes( KJob * job );
214  void setNextDirAttribute();
215 
216  void startRenameJob(const KUrl &slave_url);
217  bool shouldOverwriteDir( const QString& path ) const;
218  bool shouldOverwriteFile( const QString& path ) const;
219  bool shouldSkip( const QString& path ) const;
220  void skipSrc(bool isDir);
221 
222  void slotStart();
223  void slotEntries( KIO::Job*, const KIO::UDSEntryList& list );
224  void addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest);
228  void slotProcessedSize( KJob*, qulonglong data_size );
233  void slotTotalSize( KJob*, qulonglong size );
234 
235  void slotReport();
236 
237  Q_DECLARE_PUBLIC(CopyJob)
238 
239  static inline CopyJob *newJob(const KUrl::List& src, const KUrl& dest,
240  CopyJob::CopyMode mode, bool asMethod, JobFlags flags)
241  {
242  CopyJob *job = new CopyJob(*new CopyJobPrivate(src,dest,mode,asMethod));
243  job->setUiDelegate(new JobUiDelegate);
244  if (!(flags & HideProgressInfo))
245  KIO::getJobTracker()->registerJob(job);
246  if (flags & KIO::Overwrite) {
247  job->d_func()->m_bOverwriteAllDirs = true;
248  job->d_func()->m_bOverwriteAllFiles = true;
249  }
250  return job;
251  }
252 };
253 
254 CopyJob::CopyJob(CopyJobPrivate &dd)
255  : Job(dd)
256 {
257  setProperty("destUrl", d_func()->m_dest.url());
258  QTimer::singleShot(0, this, SLOT(slotStart()));
259 }
260 
261 CopyJob::~CopyJob()
262 {
263 }
264 
265 KUrl::List CopyJob::srcUrls() const
266 {
267  return d_func()->m_srcList;
268 }
269 
270 KUrl CopyJob::destUrl() const
271 {
272  return d_func()->m_dest;
273 }
274 
275 void CopyJobPrivate::slotStart()
276 {
277  Q_Q(CopyJob);
283  m_reportTimer = new QTimer(q);
284 
285  q->connect(m_reportTimer,SIGNAL(timeout()),q,SLOT(slotReport()));
286  m_reportTimer->start(REPORT_TIMEOUT);
287 
288  // Stat the dest
289  KIO::Job * job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
290  //kDebug(7007) << "CopyJob:stating the dest " << m_dest;
291  q->addSubjob(job);
292 }
293 
294 // For unit test purposes
295 KIO_EXPORT bool kio_resolve_local_urls = true;
296 
297 void CopyJobPrivate::slotResultStating( KJob *job )
298 {
299  Q_Q(CopyJob);
300  //kDebug(7007);
301  // Was there an error while stating the src ?
302  if (job->error() && destinationState != DEST_NOT_STATED )
303  {
304  const KUrl srcurl = static_cast<SimpleJob*>(job)->url();
305  if ( !srcurl.isLocalFile() )
306  {
307  // Probably : src doesn't exist. Well, over some protocols (e.g. FTP)
308  // this info isn't really reliable (thanks to MS FTP servers).
309  // We'll assume a file, and try to download anyway.
310  kDebug(7007) << "Error while stating source. Activating hack";
311  q->removeSubjob( job );
312  assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
313  struct CopyInfo info;
314  info.permissions = (mode_t) -1;
315  info.mtime = (time_t) -1;
316  info.ctime = (time_t) -1;
317  info.size = (KIO::filesize_t)-1;
318  info.uSource = srcurl;
319  info.uDest = m_dest;
320  // Append filename or dirname to destination URL, if allowed
321  if ( destinationState == DEST_IS_DIR && !m_asMethod )
322  info.uDest.addPath( srcurl.fileName() );
323 
324  files.append( info );
325  statNextSrc();
326  return;
327  }
328  // Local file. If stat fails, the file definitely doesn't exist.
329  // yes, q->Job::, because we don't want to call our override
330  q->Job::slotResult( job ); // will set the error and emit result(this)
331  return;
332  }
333 
334  // Keep copy of the stat result
335  const UDSEntry entry = static_cast<StatJob*>(job)->statResult();
336 
337  if ( destinationState == DEST_NOT_STATED ) {
338  if ( m_dest.isLocalFile() ) { //works for dirs as well
339  QString path = m_dest.toLocalFile();
340  KFileSystemType::Type fsType = KFileSystemType::fileSystemType( path );
341  if ( fsType != KFileSystemType::Nfs && fsType != KFileSystemType::Smb ) {
342  m_freeSpace = KDiskFreeSpaceInfo::freeSpaceInfo( path ).available();
343  }
344  //TODO actually preliminary check is even more valuable for slow NFS/SMB mounts,
345  //but we need to find a way to report connection errors to user
346  }
347 
348  const bool isGlobalDest = m_dest == m_globalDest;
349  const bool isDir = entry.isDir();
350  // we were stating the dest
351  if (job->error()) {
352  destinationState = DEST_DOESNT_EXIST;
353  //kDebug(7007) << "dest does not exist";
354  } else {
355  // Treat symlinks to dirs as dirs here, so no test on isLink
356  destinationState = isDir ? DEST_IS_DIR : DEST_IS_FILE;
357  //kDebug(7007) << "dest is dir:" << isDir;
358 
359  const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
360  if ( !sLocalPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST ) {
361  m_dest = KUrl();
362  m_dest.setPath(sLocalPath);
363  if ( isGlobalDest )
364  m_globalDest = m_dest;
365  }
366  }
367  if ( isGlobalDest )
368  m_globalDestinationState = destinationState;
369 
370  q->removeSubjob( job );
371  assert ( !q->hasSubjobs() );
372 
373  // After knowing what the dest is, we can start stat'ing the first src.
374  statCurrentSrc();
375  } else {
376  sourceStated(entry, static_cast<SimpleJob*>(job)->url());
377  q->removeSubjob( job );
378  }
379 }
380 
381 void CopyJobPrivate::sourceStated(const UDSEntry& entry, const KUrl& sourceUrl)
382 {
383  const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
384  const bool isDir = entry.isDir();
385 
386  // We were stating the current source URL
387  // Is it a file or a dir ?
388 
389  // There 6 cases, and all end up calling addCopyInfoFromUDSEntry first :
390  // 1 - src is a dir, destination is a directory,
391  // slotEntries will append the source-dir-name to the destination
392  // 2 - src is a dir, destination is a file -- will offer to overwrite, later on.
393  // 3 - src is a dir, destination doesn't exist, then it's the destination dirname,
394  // so slotEntries will use it as destination.
395 
396  // 4 - src is a file, destination is a directory,
397  // slotEntries will append the filename to the destination.
398  // 5 - src is a file, destination is a file, m_dest is the exact destination name
399  // 6 - src is a file, destination doesn't exist, m_dest is the exact destination name
400 
401  KUrl srcurl;
402  if (!sLocalPath.isEmpty() && destinationState != DEST_DOESNT_EXIST) {
403  kDebug() << "Using sLocalPath. destinationState=" << destinationState;
404  // Prefer the local path -- but only if we were able to stat() the dest.
405  // Otherwise, renaming a desktop:/ url would copy from src=file to dest=desktop (#218719)
406  srcurl.setPath(sLocalPath);
407  } else {
408  srcurl = sourceUrl;
409  }
410  addCopyInfoFromUDSEntry(entry, srcurl, false, m_dest);
411 
412  m_currentDest = m_dest;
413  m_bCurrentSrcIsDir = false;
414 
415  if ( isDir
416  // treat symlinks as files (no recursion)
417  && !entry.isLink()
418  && m_mode != CopyJob::Link ) // No recursion in Link mode either.
419  {
420  //kDebug(7007) << "Source is a directory";
421 
422  if (srcurl.isLocalFile()) {
423  const QString parentDir = srcurl.toLocalFile(KUrl::RemoveTrailingSlash);
424  m_parentDirs.insert(parentDir);
425  }
426 
427  m_bCurrentSrcIsDir = true; // used by slotEntries
428  if ( destinationState == DEST_IS_DIR ) // (case 1)
429  {
430  if ( !m_asMethod )
431  {
432  // Use <desturl>/<directory_copied> as destination, from now on
433  QString directory = srcurl.fileName();
434  const QString sName = entry.stringValue( KIO::UDSEntry::UDS_NAME );
435  KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(srcurl);
436  if (fnu == KProtocolInfo::Name) {
437  if (!sName.isEmpty())
438  directory = sName;
439  } else if (fnu == KProtocolInfo::DisplayName) {
440  const QString dispName = entry.stringValue( KIO::UDSEntry::UDS_DISPLAY_NAME );
441  if (!dispName.isEmpty())
442  directory = dispName;
443  else if (!sName.isEmpty())
444  directory = sName;
445  }
446  m_currentDest.addPath( directory );
447  }
448  }
449  else // (case 3)
450  {
451  // otherwise dest is new name for toplevel dir
452  // so the destination exists, in fact, from now on.
453  // (This even works with other src urls in the list, since the
454  // dir has effectively been created)
455  destinationState = DEST_IS_DIR;
456  if ( m_dest == m_globalDest )
457  m_globalDestinationState = destinationState;
458  }
459 
460  startListing( srcurl );
461  }
462  else
463  {
464  //kDebug(7007) << "Source is a file (or a symlink), or we are linking -> no recursive listing";
465 
466  if (srcurl.isLocalFile()) {
467  const QString parentDir = srcurl.directory(KUrl::ObeyTrailingSlash);
468  m_parentDirs.insert(parentDir);
469  }
470 
471  statNextSrc();
472  }
473 }
474 
475 bool CopyJob::doSuspend()
476 {
477  Q_D(CopyJob);
478  d->slotReport();
479  return Job::doSuspend();
480 }
481 
482 void CopyJobPrivate::slotReport()
483 {
484  Q_Q(CopyJob);
485  if ( q->isSuspended() )
486  return;
487  // If showProgressInfo was set, progressId() is > 0.
488  switch (state) {
489  case STATE_RENAMING:
490  q->setTotalAmount(KJob::Files, m_srcList.count());
491  // fall-through intended
492  case STATE_COPYING_FILES:
493  q->setProcessedAmount( KJob::Files, m_processedFiles );
494  if (m_bURLDirty)
495  {
496  // Only emit urls when they changed. This saves time, and fixes #66281
497  m_bURLDirty = false;
498  if (m_mode==CopyJob::Move)
499  {
500  emitMoving(q, m_currentSrcURL, m_currentDestURL);
501  emit q->moving( q, m_currentSrcURL, m_currentDestURL);
502  }
503  else if (m_mode==CopyJob::Link)
504  {
505  emitCopying( q, m_currentSrcURL, m_currentDestURL ); // we don't have a delegate->linking
506  emit q->linking( q, m_currentSrcURL.path(), m_currentDestURL );
507  }
508  else
509  {
510  emitCopying( q, m_currentSrcURL, m_currentDestURL );
511  emit q->copying( q, m_currentSrcURL, m_currentDestURL );
512  }
513  }
514  break;
515 
516  case STATE_CREATING_DIRS:
517  q->setProcessedAmount( KJob::Directories, m_processedDirs );
518  if (m_bURLDirty)
519  {
520  m_bURLDirty = false;
521  emit q->creatingDir( q, m_currentDestURL );
522  emitCreatingDir( q, m_currentDestURL );
523  }
524  break;
525 
526  case STATE_STATING:
527  case STATE_LISTING:
528  if (m_bURLDirty)
529  {
530  m_bURLDirty = false;
531  if (m_mode==CopyJob::Move)
532  {
533  emitMoving( q, m_currentSrcURL, m_currentDestURL );
534  }
535  else
536  {
537  emitCopying( q, m_currentSrcURL, m_currentDestURL );
538  }
539  }
540  q->setTotalAmount(KJob::Bytes, m_totalSize);
541  q->setTotalAmount(KJob::Files, files.count());
542  q->setTotalAmount(KJob::Directories, dirs.count());
543  break;
544 
545  default:
546  break;
547  }
548 }
549 
550 void CopyJobPrivate::slotEntries(KIO::Job* job, const UDSEntryList& list)
551 {
552  //Q_Q(CopyJob);
553  UDSEntryList::ConstIterator it = list.constBegin();
554  UDSEntryList::ConstIterator end = list.constEnd();
555  for (; it != end; ++it) {
556  const UDSEntry& entry = *it;
557  addCopyInfoFromUDSEntry(entry, static_cast<SimpleJob *>(job)->url(), m_bCurrentSrcIsDir, m_currentDest);
558  }
559 }
560 
561 void CopyJobPrivate::addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest)
562 {
563  struct CopyInfo info;
564  info.permissions = entry.numberValue(KIO::UDSEntry::UDS_ACCESS, -1);
565  info.mtime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
566  info.ctime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
567  info.size = (KIO::filesize_t) entry.numberValue(KIO::UDSEntry::UDS_SIZE, -1);
568  if (info.size != (KIO::filesize_t) -1)
569  m_totalSize += info.size;
570 
571  // recursive listing, displayName can be a/b/c/d
572  const QString fileName = entry.stringValue(KIO::UDSEntry::UDS_NAME);
573  const QString urlStr = entry.stringValue(KIO::UDSEntry::UDS_URL);
574  KUrl url;
575  if (!urlStr.isEmpty())
576  url = urlStr;
577  QString localPath = entry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH);
578  const bool isDir = entry.isDir();
579  info.linkDest = entry.stringValue(KIO::UDSEntry::UDS_LINK_DEST);
580 
581  if (fileName != QLatin1String("..") && fileName != QLatin1String(".")) {
582  const bool hasCustomURL = !url.isEmpty() || !localPath.isEmpty();
583  if (!hasCustomURL) {
584  // Make URL from displayName
585  url = srcUrl;
586  if (srcIsDir) { // Only if src is a directory. Otherwise uSource is fine as is
587  //kDebug(7007) << "adding path" << displayName;
588  url.addPath(fileName);
589  }
590  }
591  //kDebug(7007) << "displayName=" << displayName << "url=" << url;
592  if (!localPath.isEmpty() && kio_resolve_local_urls && destinationState != DEST_DOESNT_EXIST) {
593  url = KUrl(localPath);
594  }
595 
596  info.uSource = url;
597  info.uDest = currentDest;
598  //kDebug(7007) << "uSource=" << info.uSource << "uDest(1)=" << info.uDest;
599  // Append filename or dirname to destination URL, if allowed
600  if (destinationState == DEST_IS_DIR &&
601  // "copy/move as <foo>" means 'foo' is the dest for the base srcurl
602  // (passed here during stating) but not its children (during listing)
603  (! (m_asMethod && state == STATE_STATING)))
604  {
605  QString destFileName;
606  KProtocolInfo::FileNameUsedForCopying fnu = KProtocolManager::fileNameUsedForCopying(url);
607  if (hasCustomURL &&
608  fnu == KProtocolInfo::FromUrl) {
609  //destFileName = url.fileName(); // Doesn't work for recursive listing
610  // Count the number of prefixes used by the recursive listjob
611  int numberOfSlashes = fileName.count('/'); // don't make this a find()!
612  QString path = url.path();
613  int pos = 0;
614  for (int n = 0; n < numberOfSlashes + 1; ++n) {
615  pos = path.lastIndexOf('/', pos - 1);
616  if (pos == -1) { // error
617  kWarning(7007) << "kioslave bug: not enough slashes in UDS_URL" << path << "- looking for" << numberOfSlashes << "slashes";
618  break;
619  }
620  }
621  if (pos >= 0) {
622  destFileName = path.mid(pos + 1);
623  }
624 
625  } else if ( fnu == KProtocolInfo::Name ) { // destination filename taken from UDS_NAME
626  destFileName = fileName;
627  } else { // from display name (with fallback to name)
628  const QString displayName = entry.stringValue(KIO::UDSEntry::UDS_DISPLAY_NAME);
629  destFileName = displayName.isEmpty() ? fileName : displayName;
630  }
631 
632  // Here we _really_ have to add some filename to the dest.
633  // Otherwise, we end up with e.g. dest=..../Desktop/ itself.
634  // (This can happen when dropping a link to a webpage with no path)
635  if (destFileName.isEmpty()) {
636  destFileName = KIO::encodeFileName(info.uSource.prettyUrl());
637  }
638 
639  //kDebug(7007) << " adding destFileName=" << destFileName;
640  info.uDest.addPath(destFileName);
641  }
642  //kDebug(7007) << " uDest(2)=" << info.uDest;
643  //kDebug(7007) << " " << info.uSource << "->" << info.uDest;
644  if (info.linkDest.isEmpty() && isDir && m_mode != CopyJob::Link) { // Dir
645  dirs.append(info); // Directories
646  if (m_mode == CopyJob::Move) {
647  dirsToRemove.append(info.uSource);
648  }
649  } else {
650  files.append(info); // Files and any symlinks
651  }
652  }
653 }
654 
655 void CopyJobPrivate::skipSrc(bool isDir)
656 {
657  m_dest = m_globalDest;
658  destinationState = m_globalDestinationState;
659  skip(*m_currentStatSrc, isDir);
660  ++m_currentStatSrc;
661  statCurrentSrc();
662 }
663 
664 void CopyJobPrivate::statNextSrc()
665 {
666  /* Revert to the global destination, the one that applies to all source urls.
667  * Imagine you copy the items a b and c into /d, but /d/b exists so the user uses "Rename" to put it in /foo/b instead.
668  * d->m_dest is /foo/b for b, but we have to revert to /d for item c and following.
669  */
670  m_dest = m_globalDest;
671  destinationState = m_globalDestinationState;
672  ++m_currentStatSrc;
673  statCurrentSrc();
674 }
675 
676 void CopyJobPrivate::statCurrentSrc()
677 {
678  Q_Q(CopyJob);
679  if (m_currentStatSrc != m_srcList.constEnd()) {
680  m_currentSrcURL = (*m_currentStatSrc);
681  m_bURLDirty = true;
682  if (m_mode == CopyJob::Link) {
683  // Skip the "stating the source" stage, we don't need it for linking
684  m_currentDest = m_dest;
685  struct CopyInfo info;
686  info.permissions = -1;
687  info.mtime = (time_t) -1;
688  info.ctime = (time_t) -1;
689  info.size = (KIO::filesize_t)-1;
690  info.uSource = m_currentSrcURL;
691  info.uDest = m_currentDest;
692  // Append filename or dirname to destination URL, if allowed
693  if (destinationState == DEST_IS_DIR && !m_asMethod) {
694  if (
695  (m_currentSrcURL.protocol() == info.uDest.protocol()) &&
696  (m_currentSrcURL.host() == info.uDest.host()) &&
697  (m_currentSrcURL.port() == info.uDest.port()) &&
698  (m_currentSrcURL.user() == info.uDest.user()) &&
699  (m_currentSrcURL.pass() == info.uDest.pass()) ) {
700  // This is the case of creating a real symlink
701  info.uDest.addPath( m_currentSrcURL.fileName() );
702  } else {
703  // Different protocols, we'll create a .desktop file
704  // We have to change the extension anyway, so while we're at it,
705  // name the file like the URL
706  info.uDest.addPath(KIO::encodeFileName(m_currentSrcURL.prettyUrl()) + ".desktop");
707  }
708  }
709  files.append( info ); // Files and any symlinks
710  statNextSrc(); // we could use a loop instead of a recursive call :)
711  return;
712  }
713 
714  // Let's see if we can skip stat'ing, for the case where a directory view has the info already
715  const KFileItem cachedItem = KDirLister::cachedItemForUrl(m_currentSrcURL);
716  KIO::UDSEntry entry;
717  if (!cachedItem.isNull()) {
718  entry = cachedItem.entry();
719  if (destinationState != DEST_DOESNT_EXIST) { // only resolve src if we could resolve dest (#218719)
720  bool dummyIsLocal;
721  m_currentSrcURL = cachedItem.mostLocalUrl(dummyIsLocal); // #183585
722  }
723  }
724 
725  if (m_mode == CopyJob::Move && (
726  // Don't go renaming right away if we need a stat() to find out the destination filename
727  KProtocolManager::fileNameUsedForCopying(m_currentSrcURL) == KProtocolInfo::FromUrl ||
728  destinationState != DEST_IS_DIR || m_asMethod)
729  ) {
730  // If moving, before going for the full stat+[list+]copy+del thing, try to rename
731  // The logic is pretty similar to FileCopyJobPrivate::slotStart()
732  if ( (m_currentSrcURL.protocol() == m_dest.protocol()) &&
733  (m_currentSrcURL.host() == m_dest.host()) &&
734  (m_currentSrcURL.port() == m_dest.port()) &&
735  (m_currentSrcURL.user() == m_dest.user()) &&
736  (m_currentSrcURL.pass() == m_dest.pass()) )
737  {
738  startRenameJob( m_currentSrcURL );
739  return;
740  }
741  else if ( m_currentSrcURL.isLocalFile() && KProtocolManager::canRenameFromFile( m_dest ) )
742  {
743  startRenameJob( m_dest );
744  return;
745  }
746  else if ( m_dest.isLocalFile() && KProtocolManager::canRenameToFile( m_currentSrcURL ) )
747  {
748  startRenameJob( m_currentSrcURL );
749  return;
750  }
751  }
752 
753  // if the file system doesn't support deleting, we do not even stat
754  if (m_mode == CopyJob::Move && !KProtocolManager::supportsDeleting(m_currentSrcURL)) {
755  QPointer<CopyJob> that = q;
756  emit q->warning( q, buildErrorString(ERR_CANNOT_DELETE, m_currentSrcURL.prettyUrl()) );
757  if (that)
758  statNextSrc(); // we could use a loop instead of a recursive call :)
759  return;
760  }
761 
762  m_bOnlyRenames = false;
763 
764  // Testing for entry.count()>0 here is not good enough; KFileItem inserts
765  // entries for UDS_USER and UDS_GROUP even on initially empty UDSEntries (#192185)
766  if (entry.contains(KIO::UDSEntry::UDS_NAME)) {
767  kDebug(7007) << "fast path! found info about" << m_currentSrcURL << "in KDirLister";
768  sourceStated(entry, m_currentSrcURL);
769  return;
770  }
771 
772  // Stat the next src url
773  Job * job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
774  //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
775  state = STATE_STATING;
776  q->addSubjob(job);
777  m_currentDestURL = m_dest;
778  m_bURLDirty = true;
779  }
780  else
781  {
782  // Finished the stat'ing phase
783  // First make sure that the totals were correctly emitted
784  state = STATE_STATING;
785  m_bURLDirty = true;
786  slotReport();
787 
788  kDebug(7007)<<"Stating finished. To copy:"<<m_totalSize<<", available:"<<m_freeSpace;
789  //TODO warn user beforehand if space is not enough
790 
791  if (!dirs.isEmpty())
792  emit q->aboutToCreate( q, dirs );
793  if (!files.isEmpty())
794  emit q->aboutToCreate( q, files );
795  // Check if we are copying a single file
796  m_bSingleFileCopy = ( files.count() == 1 && dirs.isEmpty() );
797  // Then start copying things
798  state = STATE_CREATING_DIRS;
799  createNextDir();
800  }
801 }
802 
803 void CopyJobPrivate::startRenameJob( const KUrl& slave_url )
804 {
805  Q_Q(CopyJob);
806 
807  // Silence KDirWatch notifications, otherwise performance is horrible
808  if (m_currentSrcURL.isLocalFile()) {
809  const QString parentDir = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash);
810  if (!m_parentDirs.contains(parentDir)) {
811  KDirWatch::self()->stopDirScan(parentDir);
812  m_parentDirs.insert(parentDir);
813  }
814  }
815 
816  KUrl dest = m_dest;
817  // Append filename or dirname to destination URL, if allowed
818  if ( destinationState == DEST_IS_DIR && !m_asMethod )
819  dest.addPath( m_currentSrcURL.fileName() );
820  m_currentDestURL = dest;
821  kDebug(7007) << m_currentSrcURL << "->" << dest << "trying direct rename first";
822  state = STATE_RENAMING;
823 
824  struct CopyInfo info;
825  info.permissions = -1;
826  info.mtime = (time_t) -1;
827  info.ctime = (time_t) -1;
828  info.size = (KIO::filesize_t)-1;
829  info.uSource = m_currentSrcURL;
830  info.uDest = dest;
831  QList<CopyInfo> files;
832  files.append(info);
833  emit q->aboutToCreate( q, files );
834 
835  KIO_ARGS << m_currentSrcURL << dest << (qint8) false /*no overwrite*/;
836  SimpleJob * newJob = SimpleJobPrivate::newJobNoUi(slave_url, CMD_RENAME, packedArgs);
837  Scheduler::setJobPriority(newJob, 1);
838  q->addSubjob( newJob );
839  if ( m_currentSrcURL.directory() != dest.directory() ) // For the user, moving isn't renaming. Only renaming is.
840  m_bOnlyRenames = false;
841 }
842 
843 void CopyJobPrivate::startListing( const KUrl & src )
844 {
845  Q_Q(CopyJob);
846  state = STATE_LISTING;
847  m_bURLDirty = true;
848  ListJob * newjob = listRecursive(src, KIO::HideProgressInfo);
849  newjob->setUnrestricted(true);
850  q->connect(newjob, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
851  SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList)));
852  q->addSubjob( newjob );
853 }
854 
855 void CopyJobPrivate::skip(const KUrl & sourceUrl, bool isDir)
856 {
857  KUrl dir = sourceUrl;
858  if (!isDir) {
859  // Skipping a file: make sure not to delete the parent dir (#208418)
860  dir.setPath(dir.directory());
861  }
862  while (dirsToRemove.removeAll(dir) > 0) {
863  // Do not rely on rmdir() on the parent directories aborting.
864  // Exclude the parent dirs explicitly.
865  dir.setPath(dir.directory());
866  }
867 }
868 
869 bool CopyJobPrivate::shouldOverwriteDir( const QString& path ) const
870 {
871  if ( m_bOverwriteAllDirs )
872  return true;
873  return m_overwriteList.contains(path);
874 }
875 
876 bool CopyJobPrivate::shouldOverwriteFile( const QString& path ) const
877 {
878  if ( m_bOverwriteAllFiles )
879  return true;
880  return m_overwriteList.contains(path);
881 }
882 
883 bool CopyJobPrivate::shouldSkip( const QString& path ) const
884 {
885  Q_FOREACH(const QString& skipPath, m_skipList) {
886  if ( path.startsWith(skipPath) )
887  return true;
888  }
889  return false;
890 }
891 
892 void CopyJobPrivate::slotResultCreatingDirs( KJob * job )
893 {
894  Q_Q(CopyJob);
895  // The dir we are trying to create:
896  QList<CopyInfo>::Iterator it = dirs.begin();
897  // Was there an error creating a dir ?
898  if ( job->error() )
899  {
900  m_conflictError = job->error();
901  if ( (m_conflictError == ERR_DIR_ALREADY_EXIST)
902  || (m_conflictError == ERR_FILE_ALREADY_EXIST) ) // can't happen?
903  {
904  KUrl oldURL = ((SimpleJob*)job)->url();
905  // Should we skip automatically ?
906  if ( m_bAutoSkipDirs ) {
907  // We don't want to copy files in this directory, so we put it on the skip list
908  m_skipList.append( oldURL.path( KUrl::AddTrailingSlash ) );
909  skip(oldURL, true);
910  dirs.erase( it ); // Move on to next dir
911  } else {
912  // Did the user choose to overwrite already?
913  const QString destDir = (*it).uDest.path();
914  if ( shouldOverwriteDir( destDir ) ) { // overwrite => just skip
915  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
916  dirs.erase( it ); // Move on to next dir
917  } else {
918  if (m_bAutoRenameDirs) {
919  QString oldPath = (*it).uDest.path(KUrl::AddTrailingSlash);
920 
921  KUrl destDirectory((*it).uDest);
922  destDirectory.setPath(destDirectory.directory());
923  QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName());
924 
925  KUrl newUrl((*it).uDest);
926  newUrl.setFileName(newName);
927 
928  emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg
929 
930  // Change the current one and strip the trailing '/'
931  (*it).uDest.setPath(newUrl.path(KUrl::RemoveTrailingSlash));
932 
933  QString newPath = newUrl.path(KUrl::AddTrailingSlash); // With trailing slash
934  QList<CopyInfo>::Iterator renamedirit = it;
935  ++renamedirit;
936  // Change the name of subdirectories inside the directory
937  for(; renamedirit != dirs.end() ; ++renamedirit) {
938  QString path = (*renamedirit).uDest.path();
939  if (path.startsWith(oldPath)) {
940  QString n = path;
941  n.replace(0, oldPath.length(), newPath);
942  kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path()
943  << "was going to be" << path
944  << ", changed into" << n;
945  (*renamedirit).uDest.setPath(n);
946  }
947  }
948  // Change filenames inside the directory
949  QList<CopyInfo>::Iterator renamefileit = files.begin();
950  for(; renamefileit != files.end() ; ++renamefileit) {
951  QString path = (*renamefileit).uDest.path();
952  if (path.startsWith(oldPath)) {
953  QString n = path;
954  n.replace(0, oldPath.length(), newPath);
955  kDebug(7007) << "files list:" << (*renamefileit).uSource.path()
956  << "was going to be" << path
957  << ", changed into" << n;
958  (*renamefileit).uDest.setPath(n);
959  }
960  }
961  if (!dirs.isEmpty()) {
962  emit q->aboutToCreate(q, dirs);
963  }
964  if (!files.isEmpty()) {
965  emit q->aboutToCreate(q, files);
966  }
967 
968  }
969  else {
970  if (!q->isInteractive()) {
971  q->Job::slotResult(job); // will set the error and emit result(this)
972  return;
973  }
974 
975  assert(((SimpleJob*)job)->url().url() == (*it).uDest.url());
976  q->removeSubjob(job);
977  assert (!q->hasSubjobs()); // We should have only one job at a time ...
978 
979  // We need to stat the existing dir, to get its last-modification time
980  KUrl existingDest((*it).uDest);
981  SimpleJob * newJob = KIO::stat(existingDest, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
982  Scheduler::setJobPriority(newJob, 1);
983  kDebug(7007) << "KIO::stat for resolving conflict on " << existingDest;
984  state = STATE_CONFLICT_CREATING_DIRS;
985  q->addSubjob(newJob);
986  return; // Don't move to next dir yet !
987  }
988  }
989  }
990  }
991  else
992  {
993  // Severe error, abort
994  q->Job::slotResult( job ); // will set the error and emit result(this)
995  return;
996  }
997  }
998  else // no error : remove from list, to move on to next dir
999  {
1000  //this is required for the undo feature
1001  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true, false );
1002  m_directoriesCopied.append( *it );
1003  dirs.erase( it );
1004  }
1005 
1006  m_processedDirs++;
1007  //emit processedAmount( this, KJob::Directories, m_processedDirs );
1008  q->removeSubjob( job );
1009  assert( !q->hasSubjobs() ); // We should have only one job at a time ...
1010  createNextDir();
1011 }
1012 
1013 void CopyJobPrivate::slotResultConflictCreatingDirs( KJob * job )
1014 {
1015  Q_Q(CopyJob);
1016  // We come here after a conflict has been detected and we've stated the existing dir
1017 
1018  // The dir we were trying to create:
1019  QList<CopyInfo>::Iterator it = dirs.begin();
1020 
1021  const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
1022 
1023  // Its modification time:
1024  const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
1025  const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
1026 
1027  const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
1028  const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
1029 
1030  q->removeSubjob( job );
1031  assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
1032 
1033  // Always multi and skip (since there are files after that)
1034  RenameDialog_Mode mode = (RenameDialog_Mode)( M_MULTI | M_SKIP | M_ISDIR );
1035  // Overwrite only if the existing thing is a dir (no chance with a file)
1036  if ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1037  {
1038  if( (*it).uSource == (*it).uDest ||
1039  ((*it).uSource.protocol() == (*it).uDest.protocol() &&
1040  (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
1041  mode = (RenameDialog_Mode)( mode | M_OVERWRITE_ITSELF);
1042  else
1043  mode = (RenameDialog_Mode)( mode | M_OVERWRITE );
1044  }
1045 
1046  QString existingDest = (*it).uDest.path();
1047  QString newPath;
1048  if (m_reportTimer)
1049  m_reportTimer->stop();
1050  RenameDialog_Result r = q->ui()->askFileRename( q, i18n("Folder Already Exists"),
1051  (*it).uSource.url(),
1052  (*it).uDest.url(),
1053  mode, newPath,
1054  (*it).size, destsize,
1055  (*it).ctime, destctime,
1056  (*it).mtime, destmtime );
1057  if (m_reportTimer)
1058  m_reportTimer->start(REPORT_TIMEOUT);
1059  switch ( r ) {
1060  case R_CANCEL:
1061  q->setError( ERR_USER_CANCELED );
1062  q->emitResult();
1063  return;
1064  case R_AUTO_RENAME:
1065  m_bAutoRenameDirs = true;
1066  // fall through
1067  case R_RENAME:
1068  {
1069  QString oldPath = (*it).uDest.path( KUrl::AddTrailingSlash );
1070  KUrl newUrl( (*it).uDest );
1071  newUrl.setPath( newPath );
1072  emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
1073 
1074  // Change the current one and strip the trailing '/'
1075  (*it).uDest.setPath( newUrl.path( KUrl::RemoveTrailingSlash ) );
1076  newPath = newUrl.path( KUrl::AddTrailingSlash ); // With trailing slash
1077  QList<CopyInfo>::Iterator renamedirit = it;
1078  ++renamedirit;
1079  // Change the name of subdirectories inside the directory
1080  for( ; renamedirit != dirs.end() ; ++renamedirit )
1081  {
1082  QString path = (*renamedirit).uDest.path();
1083  if ( path.startsWith( oldPath ) ) {
1084  QString n = path;
1085  n.replace( 0, oldPath.length(), newPath );
1086  kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path()
1087  << "was going to be" << path
1088  << ", changed into" << n;
1089  (*renamedirit).uDest.setPath( n );
1090  }
1091  }
1092  // Change filenames inside the directory
1093  QList<CopyInfo>::Iterator renamefileit = files.begin();
1094  for( ; renamefileit != files.end() ; ++renamefileit )
1095  {
1096  QString path = (*renamefileit).uDest.path();
1097  if ( path.startsWith( oldPath ) ) {
1098  QString n = path;
1099  n.replace( 0, oldPath.length(), newPath );
1100  kDebug(7007) << "files list:" << (*renamefileit).uSource.path()
1101  << "was going to be" << path
1102  << ", changed into" << n;
1103  (*renamefileit).uDest.setPath( n );
1104  }
1105  }
1106  if (!dirs.isEmpty())
1107  emit q->aboutToCreate( q, dirs );
1108  if (!files.isEmpty())
1109  emit q->aboutToCreate( q, files );
1110  }
1111  break;
1112  case R_AUTO_SKIP:
1113  m_bAutoSkipDirs = true;
1114  // fall through
1115  case R_SKIP:
1116  m_skipList.append( existingDest );
1117  skip((*it).uSource, true);
1118  // Move on to next dir
1119  dirs.erase( it );
1120  m_processedDirs++;
1121  break;
1122  case R_OVERWRITE:
1123  m_overwriteList.insert( existingDest );
1124  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
1125  // Move on to next dir
1126  dirs.erase( it );
1127  m_processedDirs++;
1128  break;
1129  case R_OVERWRITE_ALL:
1130  m_bOverwriteAllDirs = true;
1131  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
1132  // Move on to next dir
1133  dirs.erase( it );
1134  m_processedDirs++;
1135  break;
1136  default:
1137  assert( 0 );
1138  }
1139  state = STATE_CREATING_DIRS;
1140  //emit processedAmount( this, KJob::Directories, m_processedDirs );
1141  createNextDir();
1142 }
1143 
1144 void CopyJobPrivate::createNextDir()
1145 {
1146  Q_Q(CopyJob);
1147  KUrl udir;
1148  if ( !dirs.isEmpty() )
1149  {
1150  // Take first dir to create out of list
1151  QList<CopyInfo>::Iterator it = dirs.begin();
1152  // Is this URL on the skip list or the overwrite list ?
1153  while( it != dirs.end() && udir.isEmpty() )
1154  {
1155  const QString dir = (*it).uDest.path();
1156  if ( shouldSkip( dir ) ) {
1157  dirs.erase( it );
1158  it = dirs.begin();
1159  } else
1160  udir = (*it).uDest;
1161  }
1162  }
1163  if ( !udir.isEmpty() ) // any dir to create, finally ?
1164  {
1165  // Create the directory - with default permissions so that we can put files into it
1166  // TODO : change permissions once all is finished; but for stuff coming from CDROM it sucks...
1167  KIO::SimpleJob *newjob = KIO::mkdir( udir, -1 );
1168  Scheduler::setJobPriority(newjob, 1);
1169  if (shouldOverwriteFile(udir.path())) { // if we are overwriting an existing file or symlink
1170  newjob->addMetaData("overwrite", "true");
1171  }
1172 
1173  m_currentDestURL = udir;
1174  m_bURLDirty = true;
1175 
1176  q->addSubjob(newjob);
1177  return;
1178  }
1179  else // we have finished creating dirs
1180  {
1181  q->setProcessedAmount( KJob::Directories, m_processedDirs ); // make sure final number appears
1182 
1183  if (m_mode == CopyJob::Move) {
1184  // Now we know which dirs hold the files we're going to delete.
1185  // To speed things up and prevent double-notification, we disable KDirWatch
1186  // on those dirs temporarily (using KDirWatch::self, that's the instanced
1187  // used by e.g. kdirlister).
1188  for ( QSet<QString>::const_iterator it = m_parentDirs.constBegin() ; it != m_parentDirs.constEnd() ; ++it )
1189  KDirWatch::self()->stopDirScan( *it );
1190  }
1191 
1192  state = STATE_COPYING_FILES;
1193  m_processedFiles++; // Ralf wants it to start at 1, not 0
1194  copyNextFile();
1195  }
1196 }
1197 
1198 void CopyJobPrivate::slotResultCopyingFiles( KJob * job )
1199 {
1200  Q_Q(CopyJob);
1201  // The file we were trying to copy:
1202  QList<CopyInfo>::Iterator it = files.begin();
1203  if ( job->error() )
1204  {
1205  // Should we skip automatically ?
1206  if ( m_bAutoSkipFiles )
1207  {
1208  skip((*it).uSource, false);
1209  m_fileProcessedSize = (*it).size;
1210  files.erase( it ); // Move on to next file
1211  }
1212  else
1213  {
1214  m_conflictError = job->error(); // save for later
1215  // Existing dest ?
1216  if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
1217  || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1218  || ( m_conflictError == ERR_IDENTICAL_FILES ) )
1219  {
1220  if (m_bAutoRenameFiles) {
1221  KUrl destDirectory((*it).uDest);
1222  destDirectory.setPath(destDirectory.directory());
1223  const QString newName = KIO::RenameDialog::suggestName(destDirectory, (*it).uDest.fileName());
1224 
1225  KUrl newUrl((*it).uDest);
1226  newUrl.setFileName(newName);
1227 
1228  emit q->renamed(q, (*it).uDest, newUrl); // for e.g. kpropsdlg
1229  (*it).uDest = newUrl;
1230 
1231  QList<CopyInfo> files;
1232  files.append(*it);
1233  emit q->aboutToCreate(q, files);
1234  }
1235  else {
1236  if ( !q->isInteractive() ) {
1237  q->Job::slotResult( job ); // will set the error and emit result(this)
1238  return;
1239  }
1240 
1241  q->removeSubjob(job);
1242  assert (!q->hasSubjobs());
1243  // We need to stat the existing file, to get its last-modification time
1244  KUrl existingFile((*it).uDest);
1245  SimpleJob * newJob = KIO::stat(existingFile, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
1246  Scheduler::setJobPriority(newJob, 1);
1247  kDebug(7007) << "KIO::stat for resolving conflict on " << existingFile;
1248  state = STATE_CONFLICT_COPYING_FILES;
1249  q->addSubjob(newJob);
1250  return; // Don't move to next file yet !
1251  }
1252  }
1253  else
1254  {
1255  if ( m_bCurrentOperationIsLink && qobject_cast<KIO::DeleteJob*>( job ) )
1256  {
1257  // Very special case, see a few lines below
1258  // We are deleting the source of a symlink we successfully moved... ignore error
1259  m_fileProcessedSize = (*it).size;
1260  files.erase( it );
1261  } else {
1262  if ( !q->isInteractive() ) {
1263  q->Job::slotResult( job ); // will set the error and emit result(this)
1264  return;
1265  }
1266 
1267  // Go directly to the conflict resolution, there is nothing to stat
1268  slotResultConflictCopyingFiles( job );
1269  return;
1270  }
1271  }
1272  }
1273  } else // no error
1274  {
1275  // Special case for moving links. That operation needs two jobs, unlike others.
1276  if ( m_bCurrentOperationIsLink && m_mode == CopyJob::Move
1277  && !qobject_cast<KIO::DeleteJob *>( job ) // Deleting source not already done
1278  )
1279  {
1280  q->removeSubjob( job );
1281  assert ( !q->hasSubjobs() );
1282  // The only problem with this trick is that the error handling for this del operation
1283  // is not going to be right... see 'Very special case' above.
1284  KIO::Job * newjob = KIO::del( (*it).uSource, HideProgressInfo );
1285  q->addSubjob( newjob );
1286  return; // Don't move to next file yet !
1287  }
1288 
1289  if ( m_bCurrentOperationIsLink )
1290  {
1291  QString target = ( m_mode == CopyJob::Link ? (*it).uSource.path() : (*it).linkDest );
1292  //required for the undo feature
1293  emit q->copyingLinkDone( q, (*it).uSource, target, (*it).uDest );
1294  }
1295  else {
1296  //required for the undo feature
1297  emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, false, false );
1298  if (m_mode == CopyJob::Move)
1299  {
1300  org::kde::KDirNotify::emitFileMoved( (*it).uSource.url(), (*it).uDest.url() );
1301  }
1302  m_successSrcList.append((*it).uSource);
1303  if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) {
1304  m_freeSpace -= (*it).size;
1305  }
1306 
1307  }
1308  // remove from list, to move on to next file
1309  files.erase( it );
1310  }
1311  m_processedFiles++;
1312 
1313  // clear processed size for last file and add it to overall processed size
1314  m_processedSize += m_fileProcessedSize;
1315  m_fileProcessedSize = 0;
1316 
1317  //kDebug(7007) << files.count() << "files remaining";
1318 
1319  // Merge metadata from subjob
1320  KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
1321  Q_ASSERT(kiojob);
1322  m_incomingMetaData += kiojob->metaData();
1323  q->removeSubjob( job );
1324  assert( !q->hasSubjobs() ); // We should have only one job at a time ...
1325  copyNextFile();
1326 }
1327 
1328 void CopyJobPrivate::slotResultConflictCopyingFiles( KJob * job )
1329 {
1330  Q_Q(CopyJob);
1331  // We come here after a conflict has been detected and we've stated the existing file
1332  // The file we were trying to create:
1333  QList<CopyInfo>::Iterator it = files.begin();
1334 
1335  RenameDialog_Result res;
1336  QString newPath;
1337 
1338  if (m_reportTimer)
1339  m_reportTimer->stop();
1340 
1341  if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
1342  || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
1343  || ( m_conflictError == ERR_IDENTICAL_FILES ) )
1344  {
1345  // Its modification time:
1346  const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
1347 
1348  const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
1349  const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
1350  const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
1351  const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
1352 
1353  // Offer overwrite only if the existing thing is a file
1354  // If src==dest, use "overwrite-itself"
1355  RenameDialog_Mode mode;
1356  bool isDir = true;
1357 
1358  if( m_conflictError == ERR_DIR_ALREADY_EXIST )
1359  mode = M_ISDIR;
1360  else
1361  {
1362  if ( (*it).uSource == (*it).uDest ||
1363  ((*it).uSource.protocol() == (*it).uDest.protocol() &&
1364  (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
1365  mode = M_OVERWRITE_ITSELF;
1366  else
1367  mode = M_OVERWRITE;
1368  isDir = false;
1369  }
1370 
1371  if ( !m_bSingleFileCopy )
1372  mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
1373 
1374  res = q->ui()->askFileRename( q, !isDir ?
1375  i18n("File Already Exists") : i18n("Already Exists as Folder"),
1376  (*it).uSource.url(),
1377  (*it).uDest.url(),
1378  mode, newPath,
1379  (*it).size, destsize,
1380  (*it).ctime, destctime,
1381  (*it).mtime, destmtime );
1382 
1383  }
1384  else
1385  {
1386  if ( job->error() == ERR_USER_CANCELED )
1387  res = R_CANCEL;
1388  else if ( !q->isInteractive() ) {
1389  q->Job::slotResult( job ); // will set the error and emit result(this)
1390  return;
1391  }
1392  else
1393  {
1394  SkipDialog_Result skipResult = q->ui()->askSkip( q, files.count() > 1,
1395  job->errorString() );
1396 
1397  // Convert the return code from SkipDialog into a RenameDialog code
1398  res = ( skipResult == S_SKIP ) ? R_SKIP :
1399  ( skipResult == S_AUTO_SKIP ) ? R_AUTO_SKIP :
1400  R_CANCEL;
1401  }
1402  }
1403 
1404  if (m_reportTimer)
1405  m_reportTimer->start(REPORT_TIMEOUT);
1406 
1407  q->removeSubjob( job );
1408  assert ( !q->hasSubjobs() );
1409  switch ( res ) {
1410  case R_CANCEL:
1411  q->setError( ERR_USER_CANCELED );
1412  q->emitResult();
1413  return;
1414  case R_AUTO_RENAME:
1415  m_bAutoRenameFiles = true;
1416  // fall through
1417  case R_RENAME:
1418  {
1419  KUrl newUrl( (*it).uDest );
1420  newUrl.setPath( newPath );
1421  emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
1422  (*it).uDest = newUrl;
1423 
1424  QList<CopyInfo> files;
1425  files.append(*it);
1426  emit q->aboutToCreate( q, files );
1427  }
1428  break;
1429  case R_AUTO_SKIP:
1430  m_bAutoSkipFiles = true;
1431  // fall through
1432  case R_SKIP:
1433  // Move on to next file
1434  skip((*it).uSource, false);
1435  m_processedSize += (*it).size;
1436  files.erase( it );
1437  m_processedFiles++;
1438  break;
1439  case R_OVERWRITE_ALL:
1440  m_bOverwriteAllFiles = true;
1441  break;
1442  case R_OVERWRITE:
1443  // Add to overwrite list, so that copyNextFile knows to overwrite
1444  m_overwriteList.insert( (*it).uDest.path() );
1445  break;
1446  default:
1447  assert( 0 );
1448  }
1449  state = STATE_COPYING_FILES;
1450  copyNextFile();
1451 }
1452 
1453 KIO::Job* CopyJobPrivate::linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags )
1454 {
1455  //kDebug(7007) << "Linking";
1456  if (
1457  (uSource.protocol() == uDest.protocol()) &&
1458  (uSource.host() == uDest.host()) &&
1459  (uSource.port() == uDest.port()) &&
1460  (uSource.user() == uDest.user()) &&
1461  (uSource.pass() == uDest.pass()) )
1462  {
1463  // This is the case of creating a real symlink
1464  KIO::SimpleJob *newJob = KIO::symlink( uSource.path(), uDest, flags|HideProgressInfo /*no GUI*/ );
1465  Scheduler::setJobPriority(newJob, 1);
1466  //kDebug(7007) << "Linking target=" << uSource.path() << "link=" << uDest;
1467  //emit linking( this, uSource.path(), uDest );
1468  m_bCurrentOperationIsLink = true;
1469  m_currentSrcURL=uSource;
1470  m_currentDestURL=uDest;
1471  m_bURLDirty = true;
1472  //Observer::self()->slotCopying( this, uSource, uDest ); // should be slotLinking perhaps
1473  return newJob;
1474  } else {
1475  Q_Q(CopyJob);
1476  //kDebug(7007) << "Linking URL=" << uSource << "link=" << uDest;
1477  if ( uDest.isLocalFile() ) {
1478  // if the source is a devices url, handle it a littlebit special
1479 
1480  QString path = uDest.toLocalFile();
1481  //kDebug(7007) << "path=" << path;
1482  QFile f( path );
1483  if ( f.open( QIODevice::ReadWrite ) )
1484  {
1485  f.close();
1486  KDesktopFile desktopFile( path );
1487  KConfigGroup config = desktopFile.desktopGroup();
1488  KUrl url = uSource;
1489  url.setPass( "" );
1490  config.writePathEntry( "URL", url.url() );
1491  config.writeEntry( "Name", url.url() );
1492  config.writeEntry( "Type", QString::fromLatin1("Link") );
1493  QString protocol = uSource.protocol();
1494  if ( protocol == QLatin1String("ftp") )
1495  config.writeEntry( "Icon", QString::fromLatin1("folder-remote") );
1496  else if ( protocol == QLatin1String("http") )
1497  config.writeEntry( "Icon", QString::fromLatin1("text-html") );
1498  else if ( protocol == QLatin1String("info") )
1499  config.writeEntry( "Icon", QString::fromLatin1("text-x-texinfo") );
1500  else if ( protocol == QLatin1String("mailto") ) // sven:
1501  config.writeEntry( "Icon", QString::fromLatin1("internet-mail") ); // added mailto: support
1502  else
1503  config.writeEntry( "Icon", QString::fromLatin1("unknown") );
1504  config.sync();
1505  files.erase( files.begin() ); // done with this one, move on
1506  m_processedFiles++;
1507  //emit processedAmount( this, KJob::Files, m_processedFiles );
1508  copyNextFile();
1509  return 0;
1510  }
1511  else
1512  {
1513  kDebug(7007) << "ERR_CANNOT_OPEN_FOR_WRITING";
1514  q->setError( ERR_CANNOT_OPEN_FOR_WRITING );
1515  q->setErrorText( uDest.toLocalFile() );
1516  q->emitResult();
1517  return 0;
1518  }
1519  } else {
1520  // Todo: not show "link" on remote dirs if the src urls are not from the same protocol+host+...
1521  q->setError( ERR_CANNOT_SYMLINK );
1522  q->setErrorText( uDest.prettyUrl() );
1523  q->emitResult();
1524  return 0;
1525  }
1526  }
1527 }
1528 
1529 void CopyJobPrivate::copyNextFile()
1530 {
1531  Q_Q(CopyJob);
1532  bool bCopyFile = false;
1533  //kDebug(7007);
1534  // Take the first file in the list
1535  QList<CopyInfo>::Iterator it = files.begin();
1536  // Is this URL on the skip list ?
1537  while (it != files.end() && !bCopyFile)
1538  {
1539  const QString destFile = (*it).uDest.path();
1540  bCopyFile = !shouldSkip( destFile );
1541  if ( !bCopyFile ) {
1542  files.erase( it );
1543  it = files.begin();
1544  }
1545  }
1546 
1547  if (bCopyFile) // any file to create, finally ?
1548  {
1549  //kDebug()<<"preparing to copy"<<(*it).uSource<<(*it).size<<m_freeSpace;
1550  if (m_freeSpace != (KIO::filesize_t)-1 && (*it).size != (KIO::filesize_t)-1) {
1551  if (m_freeSpace < (*it).size) {
1552  q->setError( ERR_DISK_FULL );
1553  q->emitResult();
1554  return;
1555  }
1556  //TODO check if dst mount is msdos and (*it).size exceeds it's limits
1557  }
1558 
1559  const KUrl& uSource = (*it).uSource;
1560  const KUrl& uDest = (*it).uDest;
1561  // Do we set overwrite ?
1562  bool bOverwrite;
1563  const QString destFile = uDest.path();
1564  // kDebug(7007) << "copying" << destFile;
1565  if ( uDest == uSource )
1566  bOverwrite = false;
1567  else
1568  bOverwrite = shouldOverwriteFile( destFile );
1569 
1570  m_bCurrentOperationIsLink = false;
1571  KIO::Job * newjob = 0;
1572  if ( m_mode == CopyJob::Link ) {
1573  // User requested that a symlink be made
1574  const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1575  newjob = linkNextFile(uSource, uDest, flags);
1576  if (!newjob)
1577  return;
1578  } else if ( !(*it).linkDest.isEmpty() &&
1579  (uSource.protocol() == uDest.protocol()) &&
1580  (uSource.host() == uDest.host()) &&
1581  (uSource.port() == uDest.port()) &&
1582  (uSource.user() == uDest.user()) &&
1583  (uSource.pass() == uDest.pass()))
1584  // Copying a symlink - only on the same protocol/host/etc. (#5601, downloading an FTP file through its link),
1585  {
1586  const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1587  KIO::SimpleJob *newJob = KIO::symlink( (*it).linkDest, uDest, flags | HideProgressInfo /*no GUI*/ );
1588  Scheduler::setJobPriority(newJob, 1);
1589  newjob = newJob;
1590  //kDebug(7007) << "Linking target=" << (*it).linkDest << "link=" << uDest;
1591  m_currentSrcURL = KUrl( (*it).linkDest );
1592  m_currentDestURL = uDest;
1593  m_bURLDirty = true;
1594  //emit linking( this, (*it).linkDest, uDest );
1595  //Observer::self()->slotCopying( this, m_currentSrcURL, uDest ); // should be slotLinking perhaps
1596  m_bCurrentOperationIsLink = true;
1597  // NOTE: if we are moving stuff, the deletion of the source will be done in slotResultCopyingFiles
1598  } else if (m_mode == CopyJob::Move) // Moving a file
1599  {
1600  JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1601  KIO::FileCopyJob * moveJob = KIO::file_move( uSource, uDest, (*it).permissions, flags | HideProgressInfo/*no GUI*/ );
1602  moveJob->setSourceSize( (*it).size );
1603  if ((*it).mtime != -1) {
1604  moveJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) ); // #55804
1605  }
1606  newjob = moveJob;
1607  //kDebug(7007) << "Moving" << uSource << "to" << uDest;
1608  //emit moving( this, uSource, uDest );
1609  m_currentSrcURL=uSource;
1610  m_currentDestURL=uDest;
1611  m_bURLDirty = true;
1612  //Observer::self()->slotMoving( this, uSource, uDest );
1613  }
1614  else // Copying a file
1615  {
1616  // If source isn't local and target is local, we ignore the original permissions
1617  // Otherwise, files downloaded from HTTP end up with -r--r--r--
1618  bool remoteSource = !KProtocolManager::supportsListing(uSource);
1619  int permissions = (*it).permissions;
1620  if ( m_defaultPermissions || ( remoteSource && uDest.isLocalFile() ) )
1621  permissions = -1;
1622  JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
1623  KIO::FileCopyJob * copyJob = KIO::file_copy( uSource, uDest, permissions, flags | HideProgressInfo/*no GUI*/ );
1624  copyJob->setParentJob( q ); // in case of rename dialog
1625  copyJob->setSourceSize( (*it).size );
1626  if ((*it).mtime != -1) {
1627  copyJob->setModificationTime( QDateTime::fromTime_t( (*it).mtime ) );
1628  }
1629  newjob = copyJob;
1630  //kDebug(7007) << "Copying" << uSource << "to" << uDest;
1631  m_currentSrcURL=uSource;
1632  m_currentDestURL=uDest;
1633  m_bURLDirty = true;
1634  }
1635  q->addSubjob(newjob);
1636  q->connect( newjob, SIGNAL(processedSize(KJob*,qulonglong)),
1637  SLOT(slotProcessedSize(KJob*,qulonglong)) );
1638  q->connect( newjob, SIGNAL(totalSize(KJob*,qulonglong)),
1639  SLOT(slotTotalSize(KJob*,qulonglong)) );
1640  }
1641  else
1642  {
1643  // We're done
1644  //kDebug(7007) << "copyNextFile finished";
1645  deleteNextDir();
1646  }
1647 }
1648 
1649 void CopyJobPrivate::deleteNextDir()
1650 {
1651  Q_Q(CopyJob);
1652  if ( m_mode == CopyJob::Move && !dirsToRemove.isEmpty() ) // some dirs to delete ?
1653  {
1654  state = STATE_DELETING_DIRS;
1655  m_bURLDirty = true;
1656  // Take first dir to delete out of list - last ones first !
1657  KUrl::List::Iterator it = --dirsToRemove.end();
1658  SimpleJob *job = KIO::rmdir( *it );
1659  Scheduler::setJobPriority(job, 1);
1660  dirsToRemove.erase(it);
1661  q->addSubjob( job );
1662  }
1663  else
1664  {
1665  // This step is done, move on
1666  state = STATE_SETTING_DIR_ATTRIBUTES;
1667  m_directoriesCopiedIterator = m_directoriesCopied.constBegin();
1668  setNextDirAttribute();
1669  }
1670 }
1671 
1672 void CopyJobPrivate::setNextDirAttribute()
1673 {
1674  Q_Q(CopyJob);
1675  while (m_directoriesCopiedIterator != m_directoriesCopied.constEnd() &&
1676  (*m_directoriesCopiedIterator).mtime == -1) {
1677  ++m_directoriesCopiedIterator;
1678  }
1679  if ( m_directoriesCopiedIterator != m_directoriesCopied.constEnd() ) {
1680  const KUrl url = (*m_directoriesCopiedIterator).uDest;
1681  const time_t mtime = (*m_directoriesCopiedIterator).mtime;
1682  const QDateTime dt = QDateTime::fromTime_t(mtime);
1683  ++m_directoriesCopiedIterator;
1684 
1685  KIO::SimpleJob *job = KIO::setModificationTime( url, dt );
1686  Scheduler::setJobPriority(job, 1);
1687  q->addSubjob( job );
1688 
1689 
1690 #if 0 // ifdef Q_OS_UNIX
1691  // TODO: can be removed now. Or reintroduced as a fast path for local files
1692  // if launching even more jobs as done above is a performance problem.
1693  //
1694  QLinkedList<CopyInfo>::const_iterator it = m_directoriesCopied.constBegin();
1695  for ( ; it != m_directoriesCopied.constEnd() ; ++it ) {
1696  const KUrl& url = (*it).uDest;
1697  if ( url.isLocalFile() && (*it).mtime != (time_t)-1 ) {
1698  KDE_struct_stat statbuf;
1699  if (KDE::lstat(url.path(), &statbuf) == 0) {
1700  struct utimbuf utbuf;
1701  utbuf.actime = statbuf.st_atime; // access time, unchanged
1702  utbuf.modtime = (*it).mtime; // modification time
1703  utime( path, &utbuf );
1704  }
1705 
1706  }
1707  }
1708  m_directoriesCopied.clear();
1709  // but then we need to jump to the else part below. Maybe with a recursive call?
1710 #endif
1711  } else {
1712  if (m_reportTimer)
1713  m_reportTimer->stop();
1714  --m_processedFiles; // undo the "start at 1" hack
1715  slotReport(); // display final numbers, important if progress dialog stays up
1716 
1717  q->emitResult();
1718  }
1719 }
1720 
1721 void CopyJob::emitResult()
1722 {
1723  Q_D(CopyJob);
1724  // Before we go, tell the world about the changes that were made.
1725  // Even if some error made us abort midway, we might still have done
1726  // part of the job so we better update the views! (#118583)
1727  if (!d->m_bOnlyRenames) {
1728  KUrl url(d->m_globalDest);
1729  if (d->m_globalDestinationState != DEST_IS_DIR || d->m_asMethod)
1730  url.setPath(url.directory());
1731  //kDebug(7007) << "KDirNotify'ing FilesAdded" << url;
1732  org::kde::KDirNotify::emitFilesAdded( url.url() );
1733 
1734  if (d->m_mode == CopyJob::Move && !d->m_successSrcList.isEmpty()) {
1735  kDebug(7007) << "KDirNotify'ing FilesRemoved" << d->m_successSrcList.toStringList();
1736  org::kde::KDirNotify::emitFilesRemoved(d->m_successSrcList.toStringList());
1737  }
1738 
1739  // Re-enable watching on the dirs that held the deleted files
1740  if (d->m_mode == CopyJob::Move) {
1741  for (QSet<QString>::const_iterator it = d->m_parentDirs.constBegin() ; it != d->m_parentDirs.constEnd() ; ++it)
1742  KDirWatch::self()->restartDirScan( *it );
1743  }
1744  }
1745  Job::emitResult();
1746 }
1747 
1748 void CopyJobPrivate::slotProcessedSize( KJob*, qulonglong data_size )
1749 {
1750  Q_Q(CopyJob);
1751  //kDebug(7007) << data_size;
1752  m_fileProcessedSize = data_size;
1753  q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
1754 
1755  if ( m_processedSize + m_fileProcessedSize > m_totalSize )
1756  {
1757  // Example: download any attachment from bugs.kde.org
1758  m_totalSize = m_processedSize + m_fileProcessedSize;
1759  //kDebug(7007) << "Adjusting m_totalSize to" << m_totalSize;
1760  q->setTotalAmount(KJob::Bytes, m_totalSize); // safety
1761  }
1762  //kDebug(7007) << "emit processedSize" << (unsigned long) (m_processedSize + m_fileProcessedSize);
1763  q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
1764 }
1765 
1766 void CopyJobPrivate::slotTotalSize( KJob*, qulonglong size )
1767 {
1768  Q_Q(CopyJob);
1769  //kDebug(7007) << size;
1770  // Special case for copying a single file
1771  // This is because some protocols don't implement stat properly
1772  // (e.g. HTTP), and don't give us a size in some cases (redirection)
1773  // so we'd rather rely on the size given for the transfer
1774  if ( m_bSingleFileCopy && size != m_totalSize)
1775  {
1776  //kDebug(7007) << "slotTotalSize: updating totalsize to" << size;
1777  m_totalSize = size;
1778  q->setTotalAmount(KJob::Bytes, size);
1779  }
1780 }
1781 
1782 void CopyJobPrivate::slotResultDeletingDirs( KJob * job )
1783 {
1784  Q_Q(CopyJob);
1785  if (job->error()) {
1786  // Couldn't remove directory. Well, perhaps it's not empty
1787  // because the user pressed Skip for a given file in it.
1788  // Let's not display "Could not remove dir ..." for each of those dir !
1789  } else {
1790  m_successSrcList.append(static_cast<KIO::SimpleJob*>(job)->url());
1791  }
1792  q->removeSubjob( job );
1793  assert( !q->hasSubjobs() );
1794  deleteNextDir();
1795 }
1796 
1797 void CopyJobPrivate::slotResultSettingDirAttributes( KJob * job )
1798 {
1799  Q_Q(CopyJob);
1800  if (job->error())
1801  {
1802  // Couldn't set directory attributes. Ignore the error, it can happen
1803  // with inferior file systems like VFAT.
1804  // Let's not display warnings for each dir like "cp -a" does.
1805  }
1806  q->removeSubjob( job );
1807  assert( !q->hasSubjobs() );
1808  setNextDirAttribute();
1809 }
1810 
1811 // We were trying to do a direct renaming, before even stat'ing
1812 void CopyJobPrivate::slotResultRenaming( KJob* job )
1813 {
1814  Q_Q(CopyJob);
1815  int err = job->error();
1816  const QString errText = job->errorText();
1817  // Merge metadata from subjob
1818  KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
1819  Q_ASSERT(kiojob);
1820  m_incomingMetaData += kiojob->metaData();
1821  q->removeSubjob( job );
1822  assert ( !q->hasSubjobs() );
1823  // Determine dest again
1824  KUrl dest = m_dest;
1825  if ( destinationState == DEST_IS_DIR && !m_asMethod )
1826  dest.addPath( m_currentSrcURL.fileName() );
1827  if ( err )
1828  {
1829  // Direct renaming didn't work. Try renaming to a temp name,
1830  // this can help e.g. when renaming 'a' to 'A' on a VFAT partition.
1831  // In that case it's the _same_ dir, we don't want to copy+del (data loss!)
1832  if ( m_currentSrcURL.isLocalFile() && m_currentSrcURL.url(KUrl::RemoveTrailingSlash) != dest.url(KUrl::RemoveTrailingSlash) &&
1833  m_currentSrcURL.url(KUrl::RemoveTrailingSlash).toLower() == dest.url(KUrl::RemoveTrailingSlash).toLower() &&
1834  ( err == ERR_FILE_ALREADY_EXIST ||
1835  err == ERR_DIR_ALREADY_EXIST ||
1836  err == ERR_IDENTICAL_FILES ) )
1837  {
1838  kDebug(7007) << "Couldn't rename directly, dest already exists. Detected special case of lower/uppercase renaming in same dir, try with 2 rename calls";
1839  const QString _src( m_currentSrcURL.toLocalFile() );
1840  const QString _dest( dest.toLocalFile() );
1841  const QString _tmpPrefix = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash|KUrl::AppendTrailingSlash);
1842  KTemporaryFile tmpFile;
1843  tmpFile.setPrefix(_tmpPrefix);
1844  const bool openOk = tmpFile.open();
1845  if (!openOk) {
1846  kWarning(7007) << "Couldn't open temp file in" << _tmpPrefix;
1847  } else {
1848  const QString _tmp( tmpFile.fileName() );
1849  tmpFile.close();
1850  tmpFile.remove();
1851  kDebug(7007) << "KTemporaryFile using" << _tmp << "as intermediary";
1852  if (KDE::rename( _src, _tmp ) == 0) {
1853  //kDebug(7007) << "Renaming" << _src << "to" << _tmp << "succeeded";
1854  if (!QFile::exists( _dest ) && KDE::rename(_tmp, _dest) == 0) {
1855  err = 0;
1856  org::kde::KDirNotify::emitFileRenamed(m_currentSrcURL.url(), dest.url());
1857  } else {
1858  kDebug(7007) << "Didn't manage to rename" << _tmp << "to" << _dest << ", reverting";
1859  // Revert back to original name!
1860  if (KDE::rename( _tmp, _src ) != 0) {
1861  kError(7007) << "Couldn't rename" << _tmp << "back to" << _src << '!';
1862  // Severe error, abort
1863  q->Job::slotResult(job); // will set the error and emit result(this)
1864  return;
1865  }
1866  }
1867  } else {
1868  kDebug(7007) << "mv" << _src << _tmp << "failed:" << strerror(errno);
1869  }
1870  }
1871  }
1872  }
1873  if ( err )
1874  {
1875  // This code is similar to CopyJobPrivate::slotResultConflictCopyingFiles
1876  // but here it's about the base src url being moved/renamed
1877  // (m_currentSrcURL) and its dest (m_dest), not about a single file.
1878  // It also means we already stated the dest, here.
1879  // On the other hand we haven't stated the src yet (we skipped doing it
1880  // to save time, since it's not necessary to rename directly!)...
1881 
1882  // Existing dest?
1883  if ( err == ERR_DIR_ALREADY_EXIST ||
1884  err == ERR_FILE_ALREADY_EXIST ||
1885  err == ERR_IDENTICAL_FILES )
1886  {
1887  // Should we skip automatically ?
1888  bool isDir = (err == ERR_DIR_ALREADY_EXIST); // ## technically, isDir means "source is dir", not "dest is dir" #######
1889  if ((isDir && m_bAutoSkipDirs) || (!isDir && m_bAutoSkipFiles)) {
1890  // Move on to next source url
1891  skipSrc(isDir);
1892  return;
1893  } else if ((isDir && m_bOverwriteAllDirs) || (!isDir && m_bOverwriteAllFiles)) {
1894  ; // nothing to do, stat+copy+del will overwrite
1895  } else if ((isDir && m_bAutoRenameDirs) || (!isDir && m_bAutoRenameFiles)) {
1896  KUrl destDirectory(m_currentDestURL); // dest including filename
1897  destDirectory.setPath(destDirectory.directory());
1898  const QString newName = KIO::RenameDialog::suggestName(destDirectory, m_currentDestURL.fileName());
1899 
1900  m_dest.setPath(m_currentDestURL.path());
1901  m_dest.setFileName(newName);
1902  KIO::Job* job = KIO::stat(m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo);
1903  state = STATE_STATING;
1904  destinationState = DEST_NOT_STATED;
1905  q->addSubjob(job);
1906  return;
1907  } else if ( q->isInteractive() ) {
1908  QString newPath;
1909  // we lack mtime info for both the src (not stated)
1910  // and the dest (stated but this info wasn't stored)
1911  // Let's do it for local files, at least
1912  KIO::filesize_t sizeSrc = (KIO::filesize_t) -1;
1913  KIO::filesize_t sizeDest = (KIO::filesize_t) -1;
1914  time_t ctimeSrc = (time_t) -1;
1915  time_t ctimeDest = (time_t) -1;
1916  time_t mtimeSrc = (time_t) -1;
1917  time_t mtimeDest = (time_t) -1;
1918 
1919  bool destIsDir = err == ERR_DIR_ALREADY_EXIST;
1920 
1921  // ## TODO we need to stat the source using KIO::stat
1922  // so that this code is properly network-transparent.
1923 
1924  KDE_struct_stat stat_buf;
1925  if ( m_currentSrcURL.isLocalFile() &&
1926  KDE::stat(m_currentSrcURL.toLocalFile(), &stat_buf) == 0 ) {
1927  sizeSrc = stat_buf.st_size;
1928  ctimeSrc = stat_buf.st_ctime;
1929  mtimeSrc = stat_buf.st_mtime;
1930  isDir = S_ISDIR(stat_buf.st_mode);
1931  }
1932  if ( dest.isLocalFile() &&
1933  KDE::stat(dest.toLocalFile(), &stat_buf) == 0 ) {
1934  sizeDest = stat_buf.st_size;
1935  ctimeDest = stat_buf.st_ctime;
1936  mtimeDest = stat_buf.st_mtime;
1937  destIsDir = S_ISDIR(stat_buf.st_mode);
1938  }
1939 
1940  // If src==dest, use "overwrite-itself"
1941  RenameDialog_Mode mode = ( m_currentSrcURL == dest ) ? M_OVERWRITE_ITSELF : M_OVERWRITE;
1942  if (!isDir && destIsDir) {
1943  // We can't overwrite a dir with a file.
1944  mode = (RenameDialog_Mode) 0;
1945  }
1946 
1947  if ( m_srcList.count() > 1 )
1948  mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
1949  if (destIsDir)
1950  mode = (RenameDialog_Mode) ( mode | M_ISDIR );
1951 
1952  if (m_reportTimer)
1953  m_reportTimer->stop();
1954 
1955  RenameDialog_Result r = q->ui()->askFileRename(
1956  q,
1957  err != ERR_DIR_ALREADY_EXIST ? i18n("File Already Exists") : i18n("Already Exists as Folder"),
1958  m_currentSrcURL.url(),
1959  dest.url(),
1960  mode, newPath,
1961  sizeSrc, sizeDest,
1962  ctimeSrc, ctimeDest,
1963  mtimeSrc, mtimeDest );
1964 
1965  if (m_reportTimer)
1966  m_reportTimer->start(REPORT_TIMEOUT);
1967 
1968  switch ( r )
1969  {
1970  case R_CANCEL:
1971  {
1972  q->setError( ERR_USER_CANCELED );
1973  q->emitResult();
1974  return;
1975  }
1976  case R_AUTO_RENAME:
1977  if (isDir) {
1978  m_bAutoRenameDirs = true;
1979  }
1980  else {
1981  m_bAutoRenameFiles = true;
1982  }
1983  // fall through
1984  case R_RENAME:
1985  {
1986  // Set m_dest to the chosen destination
1987  // This is only for this src url; the next one will revert to m_globalDest
1988  m_dest.setPath( newPath );
1989  KIO::Job* job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
1990  state = STATE_STATING;
1991  destinationState = DEST_NOT_STATED;
1992  q->addSubjob(job);
1993  return;
1994  }
1995  case R_AUTO_SKIP:
1996  if (isDir)
1997  m_bAutoSkipDirs = true;
1998  else
1999  m_bAutoSkipFiles = true;
2000  // fall through
2001  case R_SKIP:
2002  // Move on to next url
2003  skipSrc(isDir);
2004  return;
2005  case R_OVERWRITE_ALL:
2006  if (destIsDir)
2007  m_bOverwriteAllDirs = true;
2008  else
2009  m_bOverwriteAllFiles = true;
2010  break;
2011  case R_OVERWRITE:
2012  // Add to overwrite list
2013  // Note that we add dest, not m_dest.
2014  // This ensures that when moving several urls into a dir (m_dest),
2015  // we only overwrite for the current one, not for all.
2016  // When renaming a single file (m_asMethod), it makes no difference.
2017  kDebug(7007) << "adding to overwrite list: " << dest.path();
2018  m_overwriteList.insert( dest.path() );
2019  break;
2020  default:
2021  //assert( 0 );
2022  break;
2023  }
2024  } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
2025  // Dest already exists, and job is not interactive -> abort with error
2026  q->setError( err );
2027  q->setErrorText( errText );
2028  q->emitResult();
2029  return;
2030  }
2031  } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
2032  kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", aborting";
2033  q->setError( err );
2034  q->setErrorText( errText );
2035  q->emitResult();
2036  return;
2037  }
2038  kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", reverting to normal way, starting with stat";
2039  //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
2040  KIO::Job* job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
2041  state = STATE_STATING;
2042  q->addSubjob(job);
2043  m_bOnlyRenames = false;
2044  }
2045  else
2046  {
2047  kDebug(7007) << "Renaming succeeded, move on";
2048  ++m_processedFiles;
2049  emit q->copyingDone( q, *m_currentStatSrc, dest, -1 /*mtime unknown, and not needed*/, true, true );
2050  m_successSrcList.append(*m_currentStatSrc);
2051  statNextSrc();
2052  }
2053 }
2054 
2055 void CopyJob::slotResult( KJob *job )
2056 {
2057  Q_D(CopyJob);
2058  //kDebug(7007) << "d->state=" << (int) d->state;
2059  // In each case, what we have to do is :
2060  // 1 - check for errors and treat them
2061  // 2 - removeSubjob(job);
2062  // 3 - decide what to do next
2063 
2064  switch ( d->state ) {
2065  case STATE_STATING: // We were trying to stat a src url or the dest
2066  d->slotResultStating( job );
2067  break;
2068  case STATE_RENAMING: // We were trying to do a direct renaming, before even stat'ing
2069  {
2070  d->slotResultRenaming( job );
2071  break;
2072  }
2073  case STATE_LISTING: // recursive listing finished
2074  //kDebug(7007) << "totalSize:" << (unsigned int) d->m_totalSize << "files:" << d->files.count() << "d->dirs:" << d->dirs.count();
2075  // Was there an error ?
2076  if (job->error())
2077  {
2078  Job::slotResult( job ); // will set the error and emit result(this)
2079  return;
2080  }
2081 
2082  removeSubjob( job );
2083  assert ( !hasSubjobs() );
2084 
2085  d->statNextSrc();
2086  break;
2087  case STATE_CREATING_DIRS:
2088  d->slotResultCreatingDirs( job );
2089  break;
2090  case STATE_CONFLICT_CREATING_DIRS:
2091  d->slotResultConflictCreatingDirs( job );
2092  break;
2093  case STATE_COPYING_FILES:
2094  d->slotResultCopyingFiles( job );
2095  break;
2096  case STATE_CONFLICT_COPYING_FILES:
2097  d->slotResultConflictCopyingFiles( job );
2098  break;
2099  case STATE_DELETING_DIRS:
2100  d->slotResultDeletingDirs( job );
2101  break;
2102  case STATE_SETTING_DIR_ATTRIBUTES:
2103  d->slotResultSettingDirAttributes( job );
2104  break;
2105  default:
2106  assert( 0 );
2107  }
2108 }
2109 
2110 void KIO::CopyJob::setDefaultPermissions( bool b )
2111 {
2112  d_func()->m_defaultPermissions = b;
2113 }
2114 
2115 KIO::CopyJob::CopyMode KIO::CopyJob::operationMode() const
2116 {
2117  return d_func()->m_mode;
2118 }
2119 
2120 void KIO::CopyJob::setAutoSkip(bool autoSkip)
2121 {
2122  d_func()->m_bAutoSkipFiles = autoSkip;
2123  d_func()->m_bAutoSkipDirs = autoSkip;
2124 }
2125 
2126 void KIO::CopyJob::setAutoRename(bool autoRename)
2127 {
2128  d_func()->m_bAutoRenameFiles = autoRename;
2129  d_func()->m_bAutoRenameDirs = autoRename;
2130 }
2131 
2132 void KIO::CopyJob::setWriteIntoExistingDirectories(bool overwriteAll) // #65926
2133 {
2134  d_func()->m_bOverwriteAllDirs = overwriteAll;
2135 }
2136 
2137 CopyJob *KIO::copy(const KUrl& src, const KUrl& dest, JobFlags flags)
2138 {
2139  //kDebug(7007) << "src=" << src << "dest=" << dest;
2140  KUrl::List srcList;
2141  srcList.append( src );
2142  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, false, flags);
2143 }
2144 
2145 CopyJob *KIO::copyAs(const KUrl& src, const KUrl& dest, JobFlags flags)
2146 {
2147  //kDebug(7007) << "src=" << src << "dest=" << dest;
2148  KUrl::List srcList;
2149  srcList.append( src );
2150  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, true, flags);
2151 }
2152 
2153 CopyJob *KIO::copy( const KUrl::List& src, const KUrl& dest, JobFlags flags )
2154 {
2155  //kDebug(7007) << src << dest;
2156  return CopyJobPrivate::newJob(src, dest, CopyJob::Copy, false, flags);
2157 }
2158 
2159 CopyJob *KIO::move(const KUrl& src, const KUrl& dest, JobFlags flags)
2160 {
2161  //kDebug(7007) << src << dest;
2162  KUrl::List srcList;
2163  srcList.append( src );
2164  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, false, flags);
2165 }
2166 
2167 CopyJob *KIO::moveAs(const KUrl& src, const KUrl& dest, JobFlags flags)
2168 {
2169  //kDebug(7007) << src << dest;
2170  KUrl::List srcList;
2171  srcList.append( src );
2172  return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, true, flags);
2173 }
2174 
2175 CopyJob *KIO::move( const KUrl::List& src, const KUrl& dest, JobFlags flags)
2176 {
2177  //kDebug(7007) << src << dest;
2178  return CopyJobPrivate::newJob(src, dest, CopyJob::Move, false, flags);
2179 }
2180 
2181 CopyJob *KIO::link(const KUrl& src, const KUrl& destDir, JobFlags flags)
2182 {
2183  KUrl::List srcList;
2184  srcList.append( src );
2185  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2186 }
2187 
2188 CopyJob *KIO::link(const KUrl::List& srcList, const KUrl& destDir, JobFlags flags)
2189 {
2190  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2191 }
2192 
2193 CopyJob *KIO::linkAs(const KUrl& src, const KUrl& destDir, JobFlags flags )
2194 {
2195  KUrl::List srcList;
2196  srcList.append( src );
2197  return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
2198 }
2199 
2200 CopyJob *KIO::trash(const KUrl& src, JobFlags flags)
2201 {
2202  KUrl::List srcList;
2203  srcList.append( src );
2204  return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
2205 }
2206 
2207 CopyJob *KIO::trash(const KUrl::List& srcList, JobFlags flags)
2208 {
2209  return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
2210 }
2211 
2212 #include "copyjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Jul 17 2012 07:34:13 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.8.4 API Reference

Skip menu "kdelibs-4.8.4 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal