code_app/app/scanner/dialog_clrcache.cpp

183 lines
4.7 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>
2022-11-30 02:34:36 +00:00
#include <QFileDialog>
2022-05-03 10:25:52 +00:00
#include "base/HGUtility.h"
#include "HGUIGlobal.h"
2022-05-18 03:39:16 +00:00
#include "HGString.h"
2022-11-30 02:34:36 +00:00
#include "app_cfg.h"
2022-05-03 10:25:52 +00:00
2022-11-30 02:34:36 +00:00
Dialog_ClrCache::Dialog_ClrCache(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog_ClrCache)
, m_mainWindow(nullptr)
2022-05-03 10:25:52 +00:00
{
ui->setupUi(this);
2022-11-30 02:34:36 +00:00
2022-05-03 10:25:52 +00:00
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);
HGChar procName[512];
HGBase_GetProcessName(procName, 512);
strcat(cachePath, procName);
strcat(cachePath, "/Cache/");
2022-11-30 02:34:36 +00:00
return getCfgValue("save", "cachePath", getStdFileName(StdStringToUtf8(cachePath).c_str()));
2022-05-03 10:25:52 +00:00
}
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);
}
bool Dialog_ClrCache::clrCache(const QString &path)
{
QDir dir = QDir(path);
bool isSuccessful = dir.removeRecursively();
2022-11-30 02:34:36 +00:00
// dir.mkpath(m_cachePath);
2022-05-03 10:25:52 +00:00
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));
}
2022-11-30 02:34:36 +00:00
2022-11-30 06:29:02 +00:00
void Dialog_ClrCache::on_btn_directory_clicked()
{
QString save_dir = QFileDialog::getExistingDirectory(this, tr("Browse directory"), "");
if (!save_dir.isEmpty())
{
if (save_dir[save_dir.size() - 1] != '/')
save_dir += "/";
ui->lineEdit->setText(getStdFileName(save_dir));
}
}
void Dialog_ClrCache::on_pbtn_ok_clicked()
2022-11-30 02:34:36 +00:00
{
if (ui->lineEdit->text().endsWith(getStdFileName(QString("Cache/")), Qt::CaseInsensitive))
2022-11-30 02:34:36 +00:00
{
saveCfgValue("save", "cachePath", getStdFileName(ui->lineEdit->text()));
2022-11-30 02:34:36 +00:00
}
else
{
QString filePath = ui->lineEdit->text() + "Cache/";
HGResult ret = HGBase_CreateDir(getStdString(filePath).c_str());
2022-11-30 02:34:36 +00:00
if (ret != HGBASE_ERR_OK)
{
QString str = m_mainWindow->getLogInfo(ret);
QMessageBox::information(this, tr("tips"), str);
return;
}
saveCfgValue("save", "cachePath", getStdFileName(filePath));
}
close();
}
2022-11-30 06:29:02 +00:00
void Dialog_ClrCache::on_pbtn_cancel_clicked()
2022-11-30 02:34:36 +00:00
{
2022-11-30 06:29:02 +00:00
close();
2022-11-30 02:34:36 +00:00
}