code_app/app/scanner/dialog_clrcache.cpp

142 lines
3.6 KiB
C++
Raw Normal View History

2022-05-03 10:25:52 +00:00
#include "dialog_clrcache.h"
#include "ui_dialog_clrcache.h"
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
#include <QKeyEvent>
#include "base/HGUtility.h"
#include "HGUIGlobal.h"
#include "../../utility/HGString.h"
Dialog_ClrCache::Dialog_ClrCache(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_ClrCache)
{
ui->setupUi(this);
m_cachePath = getCachePath();
ui->lineEdit->setText(m_cachePath);
setInformation(m_cachePath);
}
Dialog_ClrCache::~Dialog_ClrCache()
{
delete ui;
}
QString Dialog_ClrCache::getCachePath()
{
HGChar cachePath[512];
HGBase_GetDocumentsPath(cachePath, 512);
strcat(cachePath, "Cache/");
2022-05-03 10:25:52 +00:00
QString filePath = getStdFileName(StdStringToUtf8(cachePath).c_str());
return filePath;
}
void Dialog_ClrCache::on_btn_clr_clicked()
{
QFileInfo file(m_cachePath);
if (!file.exists())
{
QMessageBox::information(this, tr("Information"), tr("No cached file is available"));
return;
}
QMessageBox msg(QMessageBox::Question, tr("Question"),
tr("Main window contains temporary files, clear cache would remove all of them.\n Continue to clear?"),
QMessageBox::Yes | QMessageBox::No, this);
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.setButtonText(QMessageBox::No, tr("no"));
msg.exec();
if (msg.clickedButton() != msg.button(QMessageBox::Yes))
{
return;
}
bool ret = clrCache(m_cachePath);
if (ret)
{
emit clearCache();
QMessageBox::information(this, tr("Information"), tr("Cache clear successfully."));
}
else
{
QMessageBox::warning(this, tr("Warning"), tr("Cache clear failed or incompletely clear."));
}
setInformation(m_cachePath);
}
void Dialog_ClrCache::on_btn_close_clicked()
{
close();
}
bool Dialog_ClrCache::clrCache(const QString &path)
{
QDir dir = QDir(path);
bool isSuccessful = dir.removeRecursively();
dir.mkpath(m_cachePath);
return isSuccessful;
}
qint64 Dialog_ClrCache::getDirSize(const QString &path)
{
QDir dir(path);
qint64 size = 0;
foreach(QFileInfo info, dir.entryInfoList(QDir::Files))
size += info.size();
foreach(QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
size += getDirSize(path + QDir::separator() + subDir);
return size;
}
int Dialog_ClrCache::getFileNum(const QString &path)
{
QDir dir(path);
int num = 0;
foreach(QFileInfo info, dir.entryInfoList(QDir::Files))
num++;
foreach(QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
num += getFileNum(path + QDir::separator() + subDir);
return num;
}
void Dialog_ClrCache::setInformation(const QString &path)
{
//生成缓存占用空间大小信息
qint64 size = getDirSize(path);
double s = size;
int unit = 0;
while(true)
{
if(s > 1024.0)
{
s /= 1024.0;
unit += 1;
}
else
break;
}
QString sstring = QString::number(s, 'f', 2);
ui->lab_size->setText(sstring);
QString unitstring = unit == 3 ? "GB" : (unit == 2 ? "MB" : (unit == 1 ? "KB" : "B"));
ui->lab_unit->setText(unitstring);
QString Bstring = QString::number(size);
int commaNum = Bstring.size() / 3;
if(Bstring.size() % 3 == 0)
commaNum -= 1;
int offset = 0;
for(int i = 0; i < commaNum; i++)
{
Bstring.insert(Bstring.size() - 3 - offset * 4, ",");
offset += 1;
}
ui->lab_sizeByB->setText("(" + Bstring + " Byte)");
//生成文件个数信息
ui->lab_num->setNum(getFileNum(path));
}