Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 135 additions & 1 deletion src/source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,52 @@
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
#include <QScreen>
#include <QFormLayout>

Check warning on line 55 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFormLayout> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QShortcut>

Check warning on line 56 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QShortcut> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonObject>

Check warning on line 57 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 58 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 59 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDir>

Check warning on line 60 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDirIterator>

Check warning on line 61 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDirIterator> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QUuid>

Check warning on line 62 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QUuid> not found. Please note: Cppcheck does not need standard library headers to get proper results.

static QMutex mutex; // 静态全局变量只在定义该变量的源文件内有效
#define FILE_TRUNCATION_LENGTH 70

namespace {

bool copyDirectoryRecursively(const QString &sourcePath, const QString &targetPath)
{
QDir sourceDir(sourcePath);
if (!sourceDir.exists()) {
return false;
}

if (!QDir().mkpath(targetPath)) {
return false;
}

const QFileInfoList entries = sourceDir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files | QDir::Hidden | QDir::System);
for (const QFileInfo &info : entries) {
const QString srcFilePath = info.absoluteFilePath();
const QString dstFilePath = targetPath + QDir::separator() + info.fileName();
if (info.isDir()) {
if (!copyDirectoryRecursively(srcFilePath, dstFilePath)) {
return false;
}
} else {
QFile::remove(dstFilePath);
if (!QFile::copy(srcFilePath, dstFilePath)) {
return false;
}
}
}

return true;
}

} // namespace

MainWindow::MainWindow(QWidget *parent)
: DMainWindow(parent)
, m_strProcessID(QString::number(QCoreApplication::applicationPid())) // 获取应用进程号
Expand Down Expand Up @@ -1181,6 +1220,9 @@

// 构建压缩文件数据
listEntry = m_pCompressPage->getEntrys();
if (!prepareCompressAliasEntries(listEntry)) {
return;
}
strDestination = m_stCompressParameter.strTargetPath + QDir::separator() + m_stCompressParameter.strArchiveName;

// 构建压缩参数
Expand Down Expand Up @@ -1255,6 +1297,7 @@
m_ePageID = PI_CompressProgress;
refreshPage();
} else {
cleanupCompressAliasEntries();
// 无可用插件
showErrorMessage(FI_Compress, EI_NoPlugin);
}
Expand Down Expand Up @@ -1494,6 +1537,8 @@
// zip压缩包添加注释
qDebug() << "Adding archive comment";
addArchiveComment();

cleanupCompressAliasEntries();
}
break;
// 添加文件至压缩包
Expand Down Expand Up @@ -1734,6 +1779,7 @@
} else {
m_ePageID = PI_Compress;
}
cleanupCompressAliasEntries();
}
break;
// 添加文件至压缩包
Expand Down Expand Up @@ -1832,7 +1878,7 @@
break;
}
}

cleanupCompressAliasEntries();
}
break;
// 压缩包追加文件错误
Expand Down Expand Up @@ -2129,6 +2175,8 @@
maxFileSize_ = 0;
#endif

cleanupCompressAliasEntries();

m_ePageID = PI_Home;
m_operationtype = Operation_NULL; // 重置操作类型
m_iCompressedWatchTimerID = 0; // 初始化定时器返回值
Expand Down Expand Up @@ -2294,6 +2342,92 @@
}
}

bool MainWindow::prepareCompressAliasEntries(QList<FileEntry> &listEntry)
{
m_needCleanupCompressAlias = false;
m_strCompressAliasRoot.clear();

bool hasAlias = false;
QString aliasRoot;

for (FileEntry &entry : listEntry) {
if (entry.strAlias.isEmpty()) {
continue;
}

if (entry.strFullPath.isEmpty()) {
continue;
}

if (!hasAlias) {
const QString baseDir = TEMPPATH + QDir::separator() + m_strProcessID + QDir::separator() + "compress_alias";
if (!QDir().mkpath(baseDir)) {
showWarningDialog(tr("Failed to create temporary directory, please check and try again."));
return false;
}
aliasRoot = baseDir + QDir::separator() + QUuid::createUuid().toString(QUuid::WithoutBraces);
if (!QDir().mkpath(aliasRoot)) {
showWarningDialog(tr("Failed to create temporary directory, please check and try again."));
return false;
}
}

const QString aliasName = entry.strAlias;
const QString targetPath = aliasRoot + QDir::separator() + aliasName;

if (entry.isDirectory) {
QDir(targetPath).removeRecursively();
if (!copyDirectoryRecursively(entry.strFullPath, targetPath)) {
showWarningDialog(tr("Failed to prepare renamed item \"%1\" for compression, please check permissions and available space.")
.arg(aliasName));
if (!aliasRoot.isEmpty()) {
QDir(aliasRoot).removeRecursively();
}
return false;
}
} else {
QFile::remove(targetPath);
if (!QFile::copy(entry.strFullPath, targetPath)) {
showWarningDialog(tr("Failed to prepare renamed item \"%1\" for compression, please check permissions and available space.")
.arg(aliasName));
if (!aliasRoot.isEmpty()) {
QDir(aliasRoot).removeRecursively();
}
return false;
}
}

entry.strFullPath = targetPath;
entry.strAlias.clear();
hasAlias = true;
}

if (hasAlias) {
m_strCompressAliasRoot = aliasRoot;
m_needCleanupCompressAlias = true;
} else {
m_strCompressAliasRoot.clear();
m_needCleanupCompressAlias = false;
}

return true;
}

void MainWindow::cleanupCompressAliasEntries()
{
if (m_strCompressAliasRoot.isEmpty()) {
m_needCleanupCompressAlias = false;
return;
}

QDir aliasRoot(m_strCompressAliasRoot);
if (aliasRoot.exists()) {
aliasRoot.removeRecursively();
}
m_strCompressAliasRoot.clear();
m_needCleanupCompressAlias = false;
}

void MainWindow::showSuccessInfo(SuccessInfo eSuccessInfo, ErrorType eErrorType)
{
m_pSuccessPage->setSuccessType(eSuccessInfo);
Expand Down
15 changes: 14 additions & 1 deletion src/source/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ public :
*/
void ConstructAddOptionsByThread(const QString &path);

/**
* @brief prepareCompressAliasEntries 为重命名文件准备临时别名文件
* @param listEntry 待压缩的文件信息
* @return 是否准备成功
*/
bool prepareCompressAliasEntries(QList<FileEntry> &listEntry);

/**
* @brief cleanupCompressAliasEntries 清理临时别名文件
*/
void cleanupCompressAliasEntries();

/**
* @brief showSuccessInfo 显示成功信息
* @param eSuccessInfo 成功信息
Expand Down Expand Up @@ -576,7 +588,8 @@ private Q_SLOTS:
#endif

QString m_strCurrentName;

QString m_strCompressAliasRoot;
bool m_needCleanupCompressAlias = false;
ApplicationAdaptor *m_compressorInterface = nullptr; // DBus interface
};

Expand Down