code_app/app/scanner/dialog_clrcache.cpp

191 lines
4.9 KiB
C++

#include "dialog_clrcache.h"
#include "ui_dialog_clrcache.h"
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
#include <QKeyEvent>
#include <QFileDialog>
#include "base/HGUtility.h"
#include "HGUIGlobal.h"
#include "HGString.h"
#include "app_cfg.h"
Dialog_ClrCache::Dialog_ClrCache(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog_ClrCache)
, m_mainWindow(nullptr)
{
ui->setupUi(this);
ui->lineEdit->setEnabled(false);
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/");
return getCfgValue("save", "cachePath", getStdFileName(StdStringToUtf8(cachePath).c_str()));
}
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)
{
bool isSuccessful = false;
QDir dir = QDir(path);
if (!path.isEmpty() && (path.endsWith("Cache/") || path.endsWith("Cache\\")))
{
isSuccessful = dir.removeRecursively();
}
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));
}
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 += "/";
if (save_dir.endsWith(QString("Cache/"), Qt::CaseInsensitive))
ui->lineEdit->setText(getStdFileName(save_dir));
else
ui->lineEdit->setText(getStdFileName(save_dir + "Cache/"));
}
}
void Dialog_ClrCache::on_pbtn_ok_clicked()
{
if(ui->lineEdit->text().isEmpty())
{
QMessageBox::information(this, tr("tips"), tr("directory can not be empty"));
return;
}
QString filePath = ui->lineEdit->text();
HGResult ret = HGBase_CreateDir(getStdString(filePath).c_str());
if (ret != HGBASE_ERR_OK)
{
QMessageBox::information(this, tr("tips"), tr("create cachePath failed: ") + m_mainWindow->getLogInfo(ret));
return;
}
saveCfgValue("save", "cachePath", getStdFileName(filePath));
close();
}
void Dialog_ClrCache::on_pbtn_cancel_clicked()
{
close();
}