ark
jobs.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "jobs.h"
00027 #include "threading.h"
00028
00029 #include <kdebug.h>
00030 #include <KLocale>
00031 #include <QDir>
00032
00033 namespace Kerfuffle
00034 {
00035
00036
00037 Job::Job(ReadOnlyArchiveInterface *interface, QObject *parent)
00038 : KJob(parent),
00039 m_interface(interface)
00040 {
00041 static bool onlyOnce = false;
00042 if (!onlyOnce) {
00043 qRegisterMetaType<QPair<QString, QString> >("QPair<QString,QString>");
00044 onlyOnce = true;
00045 }
00046 }
00047
00048 void Job::start()
00049 {
00050 ThreadExecution *thread = new ThreadExecution(this);
00051 ThreadWeaver::Weaver::instance()->enqueue( thread );
00052 }
00053
00054 void Job::onError( const QString & message, const QString & details )
00055 {
00056 setError(1);
00057 setErrorText(message);
00058 }
00059
00060 void Job::onEntry( const ArchiveEntry & archiveEntry )
00061 {
00062 emit newEntry( archiveEntry );
00063 }
00064
00065 void Job::onProgress( double value )
00066 {
00067 setPercent( static_cast<unsigned long>( 100.0*value ) );
00068 }
00069
00070 void Job::onEntryRemoved( const QString & path )
00071 {
00072 emit entryRemoved( path );
00073 }
00074
00075 ListJob::ListJob( ReadOnlyArchiveInterface *interface, QObject *parent )
00076 : Job( interface, parent ),
00077 m_isSingleFolderArchive(true),
00078 m_isPasswordProtected(false),
00079 m_extractedFilesSize(0)
00080 {
00081 connect(this, SIGNAL(newEntry( const ArchiveEntry&)),
00082 this, SLOT(onNewEntry( const ArchiveEntry&)));
00083 }
00084
00085 void ListJob::doWork()
00086 {
00087 emit description( this, i18n( "Listing entries" ) );
00088 m_interface->registerObserver( this );
00089 bool result = m_interface->list();
00090 m_interface->removeObserver( this );
00091
00092 setError(!result);
00093 emitResult();
00094 }
00095
00096 void ListJob::onNewEntry(const ArchiveEntry& entry)
00097 {
00098 m_extractedFilesSize += entry[ Size ].toLongLong();
00099 m_isPasswordProtected |= entry [ IsPasswordProtected ].toBool();
00100 if (m_isSingleFolderArchive)
00101 {
00102 QString filename = entry[ FileName ].toString();
00103 if (m_previousEntry.isEmpty()) {
00104
00105 m_previousEntry = filename.split(QDir::separator()).first();
00106 }
00107 else {
00108 QString newRoot = filename.split(QDir::separator()).first();
00109 if (m_previousEntry != newRoot) {
00110 m_isSingleFolderArchive = false;
00111 m_subfolderName.clear();
00112 }
00113 else {
00114 m_previousEntry = newRoot;
00115 m_subfolderName = newRoot;
00116 }
00117 }
00118 }
00119 }
00120
00121 ExtractJob::ExtractJob( const QList<QVariant>& files, const QString& destinationDir,
00122 Archive::CopyFlags flags, ReadOnlyArchiveInterface *interface, QObject *parent )
00123 : Job(interface, parent ), m_files( files ), m_destinationDir( destinationDir ), m_flags(flags)
00124 {
00125 }
00126
00127 void ExtractJob::doWork()
00128 {
00129 QString desc;
00130 if ( m_files.count() == 0 )
00131 {
00132 desc = i18n( "Extracting all files" );
00133 }
00134 else
00135 {
00136 desc = i18np( "Extracting one file", "Extracting %1 files", m_files.count() );
00137 }
00138 emit description( this, desc );
00139
00140 m_interface->registerObserver( this );
00141
00142 kDebug(1601) << "Starting extraction with selected files "
00143 << m_files
00144 << " Destination dir " << m_destinationDir
00145 << " Preserve paths: " << (m_flags & Archive::PreservePaths)
00146 << " Truncate common base: " << (m_flags & Archive::TruncateCommonBase)
00147 ;
00148
00149 setError( !m_interface->copyFiles( m_files, m_destinationDir, m_flags ) );
00150 m_interface->removeObserver( this );
00151
00152 emitResult();
00153
00154 }
00155
00156 AddJob::AddJob( const QStringList& files, const CompressionOptions& options , ReadWriteArchiveInterface *interface, QObject *parent )
00157 : Job( interface, parent ), m_files( files ), m_options(options)
00158 {
00159 }
00160
00161 void AddJob::doWork()
00162 {
00163 emit description( this, i18np( "Adding a file", "Adding %1 files", m_files.count() ) );
00164
00165 ReadWriteArchiveInterface *m_writeInterface =
00166 qobject_cast<ReadWriteArchiveInterface*>
00167 (m_interface);
00168
00169 Q_ASSERT(m_writeInterface);
00170
00171 m_writeInterface->registerObserver( this );
00172 setError( !m_writeInterface->addFiles( m_files, m_options ) );
00173 m_writeInterface->removeObserver( this );
00174
00175 emitResult();
00176
00177 }
00178
00179 DeleteJob::DeleteJob( const QList<QVariant>& files, ReadWriteArchiveInterface *interface, QObject *parent )
00180 : Job( interface, parent ), m_files( files )
00181 {
00182 }
00183
00184 void DeleteJob::doWork()
00185 {
00186 emit description( this, i18np( "Deleting a file from the archive", "Deleting %1 files", m_files.count() ) );
00187
00188 ReadWriteArchiveInterface *m_writeInterface =
00189 qobject_cast<ReadWriteArchiveInterface*>
00190 (m_interface);
00191
00192 Q_ASSERT(m_writeInterface);
00193
00194 m_writeInterface->registerObserver( this );
00195 setError( !m_writeInterface->deleteFiles( m_files ) );
00196 m_writeInterface->removeObserver( this );
00197
00198 emitResult();
00199 }
00200
00201 }