RecordControlApplication/Tool.cpp

122 lines
2.9 KiB
C++
Executable File

#include "Tool.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#ifdef Q_OS_WIN
#include <QStorageInfo>
#endif
Tool::Tool()
{
}
/**
* @brief get file list of
*/
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 remove file or dir
*/
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 call linux shell tool
*/
#ifdef Q_OS_UNIX
QString Tool::writeCom(QString path)
{
QProcess proc;
QStringList argList;
argList << "-c" << path;
proc.start("/bin/sh", argList);
// wait process start
proc.waitForFinished();
proc.waitForReadyRead();
// read data from console
QByteArray procOutput = proc.readAll();
proc.close();
return QString(procOutput);
}
#endif
/**
* get available storage of disk
*/
int64_t Tool::getAvailableStorage(QString mountedPath)
{
#ifdef Q_OS_UNIX
// get the available size of disk
QString mountInfo = writeCom(QString("df %1").arg(mountedPath));
// parse the string like
// "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.toInt();
else if (index == 3)
available = s.toInt();
}
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
}