1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-06-24 22:42:28 +00:00

Refactor unzip password store

This commit is contained in:
2025-01-20 15:46:57 +01:00
parent 20aff3a404
commit 7a2b12419d
10 changed files with 182 additions and 85 deletions

View File

@ -0,0 +1,57 @@
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <quazip5/JlCompress.h>
#include "qdebug.h"
#include "unzipjob.h"
UnzipJob::UnzipJob(QUrl zip_url, QDir dir_out):
m_zip_url(zip_url),
m_dir_out(dir_out)
{
this->setObjectName("UnzipJob");
}
void UnzipJob::run()
{
auto tmp_dir_path = QStandardPaths::writableLocation(
QStandardPaths::CacheLocation).append("/unzip");
QDir tmp_dir(tmp_dir_path);
tmp_dir.removeRecursively();
tmp_dir.mkpath(".");
qDebug() << "Temp dir path is " << tmp_dir_path;
auto status = !JlCompress::extractDir(
this->m_zip_url.toLocalFile(),
tmp_dir_path
).isEmpty();
if (!status) {
tmp_dir.removeRecursively();
emit resultReady(false);
return;
}
qDebug() << "Guessing if it should remove a single root folder";
QStringList files_in_tmp_dir = tmp_dir.entryList(QDir::AllEntries | QDir::Hidden |
QDir::NoDotAndDotDot);
auto dir_import_path =
files_in_tmp_dir.length() == 1 ?
tmp_dir_path.append("/" + files_in_tmp_dir.first()) : tmp_dir_path;
qDebug() << "Final imported tmp path dir is " << dir_import_path;
qDebug() << "Removing destination";
this->m_dir_out.removeRecursively();
qDebug() << "Moving zip content to destination";
QDir dir;
qDebug() << dir_import_path << " to " << this->m_dir_out;
auto ret = dir.rename(dir_import_path, this->m_dir_out.absolutePath());
tmp_dir.removeRecursively();;
emit resultReady(ret);
}

View File

@ -0,0 +1,49 @@
#ifndef RMJOB_H
#define RMJOB_H
#include "qurl.h"
#include <QThread>
#include <QDir>
/**
* @class RmJob
* @brief A class to handle removing recursively a path in a separate thread.
*
*/
class UnzipJob : public QThread
{
Q_OBJECT
/**
* @brief The main function that performs the unzip operation.
*
* Handles the process of unziping a archive to a target directory.
*/
void run() override;
signals:
/**
* @brief Signal emitted when the unzip operation is complete.
*
* @param err A boolean indicating whether an error occurred during unzipping.
* `true` if an error occurred, `false` if the clone was successful.
*/
void resultReady(const bool err);
private:
QUrl m_zip_url; ///< The url of the archive.
QDir m_dir_out; ///< The directory where the content of the archive will be unzip.
public:
/**
* @brief Constructor for the UnzipJob class.
*
* Initializes the UnzipJob with the specified target path to be removed.
*
* @param zip_url Url of the archive to be unzip.
* @param dir_out Target directory where the content of the archive must be extracted.
*/
UnzipJob(QUrl zip_url, QDir dir_out);
};
#endif // RMJOB_H