Skip to content

Commit

Permalink
Add queue menu, fixes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Apr 1, 2023
1 parent 5a976a4 commit addfe2d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/menu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ target_sources(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/mainmenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mainmenubar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/playlist.cpp
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/show.cpp
${CMAKE_CURRENT_SOURCE_DIR}/track.cpp)
5 changes: 5 additions & 0 deletions src/menu/mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "util/shortcut.hpp"
#include "menu/developermenu.hpp"
#include "menu/device.hpp"
#include "menu/queue.hpp"

MainMenu::MainMenu(lib::spt::api &spotify, lib::settings &settings,
const lib::http_client &httpClient, lib::cache &cache, QWidget *parent)
Expand All @@ -14,6 +15,10 @@ MainMenu::MainMenu(lib::spt::api &spotify, lib::settings &settings,
auto *deviceMenu = new Menu::Device(spotify, this);
addMenu(deviceMenu);

// Track queue
auto *queueMenu = new Menu::Queue(spotify, this);
addMenu(queueMenu);

// Refresh and settings
auto *openSettings = MenuAction::create("configure", "Settings...",
this, QKeySequence::Preferences);
Expand Down
4 changes: 4 additions & 0 deletions src/menu/mainmenubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "menu/device.hpp"
#include "menu/developermenu.hpp"
#include "menu/queue.hpp"
#include "dialog/about.hpp"

MainMenuBar::MainMenuBar(lib::spt::api &spotify, lib::settings &settings,
Expand All @@ -23,6 +24,9 @@ MainMenuBar::MainMenuBar(lib::spt::api &spotify, lib::settings &settings,
auto *deviceMenu = new Menu::Device(spotify, this);
addMenu(deviceMenu);

auto *queueMenu = new Menu::Queue(spotify, this);
addMenu(queueMenu);

auto *accountMenu = new QMenu(QStringLiteral("Account"));

auto *logOut = accountMenu->addAction(QStringLiteral("Log out"));
Expand Down
81 changes: 81 additions & 0 deletions src/menu/queue.cpp
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);
}
24 changes: 24 additions & 0 deletions src/menu/queue.hpp
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);
};
}

0 comments on commit addfe2d

Please sign in to comment.