jobs.cpp 7.38 KB
Newer Older
1 2
/*
 * Copyright (c) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
3 4
 * Copyright (c) 2008-2009 Harald Hvaal <haraldhv )@@@( stud(dot)ntnu.no>
 * Copyright (c) 2009 Raphael Kubo da Costa <kubito@gmail.com>
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
#include "jobs.h"
29
#include "threading.h"
30

31
#include <QApplication>
32
#include <QDir>
33
#include <QTimer>
34 35 36

#include <KDebug>
#include <KLocale>
37

38
//#define KERFUFFLE_NOJOBTHREADING
Henrique Pinto's avatar
Henrique Pinto committed
39

40 41
namespace Kerfuffle
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
Job::Job(ReadOnlyArchiveInterface *interface, QObject *parent)
        : KJob(parent)
        , m_interface(interface)
        , m_workerThread(0)
{
    static bool onlyOnce = false;
    if (!onlyOnce) {
        qRegisterMetaType<QPair<QString, QString> >("QPair<QString,QString>");
        onlyOnce = true;
    }

    setCapabilities(KJob::Killable);
}

Job::~Job()
{
58
#ifndef KERFUFFLE_NOJOBTHREADING
59 60 61
    m_workerThread->wait();
    delete m_workerThread;
    m_workerThread = 0;
62
#endif
63
}
64

65 66
void Job::start()
{
67
#ifdef KERFUFFLE_NOJOBTHREADING
68
    QTimer::singleShot(0, this, SLOT(doWork()));
69
#else
70 71
    m_workerThread = new ThreadExecution(this);
    m_workerThread->start();
72
#endif
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
}

void Job::onError(const QString & message, const QString & details)
{
    Q_UNUSED(details);

    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)
{
105
    kDebug();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    m_interface->removeObserver(this);

    setError(!result);

    if (errorString().isEmpty())
        setErrorText(i18n("An error occurred while performing the operation."));

    emitResult();
}

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

bool Job::doKill()
{
123
    kDebug();
124 125
    bool ret = m_interface->doKill();
    if (!ret)
126
        kDebug() << "Killing does not seem to be supported here.";
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    return ret;
}

ListJob::ListJob(ReadOnlyArchiveInterface *interface, QObject *parent)
        : Job(interface, parent),
        m_isSingleFolderArchive(true),
        m_isPasswordProtected(false),
        m_extractedFilesSize(0)
{
    connect(this, SIGNAL(newEntry(const ArchiveEntry&)),
            this, SLOT(onNewEntry(const ArchiveEntry&)));
}

void ListJob::doWork()
{
    emit description(this, i18n("Loading archive..."));
    m_interface->registerObserver(this);
    bool ret = m_interface->list();

    if (!m_interface->waitForFinishedSignal()) m_interface->finished(ret);
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
qlonglong ListJob::extractedFilesSize()
{
    return m_extractedFilesSize;
}

bool ListJob::isPasswordProtected()
{
    return m_isPasswordProtected;
}

bool ListJob::isSingleFolderArchive()
{
    return m_isSingleFolderArchive;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
void ListJob::onNewEntry(const ArchiveEntry& entry)
{
    m_extractedFilesSize += entry[ Size ].toLongLong();
    m_isPasswordProtected |= entry [ IsPasswordProtected ].toBool();

    QString filename = entry[ FileName ].toString();
    QString fileBaseRoot = filename.split(QDir::separator()).first();

    if (m_previousEntry.isEmpty()) { // Set the root path of the filename
        m_previousEntry = fileBaseRoot;
        m_subfolderName = fileBaseRoot;
        m_isSingleFolderArchive = entry[ IsDirectory ].toBool();
    } else {
        if (m_previousEntry != fileBaseRoot) {
            m_isSingleFolderArchive = false;
            m_subfolderName.clear();
        } else {
            // The state may change only if the folder's files were added before itself
            if (entry[ IsDirectory ].toBool())
                m_isSingleFolderArchive = true;
        }
    }
}

188 189 190 191 192
QString ListJob::subfolderName() 
{
    return m_subfolderName;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
ExtractJob::ExtractJob(const QList<QVariant>& files, const QString& destinationDir,
                       ExtractionOptions options, ReadOnlyArchiveInterface *interface, QObject *parent)
        : Job(interface,  parent), m_files(files), m_destinationDir(destinationDir), m_options(options)
{
}

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);

    m_interface->registerObserver(this);

    fillInDefaultValues(m_options);

213
    kDebug() << "Starting extraction with selected files "
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    << m_files
    << " Destination dir " << m_destinationDir
    << " And options " << m_options
    ;

    bool ret = m_interface->copyFiles(m_files, m_destinationDir, m_options);

    if (!m_interface->waitForFinishedSignal()) m_interface->finished(ret);
}

void ExtractJob::fillInDefaultValues(ExtractionOptions& options)
{
    if (!options.contains("PreservePaths")) {
        options["PreservePaths"] = false;
    }
}

AddJob::AddJob(const QStringList& files, const CompressionOptions& options , ReadWriteArchiveInterface *interface, QObject *parent)
        : Job(interface, parent), m_files(files), m_options(options)
{
234
    kDebug();
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
}

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

    ReadWriteArchiveInterface *m_writeInterface =
        qobject_cast<ReadWriteArchiveInterface*>
        (m_interface);

    Q_ASSERT(m_writeInterface);

    m_writeInterface->registerObserver(this);
    bool ret = m_writeInterface->addFiles(m_files, m_options);

    if (!m_interface->waitForFinishedSignal()) m_interface->finished(ret);
}

DeleteJob::DeleteJob(const QList<QVariant>& files, ReadWriteArchiveInterface *interface, QObject *parent)
        : Job(interface, parent), m_files(files)
{
}

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

    ReadWriteArchiveInterface *m_writeInterface =
        qobject_cast<ReadWriteArchiveInterface*>
        (m_interface);

    Q_ASSERT(m_writeInterface);

    m_writeInterface->registerObserver(this);
    int ret = m_writeInterface->deleteFiles(m_files);

    if (!m_interface->waitForFinishedSignal()) m_interface->finished(ret);
}
273
} // namespace Kerfuffle
274 275

#include "jobs.moc"