jobs.h 7.25 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 30 31 32
#ifndef JOBS_H
#define JOBS_H

#include "kerfuffle_export.h"
#include "archiveinterface.h"
33
#include "archive_kerfuffle.h"
34
#include "queries.h"
35 36

#include <KJob>
37

38
#include <QElapsedTimer>
39
#include <QTemporaryDir>
40 41 42

namespace Kerfuffle
{
43

44
class KERFUFFLE_EXPORT Job : public KJob
45 46 47 48 49 50
{
    Q_OBJECT

public:
    void start();

51 52
    bool isRunning() const;

53
protected:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
54
    Job(ReadOnlyArchiveInterface *interface);
55 56
    virtual ~Job();
    virtual bool doKill();
57
    virtual void emitResult();
58

59
    ReadOnlyArchiveInterface *archiveInterface();
60

61 62
    void connectToArchiveInterfaceSignals();

63 64 65
public slots:
    virtual void doWork() = 0;

66
protected slots:
67
    virtual void onCancelled();
68 69 70 71 72 73 74 75
    virtual void onError(const QString &message, const QString &details);
    virtual void onInfo(const QString &info);
    virtual void onEntry(const ArchiveEntry &archiveEntry);
    virtual void onProgress(double progress);
    virtual void onEntryRemoved(const QString &path);
    virtual void onFinished(bool result);
    virtual void onUserQuery(Query *query);

76 77 78 79
signals:
    void entryRemoved(const QString & entry);
    void error(const QString& errorMessage, const QString& details);
    void newEntry(const ArchiveEntry &);
80
    void userQuery(Kerfuffle::Query*);
81 82

private:
83 84
    ReadOnlyArchiveInterface *m_archiveInterface;

85
    bool m_isRunning;
86
    QElapsedTimer jobTimer;
87

88 89
    class Private;
    Private * const d;
90 91
};

92
class KERFUFFLE_EXPORT ListJob : public Job
93 94 95 96
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
97
    explicit ListJob(ReadOnlyArchiveInterface *interface);
98

99 100 101 102
    qlonglong extractedFilesSize() const;
    bool isPasswordProtected() const;
    bool isSingleFolderArchive() const;
    QString subfolderName() const;
103

104
public slots:
105
    virtual void doWork() Q_DECL_OVERRIDE;
106

107 108 109 110
private:
    bool m_isSingleFolderArchive;
    bool m_isPasswordProtected;
    QString m_subfolderName;
111
    QString m_basePath;
112
    qlonglong m_extractedFilesSize;
113 114
    qlonglong m_dirCount;
    qlonglong m_filesCount;
115 116 117 118 119

private slots:
    void onNewEntry(const ArchiveEntry&);
};

120
class KERFUFFLE_EXPORT ExtractJob : public Job
121 122 123 124
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
125
    ExtractJob(const QVariantList& files, const QString& destinationDir, const ExtractionOptions& options, ReadOnlyArchiveInterface *interface);
126

127
    QString destinationDirectory() const;
128
    ExtractionOptions extractionOptions() const;
129

130
public slots:
131
    virtual void doWork() Q_DECL_OVERRIDE;
132 133

private:
134
    // TODO: Maybe this should be a method if ExtractionOptions were a class?
135
    void setDefaultOptions();
136

137
    QVariantList m_files;
138 139 140 141
    QString m_destinationDir;
    ExtractionOptions m_options;
};

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/**
 * Abstract base class for jobs that extract a single file to a temporary dir.
 * It's not possible to pass extraction options and paths will be always preserved.
 * The only option that the job needs to know is whether the file is password protected.
 */
class KERFUFFLE_EXPORT TempExtractJob : public Job
{
    Q_OBJECT

public:
    TempExtractJob(const QString& file, bool passwordProtectedHint, ReadOnlyArchiveInterface *interface);

    /**
     * @return The absolute path of the extracted file.
     * The path is validated in order to prevent directory traversal attacks.
     */
    QString validatedFilePath() const;

    ExtractionOptions extractionOptions() const;

public slots:
    virtual void doWork() Q_DECL_OVERRIDE;

private:
    virtual QString extractionDir() const = 0;

    QString m_file;
    bool m_passwordProtectedHint;
};

/**
 * This TempExtractJob can be used to preview a file.
 * The temporary extraction directory will be deleted upon job's completion.
 */
class KERFUFFLE_EXPORT PreviewJob : public TempExtractJob
{
    Q_OBJECT

public:
    PreviewJob(const QString& file, bool passwordProtectedHint, ReadOnlyArchiveInterface *interface);

private:
    QString extractionDir() const Q_DECL_OVERRIDE;

    QTemporaryDir m_tmpExtractDir;
};

/**
 * This TempExtractJob can be used to open a file in its dedicated application.
 * For this reason, the temporary extraction directory will NOT be deleted upon job's completion.
 */
class KERFUFFLE_EXPORT OpenJob : public TempExtractJob
{
    Q_OBJECT

public:
    OpenJob(const QString& file, bool passwordProtectedHint, ReadOnlyArchiveInterface *interface);

    /**
     * @return The temporary dir used for the extraction.
     * It is safe to delete this pointer in order to remove the directory.
     */
    QTemporaryDir *tempDir() const;

private:
    QString extractionDir() const Q_DECL_OVERRIDE;

    QTemporaryDir *m_tmpExtractDir;
};

class KERFUFFLE_EXPORT OpenWithJob : public OpenJob
{
    Q_OBJECT

public:
    OpenWithJob(const QString& file, bool passwordProtectedHint, ReadOnlyArchiveInterface *interface);
};

220
class KERFUFFLE_EXPORT AddJob : public Job
221 222 223 224
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
225
    AddJob(const QStringList& files, const CompressionOptions& options, ReadWriteArchiveInterface *interface);
226 227

public slots:
228
    virtual void doWork() Q_DECL_OVERRIDE;
229

230 231 232
protected slots:
    virtual void onFinished(bool result) Q_DECL_OVERRIDE;

233
private:
234
    QString m_oldWorkingDir;
235 236 237 238
    QStringList m_files;
    CompressionOptions m_options;
};

239
class KERFUFFLE_EXPORT DeleteJob : public Job
240 241 242 243
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
244
    DeleteJob(const QVariantList& files, ReadWriteArchiveInterface *interface);
245 246

public slots:
247
    virtual void doWork() Q_DECL_OVERRIDE;
248 249

private:
250
    QVariantList m_files;
251
};
252

253 254 255 256 257
class KERFUFFLE_EXPORT CommentJob : public Job
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
258
    CommentJob(const QString& comment, ReadWriteArchiveInterface *interface);
259 260 261 262 263 264 265 266

public slots:
    virtual void doWork() Q_DECL_OVERRIDE;

private:
    QString m_comment;
};

Ragnar Thomsen's avatar
Ragnar Thomsen committed
267 268 269 270 271
class KERFUFFLE_EXPORT TestJob : public Job
{
    Q_OBJECT

public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
272
    TestJob(ReadOnlyArchiveInterface *interface);
Ragnar Thomsen's avatar
Ragnar Thomsen committed
273 274 275 276 277 278 279
    bool testSucceeded();

public slots:
    virtual void doWork() Q_DECL_OVERRIDE;

private slots:
    virtual void onTestSuccess();
280

Ragnar Thomsen's avatar
Ragnar Thomsen committed
281 282 283 284
private:
    bool m_testSuccess;

};
285

286 287 288
} // namespace Kerfuffle

#endif // JOBS_H