From 9136e75e265071b199b0b29c19b83b15a8b53eaa Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Wed, 6 Oct 2021 09:14:14 +0800 Subject: [PATCH] chore: improved jpeg format checking, port away from win32only code --- caesium.cpp | 2 +- compressionthread.cpp | 24 ++++++++++-------------- compressionthread.h | 3 +-- global.cpp | 6 ++++++ global.h | 2 ++ 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/caesium.cpp b/caesium.cpp index 8c55396..31bf8cd 100644 --- a/caesium.cpp +++ b/caesium.cpp @@ -1572,7 +1572,7 @@ void Caesium::runPreview() } else if (ui->formatComboBox->currentIndex() == 3) { - if (orig.suffix().toLatin1().toLower() == "jpg") + if (isJpegSuffix(orig.suffix())) { image.save(tempFolder + "/_caesium_/" + rndString + ".cae", "jpg", ui->qualitySlider->value()); } diff --git a/compressionthread.cpp b/compressionthread.cpp index 91f5915..d0e3191 100644 --- a/compressionthread.cpp +++ b/compressionthread.cpp @@ -1,6 +1,7 @@ /******************************************************************************* # # Copyright (C) 2010-2013 Matteo Paonessa +# 2021 Gary Wang # # This file is part of the Caesium distribution. # @@ -150,7 +151,7 @@ void CompressionThread::run() if (sameFormat) { t_format = info.suffix(); - if (t_format.toLower() != "jpg") + if (!isJpegSuffix(t_format)) { t_quality.replace(i, "1"); } @@ -268,7 +269,7 @@ void CompressionThread::noEnlarge() if (sameFormat) { t_format = info.suffix(); - if (t_format.toLower() != "jpg") + if (!isJpegSuffix(t_format)) { t_quality.replace(i, "1"); } @@ -365,19 +366,14 @@ void CompressionThread::noEnlarge() } } -void CompressionThread::keepDate(QString orig, QString dest) +void CompressionThread::keepDate(const QString & orig, const QString & dest) { - HANDLE hFile, hFile2; - - FILETIME ftCreate, ftAccess, ftWrite; - hFile = CreateFile(qStringToWideChar(orig), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); - GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite); - - hFile2 = CreateFile(qStringToWideChar(dest), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - SetFileTime(hFile2, &ftCreate, &ftAccess, &ftWrite); - - CloseHandle(hFile); - CloseHandle(hFile2); + QFile origFile(orig), destFile(dest); + destFile.open(QIODevice::Append); + destFile.setFileTime(origFile.fileTime(QFileDevice::FileAccessTime), QFileDevice::FileAccessTime); + destFile.setFileTime(origFile.fileTime(QFileDevice::FileBirthTime), QFileDevice::FileBirthTime); + destFile.setFileTime(origFile.fileTime(QFileDevice::FileModificationTime), QFileDevice::FileModificationTime); + destFile.close(); } void CompressionThread::optimizePNG(QString file, int level) diff --git a/compressionthread.h b/compressionthread.h index b45a057..fa82f6f 100644 --- a/compressionthread.h +++ b/compressionthread.h @@ -13,8 +13,7 @@ public: QObject *parent); void run(); void noEnlarge(); - void writeToFile(QString orig, QString dest); - void keepDate(QString, QString); + void keepDate(const QString & orig, const QString & dest); void optimizePNG(QString file, int level); private: diff --git a/global.cpp b/global.cpp index 6a27c61..340654a 100644 --- a/global.cpp +++ b/global.cpp @@ -33,3 +33,9 @@ bool same_folder_flag = false; const int version_build = 170; const QString version_string = "1.7.0"; QString opened_list = ""; + +bool isJpegSuffix(const QString &suffix) +{ + const QStringList jpegSuffix({"jpg", "jpeg", "jfif"}); + return (jpegSuffix.contains(suffix.toLower())); +} diff --git a/global.h b/global.h index fff34a4..fbc7917 100644 --- a/global.h +++ b/global.h @@ -11,4 +11,6 @@ extern const int version_build; extern const QString version_string; extern QString opened_list; +bool isJpegSuffix(const QString & suffix); + #endif // GLOBAL_H