-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "menu/queue.hpp" | ||
|
||
#include "util/icon.hpp" | ||
|
||
Menu::Queue::Queue(lib::spt::api &spotify, QWidget *parent) | ||
: QMenu(parent), | ||
spotify(spotify) | ||
{ | ||
setTitle(QStringLiteral("Queue")); | ||
setIcon(Icon::get(QStringLiteral("media-track-show-active"))); | ||
|
||
QMenu::connect(this, &QMenu::aboutToShow, | ||
this, &Menu::Queue::refreshQueue); | ||
|
||
QMenu::connect(this, &QMenu::triggered, | ||
this, &Menu::Queue::onTriggered); | ||
|
||
addMessage(QStringLiteral("Refreshing queue...")); | ||
} | ||
|
||
void Menu::Queue::addMessage(const QString &message) | ||
{ | ||
auto *action = addAction(message); | ||
action->setDisabled(true); | ||
} | ||
|
||
void Menu::Queue::refreshQueue() | ||
{ | ||
spotify.queue([this](const lib::result<lib::spt::queue> &result) | ||
{ | ||
for (auto &action: actions()) | ||
{ | ||
removeAction(action); | ||
} | ||
|
||
if (!result.success()) | ||
{ | ||
addMessage(QString::fromStdString(result.message())); | ||
return; | ||
} | ||
|
||
const auto &queue = result.value(); | ||
if (queue.tracks.empty()) | ||
{ | ||
addMessage(QStringLiteral("No tracks in queue")); | ||
return; | ||
} | ||
|
||
for (auto i = 0; i < queue.tracks.size(); i++) | ||
{ | ||
const auto &track = queue.tracks.at(i); | ||
auto *action = addAction(QString::fromStdString(track.title())); | ||
action->setData(i + 1); | ||
} | ||
}); | ||
} | ||
|
||
void Menu::Queue::skipTracks(int skips) | ||
{ | ||
if (skips <= 0) | ||
{ | ||
return; | ||
} | ||
|
||
spotify.next([this, skips](const std::string &callback) | ||
{ | ||
if (!callback.empty()) | ||
{ | ||
lib::log::error("Failed to skip in queue: {}", callback); | ||
return; | ||
} | ||
|
||
skipTracks(skips - 1); | ||
}); | ||
} | ||
|
||
void Menu::Queue::onTriggered(QAction *action) | ||
{ | ||
const auto skips = action->data().toInt(); | ||
skipTracks(skips); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "lib/spotify/api.hpp" | ||
#include <QMenu> | ||
|
||
namespace Menu | ||
{ | ||
class Queue: public QMenu | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
Queue(lib::spt::api &spotify, QWidget *parent); | ||
|
||
private: | ||
lib::spt::api &spotify; | ||
|
||
void addMessage(const QString &message); | ||
void refreshQueue(); | ||
void skipTracks(int skips); | ||
|
||
void onTriggered(QAction *action); | ||
}; | ||
} |