Skip to content

Commit

Permalink
Replace login helper with argument on run
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Nov 17, 2024
1 parent 42a9bdf commit c79c4ac
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 166 deletions.
1 change: 0 additions & 1 deletion src/spotifyclient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
target_sources(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/login.cpp
${CMAKE_CURRENT_SOURCE_DIR}/runner.cpp
)
78 changes: 0 additions & 78 deletions src/spotifyclient/login.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions src/spotifyclient/login.hpp

This file was deleted.

65 changes: 14 additions & 51 deletions src/spotifyclient/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ void SpotifyClient::Runner::start()
return;
}

// Common arguments
QStringList arguments({
"--bitrate", QString::number(static_cast<int>(settings.spotify.bitrate)),
});

// Check if not logged in
if (!isLoggedIn())
{
Expand All @@ -65,15 +70,9 @@ void SpotifyClient::Runner::start()
return;
}

login();
return;
arguments.append(QStringLiteral("--enable-oauth"));
}

// Common arguments
QStringList arguments({
"--bitrate", QString::number(static_cast<int>(settings.spotify.bitrate)),
});

const auto initialVolume = QString::number(settings.spotify.volume);

// librespot specific
Expand Down Expand Up @@ -145,26 +144,6 @@ void SpotifyClient::Runner::start()
process->start(path, arguments);
}

void SpotifyClient::Runner::login()
{
if (loginHelper != nullptr)
{
loginHelper->deleteLater();
loginHelper = nullptr;
}

loginHelper = new Login(parentWidget);

connect(loginHelper, &Login::loginSuccess,
this, &Runner::onLoginSuccess);

connect(loginHelper, &Login::loginFailed,
this, &Runner::onLoginFailed);

const auto cachePath = QString::fromStdString(getCachePath().string());
loginHelper->run(path, cachePath);
}

auto SpotifyClient::Runner::isRunning() const -> bool
{
return process == nullptr
Expand All @@ -183,6 +162,14 @@ void SpotifyClient::Runner::logOutput(const QByteArray &output, lib::log_type lo

log.emplace_back(lib::date_time::now(), logType, line.toStdString());

const auto urlIndex = line.indexOf(QStringLiteral("https://accounts.spotify.com/authorize"));
if (urlIndex >= 0)
{
const auto url = line.right(line.length() - urlIndex);
auto *parent = qobject_cast<QWidget *>(QObject::parent());
Url::open(url, LinkType::Web, parent);
}

if (line.contains(QStringLiteral("Bad credentials")))
{
emit statusChanged(QStringLiteral("Bad credentials, please try again"));
Expand Down Expand Up @@ -246,30 +233,6 @@ void SpotifyClient::Runner::onErrorOccurred(QProcess::ProcessError error)
emit statusChanged(message);
}

void SpotifyClient::Runner::onLoginSuccess()
{
if (!isLoggedIn())
{
lib::log::warn("Login successful, but not login found");
emit statusChanged(QStringLiteral("Unknown error"));
return;
}

loginHelper->deleteLater();
loginHelper = nullptr;

start();
}

void SpotifyClient::Runner::onLoginFailed(const QString &message)
{
lib::log::warn(message.toStdString());
emit statusChanged(message);

loginHelper->deleteLater();
loginHelper = nullptr;
}

auto SpotifyClient::Runner::getLog() -> const std::vector<lib::log_message> &
{
return log;
Expand Down
6 changes: 0 additions & 6 deletions src/spotifyclient/runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "lib/logmessage.hpp"

#include "spotifyclient/helper.hpp"
#include "spotifyclient/login.hpp"

#include <QDateTime>
#include <QFileInfo>
Expand Down Expand Up @@ -48,11 +47,9 @@ namespace SpotifyClient
const lib::settings &settings;
const lib::paths &paths;
lib::client_type clientType;
Login *loginHelper = nullptr;

void logOutput(const QByteArray &output, lib::log_type logType);
static auto joinArgs(const QStringList &args) -> QString;
void login();

auto getCachePath() const -> ghc::filesystem::path;
auto isLoggedIn() const -> bool;
Expand All @@ -62,8 +59,5 @@ namespace SpotifyClient
void onReadyReadError();
void onStarted();
void onErrorOccurred(QProcess::ProcessError error);

void onLoginSuccess();
void onLoginFailed(const QString &message);
};
}

0 comments on commit c79c4ac

Please sign in to comment.