From 7723783f3cbc6441f129c5cd70e6ec2cc1bb6f14 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 10 Oct 2021 14:40:12 +0800 Subject: [PATCH] chore: continue port away from win32 api --- caesium.cpp | 48 ++--------------------------------- compressionthread.cpp | 58 +++++-------------------------------------- global.cpp | 10 ++++++++ global.h | 1 + 4 files changed, 19 insertions(+), 98 deletions(-) diff --git a/caesium.cpp b/caesium.cpp index 56bdf60..03ca168 100644 --- a/caesium.cpp +++ b/caesium.cpp @@ -56,8 +56,6 @@ #include #include #include -#include -#include QElapsedTimer t; int error_count = 0, iteration_count = 0, skipped_count; @@ -72,48 +70,6 @@ int item_count, saved_space; QImage currentImage; double img_ratio; -WCHAR *m_qStringToWideChar(const QString &str) -{ - if (str.isNull()) - return 0; - WCHAR *result = new WCHAR[str.length() + 1]; - for (int i = 0; i < str.length(); ++i) - result[i] = str[i].unicode(); - result[str.length()] = 0; - return result; -} - -void m_startProcess(QString argv) -{ - WCHAR* exe = m_qStringToWideChar(argv); - STARTUPINFO si; - PROCESS_INFORMATION pi; - - ZeroMemory( &si, sizeof(si) ); - si.cb = sizeof(si); - ZeroMemory( &pi, sizeof(pi) ); - - if( !CreateProcessW( NULL, - exe, - NULL, - NULL, - FALSE, - CREATE_NO_WINDOW , - NULL, - NULL, - &si, - &pi ) - ) - { - return; - } - - WaitForSingleObject( pi.hProcess, INFINITE ); - - CloseHandle( pi.hProcess ); - CloseHandle( pi.hThread ); -} - void removeDir(QString path) { QDir dir(path); @@ -1587,8 +1543,8 @@ void Caesium::runPreview() } if (settings.value("Preferences/exif").value()) { - QString exec = "tools\\exif_copy.exe \"" + ui->listTreeWidget->selectedItems().at(i)->text(7) + "\" \"" + tempFolder + "/_caesium_/" + rndString + ".cae\""; - m_startProcess(exec); + QString exec = "\"" + ui->listTreeWidget->selectedItems().at(i)->text(7) + "\" \"" + tempFolder + "/_caesium_/" + rndString + ".cae\""; + startBlockingProcess("tools\\exif_copy.exe", exec); } ui->listTreeWidget->selectedItems().at(i)->setText(2, fixedSize(info.size(), 0)); ui->listTreeWidget->selectedItems().at(i)->setText(3, getRatio(orig.size(), info.size())); diff --git a/compressionthread.cpp b/compressionthread.cpp index d0e3191..eeb2f46 100644 --- a/compressionthread.cpp +++ b/compressionthread.cpp @@ -37,54 +37,8 @@ #include #include -#include -#include -#include #include - -WCHAR *qStringToWideChar(const QString &str) -{ - if (str.isNull()) - return 0; - WCHAR *result = new WCHAR[str.length() + 1]; - for (int i = 0; i < str.length(); ++i) - result[i] = str[i].unicode(); - result[str.length()] = 0; - return result; -} - -void startProcess(QString argv) -{ - WCHAR* exe = qStringToWideChar(argv); - STARTUPINFO si; - PROCESS_INFORMATION pi; - - ZeroMemory( &si, sizeof(si) ); - si.cb = sizeof(si); - ZeroMemory( &pi, sizeof(pi) ); - - if( !CreateProcessW( NULL, - exe, - NULL, - NULL, - FALSE, - CREATE_NO_WINDOW, - NULL, - NULL, - &si, - &pi ) - ) - { - return; - } - - WaitForSingleObject( pi.hProcess, INFINITE ); - - CloseHandle( pi.hProcess ); - CloseHandle( pi.hThread ); -} - CompressionThread::CompressionThread(QStringList list, QString orig_dir, QString dir, QString format, QString suffix, QStringList quality, bool checked, QList w, QList h, bool fixed, bool keepARatio, bool notEnlarge, bool resize, bool structure, QObject *parent) : QThread(parent) @@ -217,8 +171,8 @@ void CompressionThread::run() if (settings.value("Preferences/exif").value() && t_format.toLower() == "jpg") { - QString exec = "tools\\exif_copy.exe \"" + t_list.at(i) + "\" \"" + t_dir + "\\" + info.completeBaseName() + t_suffix +"\""; - startProcess(exec); + QString exec = "\"" + t_list.at(i) + "\" \"" + t_dir + "\\" + info.completeBaseName() + t_suffix +"\""; + startBlockingProcess("tools\\exif_copy.exe", exec); } QFile newImage(t_dir + "/" + info.completeBaseName() + t_suffix); QFileInfo newInfo; @@ -329,8 +283,8 @@ void CompressionThread::noEnlarge() if (settings.value("Preferences/exif").value() && t_format.toLower() == "jpg") { - QString exec = "tools\\exif_copy.exe \"" + t_list.at(i) + "\" \"" + t_dir + "\\" + info.completeBaseName() + t_suffix + "." + t_format + "\""; - startProcess(exec); + QString exec = "\"" + t_list.at(i) + "\" \"" + t_dir + "\\" + info.completeBaseName() + t_suffix + "." + t_format + "\""; + startBlockingProcess("tools\\exif_copy.exe", exec); } QFileInfo newInfo(t_dir + "/" + info.completeBaseName() + t_suffix + "." + t_format + ".ckd"); @@ -378,6 +332,6 @@ void CompressionThread::keepDate(const QString & orig, const QString & dest) void CompressionThread::optimizePNG(QString file, int level) { - QString exec = "tools\\optipng.exe -o" + QString::number(level) + " \"" + file + "\""; - startProcess(exec); + QString exec = "-o" + QString::number(level) + " \"" + file + "\""; + startBlockingProcess("tools\\optipng.exe", exec); } diff --git a/global.cpp b/global.cpp index 340654a..7e3c2da 100644 --- a/global.cpp +++ b/global.cpp @@ -27,6 +27,8 @@ #include #include "global.h" +#include + QSettings settings(QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation).at(0) + "/Caesium/settings.ini", QSettings::IniFormat); QString out_folder = ""; bool same_folder_flag = false; @@ -39,3 +41,11 @@ bool isJpegSuffix(const QString &suffix) const QStringList jpegSuffix({"jpg", "jpeg", "jfif"}); return (jpegSuffix.contains(suffix.toLower())); } + +void startBlockingProcess(const QString & program, const QString & argv) +{ + QProcess proc; + proc.start(program, QProcess::splitCommand(argv)); + proc.waitForFinished(INT_MAX); + return; +} diff --git a/global.h b/global.h index fbc7917..bcfa0af 100644 --- a/global.h +++ b/global.h @@ -12,5 +12,6 @@ extern const QString version_string; extern QString opened_list; bool isJpegSuffix(const QString & suffix); +void startBlockingProcess(const QString &program, const QString & exec); #endif // GLOBAL_H