2024-03-04 16:22:40 +08:00
|
|
|
|
#include "Tool.h"
|
2024-12-24 03:51:48 +08:00
|
|
|
|
#include "DatabaseManager.h"
|
2024-08-22 15:29:39 +08:00
|
|
|
|
#include "QTimer"
|
2024-12-24 03:51:48 +08:00
|
|
|
|
#include "TimeSlider.h"
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-05 13:47:32 +08:00
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-05 13:47:32 +08:00
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-05 13:47:32 +08:00
|
|
|
|
* @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-05 13:47:32 +08:00
|
|
|
|
// 等待进程启动
|
2024-03-04 16:22:40 +08:00
|
|
|
|
proc.waitForFinished();
|
|
|
|
|
proc.waitForReadyRead();
|
2024-03-05 13:47:32 +08:00
|
|
|
|
// 从控制台读取数据
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QByteArray procOutput = proc.readAll();
|
|
|
|
|
proc.close();
|
|
|
|
|
return QString(procOutput);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-05 13:47:32 +08:00
|
|
|
|
* @brief 获取磁盘的可用空间
|
|
|
|
|
* @param mountedPath 磁盘挂载位置
|
2024-03-04 16:22:40 +08:00
|
|
|
|
*/
|
|
|
|
|
int64_t Tool::getAvailableStorage(QString mountedPath)
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_UNIX
|
2024-03-05 13:47:32 +08:00
|
|
|
|
// 使用linux命令获取磁盘挂载信息
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QString mountInfo = writeCom(QString("df %1").arg(mountedPath));
|
2024-03-05 13:47:32 +08:00
|
|
|
|
// 解析挂载信息获取可用磁盘大侠
|
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)
|
2024-05-11 15:40:07 +08:00
|
|
|
|
used = s.toLong();
|
2024-03-04 16:22:40 +08:00
|
|
|
|
else if (index == 3)
|
2024-05-11 15:40:07 +08:00
|
|
|
|
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;
|
2024-12-24 03:51:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 从数据库中获取某天的文件信息
|
|
|
|
|
* @param chn
|
|
|
|
|
* @param params
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
QList<TimeSlider::TimeSegment> Tool::calTimeSegments(QVariantMap params)
|
|
|
|
|
{
|
|
|
|
|
QList<TimeSlider::TimeSegment> result;
|
|
|
|
|
DatabaseManager* db = DatabaseManager::getInstace();
|
|
|
|
|
QList<DatabaseManager::File> fileList = db->get(params);
|
|
|
|
|
if (fileList.isEmpty()) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
// 构造时间片信息列表
|
|
|
|
|
for (const auto& file : fileList) {
|
|
|
|
|
TimeSlider::TimeSegment seg;
|
|
|
|
|
QString startTime;
|
|
|
|
|
QStringList timeList = file.datetime.split(" ");
|
|
|
|
|
if (timeList.length() < 2) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
seg.startTime = QTime(0, 0, 0).secsTo(QTime::fromString(timeList[1], "hh:mm:ss"));
|
|
|
|
|
seg.duration = file.duration;
|
|
|
|
|
seg.filename = file.filename;
|
|
|
|
|
result.push_back(seg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 对时间片信息进行排序
|
|
|
|
|
std::sort(result.begin(), result.end(), [](const TimeSlider::TimeSegment& a, const TimeSlider::TimeSegment& b) {
|
|
|
|
|
return a.startTime < b.startTime;
|
|
|
|
|
});
|
|
|
|
|
return result;
|
2024-08-22 15:29:39 +08:00
|
|
|
|
}
|