File: krjob.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (148 lines) | stat: -rw-r--r-- 4,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
    SPDX-FileCopyrightText: 2016-2022 Krusader Krew <https://krusader.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "krjob.h"

#include <KIO/DeleteJob>
#include <KIO/FileUndoManager>
#include <KLocalizedString>

KrJob *KrJob::createCopyJob(KIO::CopyJob::CopyMode mode, const QList<QUrl> &src, const QUrl &destination, KIO::JobFlags flags)
{
    return createKrCopyJob(mode, src, destination, flags);
}

KrJob *KrJob::createDeleteJob(const QList<QUrl> &urls, bool moveToTrash)
{
    const Type type = moveToTrash ? Trash : Delete;
    const bool oneFile = urls.length() == 1;
    const QString description = moveToTrash
        ? (oneFile ? i18n("Move %1 to trash", urls.first().toDisplayString()) : i18np("Move %1 file to trash", "Move %1 files to trash", urls.length()))
        : (oneFile ? i18n("Delete %1", urls.first().toDisplayString()) : i18np("Delete %1 file", "Delete %1 files", urls.length()));

    return new KrJob(type, urls, QUrl(), KIO::DefaultFlags /*dummy*/, description);
}

KrJob *KrJob::createDropJob(KIO::DropJob *dropJob, KIO::CopyJob *job)
{
    // NOTE: DrobJobs are internally recorded
    // KIO::FileUndoManager::self()->recordCopyJob(job);
    return createKrCopyJob(job->operationMode(), job->srcUrls(), job->destUrl(), KIO::DefaultFlags /*dummy*/, job, dropJob);
}

KrJob *KrJob::createKrCopyJob(KIO::CopyJob::CopyMode mode,
                              const QList<QUrl> &src,
                              const QUrl &destination,
                              KIO::JobFlags flags,
                              KIO::CopyJob *job,
                              KIO::DropJob *dropJob)
{
    Type type(Copy);
    QString description;
    switch (mode) {
    case KIO::CopyJob::Copy:
        type = Copy;
        description = i18n("Copy to %1", destination.toDisplayString());
        break;
    case KIO::CopyJob::Move:
        type = Move;
        description = i18n("Move to %1", destination.toDisplayString());
        break;
    case KIO::CopyJob::Link:
        type = Link;
        description = i18n("Link to %1", destination.toDisplayString());
        break;
    }

    return new KrJob(type, src, destination, flags, description, job, dropJob);
}

KrJob::KrJob(Type type,
             const QList<QUrl> &urls,
             const QUrl &dest,
             KIO::JobFlags flags,
             const QString &description,
             KIO::CopyJob *copyJob,
             KIO::DropJob *dropJob)
    : m_type(type)
    , m_urls(urls)
    , m_dest(dest)
    , m_flags(flags)
    , m_description(description)
    , m_job(copyJob)
    , m_dropJob(dropJob)
{
    if (m_job) {
        connectStartedJob();
    }
}

void KrJob::start()
{
    if (m_job) {
        // job was already started
        m_job->resume();
        return;
    }

    switch (m_type) {
    case Copy: {
        KIO::CopyJob *job = KIO::copy(m_urls, m_dest, m_flags);
        KIO::FileUndoManager::self()->recordCopyJob(job);
        m_job = job;
        break;
    }
    case Move: {
        KIO::CopyJob *job = KIO::move(m_urls, m_dest, m_flags);
        KIO::FileUndoManager::self()->recordCopyJob(job);
        m_job = job;
        break;
    }
    case Link: {
        KIO::CopyJob *job = KIO::link(m_urls, m_dest, m_flags);
        KIO::FileUndoManager::self()->recordCopyJob(job);
        m_job = job;
        break;
    }
    case Trash: {
        m_job = KIO::trash(m_urls);
        KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, m_urls, QUrl("trash:/"), m_job);
        break;
    }
    case Delete:
        m_job = KIO::del(m_urls);
    }

    connectStartedJob();

    emit started(m_job);
}

void KrJob::connectStartedJob()
{
    connect(m_job, &KIO::Job::finished, this, [=]() {
        emit terminated(this);
        deleteLater();
    });
}

void KrJob::cancel()
{
    if (m_dropJob) { // kill the parent; killing the copy subjob does not kill the parent dropjob
        m_dropJob->kill();
    } else if (m_job) {
        m_job->kill();
    } else {
        emit terminated(this);
        deleteLater();
    }
}

void KrJob::pause()
{
    if (m_job)
        m_job->suspend();
}