Skip to content

Commit

Permalink
Display docset extraction progress (fixes #252)
Browse files Browse the repository at this point in the history
  • Loading branch information
trollixx committed Feb 16, 2015
1 parent 2013e90 commit be6a937
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Application::Application(const QString &query, QObject *parent) :
m_extractorThread->start();
connect(m_extractor, &Extractor::completed, this, &Application::extractionCompleted);
connect(m_extractor, &Extractor::error, this, &Application::extractionError);
connect(m_extractor, &Extractor::progress, this, &Application::extractionProgress);

connect(m_settings, &Settings::updated, this, &Application::applySettings);
applySettings();
Expand Down
1 change: 1 addition & 0 deletions src/core/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public slots:
signals:
void extractionCompleted(const QString &filePath);
void extractionError(const QString &filePath, const QString &errorString);
void extractionProgress(const QString &filePath, qint64 extracted, qint64 total);

private slots:
void applySettings();
Expand Down
39 changes: 31 additions & 8 deletions src/core/extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,54 @@ Extractor::Extractor(QObject *parent) :

void Extractor::extract(const QString &filePath, const QString &destination, const QString &root)
{
archive *a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
ExtractInfo info = {
.extractor = this,
.archiveHandle = archive_read_new(),
.filePath = filePath,
.totalBytes = QFileInfo(filePath).size(),
.extractedBytes = 0
};

int r = archive_read_open_filename(a, qPrintable(filePath), 10240);
archive_read_support_filter_all(info.archiveHandle);
archive_read_support_format_all(info.archiveHandle);

int r = archive_read_open_filename(info.archiveHandle, qPrintable(filePath), 10240);
if (r) {
emit error(filePath, QString::fromLocal8Bit(archive_error_string(a)));
emit error(filePath, QString::fromLocal8Bit(archive_error_string(info.archiveHandle)));
return;
}

archive_read_extract_set_progress_callback(info.archiveHandle, &Extractor::progressCallback,
&info);

QDir destinationDir(destination);
if (!root.isEmpty())
destinationDir = destinationDir.absoluteFilePath(root);

// TODO: Do not strip root directory in archive if it equals to 'root'
archive_entry *entry;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
while (archive_read_next_header(info.archiveHandle, &entry) == ARCHIVE_OK) {
QString pathname = archive_entry_pathname(entry);
if (!root.isEmpty())
pathname.remove(0, pathname.indexOf(QLatin1String("/")) + 1);
archive_entry_set_pathname(entry, qPrintable(destinationDir.absoluteFilePath(pathname)));
archive_read_extract(a, entry, 0);
archive_read_extract(info.archiveHandle, entry, 0);
}

emit completed(filePath);
archive_read_free(a);
archive_read_free(info.archiveHandle);
}

void Extractor::progressCallback(void *ptr)
{
ExtractInfo *info = reinterpret_cast<ExtractInfo *>(ptr);

const qint64 extractedBytes = archive_filter_bytes(info->archiveHandle, -1);
if (extractedBytes == info->extractedBytes)
return;

info->extractedBytes = extractedBytes;

emit info->extractor->progress(info->filePath, extractedBytes, info->totalBytes);
}

14 changes: 14 additions & 0 deletions src/core/extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QObject>

struct archive;

namespace Zeal {
namespace Core {

Expand All @@ -18,6 +20,18 @@ public slots:
signals:
void error(const QString &filePath, const QString &message);
void completed(const QString &filePath);
void progress(const QString &filePath, qint64 extracted, qint64 total);

private:
struct ExtractInfo {
Extractor *extractor;
archive *archiveHandle;
QString filePath;
qint64 totalBytes;
qint64 extractedBytes;
};

static void progressCallback(void *ptr);
};

} // namespace Core
Expand Down
23 changes: 23 additions & 0 deletions src/ui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ SettingsDialog::SettingsDialog(Core::Application *app, ListModel *listModel, QWi
this, &SettingsDialog::extractionCompleted);
connect(m_application, &Core::Application::extractionError,
this, &SettingsDialog::extractionError);
connect(m_application, &Core::Application::extractionProgress,
this, &SettingsDialog::extractionProgress);

loadSettings();
}
Expand Down Expand Up @@ -119,6 +121,27 @@ void SettingsDialog::extractionError(const QString &filePath, const QString &err
delete m_tmpFiles.take(docsetName);
}

void SettingsDialog::extractionProgress(const QString &filePath, qint64 extracted, qint64 total)
{
QString docsetName;

/// FIXME: Come up with a better approach
for (const QString &key : m_tmpFiles.keys()) {
if (m_tmpFiles[key]->fileName() == filePath) {
docsetName = key;
break;
}
}

DocsetMetadata metadata = m_availableDocsets.contains(docsetName)
? m_availableDocsets[docsetName]
: m_userFeeds[docsetName];

QListWidgetItem *listItem = findDocsetListItem(metadata.title());
if (listItem)
listItem->setData(ProgressItemDelegate::ValueRole, percent(extracted, total));
}

/*!
\internal
Should be connected to all \l QNetworkReply::finished signals in order to process possible
Expand Down
1 change: 1 addition & 0 deletions src/ui/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SettingsDialog : public QDialog
private slots:
void extractionCompleted(const QString &filePath);
void extractionError(const QString &filePath, const QString &errorString);
void extractionProgress(const QString &filePath, qint64 extracted, qint64 total);

void downloadCompleted();

Expand Down

0 comments on commit be6a937

Please sign in to comment.