jobs.cpp 8.43 KB
Newer Older
1 2
/*
 * Copyright (c) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
3
 * Copyright (c) 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
4
 * Copyright (c) 2009-2012 Raphael Kubo da Costa <rakuco@FreeBSD.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
27

28 29
#include "jobs.h"

30
#include <QThread>
31 32 33

#include <KDebug>
#include <KLocale>
34

35 36
//#define DEBUG_RACECONDITION

37 38
namespace Kerfuffle
{
39

40 41 42
class Job::Private : public QThread
{
public:
43 44 45 46 47 48 49
    Private(Job *job, QObject *parent = 0)
        : QThread(parent)
        , q(job)
    {
        connect(q, SIGNAL(result(KJob*)), SLOT(quit()));
    }

50 51
    virtual void run();

52
private:
53 54 55 56 57
    Job *q;
};

void Job::Private::run()
{
58
    q->doWork();
59

60 61 62
    if (q->isRunning()) {
        exec();
    }
63 64 65 66 67 68

#ifdef DEBUG_RACECONDITION
    QThread::sleep(2);
#endif
}

69
Job::Job(ReadOnlyArchiveInterface *interface, QObject *parent)
70
    : KJob(parent)
71
    , m_archiveInterface(interface)
72 73
    , m_isRunning(false)
    , d(new Private(this))
74 75 76 77 78 79 80 81 82 83 84 85
{
    static bool onlyOnce = false;
    if (!onlyOnce) {
        qRegisterMetaType<QPair<QString, QString> >("QPair<QString,QString>");
        onlyOnce = true;
    }

    setCapabilities(KJob::Killable);
}

Job::~Job()
{
86 87
    if (d->isRunning()) {
        d->wait();
88 89
    }

90
    delete d;
91
}
92

93 94 95 96 97
ReadOnlyArchiveInterface *Job::archiveInterface()
{
    return m_archiveInterface;
}

98 99 100 101 102
bool Job::isRunning() const
{
    return m_isRunning;
}

103 104
void Job::start()
{
105
    m_isRunning = true;
106
    d->start();
107 108
}

109 110 111 112 113 114
void Job::emitResult()
{
    m_isRunning = false;
    KJob::emitResult();
}

115 116 117 118 119 120 121
void Job::connectToArchiveInterfaceSignals()
{
    connect(archiveInterface(), SIGNAL(error(QString,QString)), SLOT(onError(QString,QString)));
    connect(archiveInterface(), SIGNAL(entry(ArchiveEntry)), SLOT(onEntry(ArchiveEntry)));
    connect(archiveInterface(), SIGNAL(entryRemoved(QString)), SLOT(onEntryRemoved(QString)));
    connect(archiveInterface(), SIGNAL(progress(double)), SLOT(onProgress(double)));
    connect(archiveInterface(), SIGNAL(info(QString)), SLOT(onInfo(QString)));
122
    connect(archiveInterface(), SIGNAL(finished(bool)), SLOT(onFinished(bool)), Qt::DirectConnection);
123 124 125
    connect(archiveInterface(), SIGNAL(userQuery(Query*)), SLOT(onUserQuery(Query*)));
}

126 127
void Job::onError(const QString & message, const QString & details)
{
128
    Q_UNUSED(details)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    setError(1);
    setErrorText(message);
}

void Job::onEntry(const ArchiveEntry & archiveEntry)
{
    emit newEntry(archiveEntry);
}

void Job::onProgress(double value)
{
    setPercent(static_cast<unsigned long>(100.0*value));
}

void Job::onInfo(const QString& info)
{
    emit infoMessage(this, info);
}

void Job::onEntryRemoved(const QString & path)
{
    emit entryRemoved(path);
}

void Job::onFinished(bool result)
{
156
    kDebug() << result;
157

158
    archiveInterface()->disconnect(this);
159 160 161 162 163 164 165 166 167 168 169

    emitResult();
}

void Job::onUserQuery(Query *query)
{
    emit userQuery(query);
}

bool Job::doKill()
{
170
    kDebug();
171
    bool ret = archiveInterface()->doKill();
172
    if (!ret) {
173
        kDebug() << "Killing does not seem to be supported here.";
174
    }
175 176 177 178
    return ret;
}

ListJob::ListJob(ReadOnlyArchiveInterface *interface, QObject *parent)
179 180 181 182
    : Job(interface, parent)
    , m_isSingleFolderArchive(true)
    , m_isPasswordProtected(false)
    , m_extractedFilesSize(0)
183
{
Laurent Montel's avatar
Laurent Montel committed
184
    connect(this, &ListJob::newEntry, this, &ListJob::onNewEntry);
185 186 187 188 189
}

void ListJob::doWork()
{
    emit description(this, i18n("Loading archive..."));
190
    connectToArchiveInterfaceSignals();
191
    bool ret = archiveInterface()->list();
192

193
    if (!archiveInterface()->waitForFinishedSignal()) {
194
        onFinished(ret);
195
    }
196 197
}

198
qlonglong ListJob::extractedFilesSize() const
199 200 201 202
{
    return m_extractedFilesSize;
}

203
bool ListJob::isPasswordProtected() const
204 205 206 207
{
    return m_isPasswordProtected;
}

208
bool ListJob::isSingleFolderArchive() const
209 210 211 212
{
    return m_isSingleFolderArchive;
}

213 214 215 216 217
void ListJob::onNewEntry(const ArchiveEntry& entry)
{
    m_extractedFilesSize += entry[ Size ].toLongLong();
    m_isPasswordProtected |= entry [ IsPasswordProtected ].toBool();

218 219
    if (m_isSingleFolderArchive) {
        const QString fileName(entry[FileName].toString());
220
        const QString basePath(fileName.split(QLatin1Char( '/' )).at(0));
221

222 223 224
        if (m_basePath.isEmpty()) {
            m_basePath = basePath;
            m_subfolderName = basePath;
225
        } else {
226 227 228 229
            if (m_basePath != basePath) {
                m_isSingleFolderArchive = false;
                m_subfolderName.clear();
            }
230 231 232 233
        }
    }
}

234
QString ListJob::subfolderName() const
235 236 237 238
{
    return m_subfolderName;
}

239
ExtractJob::ExtractJob(const QVariantList& files, const QString& destinationDir, ExtractionOptions options, ReadOnlyArchiveInterface *interface, QObject *parent)
240 241 242 243
    : Job(interface, parent)
    , m_files(files)
    , m_destinationDir(destinationDir)
    , m_options(options)
244
{
245
    setDefaultOptions();
246 247 248 249 250 251 252 253 254 255 256 257
}

void ExtractJob::doWork()
{
    QString desc;
    if (m_files.count() == 0) {
        desc = i18n("Extracting all files");
    } else {
        desc = i18np("Extracting one file", "Extracting %1 files", m_files.count());
    }
    emit description(this, desc);

258
    connectToArchiveInterfaceSignals();
259

260 261 262 263
    kDebug() << "Starting extraction with selected files:"
             << m_files
             << "Destination dir:" << m_destinationDir
             << "Options:" << m_options;
264

265
    bool ret = archiveInterface()->copyFiles(m_files, m_destinationDir, m_options);
266

267
    if (!archiveInterface()->waitForFinishedSignal()) {
268
        onFinished(ret);
269
    }
270 271
}

272
void ExtractJob::setDefaultOptions()
273
{
274 275 276 277 278 279 280 281 282
    ExtractionOptions defaultOptions;

    defaultOptions[QLatin1String("PreservePaths")] = false;

    ExtractionOptions::const_iterator it = defaultOptions.constBegin();
    for (; it != defaultOptions.constEnd(); ++it) {
        if (!m_options.contains(it.key())) {
            m_options[it.key()] = it.value();
        }
283 284 285
    }
}

286 287 288 289 290
QString ExtractJob::destinationDirectory() const
{
    return m_destinationDir;
}

291 292 293 294 295
ExtractionOptions ExtractJob::extractionOptions() const
{
    return m_options;
}

296
AddJob::AddJob(const QStringList& files, const CompressionOptions& options , ReadWriteArchiveInterface *interface, QObject *parent)
297 298 299
    : Job(interface, parent)
    , m_files(files)
    , m_options(options)
300 301 302 303 304 305 306 307
{
}

void AddJob::doWork()
{
    emit description(this, i18np("Adding a file", "Adding %1 files", m_files.count()));

    ReadWriteArchiveInterface *m_writeInterface =
308
        qobject_cast<ReadWriteArchiveInterface*>(archiveInterface());
309 310 311

    Q_ASSERT(m_writeInterface);

312
    connectToArchiveInterfaceSignals();
313 314
    bool ret = m_writeInterface->addFiles(m_files, m_options);

315
    if (!archiveInterface()->waitForFinishedSignal()) {
316
        onFinished(ret);
317
    }
318 319
}

320
DeleteJob::DeleteJob(const QVariantList& files, ReadWriteArchiveInterface *interface, QObject *parent)
321 322
    : Job(interface, parent)
    , m_files(files)
323 324 325 326 327 328 329 330
{
}

void DeleteJob::doWork()
{
    emit description(this, i18np("Deleting a file from the archive", "Deleting %1 files", m_files.count()));

    ReadWriteArchiveInterface *m_writeInterface =
331
        qobject_cast<ReadWriteArchiveInterface*>(archiveInterface());
332 333 334

    Q_ASSERT(m_writeInterface);

335
    connectToArchiveInterfaceSignals();
336 337
    int ret = m_writeInterface->deleteFiles(m_files);

338
    if (!archiveInterface()->waitForFinishedSignal()) {
339
        onFinished(ret);
340
    }
341
}
342

343
} // namespace Kerfuffle
344

345