177 lines
4.6 KiB
C++
Executable File
177 lines
4.6 KiB
C++
Executable File
#include "Tool.h"
|
||
#include "DatabaseManager.h"
|
||
#include "QTimer"
|
||
#include "TimeSlider.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;
|
||
}
|
||
|
||
/**
|
||
* @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;
|
||
} |