batchextract.h 6.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * ark -- archiver for the KDE project
 *
 * Copyright (C) 2008 Harald Hvaal <haraldhv (at@at) stud.ntnu.no>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef BATCHEXTRACT_H
#define BATCHEXTRACT_H

25
#include "kerfuffle_export.h"
26

27
28
29
#include <kcompositejob.h>
#include <KUrl>

30
31
32
#include <QtCore/QMap>
#include <QtCore/QPair>
#include <QtCore/QString>
33
#include <QtCore/QStringList>
34

35
36
37
38
39
40
41
42
/**
 * This class schedules the extraction of all given compressed archives.
 *
 * Like AddToArchive, this class does not need the GUI to be active, and
 * provides the functionality avaliable from the --batch command-line option.
 *
 * @author Harald Hvaal <haraldhv@stud.ntnu.no>
 */
43
namespace Kerfuffle
44
{
45
46
class Archive;
class Query;
47

48
49
50
class KERFUFFLE_EXPORT BatchExtract : public KCompositeJob
{
    Q_OBJECT
51

52
public:
53
54
55
    /**
     * Creates a new BatchExtract object.
     */
56
    BatchExtract();
57
58
59
60

    /**
     * Destroys a BatchExtract object.
     */
61
    virtual ~BatchExtract();
62

63
64
65
66
67
68
69
70
    /**
     * Creates an ExtractJob for the given @p archive and put it on the queue.
     *
     * @param archive           The archive that will be extracted.
     * @param destinationFolder The location the archive will be extracted.
     *
     * @see setAutoSubfolder
     */
71
    void addExtraction(Archive* archive, QString destinationFolder);
72
73
74
75
76
77
78

    /**
     * Starts the extraction of all files.
     *
     * Each extraction job is started after the last one finishes.
     * The jobs are executed in the order they were added via addInput.
     */
79
    void start();
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    /**
     * Whether to automatically create a folder inside the destination
     * directory if the archive has more than one directory or file
     * at top level.
     *
     * @return @c true  Create the subdirectory automatically.
     * @return @c false Do not create the subdirectory automatically.
     */
    bool autoSubfolder();

    /**
     * Set whether a folder should be created when necessary so
     * the archive is extracted to it.
     *
     * If set to @c true, when the archive does not consist of a
     * single folder with the other files and directories inside,
     * a directory will be automatically created inside the destination
     * directory and the archive will be extracted there.
     *
     * @param value Whether to create this directory automatically
     *              when needed.
     */
103
    void setAutoSubfolder(bool value);
104
105
106
107
108
109
110
111
112
113
114

    /**
     * Adds a file to the list of files that will be extracted.
     *
     * @param url The file that will be added to the list.
     *
     * @return @c true  The file exists and a suitable plugin
     *                  could be found for it.
     * @return @c false The file does not exist or a suitable
     *                  plugin could not be found.
     */
115
    bool addInput(const KUrl& url);
116
117
118
119
120
121
122

    /**
     * Shows the extract options dialog before extracting the files.
     *
     * @return @c true  The user has set some options and clicked OK.
     * @return @c false The user has canceled extraction.
     */
123
    bool showExtractDialog();
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    /**
     * Returns the destination directory where the archives
     * will be extracted to.
     *
     * @return The destination directory.
     */
    QString destinationFolder();

    /**
     * Sets the directory the archives will be extracted to.
     * If @c setSubfolder has been used, the final destination
     * directory will be the concatenation of both.
     *
     * @param folder The directory that will be used.
     */
140
    void setDestinationFolder(QString folder);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    /**
     * Returns the subdirectory into which the archives
     * will be extracted, if it has been set.
     *
     * @return The subdirectory that will be used, or an
     *         empty @c QString if none has been set.
     */
    QString subfolder();

    /**
     * Force the creation of a subdirectory inside the destination
     * directory, so that the archives are extracted into it.
     *
     * @param subfolder The subdirectory that will be created.
     */
157
    void setSubfolder(QString subfolder);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

    /**
     * Whether all files should be extracted to the same directory,
     * even if they're in different directories in the archive.
     *
     * This is also known as "flat" extraction.
     *
     * @return @c true  Paths should be preserved.
     * @return @c false Paths should be ignored.
     */
    bool preservePaths();

    /**
     * Set whether paths should be preserved during extraction.
     *
     * When it is set to false, all files are extracted to a single
     * directory, regardless of their hierarchy in the archive.
     *
     * @param value Whether to preserve paths.
     */
178
    void setPreservePaths(bool value);
179

180
private slots:
181
182
183
    /**
     * Updates the percentage of the job that has been completed.
     */
184
    void forwardProgress(KJob *job, unsigned long percent);
185
186
187
188
189

    /**
     * Shows a dialog with a list of all the files that could not
     * be successfully extracted.
     */
190
    void showFailedFiles();
191
192
193
194
195
196

    /**
     * Shows an error message if the current job hasn't finished
     * successfully, and advances to the next extraction job if
     * there are more.
     */
197
    void slotResult(KJob *job);
198
199
200
201

    /**
     * Shows a query dialog, which may happen when a file already exists.
     */
202
    void slotUserQuery(Query *query);
203

204
private:
205
206
207
    int m_initialJobCount;
    QMap<KJob*, QPair<QString, QString> > m_fileNames;
    bool m_autoSubfolders;
208

209
210
    QList<Archive*> m_inputs;
    QString m_destinationFolder;
211
    QStringList m_failedFiles;
212
    QString m_subfolder;
213
214
    bool m_preservePaths;
};
215
}
216
217

#endif // BATCHEXTRACT_H