File indexing completed on 2024-04-28 17:06:06
0001 /* 0002 SPDX-FileCopyrightText: 2016-2022 Krusader Krew <https://krusader.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "krjob.h" 0008 0009 #include <KI18n/KLocalizedString> 0010 #include <KIO/DeleteJob> 0011 #include <KIO/FileUndoManager> 0012 0013 KrJob *KrJob::createCopyJob(KIO::CopyJob::CopyMode mode, const QList<QUrl> &src, const QUrl &destination, KIO::JobFlags flags) 0014 { 0015 return createKrCopyJob(mode, src, destination, flags); 0016 } 0017 0018 KrJob *KrJob::createDeleteJob(const QList<QUrl> &urls, bool moveToTrash) 0019 { 0020 const Type type = moveToTrash ? Trash : Delete; 0021 const bool oneFile = urls.length() == 1; 0022 const QString description = moveToTrash 0023 ? (oneFile ? i18n("Move %1 to trash", urls.first().toDisplayString()) : i18np("Move %1 file to trash", "Move %1 files to trash", urls.length())) 0024 : (oneFile ? i18n("Delete %1", urls.first().toDisplayString()) : i18np("Delete %1 file", "Delete %1 files", urls.length())); 0025 0026 return new KrJob(type, urls, QUrl(), KIO::DefaultFlags /*dummy*/, description); 0027 } 0028 0029 KrJob *KrJob::createDropJob(KIO::DropJob *dropJob, KIO::CopyJob *job) 0030 { 0031 // NOTE: DrobJobs are internally recorded 0032 // KIO::FileUndoManager::self()->recordCopyJob(job); 0033 return createKrCopyJob(job->operationMode(), job->srcUrls(), job->destUrl(), KIO::DefaultFlags /*dummy*/, job, dropJob); 0034 } 0035 0036 KrJob *KrJob::createKrCopyJob(KIO::CopyJob::CopyMode mode, 0037 const QList<QUrl> &src, 0038 const QUrl &destination, 0039 KIO::JobFlags flags, 0040 KIO::CopyJob *job, 0041 KIO::DropJob *dropJob) 0042 { 0043 Type type(Copy); 0044 QString description; 0045 switch (mode) { 0046 case KIO::CopyJob::Copy: 0047 type = Copy; 0048 description = i18n("Copy to %1", destination.toDisplayString()); 0049 break; 0050 case KIO::CopyJob::Move: 0051 type = Move; 0052 description = i18n("Move to %1", destination.toDisplayString()); 0053 break; 0054 case KIO::CopyJob::Link: 0055 type = Link; 0056 description = i18n("Link to %1", destination.toDisplayString()); 0057 break; 0058 } 0059 0060 return new KrJob(type, src, destination, flags, description, job, dropJob); 0061 } 0062 0063 KrJob::KrJob(Type type, 0064 const QList<QUrl> &urls, 0065 const QUrl &dest, 0066 KIO::JobFlags flags, 0067 const QString &description, 0068 KIO::CopyJob *copyJob, 0069 KIO::DropJob *dropJob) 0070 : m_type(type) 0071 , m_urls(urls) 0072 , m_dest(dest) 0073 , m_flags(flags) 0074 , m_description(description) 0075 , m_job(copyJob) 0076 , m_dropJob(dropJob) 0077 { 0078 if (m_job) { 0079 connectStartedJob(); 0080 } 0081 } 0082 0083 void KrJob::start() 0084 { 0085 if (m_job) { 0086 // job was already started 0087 m_job->resume(); 0088 return; 0089 } 0090 0091 switch (m_type) { 0092 case Copy: { 0093 KIO::CopyJob *job = KIO::copy(m_urls, m_dest, m_flags); 0094 KIO::FileUndoManager::self()->recordCopyJob(job); 0095 m_job = job; 0096 break; 0097 } 0098 case Move: { 0099 KIO::CopyJob *job = KIO::move(m_urls, m_dest, m_flags); 0100 KIO::FileUndoManager::self()->recordCopyJob(job); 0101 m_job = job; 0102 break; 0103 } 0104 case Link: { 0105 KIO::CopyJob *job = KIO::link(m_urls, m_dest, m_flags); 0106 KIO::FileUndoManager::self()->recordCopyJob(job); 0107 m_job = job; 0108 break; 0109 } 0110 case Trash: { 0111 m_job = KIO::trash(m_urls); 0112 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, m_urls, QUrl("trash:/"), m_job); 0113 break; 0114 } 0115 case Delete: 0116 m_job = KIO::del(m_urls); 0117 } 0118 0119 connectStartedJob(); 0120 0121 emit started(m_job); 0122 } 0123 0124 void KrJob::connectStartedJob() 0125 { 0126 connect(m_job, &KIO::Job::finished, this, [=]() { 0127 emit terminated(this); 0128 deleteLater(); 0129 }); 0130 } 0131 0132 void KrJob::cancel() 0133 { 0134 if (m_dropJob) { // kill the parent; killing the copy subjob does not kill the parent dropjob 0135 m_dropJob->kill(); 0136 } else if (m_job) { 0137 m_job->kill(); 0138 } else { 0139 emit terminated(this); 0140 deleteLater(); 0141 } 0142 } 0143 0144 void KrJob::pause() 0145 { 0146 if (m_job) 0147 m_job->suspend(); 0148 }