Qt: Purge format string usage from AutoUpdaterDialog

This commit is contained in:
Stenzek 2024-07-19 13:54:00 +10:00
parent ca2a8f1e1f
commit aa94b07f30
No known key found for this signature in database
2 changed files with 25 additions and 29 deletions

View File

@ -215,14 +215,9 @@ std::string AutoUpdaterDialog::getCurrentUpdateTag() const
#endif #endif
} }
void AutoUpdaterDialog::reportError(const char* msg, ...) void AutoUpdaterDialog::reportError(const std::string_view msg)
{ {
std::va_list ap; QMessageBox::critical(this, tr("Updater Error"), QtUtils::StringViewToQString(msg));
va_start(ap, msg);
std::string full_msg = StringUtil::StdStringFromFormatV(msg, ap);
va_end(ap);
QMessageBox::critical(this, tr("Updater Error"), QString::fromStdString(full_msg));
} }
bool AutoUpdaterDialog::ensureHttpReady() bool AutoUpdaterDialog::ensureHttpReady()
@ -333,7 +328,7 @@ void AutoUpdaterDialog::getLatestTagComplete(s32 status_code, std::vector<u8> re
} }
if (m_display_messages) if (m_display_messages)
reportError("%s release not found in JSON", selected_tag.c_str()); reportError(fmt::format("{} release not found in JSON", selected_tag));
} }
else else
{ {
@ -344,7 +339,7 @@ void AutoUpdaterDialog::getLatestTagComplete(s32 status_code, std::vector<u8> re
else else
{ {
if (m_display_messages) if (m_display_messages)
reportError("Failed to download latest tag info: HTTP %d", status_code); reportError(fmt::format("Failed to download latest tag info: HTTP {}", status_code));
} }
emit updateCheckCompleted(); emit updateCheckCompleted();
@ -402,7 +397,7 @@ void AutoUpdaterDialog::getLatestReleaseComplete(s32 status_code, std::vector<u8
} }
else else
{ {
reportError("Failed to download latest release info: HTTP %d", status_code); reportError(fmt::format("Failed to download latest release info: HTTP {}", status_code));
} }
emit updateCheckCompleted(); emit updateCheckCompleted();
@ -490,7 +485,7 @@ void AutoUpdaterDialog::getChangesComplete(s32 status_code, std::vector<u8> resp
} }
else else
{ {
reportError("Failed to download change list: HTTP %d", status_code); reportError(fmt::format("Failed to download change list: HTTP {}", status_code));
} }
#endif #endif
} }
@ -514,7 +509,7 @@ void AutoUpdaterDialog::downloadUpdateClicked()
if (status_code != HTTPDownloader::HTTP_STATUS_OK) if (status_code != HTTPDownloader::HTTP_STATUS_OK)
{ {
reportError("Download failed: %d", status_code); reportError(fmt::format("Download failed: HTTP status code {}", status_code));
download_result = false; download_result = false;
return; return;
} }
@ -611,14 +606,14 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
if (!FileSystem::WriteBinaryFile(update_zip_path.c_str(), update_data.data(), update_data.size())) if (!FileSystem::WriteBinaryFile(update_zip_path.c_str(), update_data.data(), update_data.size()))
{ {
reportError("Writing update zip to '%s' failed", update_zip_path.c_str()); reportError(fmt::format("Writing update zip to '{}' failed", update_zip_path));
return false; return false;
} }
Error updater_extract_error; Error updater_extract_error;
if (!extractUpdater(update_zip_path.c_str(), updater_path.c_str(), &updater_extract_error)) if (!extractUpdater(update_zip_path.c_str(), updater_path.c_str(), &updater_extract_error))
{ {
reportError("Extracting updater failed: %s", updater_extract_error.GetDescription().c_str()); reportError(fmt::format("Extracting updater failed: {}", updater_extract_error.GetDescription()));
return false; return false;
} }
@ -707,8 +702,8 @@ bool AutoUpdaterDialog::doUpdate(const std::string& application_dir, const std::
sei.nShow = SW_SHOWNORMAL; sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteExW(&sei)) if (!ShellExecuteExW(&sei))
{ {
reportError("Failed to start %s: %s", needs_elevation ? "elevated updater" : "updater", reportError(fmt::format("Failed to start {}: {}", needs_elevation ? "elevated updater" : "updater",
Error::CreateWin32(GetLastError()).GetDescription().c_str()); Error::CreateWin32(GetLastError()).GetDescription()));
return false; return false;
} }
@ -746,12 +741,12 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
QFileInfo info(QString::fromStdString(bundle_path.value())); QFileInfo info(QString::fromStdString(bundle_path.value()));
if (!info.isBundle()) if (!info.isBundle())
{ {
reportError("Application %s isn't a bundle.", bundle_path->c_str()); reportError(fmt::format("Application {} isn't a bundle.", bundle_path.value()));
return false; return false;
} }
if (info.suffix() != QStringLiteral("app")) if (info.suffix() != QStringLiteral("app"))
{ {
reportError("Unexpected application suffix %s on %s.", info.suffix().toUtf8().constData(), bundle_path->c_str()); reportError(fmt::format("Unexpected application suffix {} on {}.", info.suffix().toStdString(), bundle_path.value()));
return false; return false;
} }
@ -759,7 +754,7 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
const std::string updater_app = Path::Combine(bundle_path.value(), "Contents/Resources/Updater.app"); const std::string updater_app = Path::Combine(bundle_path.value(), "Contents/Resources/Updater.app");
if (!FileSystem::DirectoryExists(updater_app.c_str())) if (!FileSystem::DirectoryExists(updater_app.c_str()))
{ {
reportError("Failed to find updater at %s.", updater_app.c_str()); reportError(fmt::format("Failed to find updater at {}.", updater_app));
return false; return false;
} }
@ -779,7 +774,7 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
zip_file.write(reinterpret_cast<const char*>(update_data.data()), static_cast<qint64>(update_data.size())) != zip_file.write(reinterpret_cast<const char*>(update_data.data()), static_cast<qint64>(update_data.size())) !=
static_cast<qint64>(update_data.size())) static_cast<qint64>(update_data.size()))
{ {
reportError("Writing update zip to '%s' failed", zip_path.c_str()); reportError(fmt::format("Writing update zip to '{}' failed", zip_path));
return false; return false;
} }
zip_file.close(); zip_file.close();
@ -817,25 +812,25 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
const QString qappimage_path(QString::fromUtf8(appimage_path)); const QString qappimage_path(QString::fromUtf8(appimage_path));
if (!QFile::exists(qappimage_path)) if (!QFile::exists(qappimage_path))
{ {
reportError("Current AppImage does not exist: %s", appimage_path); reportError(fmt::format("Current AppImage does not exist: {}", appimage_path));
return false; return false;
} }
const QString new_appimage_path(qappimage_path + QStringLiteral(".new")); const QString new_appimage_path(qappimage_path + QStringLiteral(".new"));
const QString backup_appimage_path(qappimage_path + QStringLiteral(".backup")); const QString backup_appimage_path(qappimage_path + QStringLiteral(".backup"));
INFO_LOG("APPIMAGE = {}", appimage_path); INFO_LOG("APPIMAGE = {}", appimage_path);
INFO_LOG("Backup AppImage path = {}", backup_appimage_path.toUtf8().constData()); INFO_LOG("Backup AppImage path = {}", backup_appimage_path.toStdString());
INFO_LOG("New AppImage path = {}", new_appimage_path.toUtf8().constData()); INFO_LOG("New AppImage path = {}", new_appimage_path.toStdString());
// Remove old "new" appimage and existing backup appimage. // Remove old "new" appimage and existing backup appimage.
if (QFile::exists(new_appimage_path) && !QFile::remove(new_appimage_path)) if (QFile::exists(new_appimage_path) && !QFile::remove(new_appimage_path))
{ {
reportError("Failed to remove old destination AppImage: %s", new_appimage_path.toUtf8().constData()); reportError(fmt::format("Failed to remove old destination AppImage: {}", new_appimage_path.toStdString()));
return false; return false;
} }
if (QFile::exists(backup_appimage_path) && !QFile::remove(backup_appimage_path)) if (QFile::exists(backup_appimage_path) && !QFile::remove(backup_appimage_path))
{ {
reportError("Failed to remove old backup AppImage: %s", new_appimage_path.toUtf8().constData()); reportError(fmt::format("Failed to remove old backup AppImage: {}", new_appimage_path.toStdString()));
return false; return false;
} }
@ -851,7 +846,7 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
!new_file.setPermissions(old_permissions)) !new_file.setPermissions(old_permissions))
{ {
QFile::remove(new_appimage_path); QFile::remove(new_appimage_path);
reportError("Failed to write new destination AppImage: %s", new_appimage_path.toUtf8().constData()); reportError(fmt::format("Failed to write new destination AppImage: {}", new_appimage_path.toStdString()));
return false; return false;
} }
} }
@ -859,7 +854,7 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
// Rename "old" appimage. // Rename "old" appimage.
if (!QFile::rename(qappimage_path, backup_appimage_path)) if (!QFile::rename(qappimage_path, backup_appimage_path))
{ {
reportError("Failed to rename old AppImage to %s", backup_appimage_path.toUtf8().constData()); reportError(fmt::format("Failed to rename old AppImage to {}", backup_appimage_path.toStdString()));
QFile::remove(new_appimage_path); QFile::remove(new_appimage_path);
return false; return false;
} }
@ -867,7 +862,7 @@ bool AutoUpdaterDialog::processUpdate(const std::vector<u8>& update_data)
// Rename "new" appimage. // Rename "new" appimage.
if (!QFile::rename(new_appimage_path, qappimage_path)) if (!QFile::rename(new_appimage_path, qappimage_path))
{ {
reportError("Failed to rename new AppImage to %s", qappimage_path.toUtf8().constData()); reportError(fmt::format("Failed to rename new AppImage to {}", qappimage_path.toStdString()));
return false; return false;
} }

View File

@ -9,6 +9,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <string_view>
#include <QtCore/QDateTime> #include <QtCore/QDateTime>
#include <QtCore/QStringList> #include <QtCore/QStringList>
@ -49,7 +50,7 @@ private Q_SLOTS:
void remindMeLaterClicked(); void remindMeLaterClicked();
private: private:
void reportError(const char* msg, ...); void reportError(const std::string_view msg);
bool ensureHttpReady(); bool ensureHttpReady();