Skip to content

Commit

Permalink
Fixes #251: segfault (stopClient) and showing error (not active windo…
Browse files Browse the repository at this point in the history
…w) (#252)

* fix: segfault (stopClient) and showing error (not active window)

* refact+fix: better error messages + avoid showing error dialogs on shutdown

* Use QStringLiteral instead

* Use StatusMessage when window is focused

---------

Co-authored-by: kraxie <6675728+kraxarn@users.noreply.github.com>
  • Loading branch information
rNoz and kraxarn authored Sep 21, 2024
1 parent 62d375d commit d647b20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
{
trayIcon->deleteLater();
event->accept();
mainContent = nullptr;
}
}

Expand Down Expand Up @@ -593,8 +594,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)
Expand Down Expand Up @@ -830,9 +833,20 @@ void MainWindow::resetLibraryPlaylist() const

void MainWindow::onSpotifyStatusChanged(const QString &status)
{
if (status.isEmpty() || mainContent == nullptr)
{
return;
}

const auto message = QString("Spotify client error: %1")
.arg(status);

if ((windowState() & Qt::WindowActive) > 0)
{
QMessageBox::warning(this, QStringLiteral("Client error"),
QString("Failed to start Spotify client: %1").arg(status));
StatusMessage::warn(message);
}
else
{
QMessageBox::warning(this, QStringLiteral("Client error"), message);
}
}
36 changes: 32 additions & 4 deletions src/spotifyclient/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -243,7 +242,36 @@ void SpotifyClient::Runner::onStarted()

void SpotifyClient::Runner::onErrorOccurred(QProcess::ProcessError error)
{
emit statusChanged(QString("Error %1").arg(error));
QString message;

switch (error)
{
case QProcess::FailedToStart:
message = QStringLiteral("Process failed to start");
break;

case QProcess::Crashed:
message = QStringLiteral("Process stopped or crashed");
break;

case QProcess::Timedout:
message = QStringLiteral("Process timed out");
break;

case QProcess::WriteError:
message = QStringLiteral("Process with write error");
break;

case QProcess::ReadError:
message = QStringLiteral("Process with read error");
break;

default:
message = QStringLiteral("Process with unknown error");
break;
}

emit statusChanged(message);
}

auto SpotifyClient::Runner::getLog() -> const std::vector<lib::log_message> &
Expand Down

0 comments on commit d647b20

Please sign in to comment.