RecordControlApplication/Tool.cpp

140 lines
3.4 KiB
C++
Raw Normal View History

2024-03-04 16:22:40 +08:00
#include "Tool.h"
#include "QTimer"
2024-03-04 16:22:40 +08:00
#include <QDebug>
#include <QDir>
2024-08-12 11:26:42 +08:00
#include <QElapsedTimer>
2024-03-04 16:22:40 +08:00
#include <QFileInfo>
#include <QProcess>
#ifdef Q_OS_WIN
#include <QStorageInfo>
#endif
Tool::Tool()
{
}
/**
* @brief
2024-03-04 16:22:40 +08:00
*/
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
2024-03-04 16:22:40 +08:00
*/
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
2024-03-04 16:22:40 +08:00
*/
#ifdef Q_OS_UNIX
QString Tool::writeCom(QString path)
{
QProcess proc;
QStringList argList;
argList << "-c" << path;
proc.start("/bin/sh", argList);
// 等待进程启动
2024-03-04 16:22:40 +08:00
proc.waitForFinished();
proc.waitForReadyRead();
// 从控制台读取数据
2024-03-04 16:22:40 +08:00
QByteArray procOutput = proc.readAll();
proc.close();
return QString(procOutput);
}
#endif
/**
* @brief
* @param mountedPath
2024-03-04 16:22:40 +08:00
*/
int64_t Tool::getAvailableStorage(QString mountedPath)
{
#ifdef Q_OS_UNIX
// 使用linux命令获取磁盘挂载信息
2024-03-04 16:22:40 +08:00
QString mountInfo = writeCom(QString("df %1").arg(mountedPath));
// 解析挂载信息获取可用磁盘大侠
2024-03-04 16:22:40 +08:00
// "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();
2024-03-04 16:22:40 +08:00
else if (index == 3)
available = s.toLong();
2024-03-04 16:22:40 +08:00
}
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
}
2024-08-12 11:26:42 +08:00
/**
* @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;
}