1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-02-24 20:24:56 +00:00
UTPass/plugins/Utils/utils.cpp

68 lines
1.4 KiB
C++
Raw Normal View History

2019-09-20 21:29:39 +02:00
#include <QDir>
2025-01-20 15:46:57 +01:00
#include <QSemaphore>
2019-09-20 21:29:39 +02:00
2025-01-20 15:46:57 +01:00
#include "jobs/unzipjob.h"
2019-09-20 21:29:39 +02:00
#include "utils.h"
2025-01-20 15:46:57 +01:00
Utils::Utils():
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
{}
2019-09-20 21:29:39 +02:00
2025-01-20 15:46:57 +01:00
bool Utils::unzip(QUrl zip_url, QString dir_out_path)
{
if (!this->m_sem->tryAcquire(1, 500)) {
2019-09-20 21:29:39 +02:00
return false;
}
2025-02-21 15:50:27 +01:00
qInfo() << "[Utils] Unzip path " << zip_url << " to " << dir_out_path;
2025-01-20 15:46:57 +01:00
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;
2019-09-20 21:29:39 +02:00
}
2025-01-20 15:46:57 +01:00
void Utils::unzipResult(bool err)
2019-09-20 21:29:39 +02:00
{
2025-02-21 15:50:27 +01:00
qDebug() << "[Utils] Unzip Result";
2025-01-20 15:46:57 +01:00
if (err) {
2025-02-21 15:50:27 +01:00
qInfo() << "[Utils] Unzip Failed";
emit unzipFailed();
2025-01-20 15:46:57 +01:00
} else {
2025-02-21 15:50:27 +01:00
qInfo() << "[Utils] Unzip Succeed";
2025-01-20 15:46:57 +01:00
emit unzipSucceed();
}
this->m_sem->release(1);
2019-09-20 21:29:39 +02:00
}
2025-01-17 10:40:54 +01:00
2025-01-20 15:46:57 +01:00
2025-01-20 11:23:40 +01:00
QString Utils::manifestPath()
{
2025-01-17 10:40:54 +01:00
auto path = QDir(QDir::currentPath()).filePath("manifest_.json");
2025-02-21 15:50:27 +01:00
qInfo() << "[Utils] Manifest path : " << path;
2025-01-17 10:40:54 +01:00
return path;
}
bool Utils::rmFile(QUrl file_url)
{
return QFile::remove(file_url.toLocalFile());
}
bool Utils::rmDir(QUrl dir_url)
{
QDir dir(dir_url.toLocalFile());
return dir.removeRecursively();
}
2025-02-21 15:50:27 +01:00
bool Utils::fileExists(QUrl path)
{
QString p = path.toLocalFile();
auto ret = QFileInfo::exists(p) && QFileInfo(p).isFile();
qDebug() << "[Utils]" << path << " existing file :" << ret;
return ret;
}