Add progress bar to zip creation.

This commit is contained in:
sawinglogz 2020-01-22 12:59:58 -05:00
parent c4619dad35
commit 5dfbc1fbe4
3 changed files with 80 additions and 14 deletions

View File

@ -2637,17 +2637,17 @@ void MainWindow::on_actionCreate_Card_zip_triggered()
ZipFile z;
bool ok = z.Open(filename);
if (ok) {
// TODO: need to add progress bar!
ok = z.AddDirectory(cardPath);
ProgressDialog * prog = new ProgressDialog(this);
prog->setMessage(tr("Creating archive..."));
ok = z.AddDirectory(cardPath, prog);
z.Close();
} else {
qWarning() << "Unable to open" << filename;
}
if (!ok) {
qWarning() << "Unable to create" << filename;
QMessageBox::warning(nullptr, STR_MessageBox_Error,
QObject::tr("Unable to create archive!"),
QMessageBox::Ok);
} else {
qDebug() << "Created" << filename;
}
}
}

View File

@ -10,6 +10,8 @@
#include "zip.h"
#include <QDebug>
#include <QDateTime>
#include <QCoreApplication>
#include "SleepLib/progressdialog.h"
// Static functions to abstract the details of miniz from the primary logic.
@ -51,27 +53,68 @@ void ZipFile::Close()
}
}
bool ZipFile::AddDirectory(const QString & path, const QString & prefix)
bool ZipFile::AddDirectory(const QString & path, ProgressDialog* progress)
{
return AddDirectory(path, "", progress);
}
bool ZipFile::AddDirectory(const QString & path, const QString & prefix, ProgressDialog* progress)
{
bool ok;
FileQueue queue;
queue.AddDirectory(path, prefix);
ok = AddFiles(queue);
ok = AddFiles(queue, progress);
return ok;
}
bool ZipFile::AddFiles(const FileQueue & queue)
bool ZipFile::AddFiles(const FileQueue & queue, ProgressDialog* progress)
{
bool ok;
// TODO: add progress bar
qDebug() << "Adding" << queue.toString();
qDebug().noquote() << "Adding" << queue.toString();
m_abort = false;
m_progress = 0;
if (progress) {
progress->addAbortButton();
progress->setWindowModality(Qt::ApplicationModal);
progress->open();
connect(this, SIGNAL(setProgressMax(int)), progress, SLOT(setProgressMax(int)));
connect(this, SIGNAL(setProgressValue(int)), progress, SLOT(setProgressValue(int)));
connect(progress, SIGNAL(abortClicked()), this, SLOT(abort()));
}
// Always emit, since the caller may have configured and connected a progress dialog manually.
emit setProgressValue(m_progress);
emit setProgressMax(queue.byteCount() + queue.dirCount());
QCoreApplication::processEvents();
for (auto & entry : queue.files()) {
ok = AddFile(entry.path, entry.name);
if (!ok) {
if (!ok || m_abort) {
break;
}
}
if (progress) {
disconnect(progress, SIGNAL(abortClicked()), this, SLOT(abort()));
disconnect(this, SIGNAL(setProgressMax(int)), progress, SLOT(setProgressMax(int)));
disconnect(this, SIGNAL(setProgressValue(int)), progress, SLOT(setProgressValue(int)));
progress->close();
progress->deleteLater();
}
if (!ok) {
qWarning().noquote() << "Unable to create" << m_file.fileName();
Close();
m_file.remove();
} else if (aborted()) {
qDebug().noquote() << "User canceled zip creation.";
Close();
m_file.remove();
} else {
qDebug().noquote() << "Created" << m_file.fileName() << m_file.size() << "bytes";
}
return ok;
}
@ -90,6 +133,7 @@ bool ZipFile::AddFile(const QString & path, const QString & name)
if (fi.isDir()) {
archive_name += QDir::separator();
m_progress += 1;
} else {
// Open and read file into memory.
QFile f(path);
@ -98,11 +142,16 @@ bool ZipFile::AddFile(const QString & path, const QString & name)
return false;
}
data = f.readAll();
m_progress += data.size();
}
//qDebug() << "attempting to add" << archive_name << ":" << data.size() << "bytes";
bool ok = zip_add(m_ctx, archive_name, data, fi.lastModified());
emit setProgressValue(m_progress);
QCoreApplication::processEvents();
return ok;
}

View File

@ -7,25 +7,42 @@
* License. See the file COPYING in the main directory of the source code
* for more details. */
#include <QObject>
#include <QString>
#include <QDir>
#include <QFile>
class ZipFile
class ProgressDialog;
class ZipFile : public QObject
{
Q_OBJECT
public:
ZipFile();
virtual ~ZipFile();
bool Open(const QString & filepath);
bool AddDirectory(const QString & path, const QString & archive_name=""); // add a directory and recurse
bool AddDirectory(const QString & path, ProgressDialog* progress=nullptr); // add a directory and recurse
bool AddDirectory(const QString & path, const QString & archive_name, ProgressDialog* progress=nullptr); // add a directory and recurse
bool AddFiles(const class FileQueue & queue, ProgressDialog* progress=nullptr); // add a fixed list of files
bool AddFile(const QString & path, const QString & archive_name); // add a single file
bool AddFiles(const class FileQueue & queue); // add a fixed list of files
void Close();
bool aborted() const { return m_abort; }
public slots:
void abort() { m_abort = true; }
signals:
void setProgressMax(int max);
void setProgressValue(int val);
protected:
void* m_ctx;
QFile m_file;
bool m_abort;
int m_progress;
};