• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

copyjob.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright 2000       Stephan Kulow <coolo@kde.org>
00003     Copyright 2000-2006  David Faure <faure@kde.org>
00004     Copyright 2000       Waldo Bastian <bastian@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "copyjob.h"
00023 #include "kdirlister.h"
00024 #include "kfileitem.h"
00025 #include "deletejob.h"
00026 
00027 #include <klocale.h>
00028 #include <kdesktopfile.h>
00029 #include <kdebug.h>
00030 #include <kde_file.h>
00031 
00032 #include "slave.h"
00033 #include "scheduler.h"
00034 #include "kdirwatch.h"
00035 #include "kprotocolmanager.h"
00036 
00037 #include "jobuidelegate.h"
00038 
00039 #include <kdirnotify.h>
00040 #include <ktemporaryfile.h>
00041 #include <kuiserverjobtracker.h>
00042 
00043 #ifdef Q_OS_UNIX
00044 #include <utime.h>
00045 #endif
00046 #include <assert.h>
00047 
00048 #include <QtCore/QTimer>
00049 #include <QtCore/QFile>
00050 #include <sys/stat.h> // mode_t
00051 #include <QPointer>
00052 
00053 #include "job_p.h"
00054 
00055 using namespace KIO;
00056 
00057 //this will update the report dialog with 5 Hz, I think this is fast enough, aleXXX
00058 #define REPORT_TIMEOUT 200
00059 
00060 enum DestinationState {
00061     DEST_NOT_STATED,
00062     DEST_IS_DIR,
00063     DEST_IS_FILE,
00064     DEST_DOESNT_EXIST
00065 };
00066 
00081 enum CopyJobState {
00082     STATE_STATING,
00083     STATE_RENAMING,
00084     STATE_LISTING,
00085     STATE_CREATING_DIRS,
00086     STATE_CONFLICT_CREATING_DIRS,
00087     STATE_COPYING_FILES,
00088     STATE_CONFLICT_COPYING_FILES,
00089     STATE_DELETING_DIRS,
00090     STATE_SETTING_DIR_ATTRIBUTES
00091 };
00092 
00094 class KIO::CopyJobPrivate: public KIO::JobPrivate
00095 {
00096 public:
00097     CopyJobPrivate(const KUrl::List& src, const KUrl& dest,
00098                    CopyJob::CopyMode mode, bool asMethod)
00099         : m_globalDest(dest)
00100         , m_globalDestinationState(DEST_NOT_STATED)
00101         , m_defaultPermissions(false)
00102         , m_bURLDirty(false)
00103         , m_mode(mode)
00104         , m_asMethod(asMethod)
00105         , destinationState(DEST_NOT_STATED)
00106         , state(STATE_STATING)
00107         , m_totalSize(0)
00108         , m_processedSize(0)
00109         , m_fileProcessedSize(0)
00110         , m_processedFiles(0)
00111         , m_processedDirs(0)
00112         , m_srcList(src)
00113         , m_currentStatSrc(m_srcList.constBegin())
00114         , m_bCurrentOperationIsLink(false)
00115         , m_bSingleFileCopy(false)
00116         , m_bOnlyRenames(mode==CopyJob::Move)
00117         , m_dest(dest)
00118         , m_bAutoSkipFiles( false )
00119         , m_bAutoSkipDirs( false )
00120         , m_bOverwriteAllFiles( false )
00121         , m_bOverwriteAllDirs( false )
00122         , m_conflictError(0)
00123         , m_reportTimer(0)
00124     {
00125     }
00126 
00127     // This is the dest URL that was initially given to CopyJob
00128     // It is copied into m_dest, which can be changed for a given src URL
00129     // (when using the RENAME dialog in slotResult),
00130     // and which will be reset for the next src URL.
00131     KUrl m_globalDest;
00132     // The state info about that global dest
00133     DestinationState m_globalDestinationState;
00134     // See setDefaultPermissions
00135     bool m_defaultPermissions;
00136     // Whether URLs changed (and need to be emitted by the next slotReport call)
00137     bool m_bURLDirty;
00138     // Used after copying all the files into the dirs, to set mtime (TODO: and permissions?)
00139     // after the copy is done
00140     QLinkedList<CopyInfo> m_directoriesCopied;
00141     QLinkedList<CopyInfo>::const_iterator m_directoriesCopiedIterator;
00142 
00143     CopyJob::CopyMode m_mode;
00144     bool m_asMethod;
00145     DestinationState destinationState;
00146     CopyJobState state;
00147     KIO::filesize_t m_totalSize;
00148     KIO::filesize_t m_processedSize;
00149     KIO::filesize_t m_fileProcessedSize;
00150     int m_processedFiles;
00151     int m_processedDirs;
00152     QList<CopyInfo> files;
00153     QList<CopyInfo> dirs;
00154     KUrl::List dirsToRemove;
00155     KUrl::List m_srcList;
00156     KUrl::List m_successSrcList; // Entries in m_srcList that have successfully been moved
00157     KUrl::List::const_iterator m_currentStatSrc;
00158     bool m_bCurrentSrcIsDir;
00159     bool m_bCurrentOperationIsLink;
00160     bool m_bSingleFileCopy;
00161     bool m_bOnlyRenames;
00162     KUrl m_dest;
00163     KUrl m_currentDest; // set during listing, used by slotEntries
00164     //
00165     QStringList m_skipList;
00166     QStringList m_overwriteList;
00167     bool m_bAutoSkipFiles;
00168     bool m_bAutoSkipDirs;
00169     bool m_bOverwriteAllFiles;
00170     bool m_bOverwriteAllDirs;
00171     int m_conflictError;
00172 
00173     QTimer *m_reportTimer;
00174 
00175     // The current src url being stat'ed or copied
00176     // During the stat phase, this is initially equal to *m_currentStatSrc but it can be resolved to a local file equivalent (#188903).
00177     KUrl m_currentSrcURL;
00178     KUrl m_currentDestURL;
00179 
00180     QSet<QString> m_parentDirs;
00181 
00182     void statCurrentSrc();
00183     void statNextSrc();
00184 
00185     // Those aren't slots but submethods for slotResult.
00186     void slotResultStating( KJob * job );
00187     void startListing( const KUrl & src );
00188     void slotResultCreatingDirs( KJob * job );
00189     void slotResultConflictCreatingDirs( KJob * job );
00190     void createNextDir();
00191     void slotResultCopyingFiles( KJob * job );
00192     void slotResultConflictCopyingFiles( KJob * job );
00193 //     KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, bool overwrite );
00194     KIO::Job* linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags );
00195     void copyNextFile();
00196     void slotResultDeletingDirs( KJob * job );
00197     void deleteNextDir();
00198     void sourceStated(const UDSEntry& entry, const KUrl& sourceUrl);
00199     void skip(const KUrl & sourceURL, bool isDir);
00200     void slotResultRenaming( KJob * job );
00201     void slotResultSettingDirAttributes( KJob * job );
00202     void setNextDirAttribute();
00203 
00204     void startRenameJob(const KUrl &slave_url);
00205     bool shouldOverwriteDir( const QString& path ) const;
00206     bool shouldOverwriteFile( const QString& path ) const;
00207     bool shouldSkip( const QString& path ) const;
00208     void skipSrc(bool isDir);
00209 
00210     void slotStart();
00211     void slotEntries( KIO::Job*, const KIO::UDSEntryList& list );
00212     void addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest);
00216     void slotProcessedSize( KJob*, qulonglong data_size );
00221     void slotTotalSize( KJob*, qulonglong size );
00222 
00223     void slotReport();
00224 
00225     Q_DECLARE_PUBLIC(CopyJob)
00226 
00227     static inline CopyJob *newJob(const KUrl::List& src, const KUrl& dest,
00228                                   CopyJob::CopyMode mode, bool asMethod, JobFlags flags)
00229     {
00230         CopyJob *job = new CopyJob(*new CopyJobPrivate(src,dest,mode,asMethod));
00231         job->setUiDelegate(new JobUiDelegate);
00232         if (!(flags & HideProgressInfo))
00233             KIO::getJobTracker()->registerJob(job);
00234         return job;
00235     }
00236 };
00237 
00238 CopyJob::CopyJob(CopyJobPrivate &dd)
00239     : Job(dd)
00240 {
00241     QTimer::singleShot(0, this, SLOT(slotStart()));
00242 }
00243 
00244 CopyJob::~CopyJob()
00245 {
00246 }
00247 
00248 KUrl::List CopyJob::srcUrls() const
00249 {
00250     return d_func()->m_srcList;
00251 }
00252 
00253 KUrl CopyJob::destUrl() const
00254 {
00255     return d_func()->m_dest;
00256 }
00257 
00258 void CopyJobPrivate::slotStart()
00259 {
00260     Q_Q(CopyJob);
00266     m_reportTimer = new QTimer(q);
00267 
00268     q->connect(m_reportTimer,SIGNAL(timeout()),q,SLOT(slotReport()));
00269     m_reportTimer->start(REPORT_TIMEOUT);
00270 
00271     // Stat the dest
00272     KIO::Job * job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
00273     //kDebug(7007) << "CopyJob:stating the dest " << m_dest;
00274     q->addSubjob(job);
00275 }
00276 
00277 // For unit test purposes
00278 KIO_EXPORT bool kio_resolve_local_urls = true;
00279 
00280 void CopyJobPrivate::slotResultStating( KJob *job )
00281 {
00282     Q_Q(CopyJob);
00283     //kDebug(7007);
00284     // Was there an error while stating the src ?
00285     if (job->error() && destinationState != DEST_NOT_STATED )
00286     {
00287         const KUrl srcurl = static_cast<SimpleJob*>(job)->url();
00288         if ( !srcurl.isLocalFile() )
00289         {
00290             // Probably : src doesn't exist. Well, over some protocols (e.g. FTP)
00291             // this info isn't really reliable (thanks to MS FTP servers).
00292             // We'll assume a file, and try to download anyway.
00293             kDebug(7007) << "Error while stating source. Activating hack";
00294             q->removeSubjob( job );
00295             assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
00296             struct CopyInfo info;
00297             info.permissions = (mode_t) -1;
00298             info.mtime = (time_t) -1;
00299             info.ctime = (time_t) -1;
00300             info.size = (KIO::filesize_t)-1;
00301             info.uSource = srcurl;
00302             info.uDest = m_dest;
00303             // Append filename or dirname to destination URL, if allowed
00304             if ( destinationState == DEST_IS_DIR && !m_asMethod )
00305                 info.uDest.addPath( srcurl.fileName() );
00306 
00307             files.append( info );
00308             statNextSrc();
00309             return;
00310         }
00311         // Local file. If stat fails, the file definitely doesn't exist.
00312         // yes, q->Job::, because we don't want to call our override
00313         q->Job::slotResult( job ); // will set the error and emit result(this)
00314         return;
00315     }
00316 
00317     // Keep copy of the stat result
00318     const UDSEntry entry = static_cast<StatJob*>(job)->statResult();
00319 
00320     if ( destinationState == DEST_NOT_STATED ) {
00321         const bool isDir = entry.isDir();
00322         // we were stating the dest
00323         if (job->error())
00324             destinationState = DEST_DOESNT_EXIST;
00325         else {
00326             // Treat symlinks to dirs as dirs here, so no test on isLink
00327             destinationState = isDir ? DEST_IS_DIR : DEST_IS_FILE;
00328             //kDebug(7007) << "dest is dir:" << isDir;
00329         }
00330         const bool isGlobalDest = m_dest == m_globalDest;
00331         if ( isGlobalDest )
00332             m_globalDestinationState = destinationState;
00333 
00334         const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
00335         if ( !sLocalPath.isEmpty() && kio_resolve_local_urls ) {
00336             m_dest = KUrl();
00337             m_dest.setPath(sLocalPath);
00338             if ( isGlobalDest )
00339                 m_globalDest = m_dest;
00340         }
00341 
00342         q->removeSubjob( job );
00343         assert ( !q->hasSubjobs() );
00344 
00345         // After knowing what the dest is, we can start stat'ing the first src.
00346         statCurrentSrc();
00347     } else {
00348         sourceStated(entry, static_cast<SimpleJob*>(job)->url());
00349         q->removeSubjob( job );
00350     }
00351 }
00352 
00353 void CopyJobPrivate::sourceStated(const UDSEntry& entry, const KUrl& sourceUrl)
00354 {
00355     const QString sLocalPath = entry.stringValue( KIO::UDSEntry::UDS_LOCAL_PATH );
00356     const bool isDir = entry.isDir();
00357 
00358     // We were stating the current source URL
00359     // Is it a file or a dir ?
00360 
00361     // There 6 cases, and all end up calling addCopyInfoFromUDSEntry first :
00362     // 1 - src is a dir, destination is a directory,
00363     // slotEntries will append the source-dir-name to the destination
00364     // 2 - src is a dir, destination is a file -- will offer to overwrite, later on.
00365     // 3 - src is a dir, destination doesn't exist, then it's the destination dirname,
00366     // so slotEntries will use it as destination.
00367 
00368     // 4 - src is a file, destination is a directory,
00369     // slotEntries will append the filename to the destination.
00370     // 5 - src is a file, destination is a file, m_dest is the exact destination name
00371     // 6 - src is a file, destination doesn't exist, m_dest is the exact destination name
00372 
00373     KUrl srcurl;
00374     if (!sLocalPath.isEmpty())
00375         srcurl.setPath(sLocalPath);
00376     else
00377         srcurl = sourceUrl;
00378     addCopyInfoFromUDSEntry(entry, srcurl, false, m_dest);
00379 
00380     m_currentDest = m_dest;
00381     m_bCurrentSrcIsDir = false;
00382 
00383     if ( isDir
00384          // treat symlinks as files (no recursion)
00385          && !entry.isLink()
00386          && m_mode != CopyJob::Link ) // No recursion in Link mode either.
00387     {
00388         //kDebug(7007) << "Source is a directory";
00389 
00390         if (srcurl.isLocalFile()) {
00391             const QString parentDir = srcurl.toLocalFile(KUrl::RemoveTrailingSlash);
00392             m_parentDirs.insert(parentDir);
00393         }
00394 
00395         m_bCurrentSrcIsDir = true; // used by slotEntries
00396         if ( destinationState == DEST_IS_DIR ) // (case 1)
00397         {
00398             if ( !m_asMethod )
00399             {
00400                 // Use <desturl>/<directory_copied> as destination, from now on
00401                 QString directory = srcurl.fileName();
00402                 const QString sName = entry.stringValue( KIO::UDSEntry::UDS_NAME );
00403                 if (!sName.isEmpty() && KProtocolManager::fileNameUsedForCopying(srcurl) == KProtocolInfo::Name) {
00404                     directory = sName;
00405                 }
00406                 m_currentDest.addPath( directory );
00407             }
00408         }
00409         else // (case 3)
00410         {
00411             // otherwise dest is new name for toplevel dir
00412             // so the destination exists, in fact, from now on.
00413             // (This even works with other src urls in the list, since the
00414             //  dir has effectively been created)
00415             destinationState = DEST_IS_DIR;
00416             if ( m_dest == m_globalDest )
00417                 m_globalDestinationState = destinationState;
00418         }
00419 
00420         startListing( srcurl );
00421     }
00422     else
00423     {
00424         //kDebug(7007) << "Source is a file (or a symlink), or we are linking -> no recursive listing";
00425 
00426         if (srcurl.isLocalFile()) {
00427             const QString parentDir = srcurl.directory(KUrl::ObeyTrailingSlash);
00428             m_parentDirs.insert(parentDir);
00429         }
00430 
00431         statNextSrc();
00432     }
00433 }
00434 
00435 bool CopyJob::doSuspend()
00436 {
00437     Q_D(CopyJob);
00438     d->slotReport();
00439     return Job::doSuspend();
00440 }
00441 
00442 void CopyJobPrivate::slotReport()
00443 {
00444     Q_Q(CopyJob);
00445     if ( q->isSuspended() )
00446         return;
00447     // If showProgressInfo was set, progressId() is > 0.
00448     switch (state) {
00449         case STATE_RENAMING:
00450             q->setTotalAmount(KJob::Files, m_srcList.count());
00451             // fall-through intended
00452         case STATE_COPYING_FILES:
00453             q->setProcessedAmount( KJob::Files, m_processedFiles );
00454             if (m_bURLDirty)
00455             {
00456                 // Only emit urls when they changed. This saves time, and fixes #66281
00457                 m_bURLDirty = false;
00458                 if (m_mode==CopyJob::Move)
00459                 {
00460                     emitMoving(q, m_currentSrcURL, m_currentDestURL);
00461                     emit q->moving( q, m_currentSrcURL, m_currentDestURL);
00462                 }
00463                 else if (m_mode==CopyJob::Link)
00464                 {
00465                     emitCopying( q, m_currentSrcURL, m_currentDestURL ); // we don't have a delegate->linking
00466                     emit q->linking( q, m_currentSrcURL.path(), m_currentDestURL );
00467                 }
00468                 else
00469                 {
00470                     emitCopying( q, m_currentSrcURL, m_currentDestURL );
00471                     emit q->copying( q, m_currentSrcURL, m_currentDestURL );
00472                 }
00473             }
00474             break;
00475 
00476         case STATE_CREATING_DIRS:
00477             q->setProcessedAmount( KJob::Directories, m_processedDirs );
00478             if (m_bURLDirty)
00479             {
00480                 m_bURLDirty = false;
00481                 emit q->creatingDir( q, m_currentDestURL );
00482                 emitCreatingDir( q, m_currentDestURL );
00483             }
00484             break;
00485 
00486         case STATE_STATING:
00487         case STATE_LISTING:
00488             if (m_bURLDirty)
00489             {
00490                 m_bURLDirty = false;
00491                 if (m_mode==CopyJob::Move)
00492                 {
00493                     emitMoving( q, m_currentSrcURL, m_currentDestURL );
00494                 }
00495                 else
00496                 {
00497                     emitCopying( q, m_currentSrcURL, m_currentDestURL );
00498                 }
00499             }
00500             q->setTotalAmount(KJob::Bytes, m_totalSize);
00501             q->setTotalAmount(KJob::Files, files.count());
00502             q->setTotalAmount(KJob::Directories, dirs.count());
00503             break;
00504 
00505         default:
00506             break;
00507     }
00508 }
00509 
00510 void CopyJobPrivate::slotEntries(KIO::Job* job, const UDSEntryList& list)
00511 {
00512     //Q_Q(CopyJob);
00513     UDSEntryList::ConstIterator it = list.constBegin();
00514     UDSEntryList::ConstIterator end = list.constEnd();
00515     for (; it != end; ++it) {
00516         const UDSEntry& entry = *it;
00517         addCopyInfoFromUDSEntry(entry, static_cast<SimpleJob *>(job)->url(), m_bCurrentSrcIsDir, m_currentDest);
00518     }
00519 }
00520 
00521 void CopyJobPrivate::addCopyInfoFromUDSEntry(const UDSEntry& entry, const KUrl& srcUrl, bool srcIsDir, const KUrl& currentDest)
00522 {
00523     struct CopyInfo info;
00524     info.permissions = entry.numberValue(KIO::UDSEntry::UDS_ACCESS, -1);
00525     info.mtime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, -1);
00526     info.ctime = (time_t) entry.numberValue(KIO::UDSEntry::UDS_CREATION_TIME, -1);
00527     info.size = (KIO::filesize_t) entry.numberValue(KIO::UDSEntry::UDS_SIZE, -1);
00528     if (info.size != (KIO::filesize_t) -1)
00529         m_totalSize += info.size;
00530 
00531     // recursive listing, displayName can be a/b/c/d
00532     const QString displayName = entry.stringValue(KIO::UDSEntry::UDS_NAME);
00533     const QString urlStr = entry.stringValue(KIO::UDSEntry::UDS_URL);
00534     KUrl url;
00535     if (!urlStr.isEmpty())
00536         url = urlStr;
00537     QString localPath = entry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH);
00538     const bool isDir = entry.isDir();
00539     info.linkDest = entry.stringValue(KIO::UDSEntry::UDS_LINK_DEST);
00540 
00541     if (displayName != QLatin1String("..") && displayName != QLatin1String(".")) {
00542         const bool hasCustomURL = !url.isEmpty() || !localPath.isEmpty();
00543         if (!hasCustomURL) {
00544             // Make URL from displayName
00545             url = srcUrl;
00546             if (srcIsDir) { // Only if src is a directory. Otherwise uSource is fine as is
00547                 //kDebug(7007) << "adding path" << displayName;
00548                 url.addPath(displayName);
00549             }
00550         }
00551         //kDebug(7007) << "displayName=" << displayName << "url=" << url;
00552         if (!localPath.isEmpty() && kio_resolve_local_urls) {
00553             url = KUrl(localPath);
00554         }
00555 
00556         info.uSource = url;
00557         info.uDest = currentDest;
00558         //kDebug(7007) << "uSource=" << info.uSource << "uDest(1)=" << info.uDest;
00559         // Append filename or dirname to destination URL, if allowed
00560         if (destinationState == DEST_IS_DIR &&
00561              // "copy/move as <foo>" means 'foo' is the dest for the base srcurl
00562              // (passed here during stating) but not its children (during listing)
00563              (! (m_asMethod && state == STATE_STATING)))
00564         {
00565             QString destFileName;
00566             if (hasCustomURL &&
00567                  KProtocolManager::fileNameUsedForCopying(url) == KProtocolInfo::FromUrl) {
00568                 //destFileName = url.fileName(); // Doesn't work for recursive listing
00569                 // Count the number of prefixes used by the recursive listjob
00570                 int numberOfSlashes = displayName.count('/'); // don't make this a find()!
00571                 QString path = url.path();
00572                 int pos = 0;
00573                 for (int n = 0; n < numberOfSlashes + 1; ++n) {
00574                     pos = path.lastIndexOf('/', pos - 1);
00575                     if (pos == -1) { // error
00576                         kWarning(7007) << "kioslave bug: not enough slashes in UDS_URL" << path << "- looking for" << numberOfSlashes << "slashes";
00577                         break;
00578                     }
00579                 }
00580                 if (pos >= 0) {
00581                     destFileName = path.mid(pos + 1);
00582                 }
00583 
00584             } else { // destination filename taken from UDS_NAME
00585                 destFileName = displayName;
00586             }
00587 
00588             // Here we _really_ have to add some filename to the dest.
00589             // Otherwise, we end up with e.g. dest=..../Desktop/ itself.
00590             // (This can happen when dropping a link to a webpage with no path)
00591             if (destFileName.isEmpty()) {
00592                 destFileName = KIO::encodeFileName(info.uSource.prettyUrl());
00593             }
00594 
00595             //kDebug(7007) << " adding destFileName=" << destFileName;
00596             info.uDest.addPath(destFileName);
00597         }
00598         //kDebug(7007) << " uDest(2)=" << info.uDest;
00599         //kDebug(7007) << " " << info.uSource << "->" << info.uDest;
00600         if (info.linkDest.isEmpty() && isDir && m_mode != CopyJob::Link) { // Dir
00601             dirs.append(info); // Directories
00602             if (m_mode == CopyJob::Move) {
00603                 dirsToRemove.append(info.uSource);
00604             }
00605         } else {
00606             files.append(info); // Files and any symlinks
00607         }
00608     }
00609 }
00610 
00611 void CopyJobPrivate::skipSrc(bool isDir)
00612 {
00613     m_dest = m_globalDest;
00614     destinationState = m_globalDestinationState;
00615     skip(*m_currentStatSrc, isDir);
00616     ++m_currentStatSrc;
00617     statCurrentSrc();
00618 }
00619 
00620 void CopyJobPrivate::statNextSrc()
00621 {
00622     /* Revert to the global destination, the one that applies to all source urls.
00623      * 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.
00624      * d->m_dest is /foo/b for b, but we have to revert to /d for item c and following.
00625      */
00626     m_dest = m_globalDest;
00627     destinationState = m_globalDestinationState;
00628     ++m_currentStatSrc;
00629     statCurrentSrc();
00630 }
00631 
00632 void CopyJobPrivate::statCurrentSrc()
00633 {
00634     Q_Q(CopyJob);
00635     if (m_currentStatSrc != m_srcList.constEnd()) {
00636         m_currentSrcURL = (*m_currentStatSrc);
00637         m_bURLDirty = true;
00638         if (m_mode == CopyJob::Link) {
00639             // Skip the "stating the source" stage, we don't need it for linking
00640             m_currentDest = m_dest;
00641             struct CopyInfo info;
00642             info.permissions = -1;
00643             info.mtime = (time_t) -1;
00644             info.ctime = (time_t) -1;
00645             info.size = (KIO::filesize_t)-1;
00646             info.uSource = m_currentSrcURL;
00647             info.uDest = m_currentDest;
00648             // Append filename or dirname to destination URL, if allowed
00649             if (destinationState == DEST_IS_DIR && !m_asMethod) {
00650                 if (
00651                     (m_currentSrcURL.protocol() == info.uDest.protocol()) &&
00652                     (m_currentSrcURL.host() == info.uDest.host()) &&
00653                     (m_currentSrcURL.port() == info.uDest.port()) &&
00654                     (m_currentSrcURL.user() == info.uDest.user()) &&
00655                     (m_currentSrcURL.pass() == info.uDest.pass()) ) {
00656                     // This is the case of creating a real symlink
00657                     info.uDest.addPath( m_currentSrcURL.fileName() );
00658                 } else {
00659                     // Different protocols, we'll create a .desktop file
00660                     // We have to change the extension anyway, so while we're at it,
00661                     // name the file like the URL
00662                     info.uDest.addPath(KIO::encodeFileName(m_currentSrcURL.prettyUrl()) + ".desktop");
00663                 }
00664             }
00665             files.append( info ); // Files and any symlinks
00666             statNextSrc(); // we could use a loop instead of a recursive call :)
00667             return;
00668         }
00669 
00670         // Let's see if we can skip stat'ing, for the case where a directory view has the info already
00671         const KFileItem cachedItem = KDirLister::cachedItemForUrl(m_currentSrcURL);
00672         KIO::UDSEntry entry;
00673         if (!cachedItem.isNull()) {
00674             entry = cachedItem.entry();
00675             bool dummyIsLocal;
00676             m_currentSrcURL = cachedItem.mostLocalUrl(dummyIsLocal); // #183585
00677         }
00678 
00679         if (m_mode == CopyJob::Move && (
00680                 // Don't go renaming right away if we need a stat() to find out the destination filename
00681                 KProtocolManager::fileNameUsedForCopying(m_currentSrcURL) == KProtocolInfo::FromUrl ||
00682                 destinationState != DEST_IS_DIR || m_asMethod)
00683             ) {
00684            // If moving, before going for the full stat+[list+]copy+del thing, try to rename
00685            // The logic is pretty similar to FileCopyJobPrivate::slotStart()
00686            if ( (m_currentSrcURL.protocol() == m_dest.protocol()) &&
00687               (m_currentSrcURL.host() == m_dest.host()) &&
00688               (m_currentSrcURL.port() == m_dest.port()) &&
00689               (m_currentSrcURL.user() == m_dest.user()) &&
00690               (m_currentSrcURL.pass() == m_dest.pass()) )
00691            {
00692               startRenameJob( m_currentSrcURL );
00693               return;
00694            }
00695            else if ( m_currentSrcURL.isLocalFile() && KProtocolManager::canRenameFromFile( m_dest ) )
00696            {
00697               startRenameJob( m_dest );
00698               return;
00699            }
00700            else if ( m_dest.isLocalFile() && KProtocolManager::canRenameToFile( m_currentSrcURL ) )
00701            {
00702               startRenameJob( m_currentSrcURL );
00703               return;
00704            }
00705         }
00706 
00707         // if the file system doesn't support deleting, we do not even stat
00708         if (m_mode == CopyJob::Move && !KProtocolManager::supportsDeleting(m_currentSrcURL)) {
00709             QPointer<CopyJob> that = q;
00710             emit q->warning( q, buildErrorString(ERR_CANNOT_DELETE, m_currentSrcURL.prettyUrl()) );
00711             if (that)
00712                 statNextSrc(); // we could use a loop instead of a recursive call :)
00713             return;
00714         }
00715 
00716         m_bOnlyRenames = false;
00717 
00718         // Testing for entry.count()>0 here is not good enough; KFileItem inserts
00719         // entries for UDS_USER and UDS_GROUP even on initially empty UDSEntries (#192185)
00720         if (entry.contains(KIO::UDSEntry::UDS_NAME)) {
00721             kDebug(7007) << "fast path! found info about" << m_currentSrcURL << "in KDirLister";
00722             sourceStated(entry, m_currentSrcURL);
00723             return;
00724         }
00725 
00726         // Stat the next src url
00727         Job * job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
00728         //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
00729         state = STATE_STATING;
00730         q->addSubjob(job);
00731         m_currentDestURL = m_dest;
00732         m_bURLDirty = true;
00733     }
00734     else
00735     {
00736         // Finished the stat'ing phase
00737         // First make sure that the totals were correctly emitted
00738         state = STATE_STATING;
00739         m_bURLDirty = true;
00740         slotReport();
00741         if (!dirs.isEmpty())
00742            emit q->aboutToCreate( q, dirs );
00743         if (!files.isEmpty())
00744            emit q->aboutToCreate( q, files );
00745         // Check if we are copying a single file
00746         m_bSingleFileCopy = ( files.count() == 1 && dirs.isEmpty() );
00747         // Then start copying things
00748         state = STATE_CREATING_DIRS;
00749         createNextDir();
00750     }
00751 }
00752 
00753 void CopyJobPrivate::startRenameJob( const KUrl& slave_url )
00754 {
00755     Q_Q(CopyJob);
00756 
00757     // Silence KDirWatch notifications, otherwise performance is horrible
00758     if (m_currentSrcURL.isLocalFile()) {
00759         const QString parentDir = m_currentSrcURL.directory(KUrl::ObeyTrailingSlash);
00760         if (!m_parentDirs.contains(parentDir)) {
00761             KDirWatch::self()->stopDirScan(parentDir);
00762             m_parentDirs.insert(parentDir);
00763         }
00764     }
00765 
00766     KUrl dest = m_dest;
00767     // Append filename or dirname to destination URL, if allowed
00768     if ( destinationState == DEST_IS_DIR && !m_asMethod )
00769         dest.addPath( m_currentSrcURL.fileName() );
00770     m_currentDestURL = dest;
00771     kDebug(7007) << "This seems to be a suitable case for trying to rename before stat+[list+]copy+del";
00772     state = STATE_RENAMING;
00773 
00774     struct CopyInfo info;
00775     info.permissions = -1;
00776     info.mtime = (time_t) -1;
00777     info.ctime = (time_t) -1;
00778     info.size = (KIO::filesize_t)-1;
00779     info.uSource = m_currentSrcURL;
00780     info.uDest = dest;
00781     QList<CopyInfo> files;
00782     files.append(info);
00783     emit q->aboutToCreate( q, files );
00784 
00785     KIO_ARGS << m_currentSrcURL << dest << (qint8) false /*no overwrite*/;
00786     SimpleJob * newJob = SimpleJobPrivate::newJobNoUi(slave_url, CMD_RENAME, packedArgs);
00787     Scheduler::scheduleJob(newJob);
00788     q->addSubjob( newJob );
00789     if ( m_currentSrcURL.directory() != dest.directory() ) // For the user, moving isn't renaming. Only renaming is.
00790         m_bOnlyRenames = false;
00791 }
00792 
00793 void CopyJobPrivate::startListing( const KUrl & src )
00794 {
00795     Q_Q(CopyJob);
00796     state = STATE_LISTING;
00797     m_bURLDirty = true;
00798     ListJob * newjob = listRecursive(src, KIO::HideProgressInfo);
00799     newjob->setUnrestricted(true);
00800     q->connect(newjob, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)),
00801                SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList)));
00802     q->addSubjob( newjob );
00803 }
00804 
00805 void CopyJobPrivate::skip(const KUrl & sourceUrl, bool isDir)
00806 {
00807     KUrl dir = sourceUrl;
00808     if (!isDir) {
00809         // Skipping a file: make sure not to delete the parent dir (#208418)
00810         dir.setPath(dir.directory());
00811     }
00812     while (dirsToRemove.removeAll(dir) > 0) {
00813         // Do not rely on rmdir() on the parent directories aborting.
00814         // Exclude the parent dirs explicitely.
00815         dir.setPath(dir.directory());
00816     }
00817 }
00818 
00819 bool CopyJobPrivate::shouldOverwriteDir( const QString& path ) const
00820 {
00821     if ( m_bOverwriteAllDirs )
00822         return true;
00823     return m_overwriteList.contains(path);
00824 }
00825 
00826 bool CopyJobPrivate::shouldOverwriteFile( const QString& path ) const
00827 {
00828     if ( m_bOverwriteAllFiles )
00829         return true;
00830     return m_overwriteList.contains(path);
00831 }
00832 
00833 bool CopyJobPrivate::shouldSkip( const QString& path ) const
00834 {
00835     Q_FOREACH(const QString& skipPath, m_skipList) {
00836         if ( path.startsWith(skipPath) )
00837             return true;
00838     }
00839     return false;
00840 }
00841 
00842 void CopyJobPrivate::slotResultCreatingDirs( KJob * job )
00843 {
00844     Q_Q(CopyJob);
00845     // The dir we are trying to create:
00846     QList<CopyInfo>::Iterator it = dirs.begin();
00847     // Was there an error creating a dir ?
00848     if ( job->error() )
00849     {
00850         m_conflictError = job->error();
00851         if ( (m_conflictError == ERR_DIR_ALREADY_EXIST)
00852              || (m_conflictError == ERR_FILE_ALREADY_EXIST) ) // can't happen?
00853         {
00854             KUrl oldURL = ((SimpleJob*)job)->url();
00855             // Should we skip automatically ?
00856             if ( m_bAutoSkipDirs ) {
00857                 // We don't want to copy files in this directory, so we put it on the skip list
00858                 m_skipList.append( oldURL.path( KUrl::AddTrailingSlash ) );
00859                 skip(oldURL, true);
00860                 dirs.erase( it ); // Move on to next dir
00861             } else {
00862                 // Did the user choose to overwrite already?
00863                 const QString destDir = (*it).uDest.path();
00864                 if ( shouldOverwriteDir( destDir ) ) { // overwrite => just skip
00865                     emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
00866                     dirs.erase( it ); // Move on to next dir
00867                 } else {
00868                     if ( !q->isInteractive() ) {
00869                         q->Job::slotResult( job ); // will set the error and emit result(this)
00870                         return;
00871                     }
00872 
00873                     assert( ((SimpleJob*)job)->url().url() == (*it).uDest.url() );
00874                     q->removeSubjob( job );
00875                     assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
00876 
00877                     // We need to stat the existing dir, to get its last-modification time
00878                     KUrl existingDest( (*it).uDest );
00879                     SimpleJob * newJob = KIO::stat( existingDest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
00880                     Scheduler::scheduleJob(newJob);
00881                     kDebug(7007) << "KIO::stat for resolving conflict on " << existingDest;
00882                     state = STATE_CONFLICT_CREATING_DIRS;
00883                     q->addSubjob(newJob);
00884                     return; // Don't move to next dir yet !
00885                 }
00886             }
00887         }
00888         else
00889         {
00890             // Severe error, abort
00891             q->Job::slotResult( job ); // will set the error and emit result(this)
00892             return;
00893         }
00894     }
00895     else // no error : remove from list, to move on to next dir
00896     {
00897         //this is required for the undo feature
00898         emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true, false );
00899         m_directoriesCopied.append( *it );
00900         dirs.erase( it );
00901     }
00902 
00903     m_processedDirs++;
00904     //emit processedAmount( this, KJob::Directories, m_processedDirs );
00905     q->removeSubjob( job );
00906     assert( !q->hasSubjobs() ); // We should have only one job at a time ...
00907     createNextDir();
00908 }
00909 
00910 void CopyJobPrivate::slotResultConflictCreatingDirs( KJob * job )
00911 {
00912     Q_Q(CopyJob);
00913     // We come here after a conflict has been detected and we've stated the existing dir
00914 
00915     // The dir we were trying to create:
00916     QList<CopyInfo>::Iterator it = dirs.begin();
00917 
00918     const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
00919 
00920     // Its modification time:
00921     const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
00922     const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
00923 
00924     const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
00925     const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
00926 
00927     q->removeSubjob( job );
00928     assert ( !q->hasSubjobs() ); // We should have only one job at a time ...
00929 
00930     // Always multi and skip (since there are files after that)
00931     RenameDialog_Mode mode = (RenameDialog_Mode)( M_MULTI | M_SKIP | M_ISDIR );
00932     // Overwrite only if the existing thing is a dir (no chance with a file)
00933     if ( m_conflictError == ERR_DIR_ALREADY_EXIST )
00934     {
00935         if( (*it).uSource == (*it).uDest ||
00936             ((*it).uSource.protocol() == (*it).uDest.protocol() &&
00937               (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
00938           mode = (RenameDialog_Mode)( mode | M_OVERWRITE_ITSELF);
00939         else
00940           mode = (RenameDialog_Mode)( mode | M_OVERWRITE );
00941     }
00942 
00943     QString existingDest = (*it).uDest.path();
00944     QString newPath;
00945     if (m_reportTimer)
00946         m_reportTimer->stop();
00947     RenameDialog_Result r = q->ui()->askFileRename( q, i18n("Folder Already Exists"),
00948                                          (*it).uSource.url(),
00949                                          (*it).uDest.url(),
00950                                          mode, newPath,
00951                                          (*it).size, destsize,
00952                                          (*it).ctime, destctime,
00953                                          (*it).mtime, destmtime );
00954     if (m_reportTimer)
00955         m_reportTimer->start(REPORT_TIMEOUT);
00956     switch ( r ) {
00957         case R_CANCEL:
00958             q->setError( ERR_USER_CANCELED );
00959             q->emitResult();
00960             return;
00961         case R_RENAME:
00962         {
00963             QString oldPath = (*it).uDest.path( KUrl::AddTrailingSlash );
00964             KUrl newUrl( (*it).uDest );
00965             newUrl.setPath( newPath );
00966             emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
00967 
00968             // Change the current one and strip the trailing '/'
00969             (*it).uDest.setPath( newUrl.path( KUrl::RemoveTrailingSlash ) );
00970             newPath = newUrl.path( KUrl::AddTrailingSlash ); // With trailing slash
00971             QList<CopyInfo>::Iterator renamedirit = it;
00972             ++renamedirit;
00973             // Change the name of subdirectories inside the directory
00974             for( ; renamedirit != dirs.end() ; ++renamedirit )
00975             {
00976                 QString path = (*renamedirit).uDest.path();
00977                 if ( path.startsWith( oldPath ) ) {
00978                     QString n = path;
00979                     n.replace( 0, oldPath.length(), newPath );
00980                     kDebug(7007) << "dirs list:" << (*renamedirit).uSource.path()
00981                                   << "was going to be" << path
00982                                   << ", changed into" << n;
00983                     (*renamedirit).uDest.setPath( n );
00984                 }
00985             }
00986             // Change filenames inside the directory
00987             QList<CopyInfo>::Iterator renamefileit = files.begin();
00988             for( ; renamefileit != files.end() ; ++renamefileit )
00989             {
00990                 QString path = (*renamefileit).uDest.path();
00991                 if ( path.startsWith( oldPath ) ) {
00992                     QString n = path;
00993                     n.replace( 0, oldPath.length(), newPath );
00994                     kDebug(7007) << "files list:" << (*renamefileit).uSource.path()
00995                                   << "was going to be" << path
00996                                   << ", changed into" << n;
00997                     (*renamefileit).uDest.setPath( n );
00998                 }
00999             }
01000             if (!dirs.isEmpty())
01001                 emit q->aboutToCreate( q, dirs );
01002             if (!files.isEmpty())
01003                 emit q->aboutToCreate( q, files );
01004         }
01005         break;
01006         case R_AUTO_SKIP:
01007             m_bAutoSkipDirs = true;
01008             // fall through
01009         case R_SKIP:
01010             m_skipList.append( existingDest );
01011             skip((*it).uSource, true);
01012             // Move on to next dir
01013             dirs.erase( it );
01014             m_processedDirs++;
01015             break;
01016         case R_OVERWRITE:
01017             m_overwriteList.append( existingDest );
01018             emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
01019             // Move on to next dir
01020             dirs.erase( it );
01021             m_processedDirs++;
01022             break;
01023         case R_OVERWRITE_ALL:
01024             m_bOverwriteAllDirs = true;
01025             emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, true /* directory */, false /* renamed */ );
01026             // Move on to next dir
01027             dirs.erase( it );
01028             m_processedDirs++;
01029             break;
01030         default:
01031             assert( 0 );
01032     }
01033     state = STATE_CREATING_DIRS;
01034     //emit processedAmount( this, KJob::Directories, m_processedDirs );
01035     createNextDir();
01036 }
01037 
01038 void CopyJobPrivate::createNextDir()
01039 {
01040     Q_Q(CopyJob);
01041     KUrl udir;
01042     if ( !dirs.isEmpty() )
01043     {
01044         // Take first dir to create out of list
01045         QList<CopyInfo>::Iterator it = dirs.begin();
01046         // Is this URL on the skip list or the overwrite list ?
01047         while( it != dirs.end() && udir.isEmpty() )
01048         {
01049             const QString dir = (*it).uDest.path();
01050             if ( shouldSkip( dir ) ) {
01051                 dirs.erase( it );
01052                 it = dirs.begin();
01053             } else
01054                 udir = (*it).uDest;
01055         }
01056     }
01057     if ( !udir.isEmpty() ) // any dir to create, finally ?
01058     {
01059         // Create the directory - with default permissions so that we can put files into it
01060         // TODO : change permissions once all is finished; but for stuff coming from CDROM it sucks...
01061         KIO::SimpleJob *newjob = KIO::mkdir( udir, -1 );
01062         Scheduler::scheduleJob(newjob);
01063         if (shouldOverwriteFile(udir.path())) { // if we are overwriting an existing file or symlink
01064             newjob->addMetaData("overwrite", "true");
01065         }
01066 
01067         m_currentDestURL = udir;
01068         m_bURLDirty = true;
01069 
01070         q->addSubjob(newjob);
01071         return;
01072     }
01073     else // we have finished creating dirs
01074     {
01075         q->setProcessedAmount( KJob::Directories, m_processedDirs ); // make sure final number appears
01076 
01077         if (m_mode == CopyJob::Move) {
01078             // Now we know which dirs hold the files we're going to delete.
01079             // To speed things up and prevent double-notification, we disable KDirWatch
01080             // on those dirs temporarily (using KDirWatch::self, that's the instanced
01081             // used by e.g. kdirlister).
01082             for ( QSet<QString>::const_iterator it = m_parentDirs.constBegin() ; it != m_parentDirs.constEnd() ; ++it )
01083                 KDirWatch::self()->stopDirScan( *it );
01084         }
01085 
01086         state = STATE_COPYING_FILES;
01087         m_processedFiles++; // Ralf wants it to start at 1, not 0
01088         copyNextFile();
01089     }
01090 }
01091 
01092 void CopyJobPrivate::slotResultCopyingFiles( KJob * job )
01093 {
01094     Q_Q(CopyJob);
01095     // The file we were trying to copy:
01096     QList<CopyInfo>::Iterator it = files.begin();
01097     if ( job->error() )
01098     {
01099         // Should we skip automatically ?
01100         if ( m_bAutoSkipFiles )
01101         {
01102             skip((*it).uSource, false);
01103             m_fileProcessedSize = (*it).size;
01104             files.erase( it ); // Move on to next file
01105         }
01106         else
01107         {
01108             if ( !q->isInteractive() ) {
01109                 q->Job::slotResult( job ); // will set the error and emit result(this)
01110                 return;
01111             }
01112 
01113             m_conflictError = job->error(); // save for later
01114             // Existing dest ?
01115             if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
01116                  || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
01117                  || ( m_conflictError == ERR_IDENTICAL_FILES ) )
01118             {
01119                 q->removeSubjob( job );
01120                 assert ( !q->hasSubjobs() );
01121                 // We need to stat the existing file, to get its last-modification time
01122                 KUrl existingFile( (*it).uDest );
01123                 SimpleJob * newJob = KIO::stat( existingFile, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
01124                 Scheduler::scheduleJob(newJob);
01125                 kDebug(7007) << "KIO::stat for resolving conflict on " << existingFile;
01126                 state = STATE_CONFLICT_COPYING_FILES;
01127                 q->addSubjob(newJob);
01128                 return; // Don't move to next file yet !
01129             }
01130             else
01131             {
01132                 if ( m_bCurrentOperationIsLink && qobject_cast<KIO::DeleteJob*>( job ) )
01133                 {
01134                     // Very special case, see a few lines below
01135                     // We are deleting the source of a symlink we successfully moved... ignore error
01136                     m_fileProcessedSize = (*it).size;
01137                     files.erase( it );
01138                 } else {
01139                     // Go directly to the conflict resolution, there is nothing to stat
01140                     slotResultConflictCopyingFiles( job );
01141                     return;
01142                 }
01143             }
01144         }
01145     } else // no error
01146     {
01147         // Special case for moving links. That operation needs two jobs, unlike others.
01148         if ( m_bCurrentOperationIsLink && m_mode == CopyJob::Move
01149              && !qobject_cast<KIO::DeleteJob *>( job ) // Deleting source not already done
01150              )
01151         {
01152             q->removeSubjob( job );
01153             assert ( !q->hasSubjobs() );
01154             // The only problem with this trick is that the error handling for this del operation
01155             // is not going to be right... see 'Very special case' above.
01156             KIO::Job * newjob = KIO::del( (*it).uSource, HideProgressInfo );
01157             q->addSubjob( newjob );
01158             return; // Don't move to next file yet !
01159         }
01160 
01161         if ( m_bCurrentOperationIsLink )
01162         {
01163             QString target = ( m_mode == CopyJob::Link ? (*it).uSource.path() : (*it).linkDest );
01164             //required for the undo feature
01165             emit q->copyingLinkDone( q, (*it).uSource, target, (*it).uDest );
01166         }
01167         else {
01168             //required for the undo feature
01169             emit q->copyingDone( q, (*it).uSource, (*it).uDest, (*it).mtime, false, false );
01170             if (m_mode == CopyJob::Move)
01171                 org::kde::KDirNotify::emitFileMoved( (*it).uSource.url(), (*it).uDest.url() );
01172             m_successSrcList.append((*it).uSource);
01173         }
01174         // remove from list, to move on to next file
01175         files.erase( it );
01176     }
01177     m_processedFiles++;
01178 
01179     // clear processed size for last file and add it to overall processed size
01180     m_processedSize += m_fileProcessedSize;
01181     m_fileProcessedSize = 0;
01182 
01183     //kDebug(7007) << files.count() << "files remaining";
01184 
01185     // Merge metadata from subjob
01186     KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
01187     Q_ASSERT(kiojob);
01188     m_incomingMetaData += kiojob->metaData();
01189     q->removeSubjob( job );
01190     assert( !q->hasSubjobs() ); // We should have only one job at a time ...
01191     copyNextFile();
01192 }
01193 
01194 void CopyJobPrivate::slotResultConflictCopyingFiles( KJob * job )
01195 {
01196     Q_Q(CopyJob);
01197     // We come here after a conflict has been detected and we've stated the existing file
01198     // The file we were trying to create:
01199     QList<CopyInfo>::Iterator it = files.begin();
01200 
01201     RenameDialog_Result res;
01202     QString newPath;
01203 
01204     if (m_reportTimer)
01205         m_reportTimer->stop();
01206 
01207     if ( ( m_conflictError == ERR_FILE_ALREADY_EXIST )
01208          || ( m_conflictError == ERR_DIR_ALREADY_EXIST )
01209          || ( m_conflictError == ERR_IDENTICAL_FILES ) )
01210     {
01211         // Its modification time:
01212         const UDSEntry entry = ((KIO::StatJob*)job)->statResult();
01213 
01214         const time_t destmtime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_MODIFICATION_TIME, -1 );
01215         const time_t destctime = (time_t) entry.numberValue( KIO::UDSEntry::UDS_CREATION_TIME, -1 );
01216         const KIO::filesize_t destsize = entry.numberValue( KIO::UDSEntry::UDS_SIZE );
01217         const QString linkDest = entry.stringValue( KIO::UDSEntry::UDS_LINK_DEST );
01218 
01219         // Offer overwrite only if the existing thing is a file
01220         // If src==dest, use "overwrite-itself"
01221         RenameDialog_Mode mode;
01222         bool isDir = true;
01223 
01224         if( m_conflictError == ERR_DIR_ALREADY_EXIST )
01225             mode = M_ISDIR;
01226         else
01227         {
01228             if ( (*it).uSource == (*it).uDest  ||
01229                  ((*it).uSource.protocol() == (*it).uDest.protocol() &&
01230                    (*it).uSource.path( KUrl::RemoveTrailingSlash ) == linkDest) )
01231                 mode = M_OVERWRITE_ITSELF;
01232             else
01233                 mode = M_OVERWRITE;
01234             isDir = false;
01235         }
01236 
01237         if ( !m_bSingleFileCopy )
01238             mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
01239 
01240         res = q->ui()->askFileRename( q, !isDir ?
01241                                    i18n("File Already Exists") : i18n("Already Exists as Folder"),
01242                                    (*it).uSource.url(),
01243                                    (*it).uDest.url(),
01244                                    mode, newPath,
01245                                    (*it).size, destsize,
01246                                    (*it).ctime, destctime,
01247                                    (*it).mtime, destmtime );
01248 
01249     }
01250     else
01251     {
01252         if ( job->error() == ERR_USER_CANCELED )
01253             res = R_CANCEL;
01254         else if ( !q->isInteractive() ) {
01255             q->Job::slotResult( job ); // will set the error and emit result(this)
01256             return;
01257         }
01258         else
01259         {
01260             SkipDialog_Result skipResult = q->ui()->askSkip( q, files.count() > 1,
01261                                                           job->errorString() );
01262 
01263             // Convert the return code from SkipDialog into a RenameDialog code
01264             res = ( skipResult == S_SKIP ) ? R_SKIP :
01265                          ( skipResult == S_AUTO_SKIP ) ? R_AUTO_SKIP :
01266                                         R_CANCEL;
01267         }
01268     }
01269 
01270     if (m_reportTimer)
01271         m_reportTimer->start(REPORT_TIMEOUT);
01272 
01273     q->removeSubjob( job );
01274     assert ( !q->hasSubjobs() );
01275     switch ( res ) {
01276         case R_CANCEL:
01277             q->setError( ERR_USER_CANCELED );
01278             q->emitResult();
01279             return;
01280         case R_RENAME:
01281         {
01282             KUrl newUrl( (*it).uDest );
01283             newUrl.setPath( newPath );
01284             emit q->renamed( q, (*it).uDest, newUrl ); // for e.g. kpropsdlg
01285             (*it).uDest = newUrl;
01286 
01287             QList<CopyInfo> files;
01288             files.append(*it);
01289             emit q->aboutToCreate( q, files );
01290         }
01291         break;
01292         case R_AUTO_SKIP:
01293             m_bAutoSkipFiles = true;
01294             // fall through
01295         case R_SKIP:
01296             // Move on to next file
01297             skip((*it).uSource, false);
01298             m_processedSize += (*it).size;
01299             files.erase( it );
01300             m_processedFiles++;
01301             break;
01302        case R_OVERWRITE_ALL:
01303             m_bOverwriteAllFiles = true;
01304             break;
01305         case R_OVERWRITE:
01306             // Add to overwrite list, so that copyNextFile knows to overwrite
01307             m_overwriteList.append( (*it).uDest.path() );
01308             break;
01309         default:
01310             assert( 0 );
01311     }
01312     state = STATE_COPYING_FILES;
01313     copyNextFile();
01314 }
01315 
01316 KIO::Job* CopyJobPrivate::linkNextFile( const KUrl& uSource, const KUrl& uDest, JobFlags flags )
01317 {
01318     //kDebug(7007) << "Linking";
01319     if (
01320         (uSource.protocol() == uDest.protocol()) &&
01321         (uSource.host() == uDest.host()) &&
01322         (uSource.port() == uDest.port()) &&
01323         (uSource.user() == uDest.user()) &&
01324         (uSource.pass() == uDest.pass()) )
01325     {
01326         // This is the case of creating a real symlink
01327         KIO::SimpleJob *newJob = KIO::symlink( uSource.path(), uDest, flags|HideProgressInfo /*no GUI*/ );
01328         Scheduler::scheduleJob(newJob);
01329         //kDebug(7007) << "Linking target=" << uSource.path() << "link=" << uDest;
01330         //emit linking( this, uSource.path(), uDest );
01331         m_bCurrentOperationIsLink = true;
01332         m_currentSrcURL=uSource;
01333         m_currentDestURL=uDest;
01334         m_bURLDirty = true;
01335         //Observer::self()->slotCopying( this, uSource, uDest ); // should be slotLinking perhaps
01336         return newJob;
01337     } else {
01338         Q_Q(CopyJob);
01339         //kDebug(7007) << "Linking URL=" << uSource << "link=" << uDest;
01340         if ( uDest.isLocalFile() ) {
01341             // if the source is a devices url, handle it a littlebit special
01342 
01343             QString path = uDest.toLocalFile();
01344             //kDebug(7007) << "path=" << path;
01345             QFile f( path );
01346             if ( f.open( QIODevice::ReadWrite ) )
01347             {
01348                 f.close();
01349                 KDesktopFile desktopFile( path );
01350                 KConfigGroup config = desktopFile.desktopGroup();
01351                 KUrl url = uSource;
01352                 url.setPass( "" );
01353                 config.writePathEntry( "URL", url.url() );
01354                 config.writeEntry( "Name", url.url() );
01355                 config.writeEntry( "Type", QString::fromLatin1("Link") );
01356                 QString protocol = uSource.protocol();
01357                 if ( protocol == QLatin1String("ftp") )
01358                     config.writeEntry( "Icon", QString::fromLatin1("folder-remote") );
01359                 else if ( protocol == QLatin1String("http") )
01360                     config.writeEntry( "Icon", QString::fromLatin1("text-html") );
01361                 else if ( protocol == QLatin1String("info") )
01362                     config.writeEntry( "Icon", QString::fromLatin1("text-x-texinfo") );
01363                 else if ( protocol == QLatin1String("mailto") )   // sven:
01364                     config.writeEntry( "Icon", QString::fromLatin1("internet-mail") ); // added mailto: support
01365                 else
01366                     config.writeEntry( "Icon", QString::fromLatin1("unknown") );
01367                 config.sync();
01368                 files.erase( files.begin() ); // done with this one, move on
01369                 m_processedFiles++;
01370                 //emit processedAmount( this, KJob::Files, m_processedFiles );
01371                 copyNextFile();
01372                 return 0;
01373             }
01374             else
01375             {
01376                 kDebug(7007) << "ERR_CANNOT_OPEN_FOR_WRITING";
01377                 q->setError( ERR_CANNOT_OPEN_FOR_WRITING );
01378                 q->setErrorText( uDest.toLocalFile() );
01379                 q->emitResult();
01380                 return 0;
01381             }
01382         } else {
01383             // Todo: not show "link" on remote dirs if the src urls are not from the same protocol+host+...
01384             q->setError( ERR_CANNOT_SYMLINK );
01385             q->setErrorText( uDest.prettyUrl() );
01386             q->emitResult();
01387             return 0;
01388         }
01389     }
01390 }
01391 
01392 void CopyJobPrivate::copyNextFile()
01393 {
01394     Q_Q(CopyJob);
01395     bool bCopyFile = false;
01396     //kDebug(7007);
01397     // Take the first file in the list
01398     QList<CopyInfo>::Iterator it = files.begin();
01399     // Is this URL on the skip list ?
01400     while (it != files.end() && !bCopyFile)
01401     {
01402         const QString destFile = (*it).uDest.path();
01403         bCopyFile = !shouldSkip( destFile );
01404         if ( !bCopyFile ) {
01405             files.erase( it );
01406             it = files.begin();
01407         }
01408     }
01409 
01410     if (bCopyFile) // any file to create, finally ?
01411     {
01412         const KUrl& uSource = (*it).uSource;
01413         const KUrl& uDest = (*it).uDest;
01414         // Do we set overwrite ?
01415         bool bOverwrite;
01416         const QString destFile = uDest.path();
01417         // kDebug(7007) << "copying" << destFile;
01418         if ( uDest == uSource )
01419             bOverwrite = false;
01420         else
01421             bOverwrite = shouldOverwriteFile( destFile );
01422 
01423         m_bCurrentOperationIsLink = false;
01424         KIO::Job * newjob = 0;
01425         if ( m_mode == CopyJob::Link ) {
01426             // User requested that a symlink be made
01427             const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
01428             newjob = linkNextFile(uSource, uDest, flags);
01429             if (!newjob)
01430                 return;
01431         } else if ( !(*it).linkDest.isEmpty() &&
01432                   (uSource.protocol() == uDest.protocol()) &&
01433                   (uSource.host() == uDest.host()) &&
01434                   (uSource.port() == uDest.port()) &&
01435                   (uSource.user() == uDest.user()) &&
01436                   (uSource.pass() == uDest.pass()))
01437             // Copying a symlink - only on the same protocol/host/etc. (#5601, downloading an FTP file through its link),
01438         {
01439             const JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
01440             KIO::SimpleJob *newJob = KIO::symlink( (*it).linkDest, uDest, flags | HideProgressInfo /*no GUI*/ );
01441             Scheduler::scheduleJob(newJob);
01442             newjob = newJob;
01443             //kDebug(7007) << "Linking target=" << (*it).linkDest << "link=" << uDest;
01444             m_currentSrcURL = KUrl( (*it).linkDest );
01445             m_currentDestURL = uDest;
01446             m_bURLDirty = true;
01447             //emit linking( this, (*it).linkDest, uDest );
01448             //Observer::self()->slotCopying( this, m_currentSrcURL, uDest ); // should be slotLinking perhaps
01449             m_bCurrentOperationIsLink = true;
01450             // NOTE: if we are moving stuff, the deletion of the source will be done in slotResultCopyingFiles
01451         } else if (m_mode == CopyJob::Move) // Moving a file
01452         {
01453             JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
01454             KIO::FileCopyJob * moveJob = KIO::file_move( uSource, uDest, (*it).permissions, flags | HideProgressInfo/*no GUI*/ );
01455             moveJob->setSourceSize( (*it).size );
01456             newjob = moveJob;
01457             //kDebug(7007) << "Moving" << uSource << "to" << uDest;
01458             //emit moving( this, uSource, uDest );
01459             m_currentSrcURL=uSource;
01460             m_currentDestURL=uDest;
01461             m_bURLDirty = true;
01462             //Observer::self()->slotMoving( this, uSource, uDest );
01463         }
01464         else // Copying a file
01465         {
01466             // If source isn't local and target is local, we ignore the original permissions
01467             // Otherwise, files downloaded from HTTP end up with -r--r--r--
01468             bool remoteSource = !KProtocolManager::supportsListing(uSource);
01469             int permissions = (*it).permissions;
01470             if ( m_defaultPermissions || ( remoteSource && uDest.isLocalFile() ) )
01471                 permissions = -1;
01472             JobFlags flags = bOverwrite ? Overwrite : DefaultFlags;
01473             KIO::FileCopyJob * copyJob = KIO::file_copy( uSource, uDest, permissions, flags | HideProgressInfo/*no GUI*/ );
01474             copyJob->setParentJob( q ); // in case of rename dialog
01475             copyJob->setSourceSize( (*it).size );
01476             if ((*it).mtime != -1) {
01477                 QDateTime dt; dt.setTime_t( (*it).mtime );
01478                 copyJob->setModificationTime( dt );
01479             }
01480             newjob = copyJob;
01481             //kDebug(7007) << "Copying" << uSource << "to" << uDest;
01482             m_currentSrcURL=uSource;
01483             m_currentDestURL=uDest;
01484             m_bURLDirty = true;
01485         }
01486         q->addSubjob(newjob);
01487         q->connect( newjob, SIGNAL( processedSize( KJob*, qulonglong ) ),
01488                     SLOT( slotProcessedSize( KJob*, qulonglong ) ) );
01489         q->connect( newjob, SIGNAL( totalSize( KJob*, qulonglong ) ),
01490                     SLOT( slotTotalSize( KJob*, qulonglong ) ) );
01491     }
01492     else
01493     {
01494         // We're done
01495         //kDebug(7007) << "copyNextFile finished";
01496         deleteNextDir();
01497     }
01498 }
01499 
01500 void CopyJobPrivate::deleteNextDir()
01501 {
01502     Q_Q(CopyJob);
01503     if ( m_mode == CopyJob::Move && !dirsToRemove.isEmpty() ) // some dirs to delete ?
01504     {
01505         state = STATE_DELETING_DIRS;
01506         m_bURLDirty = true;
01507         // Take first dir to delete out of list - last ones first !
01508         KUrl::List::Iterator it = --dirsToRemove.end();
01509         SimpleJob *job = KIO::rmdir( *it );
01510         Scheduler::scheduleJob(job);
01511         dirsToRemove.erase(it);
01512         q->addSubjob( job );
01513     }
01514     else
01515     {
01516         // This step is done, move on
01517         state = STATE_SETTING_DIR_ATTRIBUTES;
01518         m_directoriesCopiedIterator = m_directoriesCopied.constBegin();
01519         setNextDirAttribute();
01520     }
01521 }
01522 
01523 void CopyJobPrivate::setNextDirAttribute()
01524 {
01525     Q_Q(CopyJob);
01526     while (m_directoriesCopiedIterator != m_directoriesCopied.constEnd() &&
01527            (*m_directoriesCopiedIterator).mtime == -1) {
01528         ++m_directoriesCopiedIterator;
01529     }
01530     if ( m_directoriesCopiedIterator != m_directoriesCopied.constEnd() ) {
01531         const KUrl url = (*m_directoriesCopiedIterator).uDest;
01532         const time_t mtime = (*m_directoriesCopiedIterator).mtime;
01533         const QDateTime dt = QDateTime::fromTime_t(mtime);
01534         ++m_directoriesCopiedIterator;
01535 
01536         KIO::SimpleJob *job = KIO::setModificationTime( url, dt );
01537         Scheduler::scheduleJob(job);
01538         q->addSubjob( job );
01539 
01540 
01541 #if 0 // ifdef Q_OS_UNIX
01542         // TODO: can be removed now. Or reintroduced as a fast path for local files
01543         // if launching even more jobs as done above is a performance problem.
01544         //
01545         QLinkedList<CopyInfo>::const_iterator it = m_directoriesCopied.constBegin();
01546         for ( ; it != m_directoriesCopied.constEnd() ; ++it ) {
01547             const KUrl& url = (*it).uDest;
01548             if ( url.isLocalFile() && (*it).mtime != (time_t)-1 ) {
01549                 KDE_struct_stat statbuf;
01550                 if (KDE::lstat(url.path(), &statbuf) == 0) {
01551                     struct utimbuf utbuf;
01552                     utbuf.actime = statbuf.st_atime; // access time, unchanged
01553                     utbuf.modtime = (*it).mtime; // modification time
01554                     utime( path, &utbuf );
01555                 }
01556 
01557             }
01558         }
01559         m_directoriesCopied.clear();
01560         // but then we need to jump to the else part below. Maybe with a recursive call?
01561 #endif
01562     } else {
01563         if (m_reportTimer)
01564             m_reportTimer->stop();
01565         --m_processedFiles; // undo the "start at 1" hack
01566         slotReport(); // display final numbers, important if progress dialog stays up
01567 
01568         q->emitResult();
01569     }
01570 }
01571 
01572 void CopyJob::emitResult()
01573 {
01574     Q_D(CopyJob);
01575     // Before we go, tell the world about the changes that were made.
01576     // Even if some error made us abort midway, we might still have done
01577     // part of the job so we better update the views! (#118583)
01578     if (!d->m_bOnlyRenames) {
01579         KUrl url(d->m_globalDest);
01580         if (d->m_globalDestinationState != DEST_IS_DIR || d->m_asMethod)
01581             url.setPath(url.directory());
01582         //kDebug(7007) << "KDirNotify'ing FilesAdded" << url;
01583         org::kde::KDirNotify::emitFilesAdded( url.url() );
01584 
01585         if (d->m_mode == CopyJob::Move && !d->m_successSrcList.isEmpty()) {
01586             kDebug(7007) << "KDirNotify'ing FilesRemoved" << d->m_successSrcList.toStringList();
01587             org::kde::KDirNotify::emitFilesRemoved(d->m_successSrcList.toStringList());
01588         }
01589 
01590         // Re-enable watching on the dirs that held the deleted files
01591         if (d->m_mode == CopyJob::Move) {
01592             for (QSet<QString>::const_iterator it = d->m_parentDirs.constBegin() ; it != d->m_parentDirs.constEnd() ; ++it)
01593                 KDirWatch::self()->restartDirScan( *it );
01594         }
01595     }
01596     Job::emitResult();
01597 }
01598 
01599 void CopyJobPrivate::slotProcessedSize( KJob*, qulonglong data_size )
01600 {
01601   Q_Q(CopyJob);
01602   //kDebug(7007) << data_size;
01603   m_fileProcessedSize = data_size;
01604   q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
01605 
01606   if ( m_processedSize + m_fileProcessedSize > m_totalSize )
01607   {
01608     // Example: download any attachment from bugs.kde.org
01609     m_totalSize = m_processedSize + m_fileProcessedSize;
01610     //kDebug(7007) << "Adjusting m_totalSize to" << m_totalSize;
01611     q->setTotalAmount(KJob::Bytes, m_totalSize); // safety
01612   }
01613   //kDebug(7007) << "emit processedSize" << (unsigned long) (m_processedSize + m_fileProcessedSize);
01614   q->setProcessedAmount(KJob::Bytes, m_processedSize + m_fileProcessedSize);
01615 }
01616 
01617 void CopyJobPrivate::slotTotalSize( KJob*, qulonglong size )
01618 {
01619   Q_Q(CopyJob);
01620   //kDebug(7007) << size;
01621   // Special case for copying a single file
01622   // This is because some protocols don't implement stat properly
01623   // (e.g. HTTP), and don't give us a size in some cases (redirection)
01624   // so we'd rather rely on the size given for the transfer
01625   if ( m_bSingleFileCopy && size > m_totalSize)
01626   {
01627     //kDebug(7007) << "slotTotalSize: updating totalsize to" << size;
01628     m_totalSize = size;
01629     q->setTotalAmount(KJob::Bytes, size);
01630   }
01631 }
01632 
01633 void CopyJobPrivate::slotResultDeletingDirs( KJob * job )
01634 {
01635     Q_Q(CopyJob);
01636     if (job->error()) {
01637         // Couldn't remove directory. Well, perhaps it's not empty
01638         // because the user pressed Skip for a given file in it.
01639         // Let's not display "Could not remove dir ..." for each of those dir !
01640     } else {
01641         m_successSrcList.append(static_cast<KIO::SimpleJob*>(job)->url());
01642     }
01643     q->removeSubjob( job );
01644     assert( !q->hasSubjobs() );
01645     deleteNextDir();
01646 }
01647 
01648 void CopyJobPrivate::slotResultSettingDirAttributes( KJob * job )
01649 {
01650     Q_Q(CopyJob);
01651     if (job->error())
01652     {
01653         // Couldn't set directory attributes. Ignore the error, it can happen
01654         // with inferior file systems like VFAT.
01655         // Let's not display warnings for each dir like "cp -a" does.
01656     }
01657     q->removeSubjob( job );
01658     assert( !q->hasSubjobs() );
01659     setNextDirAttribute();
01660 }
01661 
01662 // We were trying to do a direct renaming, before even stat'ing
01663 void CopyJobPrivate::slotResultRenaming( KJob* job )
01664 {
01665     Q_Q(CopyJob);
01666     int err = job->error();
01667     const QString errText = job->errorText();
01668     // Merge metadata from subjob
01669     KIO::Job* kiojob = dynamic_cast<KIO::Job*>(job);
01670     Q_ASSERT(kiojob);
01671     m_incomingMetaData += kiojob->metaData();
01672     q->removeSubjob( job );
01673     assert ( !q->hasSubjobs() );
01674     // Determine dest again
01675     KUrl dest = m_dest;
01676     if ( destinationState == DEST_IS_DIR && !m_asMethod )
01677         dest.addPath( m_currentSrcURL.fileName() );
01678     if ( err )
01679     {
01680         // Direct renaming didn't work. Try renaming to a temp name,
01681         // this can help e.g. when renaming 'a' to 'A' on a VFAT partition.
01682         // In that case it's the _same_ dir, we don't want to copy+del (data loss!)
01683       if ( m_currentSrcURL.isLocalFile() && m_currentSrcURL.url(KUrl::RemoveTrailingSlash) != dest.url(KUrl::RemoveTrailingSlash) &&
01684            m_currentSrcURL.url(KUrl::RemoveTrailingSlash).toLower() == dest.url(KUrl::RemoveTrailingSlash).toLower() &&
01685              ( err == ERR_FILE_ALREADY_EXIST ||
01686                err == ERR_DIR_ALREADY_EXIST ||
01687                err == ERR_IDENTICAL_FILES ) )
01688         {
01689             kDebug(7007) << "Couldn't rename directly, dest already exists. Detected special case of lower/uppercase renaming in same dir, try with 2 rename calls";
01690             const QString _src( m_currentSrcURL.toLocalFile() );
01691             const QString _dest( dest.toLocalFile() );
01692             KTemporaryFile tmpFile;
01693             tmpFile.setPrefix(m_currentSrcURL.directory(KUrl::ObeyTrailingSlash));
01694             tmpFile.setAutoRemove(false);
01695             tmpFile.open();
01696             const QString _tmp( tmpFile.fileName() );
01697             kDebug(7007) << "KTemporaryFile using" << _tmp << "as intermediary";
01698             if ( KDE::rename( _src, _tmp ) == 0 )
01699             {
01700                 if ( !QFile::exists( _dest ) && KDE::rename( _tmp, _dest ) == 0 )
01701                 {
01702                     kDebug(7007) << "Success.";
01703                     err = 0;
01704                 }
01705                 else
01706                 {
01707                     // Revert back to original name!
01708                     if ( KDE::rename( _tmp, _src ) != 0 ) {
01709                         kError(7007) << "Couldn't rename" << _tmp << "back to" << _src << '!';
01710                         // Severe error, abort
01711                         q->Job::slotResult( job ); // will set the error and emit result(this)
01712                         return;
01713                     }
01714                 }
01715             }
01716         }
01717     }
01718     if ( err )
01719     {
01720         // This code is similar to CopyJobPrivate::slotResultConflictCopyingFiles
01721         // but here it's about the base src url being moved/renamed
01722         // (m_currentSrcURL) and its dest (m_dest), not about a single file.
01723         // It also means we already stated the dest, here.
01724         // On the other hand we haven't stated the src yet (we skipped doing it
01725         // to save time, since it's not necessary to rename directly!)...
01726 
01727         // Existing dest?
01728         if ( err == ERR_DIR_ALREADY_EXIST ||
01729                err == ERR_FILE_ALREADY_EXIST ||
01730                err == ERR_IDENTICAL_FILES )
01731         {
01732             // Should we skip automatically ?
01733             bool isDir = (err == ERR_DIR_ALREADY_EXIST); // ## technically, isDir means "source is dir", not "dest is dir" #######
01734             if ((isDir && m_bAutoSkipDirs) || (!isDir && m_bAutoSkipFiles)) {
01735                 // Move on to next source url
01736                 skipSrc(isDir);
01737                 return;
01738             } else if ((isDir && m_bOverwriteAllDirs) || (!isDir && m_bOverwriteAllFiles)) {
01739                 ; // nothing to do, stat+copy+del will overwrite
01740             } else if ( q->isInteractive() ) {
01741                 QString newPath;
01742                 // we lack mtime info for both the src (not stated)
01743                 // and the dest (stated but this info wasn't stored)
01744                 // Let's do it for local files, at least
01745                 KIO::filesize_t sizeSrc = (KIO::filesize_t) -1;
01746                 KIO::filesize_t sizeDest = (KIO::filesize_t) -1;
01747                 time_t ctimeSrc = (time_t) -1;
01748                 time_t ctimeDest = (time_t) -1;
01749                 time_t mtimeSrc = (time_t) -1;
01750                 time_t mtimeDest = (time_t) -1;
01751 
01752                 bool destIsDir = err == ERR_DIR_ALREADY_EXIST;
01753 
01754                 // ## TODO we need to stat the source using KIO::stat
01755                 // so that this code is properly network-transparent.
01756 
01757                 KDE_struct_stat stat_buf;
01758                 if ( m_currentSrcURL.isLocalFile() &&
01759                     KDE::stat(m_currentSrcURL.toLocalFile(), &stat_buf) == 0 ) {
01760                     sizeSrc = stat_buf.st_size;
01761                     ctimeSrc = stat_buf.st_ctime;
01762                     mtimeSrc = stat_buf.st_mtime;
01763                     isDir = S_ISDIR(stat_buf.st_mode);
01764                 }
01765                 if ( dest.isLocalFile() &&
01766                     KDE::stat(dest.toLocalFile(), &stat_buf) == 0 ) {
01767                     sizeDest = stat_buf.st_size;
01768                     ctimeDest = stat_buf.st_ctime;
01769                     mtimeDest = stat_buf.st_mtime;
01770                     destIsDir = S_ISDIR(stat_buf.st_mode);
01771                 }
01772 
01773                 // If src==dest, use "overwrite-itself"
01774                 RenameDialog_Mode mode = ( m_currentSrcURL == dest ) ? M_OVERWRITE_ITSELF : M_OVERWRITE;
01775                 if (!isDir && destIsDir) {
01776                     // We can't overwrite a dir with a file.
01777                     mode = (RenameDialog_Mode) 0;
01778                 }
01779 
01780                 if ( m_srcList.count() > 1 )
01781                     mode = (RenameDialog_Mode) ( mode | M_MULTI | M_SKIP );
01782                 if (destIsDir)
01783                     mode = (RenameDialog_Mode) ( mode | M_ISDIR );
01784 
01785                 if (m_reportTimer)
01786                     m_reportTimer->stop();
01787 
01788                 RenameDialog_Result r = q->ui()->askFileRename(
01789                     q,
01790                     err != ERR_DIR_ALREADY_EXIST ? i18n("File Already Exists") : i18n("Already Exists as Folder"),
01791                     m_currentSrcURL.url(),
01792                     dest.url(),
01793                     mode, newPath,
01794                     sizeSrc, sizeDest,
01795                     ctimeSrc, ctimeDest,
01796                     mtimeSrc, mtimeDest );
01797 
01798                 if (m_reportTimer)
01799                     m_reportTimer->start(REPORT_TIMEOUT);
01800 
01801                 switch ( r )
01802                 {
01803                 case R_CANCEL:
01804                 {
01805                     q->setError( ERR_USER_CANCELED );
01806                     q->emitResult();
01807                     return;
01808                 }
01809                 case R_RENAME:
01810                 {
01811                     // Set m_dest to the chosen destination
01812                     // This is only for this src url; the next one will revert to m_globalDest
01813                     m_dest.setPath( newPath );
01814                     KIO::Job* job = KIO::stat( m_dest, StatJob::DestinationSide, 2, KIO::HideProgressInfo );
01815                     state = STATE_STATING;
01816                     destinationState = DEST_NOT_STATED;
01817                     q->addSubjob(job);
01818                     return;
01819                 }
01820                 case R_AUTO_SKIP:
01821                     if (isDir)
01822                         m_bAutoSkipDirs = true;
01823                     else
01824                         m_bAutoSkipFiles = true;
01825                     // fall through
01826                 case R_SKIP:
01827                     // Move on to next url
01828                     skipSrc(isDir);
01829                     return;
01830                 case R_OVERWRITE_ALL:
01831                     if (destIsDir)
01832                         m_bOverwriteAllDirs = true;
01833                     else
01834                         m_bOverwriteAllFiles = true;
01835                     break;
01836                 case R_OVERWRITE:
01837                     // Add to overwrite list
01838                     // Note that we add dest, not m_dest.
01839                     // This ensures that when moving several urls into a dir (m_dest),
01840                     // we only overwrite for the current one, not for all.
01841                     // When renaming a single file (m_asMethod), it makes no difference.
01842                     kDebug(7007) << "adding to overwrite list: " << dest.path();
01843                     m_overwriteList.append( dest.path() );
01844                     break;
01845                 default:
01846                     //assert( 0 );
01847                     break;
01848                 }
01849             } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
01850                 // Dest already exists, and job is not interactive -> abort with error
01851                 q->setError( err );
01852                 q->setErrorText( errText );
01853                 q->emitResult();
01854                 return;
01855             }
01856         } else if ( err != KIO::ERR_UNSUPPORTED_ACTION ) {
01857             kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", aborting";
01858             q->setError( err );
01859             q->setErrorText( errText );
01860             q->emitResult();
01861             return;
01862         }
01863         kDebug(7007) << "Couldn't rename" << m_currentSrcURL << "to" << dest << ", reverting to normal way, starting with stat";
01864         //kDebug(7007) << "KIO::stat on" << m_currentSrcURL;
01865         KIO::Job* job = KIO::stat( m_currentSrcURL, StatJob::SourceSide, 2, KIO::HideProgressInfo );
01866         state = STATE_STATING;
01867         q->addSubjob(job);
01868         m_bOnlyRenames = false;
01869     }
01870     else
01871     {
01872         kDebug(7007) << "Renaming succeeded, move on";
01873         ++m_processedFiles;
01874         emit q->copyingDone( q, *m_currentStatSrc, dest, -1 /*mtime unknown, and not needed*/, true, true );
01875         m_successSrcList.append(*m_currentStatSrc);
01876         statNextSrc();
01877     }
01878 }
01879 
01880 void CopyJob::slotResult( KJob *job )
01881 {
01882     Q_D(CopyJob);
01883     //kDebug(7007) << "d->state=" << (int) d->state;
01884     // In each case, what we have to do is :
01885     // 1 - check for errors and treat them
01886     // 2 - removeSubjob(job);
01887     // 3 - decide what to do next
01888 
01889     switch ( d->state ) {
01890         case STATE_STATING: // We were trying to stat a src url or the dest
01891             d->slotResultStating( job );
01892             break;
01893         case STATE_RENAMING: // We were trying to do a direct renaming, before even stat'ing
01894         {
01895             d->slotResultRenaming( job );
01896             break;
01897         }
01898         case STATE_LISTING: // recursive listing finished
01899             //kDebug(7007) << "totalSize:" << (unsigned int) d->m_totalSize << "files:" << d->files.count() << "d->dirs:" << d->dirs.count();
01900             // Was there an error ?
01901             if (job->error())
01902             {
01903                 Job::slotResult( job ); // will set the error and emit result(this)
01904                 return;
01905             }
01906 
01907             removeSubjob( job );
01908             assert ( !hasSubjobs() );
01909 
01910             d->statNextSrc();
01911             break;
01912         case STATE_CREATING_DIRS:
01913             d->slotResultCreatingDirs( job );
01914             break;
01915         case STATE_CONFLICT_CREATING_DIRS:
01916             d->slotResultConflictCreatingDirs( job );
01917             break;
01918         case STATE_COPYING_FILES:
01919             d->slotResultCopyingFiles( job );
01920             break;
01921         case STATE_CONFLICT_COPYING_FILES:
01922             d->slotResultConflictCopyingFiles( job );
01923             break;
01924         case STATE_DELETING_DIRS:
01925             d->slotResultDeletingDirs( job );
01926             break;
01927         case STATE_SETTING_DIR_ATTRIBUTES:
01928             d->slotResultSettingDirAttributes( job );
01929             break;
01930         default:
01931             assert( 0 );
01932     }
01933 }
01934 
01935 void KIO::CopyJob::setDefaultPermissions( bool b )
01936 {
01937     d_func()->m_defaultPermissions = b;
01938 }
01939 
01940 KIO::CopyJob::CopyMode KIO::CopyJob::operationMode() const
01941 {
01942     return d_func()->m_mode;
01943 }
01944 
01945 void KIO::CopyJob::setAutoSkip(bool autoSkip)
01946 {
01947     d_func()->m_bAutoSkipFiles = autoSkip;
01948     d_func()->m_bAutoSkipDirs = autoSkip;
01949 }
01950 
01951 void KIO::CopyJob::setWriteIntoExistingDirectories(bool overwriteAll) // #65926
01952 {
01953     d_func()->m_bOverwriteAllDirs = overwriteAll;
01954 }
01955 
01956 CopyJob *KIO::copy(const KUrl& src, const KUrl& dest, JobFlags flags)
01957 {
01958     //kDebug(7007) << "src=" << src << "dest=" << dest;
01959     KUrl::List srcList;
01960     srcList.append( src );
01961     return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, false, flags);
01962 }
01963 
01964 CopyJob *KIO::copyAs(const KUrl& src, const KUrl& dest, JobFlags flags)
01965 {
01966     //kDebug(7007) << "src=" << src << "dest=" << dest;
01967     KUrl::List srcList;
01968     srcList.append( src );
01969     return CopyJobPrivate::newJob(srcList, dest, CopyJob::Copy, true, flags);
01970 }
01971 
01972 CopyJob *KIO::copy( const KUrl::List& src, const KUrl& dest, JobFlags flags )
01973 {
01974     //kDebug(7007) << src << dest;
01975     return CopyJobPrivate::newJob(src, dest, CopyJob::Copy, false, flags);
01976 }
01977 
01978 CopyJob *KIO::move(const KUrl& src, const KUrl& dest, JobFlags flags)
01979 {
01980     //kDebug(7007) << src << dest;
01981     KUrl::List srcList;
01982     srcList.append( src );
01983     return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, false, flags);
01984 }
01985 
01986 CopyJob *KIO::moveAs(const KUrl& src, const KUrl& dest, JobFlags flags)
01987 {
01988     //kDebug(7007) << src << dest;
01989     KUrl::List srcList;
01990     srcList.append( src );
01991     return CopyJobPrivate::newJob(srcList, dest, CopyJob::Move, true, flags);
01992 }
01993 
01994 CopyJob *KIO::move( const KUrl::List& src, const KUrl& dest, JobFlags flags)
01995 {
01996     //kDebug(7007) << src << dest;
01997     return CopyJobPrivate::newJob(src, dest, CopyJob::Move, false, flags);
01998 }
01999 
02000 CopyJob *KIO::link(const KUrl& src, const KUrl& destDir, JobFlags flags)
02001 {
02002     KUrl::List srcList;
02003     srcList.append( src );
02004     return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
02005 }
02006 
02007 CopyJob *KIO::link(const KUrl::List& srcList, const KUrl& destDir, JobFlags flags)
02008 {
02009     return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
02010 }
02011 
02012 CopyJob *KIO::linkAs(const KUrl& src, const KUrl& destDir, JobFlags flags )
02013 {
02014     KUrl::List srcList;
02015     srcList.append( src );
02016     return CopyJobPrivate::newJob(srcList, destDir, CopyJob::Link, false, flags);
02017 }
02018 
02019 CopyJob *KIO::trash(const KUrl& src, JobFlags flags)
02020 {
02021     KUrl::List srcList;
02022     srcList.append( src );
02023     return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
02024 }
02025 
02026 CopyJob *KIO::trash(const KUrl::List& srcList, JobFlags flags)
02027 {
02028     return CopyJobPrivate::newJob(srcList, KUrl( "trash:/" ), CopyJob::Move, false, flags);
02029 }
02030 
02031 #include "copyjob.moc"

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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
  • KUtils
  • Nepomuk
  • Plasma
  •     Sodep
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal