• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

ark

  • sources
  • kde-4.14
  • kdeutils
  • ark
  • kerfuffle
jobs.cpp
Go to the documentation of this file.
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  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "jobs.h"
29 
30 #include <QThread>
31 
32 #include <KDebug>
33 #include <KLocale>
34 
35 //#define DEBUG_RACECONDITION
36 
37 namespace Kerfuffle
38 {
39 
40 class Job::Private : public QThread
41 {
42 public:
43  Private(Job *job, QObject *parent = 0)
44  : QThread(parent)
45  , q(job)
46  {
47  connect(q, SIGNAL(result(KJob*)), SLOT(quit()));
48  }
49 
50  virtual void run();
51 
52 private:
53  Job *q;
54 };
55 
56 void Job::Private::run()
57 {
58  q->doWork();
59 
60  if (q->isRunning()) {
61  exec();
62  }
63 
64 #ifdef DEBUG_RACECONDITION
65  QThread::sleep(2);
66 #endif
67 }
68 
69 Job::Job(ReadOnlyArchiveInterface *interface, QObject *parent)
70  : KJob(parent)
71  , m_archiveInterface(interface)
72  , m_isRunning(false)
73  , d(new Private(this))
74 {
75  static bool onlyOnce = false;
76  if (!onlyOnce) {
77  qRegisterMetaType<QPair<QString, QString> >("QPair<QString,QString>");
78  onlyOnce = true;
79  }
80 
81  setCapabilities(KJob::Killable);
82 }
83 
84 Job::~Job()
85 {
86  if (d->isRunning()) {
87  d->wait();
88  }
89 
90  delete d;
91 }
92 
93 ReadOnlyArchiveInterface *Job::archiveInterface()
94 {
95  return m_archiveInterface;
96 }
97 
98 bool Job::isRunning() const
99 {
100  return m_isRunning;
101 }
102 
103 void Job::start()
104 {
105  m_isRunning = true;
106  d->start();
107 }
108 
109 void Job::emitResult()
110 {
111  m_isRunning = false;
112  KJob::emitResult();
113 }
114 
115 void Job::connectToArchiveInterfaceSignals()
116 {
117  connect(archiveInterface(), SIGNAL(error(QString,QString)), SLOT(onError(QString,QString)));
118  connect(archiveInterface(), SIGNAL(entry(ArchiveEntry)), SLOT(onEntry(ArchiveEntry)));
119  connect(archiveInterface(), SIGNAL(entryRemoved(QString)), SLOT(onEntryRemoved(QString)));
120  connect(archiveInterface(), SIGNAL(progress(double)), SLOT(onProgress(double)));
121  connect(archiveInterface(), SIGNAL(info(QString)), SLOT(onInfo(QString)));
122  connect(archiveInterface(), SIGNAL(finished(bool)), SLOT(onFinished(bool)), Qt::DirectConnection);
123  connect(archiveInterface(), SIGNAL(userQuery(Query*)), SLOT(onUserQuery(Query*)));
124 }
125 
126 void Job::onError(const QString & message, const QString & details)
127 {
128  Q_UNUSED(details)
129 
130  setError(1);
131  setErrorText(message);
132 }
133 
134 void Job::onEntry(const ArchiveEntry & archiveEntry)
135 {
136  emit newEntry(archiveEntry);
137 }
138 
139 void Job::onProgress(double value)
140 {
141  setPercent(static_cast<unsigned long>(100.0*value));
142 }
143 
144 void Job::onInfo(const QString& info)
145 {
146  emit infoMessage(this, info);
147 }
148 
149 void Job::onEntryRemoved(const QString & path)
150 {
151  emit entryRemoved(path);
152 }
153 
154 void Job::onFinished(bool result)
155 {
156  kDebug() << result;
157 
158  archiveInterface()->disconnect(this);
159 
160  emitResult();
161 }
162 
163 void Job::onUserQuery(Query *query)
164 {
165  emit userQuery(query);
166 }
167 
168 bool Job::doKill()
169 {
170  kDebug();
171  bool ret = archiveInterface()->doKill();
172  if (!ret) {
173  kDebug() << "Killing does not seem to be supported here.";
174  }
175  return ret;
176 }
177 
178 ListJob::ListJob(ReadOnlyArchiveInterface *interface, QObject *parent)
179  : Job(interface, parent)
180  , m_isSingleFolderArchive(true)
181  , m_isPasswordProtected(false)
182  , m_extractedFilesSize(0)
183 {
184  connect(this, SIGNAL(newEntry(ArchiveEntry)),
185  this, SLOT(onNewEntry(ArchiveEntry)));
186 }
187 
188 void ListJob::doWork()
189 {
190  emit description(this, i18n("Loading archive..."));
191  connectToArchiveInterfaceSignals();
192  bool ret = archiveInterface()->list();
193 
194  if (!archiveInterface()->waitForFinishedSignal()) {
195  onFinished(ret);
196  }
197 }
198 
199 qlonglong ListJob::extractedFilesSize() const
200 {
201  return m_extractedFilesSize;
202 }
203 
204 bool ListJob::isPasswordProtected() const
205 {
206  return m_isPasswordProtected;
207 }
208 
209 bool ListJob::isSingleFolderArchive() const
210 {
211  return m_isSingleFolderArchive;
212 }
213 
214 void ListJob::onNewEntry(const ArchiveEntry& entry)
215 {
216  m_extractedFilesSize += entry[ Size ].toLongLong();
217  m_isPasswordProtected |= entry [ IsPasswordProtected ].toBool();
218 
219  if (m_isSingleFolderArchive) {
220  const QString fileName(entry[FileName].toString());
221  const QString basePath(fileName.split(QLatin1Char( '/' )).at(0));
222 
223  if (m_basePath.isEmpty()) {
224  m_basePath = basePath;
225  m_subfolderName = basePath;
226  } else {
227  if (m_basePath != basePath) {
228  m_isSingleFolderArchive = false;
229  m_subfolderName.clear();
230  }
231  }
232  }
233 }
234 
235 QString ListJob::subfolderName() const
236 {
237  return m_subfolderName;
238 }
239 
240 ExtractJob::ExtractJob(const QVariantList& files, const QString& destinationDir, ExtractionOptions options, ReadOnlyArchiveInterface *interface, QObject *parent)
241  : Job(interface, parent)
242  , m_files(files)
243  , m_destinationDir(destinationDir)
244  , m_options(options)
245 {
246  setDefaultOptions();
247 }
248 
249 void ExtractJob::doWork()
250 {
251  QString desc;
252  if (m_files.count() == 0) {
253  desc = i18n("Extracting all files");
254  } else {
255  desc = i18np("Extracting one file", "Extracting %1 files", m_files.count());
256  }
257  emit description(this, desc);
258 
259  connectToArchiveInterfaceSignals();
260 
261  kDebug() << "Starting extraction with selected files:"
262  << m_files
263  << "Destination dir:" << m_destinationDir
264  << "Options:" << m_options;
265 
266  bool ret = archiveInterface()->copyFiles(m_files, m_destinationDir, m_options);
267 
268  if (!archiveInterface()->waitForFinishedSignal()) {
269  onFinished(ret);
270  }
271 }
272 
273 void ExtractJob::setDefaultOptions()
274 {
275  ExtractionOptions defaultOptions;
276 
277  defaultOptions[QLatin1String("PreservePaths")] = false;
278 
279  ExtractionOptions::const_iterator it = defaultOptions.constBegin();
280  for (; it != defaultOptions.constEnd(); ++it) {
281  if (!m_options.contains(it.key())) {
282  m_options[it.key()] = it.value();
283  }
284  }
285 }
286 
287 QString ExtractJob::destinationDirectory() const
288 {
289  return m_destinationDir;
290 }
291 
292 ExtractionOptions ExtractJob::extractionOptions() const
293 {
294  return m_options;
295 }
296 
297 AddJob::AddJob(const QStringList& files, const CompressionOptions& options , ReadWriteArchiveInterface *interface, QObject *parent)
298  : Job(interface, parent)
299  , m_files(files)
300  , m_options(options)
301 {
302 }
303 
304 void AddJob::doWork()
305 {
306  emit description(this, i18np("Adding a file", "Adding %1 files", m_files.count()));
307 
308  ReadWriteArchiveInterface *m_writeInterface =
309  qobject_cast<ReadWriteArchiveInterface*>(archiveInterface());
310 
311  Q_ASSERT(m_writeInterface);
312 
313  connectToArchiveInterfaceSignals();
314  bool ret = m_writeInterface->addFiles(m_files, m_options);
315 
316  if (!archiveInterface()->waitForFinishedSignal()) {
317  onFinished(ret);
318  }
319 }
320 
321 DeleteJob::DeleteJob(const QVariantList& files, ReadWriteArchiveInterface *interface, QObject *parent)
322  : Job(interface, parent)
323  , m_files(files)
324 {
325 }
326 
327 void DeleteJob::doWork()
328 {
329  emit description(this, i18np("Deleting a file from the archive", "Deleting %1 files", m_files.count()));
330 
331  ReadWriteArchiveInterface *m_writeInterface =
332  qobject_cast<ReadWriteArchiveInterface*>(archiveInterface());
333 
334  Q_ASSERT(m_writeInterface);
335 
336  connectToArchiveInterfaceSignals();
337  int ret = m_writeInterface->deleteFiles(m_files);
338 
339  if (!archiveInterface()->waitForFinishedSignal()) {
340  onFinished(ret);
341  }
342 }
343 
344 } // namespace Kerfuffle
345 
346 #include "jobs.moc"
Kerfuffle::Job::~Job
virtual ~Job()
Definition: jobs.cpp:84
Kerfuffle::ListJob::doWork
virtual void doWork()
Definition: jobs.cpp:188
Kerfuffle::ListJob::isPasswordProtected
bool isPasswordProtected() const
Definition: jobs.cpp:204
Kerfuffle::Job::start
void start()
Definition: jobs.cpp:103
Kerfuffle::IsPasswordProtected
The entry is password-protected.
Definition: archive.h:74
Kerfuffle::ListJob::subfolderName
QString subfolderName() const
Definition: jobs.cpp:235
QThread::sleep
void sleep(unsigned long secs)
Kerfuffle::Query
Definition: queries.h:44
Kerfuffle::DeleteJob::doWork
virtual void doWork()
Definition: jobs.cpp:327
Kerfuffle::Job::userQuery
void userQuery(Kerfuffle::Query *)
Kerfuffle::Job::onInfo
virtual void onInfo(const QString &info)
Definition: jobs.cpp:144
Kerfuffle::Job::onFinished
virtual void onFinished(bool result)
Definition: jobs.cpp:154
Kerfuffle::ReadOnlyArchiveInterface::list
virtual bool list()=0
List archive contents.
Kerfuffle::ListJob::isSingleFolderArchive
bool isSingleFolderArchive() const
Definition: jobs.cpp:209
Kerfuffle::ExtractJob::doWork
virtual void doWork()
Definition: jobs.cpp:249
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Kerfuffle::ReadOnlyArchiveInterface
Definition: archiveinterface.h:43
Kerfuffle::ReadWriteArchiveInterface::addFiles
virtual bool addFiles(const QStringList &files, const CompressionOptions &options)=0
Kerfuffle::Job::entryRemoved
void entryRemoved(const QString &entry)
QString::clear
void clear()
Kerfuffle::Job::emitResult
virtual void emitResult()
Definition: jobs.cpp:109
QList::count
int count(const T &value) const
Kerfuffle::ReadWriteArchiveInterface
Definition: archiveinterface.h:121
QHash::constEnd
const_iterator constEnd() const
QHash
QObject
Kerfuffle::Job::error
void error(const QString &errorMessage, const QString &details)
Kerfuffle::Job::doKill
virtual bool doKill()
Definition: jobs.cpp:168
QString::isEmpty
bool isEmpty() const
Kerfuffle::DeleteJob::DeleteJob
DeleteJob(const QVariantList &files, ReadWriteArchiveInterface *interface, QObject *parent=0)
Definition: jobs.cpp:321
Kerfuffle::Job::onUserQuery
virtual void onUserQuery(Query *query)
Definition: jobs.cpp:163
QtConcurrent::run
QFuture< T > run(Function function,...)
Kerfuffle::AddJob::AddJob
AddJob(const QStringList &files, const CompressionOptions &options, ReadWriteArchiveInterface *interface, QObject *parent=0)
Definition: jobs.cpp:297
QString
Kerfuffle::ListJob::extractedFilesSize
qlonglong extractedFilesSize() const
Definition: jobs.cpp:199
Kerfuffle::ExtractJob::extractionOptions
ExtractionOptions extractionOptions() const
Definition: jobs.cpp:292
Kerfuffle::Job::onEntry
virtual void onEntry(const ArchiveEntry &archiveEntry)
Definition: jobs.cpp:134
QStringList
Kerfuffle::FileName
The entry's file name.
Definition: archive.h:59
QLatin1Char
Kerfuffle::ExtractJob::destinationDirectory
QString destinationDirectory() const
Definition: jobs.cpp:287
Kerfuffle::Job::Job
Job(ReadOnlyArchiveInterface *interface, QObject *parent=0)
Definition: jobs.cpp:69
QHash::const_iterator
QHash::constBegin
const_iterator constBegin() const
jobs.h
Kerfuffle::ReadOnlyArchiveInterface::copyFiles
virtual bool copyFiles(const QList< QVariant > &files, const QString &destinationDirectory, ExtractionOptions options)=0
Extract files from archive.
Kerfuffle::Job::onEntryRemoved
virtual void onEntryRemoved(const QString &path)
Definition: jobs.cpp:149
QLatin1String
Kerfuffle::Job::onError
virtual void onError(const QString &message, const QString &details)
Definition: jobs.cpp:126
Kerfuffle::Job::newEntry
void newEntry(const ArchiveEntry &)
Kerfuffle::ExtractJob::ExtractJob
ExtractJob(const QVariantList &files, const QString &destinationDir, ExtractionOptions options, ReadOnlyArchiveInterface *interface, QObject *parent=0)
Definition: jobs.cpp:240
Kerfuffle::ListJob::ListJob
ListJob(ReadOnlyArchiveInterface *interface, QObject *parent=0)
Definition: jobs.cpp:178
Kerfuffle::Size
The entry's original size.
Definition: archive.h:64
Kerfuffle::ReadWriteArchiveInterface::deleteFiles
virtual bool deleteFiles(const QList< QVariant > &files)=0
Kerfuffle::Job::connectToArchiveInterfaceSignals
void connectToArchiveInterfaceSignals()
Definition: jobs.cpp:115
Kerfuffle::AddJob::doWork
virtual void doWork()
Definition: jobs.cpp:304
Kerfuffle::Job
Definition: jobs.h:46
QHash::contains
bool contains(const Key &key) const
QThread::quit
void quit()
QThread
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
Kerfuffle::ReadOnlyArchiveInterface::doKill
virtual bool doKill()
Definition: archiveinterface.cpp:73
Kerfuffle::Job::isRunning
bool isRunning() const
Definition: jobs.cpp:98
KJob
Kerfuffle::Job::onProgress
virtual void onProgress(double progress)
Definition: jobs.cpp:139
Kerfuffle::Job::archiveInterface
ReadOnlyArchiveInterface * archiveInterface()
Definition: jobs.cpp:93
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:37 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ark

Skip menu "ark"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal