RecordControlApplication/Tool.cpp
2024-08-11 20:26:42 -07:00

140 lines
3.4 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Tool.h"
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QFileInfo>
#include <QProcess>
#ifdef Q_OS_WIN
#include <QStorageInfo>
#endif
Tool::Tool()
{
}
/**
* @brief 获取文件列表
*/
QStringList Tool::getFileList(QString path)
{
QStringList fileList;
QDir dir(path);
if (!dir.exists()) {
qDebug() << QString("dir %1 do not exsit...").arg(path);
return fileList;
}
dir.setFilter(QDir::Files);
dir.setSorting(QDir::Name);
QStringList nameFilters;
nameFilters << "*.mp4";
dir.setNameFilters(nameFilters);
fileList = dir.entryList();
return fileList;
}
/**
* @brief 删除文件或文件夹
*/
bool Tool::removeFile(QString path)
{
QFileInfo info(path);
if (!info.exists()) {
qDebug() << path << "do not exsit";
return false;
}
if (info.isFile()) {
QFile file(path);
file.remove();
qDebug() << "remove file" << path << "success";
return true;
} else if (info.isDir()) {
QDir dir(path);
dir.removeRecursively();
qDebug() << "remove dir" << path << "success";
return true;
}
qDebug() << path << "is not file or dir";
return false;
}
/**
* @brief 另外开启一个进程使用linux命令行工具
* @param path 命令
*/
#ifdef Q_OS_UNIX
QString Tool::writeCom(QString path)
{
QProcess proc;
QStringList argList;
argList << "-c" << path;
proc.start("/bin/sh", argList);
// 等待进程启动
proc.waitForFinished();
proc.waitForReadyRead();
// 从控制台读取数据
QByteArray procOutput = proc.readAll();
proc.close();
return QString(procOutput);
}
#endif
/**
* @brief 获取磁盘的可用空间
* @param mountedPath 磁盘挂载位置
*/
int64_t Tool::getAvailableStorage(QString mountedPath)
{
#ifdef Q_OS_UNIX
// 使用linux命令获取磁盘挂载信息
QString mountInfo = writeCom(QString("df %1").arg(mountedPath));
// 解析挂载信息获取可用磁盘大侠
// "Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/sda 487110880 45112 462630056 0% /root/usb\n"
QString diskInfo = mountInfo.split("\n")[1];
QStringList list = diskInfo.split(" ");
int index = -1;
int64_t used;
int64_t available;
for (int i = 0; i < list.length(); i++) {
QString s = list.at(i).trimmed();
if (s.isEmpty())
continue;
index += 1;
if (index == 2)
used = s.toLong();
else if (index == 3)
available = s.toLong();
}
qDebug() << QString("%1: used %2 bytes, available %3 bytes")
.arg(mountedPath)
.arg(used)
.arg(available);
return available;
#endif
#ifdef Q_OS_WIN
QList<QStorageInfo> list = QStorageInfo::mountedVolumes();
for (QStorageInfo& info : list) {
if (info.rootPath() == mountedPath) {
int64_t available = info.bytesAvailable();
return available;
}
}
return 0;
#endif
}
/**
* @brief 获取代码块运行的时间
* @param callback
* @return
*/
double Tool::getCostTime(std::function<void(void)> callback, const char* callbackName)
{
QElapsedTimer mstimer;
mstimer.start();
callback();
double time = (double)mstimer.nsecsElapsed() / (double)1000000;
qDebug() << callbackName << "cast time:" << time << "ms";
return time;
}