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
138 changes: 138 additions & 0 deletions src/source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,56 @@
#include <QtConcurrent/QtConcurrent>
#include <QScreen>
#include <QFormLayout>
#include <QShortcut>

Check warning on line 50 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 51 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 <QTimer>

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 53 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 54 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 55 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 56 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 57 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.
#ifdef DTKCORE_CLASS_DConfigFile
#include <DConfig>

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

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 @@ -1015,6 +1054,11 @@

// 构建压缩文件数据
listEntry = m_pCompressPage->getEntrys();

if (!prepareCompressAliasEntries(listEntry)) {
return;
}

strDestination = m_stCompressParameter.strTargetPath + QDir::separator() + m_stCompressParameter.strArchiveName;

// 构建压缩参数
Expand Down Expand Up @@ -1089,6 +1133,7 @@
m_ePageID = PI_CompressProgress;
refreshPage();
} else {
cleanupCompressAliasEntries();
// 无可用插件
showErrorMessage(FI_Compress, EI_NoPlugin);
}
Expand Down Expand Up @@ -1305,6 +1350,8 @@

// zip压缩包添加注释
addArchiveComment();

cleanupCompressAliasEntries();
}
break;
// 添加文件至压缩包
Expand Down Expand Up @@ -1544,6 +1591,7 @@
} else {
m_ePageID = PI_Compress;
}
cleanupCompressAliasEntries();
}
break;
// 添加文件至压缩包
Expand Down Expand Up @@ -1643,6 +1691,8 @@
}
}

cleanupCompressAliasEntries();

}
break;
// 压缩包追加文件错误
Expand Down Expand Up @@ -1938,6 +1988,8 @@
maxFileSize_ = 0;
#endif

cleanupCompressAliasEntries();

m_ePageID = PI_Home;
m_operationtype = Operation_NULL; // 重置操作类型
m_iCompressedWatchTimerID = 0; // 初始化定时器返回值
Expand Down Expand Up @@ -2079,6 +2131,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)) {
Comment on lines +2178 to +2179
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Unconditionally removing targetPath may cause issues if the file is in use.

If QFile::remove fails due to the file being in use, the subsequent copy may also fail. Please handle the return value of QFile::remove to ensure robust error handling.

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
14 changes: 14 additions & 0 deletions src/source/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,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 @@ -575,6 +587,8 @@ private Q_SLOTS:
#endif

QString m_strCurrentName;
QString m_strCompressAliasRoot;
bool m_needCleanupCompressAlias = false;
};

class TitleWidget : public QWidget
Expand Down