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:
@ -5,6 +5,7 @@ set(
|
||||
SRC
|
||||
plugin.cpp
|
||||
utils.cpp
|
||||
jobs/unzipjob.cpp
|
||||
)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
57
plugins/Utils/jobs/unzipjob.cpp
Normal file
57
plugins/Utils/jobs/unzipjob.cpp
Normal 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);
|
||||
|
||||
}
|
49
plugins/Utils/jobs/unzipjob.h
Normal file
49
plugins/Utils/jobs/unzipjob.h
Normal 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
|
@ -1,67 +1,47 @@
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <QtCore/QStandardPaths>
|
||||
#include <quazip5/JlCompress.h>
|
||||
#include <QSemaphore>
|
||||
|
||||
#include "jobs/unzipjob.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
|
||||
Utils::Utils():
|
||||
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
|
||||
{}
|
||||
|
||||
bool Utils::unzip(QUrl zip_url, QString dir_out_path)
|
||||
{
|
||||
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(
|
||||
zip_url.toLocalFile(),
|
||||
tmp_dir_path
|
||||
).isEmpty();
|
||||
|
||||
if (!status) {
|
||||
tmp_dir.removeRecursively();
|
||||
if (!this->m_sem->tryAcquire(1, 500)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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";
|
||||
QDir dir_out(dir_out_path);
|
||||
dir_out.removeRecursively();
|
||||
|
||||
qDebug() << "Moving zip content to destination";
|
||||
QDir dir;
|
||||
qDebug() << dir_import_path << " to " << dir_out_path;
|
||||
auto ret = dir.rename(dir_import_path, dir_out_path);
|
||||
tmp_dir.removeRecursively();;
|
||||
return ret;
|
||||
qInfo() << "Unzip path " << zip_url << " to " << dir_out_path;
|
||||
auto job = new UnzipJob(zip_url, QDir(dir_out_path));
|
||||
connect(job, &UnzipJob::resultReady, this, &Utils::unzipResult);
|
||||
connect(job, &UnzipJob::finished, job, &QObject::deleteLater);
|
||||
job->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::rmFile(QUrl file_url)
|
||||
void Utils::unzipResult(bool err)
|
||||
{
|
||||
return QFile::remove(file_url.toLocalFile());
|
||||
|
||||
qDebug() << "Unzip Result";
|
||||
if (err) {
|
||||
qInfo() << "Unzip Failed";
|
||||
emit unzipFailed("failed to unzip archive");
|
||||
|
||||
} else {
|
||||
qInfo() << "Unzip Succeed";
|
||||
emit unzipSucceed();
|
||||
}
|
||||
this->m_sem->release(1);
|
||||
}
|
||||
|
||||
bool Utils::rmDir(QUrl dir_url)
|
||||
{
|
||||
QDir dir(dir_url.toLocalFile());
|
||||
return dir.removeRecursively();
|
||||
}
|
||||
|
||||
QString Utils::manifestPath()
|
||||
{
|
||||
auto path = QDir(QDir::currentPath()).filePath("manifest_.json");
|
||||
qDebug() << "Manifest path : " << path;
|
||||
qInfo() << "Manifest path : " << path;
|
||||
return path;
|
||||
}
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QQuickWindow>
|
||||
#include <memory>
|
||||
#include <QSemaphore>
|
||||
|
||||
/**
|
||||
* @class Utils
|
||||
@ -16,16 +18,34 @@ class Utils : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief Default constructor for the Utils class.
|
||||
* @brief Slot to handle the result of a unzip operation.
|
||||
* @param err True if an error occurred during the operation.
|
||||
*/
|
||||
Utils() = default;
|
||||
void unzipResult(bool err);
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Emitted when the archive is successfully extracted.
|
||||
*/
|
||||
void unzipSucceed();
|
||||
|
||||
/**
|
||||
* @brief Default destructor for the Utils class.
|
||||
* @brief Emitted when the unzipping operation fails.
|
||||
* @param message The error message describing the failure.
|
||||
*/
|
||||
~Utils() override = default;
|
||||
void unzipFailed(QString message);
|
||||
|
||||
private:
|
||||
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing concurrent operations. */
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the Utils class.
|
||||
*/
|
||||
Utils();
|
||||
|
||||
/**
|
||||
* @brief Unzips a ZIP file to the specified output directory.
|
||||
@ -39,25 +59,6 @@ public:
|
||||
*/
|
||||
Q_INVOKABLE bool unzip(QUrl zip_url, QString dir_out);
|
||||
|
||||
/**
|
||||
* @brief Removes a file at the specified URL.
|
||||
*
|
||||
* This method deletes a file at the specified URL.
|
||||
*
|
||||
* @param file_url The URL of the file to delete.
|
||||
* @return `true` if the file was successfully removed, `false` otherwise.
|
||||
*/
|
||||
Q_INVOKABLE bool rmFile(QUrl file_url);
|
||||
|
||||
/**
|
||||
* @brief Removes a directory at the specified URL.
|
||||
*
|
||||
* This method deletes a directory at the specified URL, along with its contents.
|
||||
*
|
||||
* @param dir_url The URL of the directory to remove.
|
||||
* @return `true` if the directory was successfully removed, `false` otherwise.
|
||||
*/
|
||||
Q_INVOKABLE bool rmDir(QUrl dir_url);
|
||||
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user