Commit af5a33f5 authored by Leander Schulten's avatar Leander Schulten
Browse files

Add auto log in for Spotify as described in #39

parent ca4ece68
Pipeline #173173 passed with stage
in 1 minute and 42 seconds
......@@ -220,7 +220,7 @@ int main(int argc, char *argv[])
auto & spotify = Spotify::get();
Modules::ModuleManager::singletone()->setSpotify(&spotify);
spotify.autoLoginUser();
QStringList dataList;
const QMetaObject &mo = QEasingCurve::staticMetaObject;
......
......@@ -60,6 +60,7 @@ Spotify::Spotify(QObject *parent) : QObject(parent)
});
connect(this, &Spotify::currentTrackChanged,this, &Spotify::updateAudioAnalysis);
connect(this, &Spotify::currentTrackChanged,this, &Spotify::updateAudioFeatures);
connect(this, &Spotify::currentPlayingObjectChanged,this, &Spotify::checkLoginNextUser);
/**
* @brief timer a timer to poll the spotify player status every 2.5 seconds
*/
......@@ -98,6 +99,15 @@ void Spotify::loginUser(const Objects::UserObject & user){
}
}
void Spotify::autoLoginUser(){
if(isAutoLoginingUser || getKnownUser().size()==0){
return;
}
isAutoLoginingUser = true;
currentAutoLoginedUser = 0;
getKnownUser()[0].login();
}
QVariant Spotify::getCurrentUserAsVariant() const{
if(currentUser)
return QVariant::fromValue(*currentUser);
......@@ -144,6 +154,24 @@ void Spotify::updateAudioFeatures(){
}
}
void Spotify::checkLoginNextUser(){
if (!isAutoLoginingUser) {
return;
}
if(currentPlayingObject){
if(currentPlayingObject->is_playing){
isAutoLoginingUser = false;
}else{
++currentAutoLoginedUser;
if(currentAutoLoginedUser < getKnownUser().size()){
getKnownUser()[currentAutoLoginedUser].login();
}else {
isAutoLoginingUser = false;
}
}
}
}
void Spotify::updatePlayer(){
using namespace std::chrono;
if(currentUser){
......
......@@ -34,7 +34,8 @@ class Spotify : public QObject
std::optional<Objects::UserObject> currentUser;
QOAuth2AuthorizationCodeFlow spotify;
detail::KnownUserVector knownUser;
bool isAutoLoginingUser = false;
int currentAutoLoginedUser;
explicit Spotify(QObject *parent = nullptr);
public:
static Spotify& get(){static Spotify s; return s;}
......@@ -48,6 +49,10 @@ public:
* @brief loginUser log in a user that already exists
*/
void loginUser(const Objects::UserObject &);
/**
* @brief autoLoginUser log in the first user where music is played
*/
void autoLoginUser();
const std::optional<Objects::CurrentPlayingObject>& getCurrentPlayingObject(){return currentPlayingObject;}
const std::optional<Objects::AudioAnalysisObject>& getCurrentAudioAnalysis(){return currentAudioAnalysis;}
const std::optional<Objects::AudioFeaturesObject>& getCurrentAudioFeatures(){return currentAudioFeatures;}
......@@ -65,6 +70,7 @@ signals:
protected slots:
void updateAudioAnalysis();
void updateAudioFeatures();
void checkLoginNextUser();
protected:
void updatePlayer();
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment