From fb6c52852aa7c8b49e128dc04036ed58e41fca01 Mon Sep 17 00:00:00 2001 From: rNoz Date: Sat, 25 May 2024 12:37:57 +0200 Subject: [PATCH 1/4] fix: segfault (stopClient) and showing error (not active window) --- src/mainwindow.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 27fbd95a..85e1ad04 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -593,8 +593,10 @@ auto MainWindow::startClient() -> const SpotifyClient::Runner * void MainWindow::stopClient() { - spotifyRunner->deleteLater(); - spotifyRunner = nullptr; + if (spotifyRunner != nullptr){ + spotifyRunner->deleteLater(); + spotifyRunner = nullptr; + } } void MainWindow::openLyrics(const lib::spt::track &track) @@ -830,9 +832,6 @@ void MainWindow::resetLibraryPlaylist() const void MainWindow::onSpotifyStatusChanged(const QString &status) { - if ((windowState() & Qt::WindowActive) > 0) - { - QMessageBox::warning(this, QStringLiteral("Client error"), - QString("Failed to start Spotify client: %1").arg(status)); - } + QMessageBox::warning(this, QStringLiteral("Client error"), + QString("Failed to start Spotify client: %1").arg(status)); } From 23870b1cfca1df8c45d79f2da9a35f2f537d4aac Mon Sep 17 00:00:00 2001 From: rNoz Date: Sat, 25 May 2024 14:54:32 +0200 Subject: [PATCH 2/4] refact+fix: better error messages + avoid showing error dialogs on shutdown --- src/mainwindow.cpp | 7 +++++-- src/spotifyclient/runner.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 85e1ad04..a7f20009 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -94,6 +94,7 @@ void MainWindow::closeEvent(QCloseEvent *event) { trayIcon->deleteLater(); event->accept(); + mainContent = nullptr; } } @@ -832,6 +833,8 @@ void MainWindow::resetLibraryPlaylist() const void MainWindow::onSpotifyStatusChanged(const QString &status) { - QMessageBox::warning(this, QStringLiteral("Client error"), - QString("Failed to start Spotify client: %1").arg(status)); + if (!status.isEmpty() && mainContent != nullptr){ + QMessageBox::warning(this, QStringLiteral("Client error"), + QString("Spotify client error: %1").arg(status)); + } } diff --git a/src/spotifyclient/runner.cpp b/src/spotifyclient/runner.cpp index 6bef9b0c..0e8cac2c 100644 --- a/src/spotifyclient/runner.cpp +++ b/src/spotifyclient/runner.cpp @@ -198,18 +198,17 @@ void SpotifyClient::Runner::logOutput(const QByteArray &output, lib::log_type lo log.emplace_back(lib::date_time::now(), logType, line.toStdString()); -#ifdef USE_KEYCHAIN if (line.contains(QStringLiteral("Bad credentials"))) { +#ifdef USE_KEYCHAIN const auto username = QString::fromStdString(settings.spotify.username); if (Keychain::clearPassword(username)) { lib::log::warn("Bad credentials, cleared saved password"); } - +#endif emit statusChanged(QStringLiteral("Bad credentials, please try again")); } -#endif } } @@ -243,7 +242,29 @@ void SpotifyClient::Runner::onStarted() void SpotifyClient::Runner::onErrorOccurred(QProcess::ProcessError error) { - emit statusChanged(QString("Error %1").arg(error)); + std::string_view message; + switch (error) + { + case QProcess::FailedToStart: + message = "Process failed to start"; + break; + case QProcess::Crashed: + message = "Process stopped or crashed"; + break; + case QProcess::Timedout: + message = "Process timed out"; + break; + case QProcess::WriteError: + message = "Process with write error"; + break; + case QProcess::ReadError: + message = "Process with read error"; + break; + default: + message = "Process with unknown error"; + break; + } + emit statusChanged(QString::fromUtf8(message.data(), message.size())); } auto SpotifyClient::Runner::getLog() -> const std::vector & From dceb58ee1ef0953bde339d1a861a57344bea3103 Mon Sep 17 00:00:00 2001 From: kraxie <6675728+kraxarn@users.noreply.github.com> Date: Sat, 21 Sep 2024 13:38:28 +0200 Subject: [PATCH 3/4] Use QStringLiteral instead --- src/spotifyclient/runner.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/spotifyclient/runner.cpp b/src/spotifyclient/runner.cpp index 0e8cac2c..ec451d4a 100644 --- a/src/spotifyclient/runner.cpp +++ b/src/spotifyclient/runner.cpp @@ -242,29 +242,36 @@ void SpotifyClient::Runner::onStarted() void SpotifyClient::Runner::onErrorOccurred(QProcess::ProcessError error) { - std::string_view message; + QString message; + switch (error) { case QProcess::FailedToStart: - message = "Process failed to start"; + message = QStringLiteral("Process failed to start"); break; + case QProcess::Crashed: - message = "Process stopped or crashed"; + message = QStringLiteral("Process stopped or crashed"); break; + case QProcess::Timedout: - message = "Process timed out"; + message = QStringLiteral("Process timed out"); break; + case QProcess::WriteError: - message = "Process with write error"; + message = QStringLiteral("Process with write error"); break; + case QProcess::ReadError: - message = "Process with read error"; + message = QStringLiteral("Process with read error"); break; + default: - message = "Process with unknown error"; + message = QStringLiteral("Process with unknown error"); break; } - emit statusChanged(QString::fromUtf8(message.data(), message.size())); + + emit statusChanged(message); } auto SpotifyClient::Runner::getLog() -> const std::vector & From 5f6f14e562ec593613897a3355030154beece8a0 Mon Sep 17 00:00:00 2001 From: kraxie <6675728+kraxarn@users.noreply.github.com> Date: Sat, 21 Sep 2024 13:48:20 +0200 Subject: [PATCH 4/4] Use StatusMessage when window is focused --- src/mainwindow.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a7f20009..8e104f3b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -833,8 +833,20 @@ void MainWindow::resetLibraryPlaylist() const void MainWindow::onSpotifyStatusChanged(const QString &status) { - if (!status.isEmpty() && mainContent != nullptr){ - QMessageBox::warning(this, QStringLiteral("Client error"), - QString("Spotify client error: %1").arg(status)); + if (status.isEmpty() || mainContent == nullptr) + { + return; + } + + const auto message = QString("Spotify client error: %1") + .arg(status); + + if ((windowState() & Qt::WindowActive) > 0) + { + StatusMessage::warn(message); + } + else + { + QMessageBox::warning(this, QStringLiteral("Client error"), message); } }