Compare commits
9
Commits
e646080683
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bae23b7abc | ||
|
|
88cfb1d443 | ||
|
|
8ef805debd | ||
|
|
2956ef29e9 | ||
|
|
1fc5aaecfe | ||
|
|
2aa44fae9e | ||
|
|
6e789fd83f | ||
|
|
bf1cd8e08d | ||
|
|
bcf4f4770a |
No files matched your search
@@ -0,0 +1,55 @@
|
||||
name: Independent component release
|
||||
on:
|
||||
push:
|
||||
tags: [v*]
|
||||
schedule:
|
||||
- cron: '17 2 * * *'
|
||||
workflow_dispatch:
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: echoion-release-serial
|
||||
steps:
|
||||
- name: Check out component
|
||||
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
|
||||
with:
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
- name: Check out pinned public release tools
|
||||
uses: https://github.com/actions/checkout@11d5960a326750d5838078e36cf38b85af677262
|
||||
with:
|
||||
repository: Void/Echoion
|
||||
ref: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
|
||||
path: .echoion-release-tools
|
||||
fetch-depth: 0
|
||||
persist-credentials: false
|
||||
- name: Build, publish, and merge feed transaction
|
||||
shell: bash
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.COMPONENT_OBJECT_STORAGE_ACCESS_KEY }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.COMPONENT_OBJECT_STORAGE_SECRET_KEY }}
|
||||
AWS_DEFAULT_REGION: ${{ vars.COMPONENT_OBJECT_STORAGE_REGION }}
|
||||
ECHOION_COMPONENT_SSH_KEY: ${{ secrets.RELEASE_CHECKOUT_SSH_KEY }}
|
||||
ECHOION_DISTRIBUTION_BACKEND: ${{ vars.ECHOION_DISTRIBUTION_BACKEND }}
|
||||
ECHOION_DISTRIBUTION_BASE_URL: ${{ vars.ECHOION_DISTRIBUTION_BASE_URL }}
|
||||
ECHOION_FEED_BASE_URL: ${{ vars.ECHOION_FEED_BASE_URL }}
|
||||
ECHOION_FEED_COMMIT_SIGNING_KEY: ${{ secrets.RELEASE_FEED_GIT_SIGNING_KEY }}
|
||||
ECHOION_FEED_PRIVATE_KEY: ${{ secrets.RELEASE_FEED_ED25519_PRIVATE_KEY }}
|
||||
ECHOION_FEED_REPOSITORY: ssh://gitea@git.faumaray.ru/Void/Echoion.git
|
||||
ECHOION_FEED_SIGNING_KEY_ID: ${{ vars.ECHOION_FEED_SIGNING_KEY_ID }}
|
||||
ECHOION_OBJECT_STORAGE_BUCKET: ${{ vars.COMPONENT_OBJECT_STORAGE_BUCKET }}
|
||||
ECHOION_OBJECT_STORAGE_ENDPOINT: ${{ vars.COMPONENT_OBJECT_STORAGE_ENDPOINT }}
|
||||
ECHOION_OBJECT_STORAGE_PREFIX: ${{ vars.COMPONENT_OBJECT_STORAGE_PREFIX }}
|
||||
ECHOION_RELEASE_TAG_ALLOWED_SIGNERS: ${{ secrets.RELEASE_TAG_ALLOWED_SIGNERS }}
|
||||
GITEA_API_URL: ${{ gitea.api_url }}
|
||||
GITEA_EVENT_NAME: ${{ gitea.event_name }}
|
||||
GITEA_REF_NAME: ${{ gitea.ref_name }}
|
||||
GITEA_RUN_NUMBER: ${{ gitea.run_number }}
|
||||
GITEA_SHA: ${{ gitea.sha }}
|
||||
GITEA_REPOSITORY: ${{ gitea.repository }}
|
||||
GITEA_RELEASE_TOKEN: ${{ secrets.RELEASE_BOT_TOKEN }}
|
||||
RELEASE_TOOLS_REF: ${{ vars.ECHOION_RELEASE_TOOLS_REF }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
[[ "$RELEASE_TOOLS_REF" =~ ^[0-9a-f]{40}$ ]]
|
||||
test "$(git -C .echoion-release-tools rev-parse HEAD)" = "$RELEASE_TOOLS_REF"
|
||||
.echoion-release-tools/scripts/release-component.sh component.release.json
|
||||
+35
-10
@@ -1,5 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(EchoionDesktop VERSION 1.0.0 LANGUAGES CXX)
|
||||
cmake_minimum_required(VERSION 4.0.0)
|
||||
if(NOT DEFINED ECHOION_PRODUCT_VERSION)
|
||||
set(ECHOION_PRODUCT_VERSION 0.0.0)
|
||||
endif()
|
||||
if(NOT DEFINED ECHOION_COMPONENT_VERSION)
|
||||
set(ECHOION_COMPONENT_VERSION "${ECHOION_PRODUCT_VERSION}")
|
||||
endif()
|
||||
project(EchoionDesktop VERSION "${ECHOION_COMPONENT_VERSION}" LANGUAGES CXX)
|
||||
|
||||
if(PROJECT_IS_TOP_LEVEL)
|
||||
file(DOWNLOAD
|
||||
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.8/CPM.cmake
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake"
|
||||
EXPECTED_HASH
|
||||
SHA256=78ba32abdf798bc616bab7c73aac32a17bbd7b06ad9e26a6add69de8f3ae4791)
|
||||
include("${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake")
|
||||
CPMAddPackage(
|
||||
NAME Void
|
||||
GIT_REPOSITORY ssh://gitea@git.faumaray.ru/void/voidlibrary.git
|
||||
GIT_TAG main
|
||||
OPTIONS "VOID_ENABLE_TESTING OFF")
|
||||
CPMAddPackage(
|
||||
NAME EchoionApi
|
||||
GIT_REPOSITORY ssh://gitea@git.faumaray.ru/void/echoionapi.git
|
||||
GIT_TAG main)
|
||||
CPMAddPackage(
|
||||
NAME PackageProject.cmake
|
||||
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
|
||||
VERSION 1.11.2)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 26)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -11,15 +39,11 @@ set(CMAKE_AUTOUIC ON)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Network WebSockets)
|
||||
|
||||
add_executable(EchoionDesktop
|
||||
src/main.cpp
|
||||
src/MainWindow.cpp
|
||||
src/MainWindow.hpp
|
||||
src/LoginDialog.cpp
|
||||
src/LoginDialog.hpp
|
||||
src/CoreClient.cpp
|
||||
src/CoreClient.hpp
|
||||
file(GLOB_RECURSE DESKTOP_SOURCES
|
||||
CONFIGURE_DEPENDS
|
||||
"src/**.cpp"
|
||||
)
|
||||
add_executable(EchoionDesktop ${DESKTOP_SOURCES})
|
||||
|
||||
add_executable(Echoion::Desktop ALIAS EchoionDesktop)
|
||||
|
||||
@@ -29,6 +53,7 @@ target_include_directories(EchoionDesktop PRIVATE
|
||||
|
||||
target_link_libraries(EchoionDesktop PRIVATE
|
||||
Echoion::Api
|
||||
Void::Security
|
||||
Qt6::Core
|
||||
Qt6::Widgets
|
||||
Qt6::Network
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"componentId": "EchoionDesktop",
|
||||
"productCompatibility": ">=2.0.0 <3.0.0",
|
||||
"publishNightly": true,
|
||||
"cmakeOptions": {}
|
||||
}
|
||||
+596
-389
File diff suppressed because it is too large.
Load diff
+158
-109
@@ -1,132 +1,181 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QWebSocket>
|
||||
#include "storage/SettingsManager.hpp"
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QUuid>
|
||||
#include <echoion/api.hpp>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QTimer>
|
||||
#include <QWebSocket>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Echoion::Desktop {
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
// RPC request/response handling
|
||||
using RpcCallback = std::function<void(const QJsonObject&)>;
|
||||
// RPC request/response handling
|
||||
using RpcCallback = std::function<void(const QJsonObject&)>;
|
||||
|
||||
class CoreClient : public QObject {
|
||||
Q_OBJECT
|
||||
class CoreClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CoreClient(QObject* parent = nullptr);
|
||||
~CoreClient() override;
|
||||
public:
|
||||
|
||||
// Connection management
|
||||
void connectToCore(const QString& host = "localhost", int port = 8765);
|
||||
void disconnectFromCore();
|
||||
[[nodiscard]] bool isConnected() const;
|
||||
explicit CoreClient(QObject* parent = nullptr);
|
||||
~CoreClient() override;
|
||||
|
||||
// Authentication
|
||||
void authenticate(const QString& username, const QString& password);
|
||||
void logout();
|
||||
[[nodiscard]] bool isAuthenticated() const;
|
||||
// Settings integration
|
||||
void loadSettings();
|
||||
void saveSettings();
|
||||
SettingsManager* settingsManager() { return &settingsManager_; }
|
||||
|
||||
// Playback control
|
||||
void play(const QString& query);
|
||||
void pause();
|
||||
void resume();
|
||||
void stop();
|
||||
void skip(int count = 1);
|
||||
void seek(int positionMs);
|
||||
void setVolume(int volume); // 0-100
|
||||
// Auto-connect on startup
|
||||
void enableAutoConnect(bool enabled);
|
||||
bool autoConnectEnabled() const;
|
||||
void startAutoConnect();
|
||||
|
||||
// Queue management
|
||||
void queueTrack(const QString& query);
|
||||
void removeFromQueue(int position);
|
||||
void clearQueue();
|
||||
void moveInQueue(int from, int to);
|
||||
void shuffle();
|
||||
void toggleRepeat();
|
||||
void toggleShuffle();
|
||||
// Connection management
|
||||
void connectToHost(const QString& host, int port);
|
||||
void disconnectFromHost();
|
||||
[[nodiscard]] bool isConnected() const;
|
||||
|
||||
// Search
|
||||
void search(const QString& query);
|
||||
// Authentication with auto-auth support
|
||||
void requestAuthToken(const QString& nickname);
|
||||
void authenticate(const QString& token);
|
||||
void logout();
|
||||
[[nodiscard]] bool isAuthenticated() const;
|
||||
void clearCachedToken();
|
||||
|
||||
// Playlist management
|
||||
void loadPlaylist(const QString& playlistId);
|
||||
void savePlaylist(const QString& name, const QString& description);
|
||||
void getUserPlaylists();
|
||||
// Generic RPC with automatic retry
|
||||
void call(const QString& method, const QJsonObject& params = {});
|
||||
void call(const QString& method, const QJsonObject& params,
|
||||
RpcCallback callback);
|
||||
|
||||
// Data requests
|
||||
void requestPlaybackState();
|
||||
void requestQueue();
|
||||
void requestCurrentTrack();
|
||||
// Playback control
|
||||
void play(const QString& query);
|
||||
void pause();
|
||||
void resume();
|
||||
void stop();
|
||||
void skip(int count = 1);
|
||||
void seek(int positionMs);
|
||||
void setVolume(int volume);
|
||||
|
||||
// Hot reload
|
||||
void reloadCore();
|
||||
// Queue management (queueId matches GetQueue items + queue events)
|
||||
void queueTrack(const QString& query);
|
||||
void removeFromQueue(quint64 queueId);
|
||||
void clearQueue();
|
||||
void moveInQueue(quint64 queueId, int newPosition);
|
||||
void shuffle();
|
||||
void toggleRepeat();
|
||||
void toggleShuffle();
|
||||
|
||||
signals:
|
||||
// Connection signals
|
||||
void connected();
|
||||
void disconnected();
|
||||
void errorOccurred(const QString& error);
|
||||
|
||||
// Authentication signals
|
||||
void authenticationSuccess(const QString& token);
|
||||
void authenticationFailed(const QString& reason);
|
||||
void loggedOut();
|
||||
|
||||
// Playback signals
|
||||
void playbackStateReceived(const Echoion::Api::PlaybackState& state);
|
||||
void trackStarted(const Echoion::Api::TrackInfo& track);
|
||||
void trackFinished(const Echoion::Api::TrackInfo& track);
|
||||
void playbackPaused();
|
||||
void playbackResumed();
|
||||
void volumeChanged(int volume);
|
||||
|
||||
// Queue signals
|
||||
void queueReceived(const std::vector<Echoion::Api::QueueEntry>& queue);
|
||||
void trackQueued(const Echoion::Api::TrackInfo& track, int position);
|
||||
void trackRemoved(int position);
|
||||
|
||||
// Search signals
|
||||
void searchResultsReceived(const Echoion::Api::SearchResult& results);
|
||||
|
||||
// Playlist signals
|
||||
void playlistsReceived(const std::vector<Echoion::Api::PlaylistInfo>& playlists);
|
||||
void playlistLoaded(const QString& playlistId, int trackCount);
|
||||
// Search
|
||||
void search(const QString& query);
|
||||
|
||||
private slots:
|
||||
void onConnected();
|
||||
void onDisconnected();
|
||||
void onError(QAbstractSocket::SocketError error);
|
||||
void onMessageReceived(const QString& message);
|
||||
void onBinaryMessageReceived(const QByteArray& data);
|
||||
// Playlist management
|
||||
void loadPlaylist(const QString& playlistId);
|
||||
void savePlaylist(const QString& name, const QString& description);
|
||||
void deletePlaylist(const QString& playlistId);
|
||||
void getUserPlaylists();
|
||||
|
||||
private:
|
||||
void sendRpcRequest(const QString& method, const QJsonObject& params = {});
|
||||
void sendRpcRequest(const QString& method, const QJsonObject& params, RpcCallback callback);
|
||||
void handleRpcResponse(const QJsonObject& response);
|
||||
void handleEvent(const QJsonObject& event);
|
||||
|
||||
// JSON conversion helpers
|
||||
[[nodiscard]] Echoion::Api::PlaybackState parsePlaybackState(const QJsonObject& obj);
|
||||
[[nodiscard]] Echoion::Api::TrackInfo parseTrackInfo(const QJsonObject& obj);
|
||||
[[nodiscard]] Echoion::Api::QueueEntry parseQueueEntry(const QJsonObject& obj);
|
||||
[[nodiscard]] Echoion::Api::SearchResult parseSearchResult(const QJsonObject& obj);
|
||||
[[nodiscard]] QJsonObject trackInfoToJson(const Echoion::Api::TrackInfo& track);
|
||||
// Configuration
|
||||
void getConfig();
|
||||
void setConfig(const QString& configJson);
|
||||
void reloadConfig();
|
||||
|
||||
QWebSocket* socket_ = nullptr;
|
||||
QString authToken_;
|
||||
std::unordered_map<QString, RpcCallback> pendingRequests_;
|
||||
quint64 requestId_ = 0;
|
||||
|
||||
// Reconnection
|
||||
bool shouldReconnect_ = true;
|
||||
int reconnectAttempts_ = 0;
|
||||
static constexpr int MAX_RECONNECT_ATTEMPTS = 5;
|
||||
};
|
||||
// Data requests (fetch on demand). Data-returning calls may also be
|
||||
// issued via call(method, params, callback) for a one-shot reply.
|
||||
void requestPlaybackState();
|
||||
void requestQueue();
|
||||
void requestCurrentTrack();
|
||||
|
||||
signals:
|
||||
|
||||
// Connection signals
|
||||
void connected();
|
||||
void disconnected();
|
||||
void reconnecting(int attempt);
|
||||
// All automatic reconnect attempts failed; manual retry needed.
|
||||
void reconnectExhausted();
|
||||
void connectionError(const QString& error);
|
||||
void connectionFailed(const QString& reason);
|
||||
|
||||
// Authentication signals
|
||||
void authTokenRequested(); // Nickname accepted, check PM
|
||||
void authenticated();
|
||||
void authFailed(const QString& reason);
|
||||
void loggedOut();
|
||||
void tokenSaved(const QString& host, int port);
|
||||
|
||||
// All events from Core (raw JSON for flexibility)
|
||||
void eventReceived(const QString& event, const QJsonObject& data);
|
||||
|
||||
// Specific event signals
|
||||
void playbackStarted(const QJsonObject& data);
|
||||
void playbackPaused(const QJsonObject& data);
|
||||
void playbackResumed(const QJsonObject& data);
|
||||
void playbackStopped(const QJsonObject& data);
|
||||
void playbackFinished(const QJsonObject& data);
|
||||
void volumeChanged(const QJsonObject& data);
|
||||
void trackChanged(const QJsonObject& data);
|
||||
void queueUpdated(const QJsonObject& data);
|
||||
void playlistEvent(const QString& event, const QJsonObject& data);
|
||||
|
||||
private slots:
|
||||
|
||||
void onSocketConnected();
|
||||
void onSocketDisconnected();
|
||||
void onSocketError(QAbstractSocket::SocketError error);
|
||||
void onTextMessageReceived(const QString& message);
|
||||
void onReconnectTimer();
|
||||
|
||||
private:
|
||||
|
||||
void setupSocket();
|
||||
void handleRpcResponse(const QJsonObject& response);
|
||||
void handleEvent(const QJsonObject& event);
|
||||
void startReconnectTimer();
|
||||
void stopReconnectTimer();
|
||||
void attemptAutoAuth();
|
||||
void saveCurrentToken();
|
||||
|
||||
// Request tracking for retry logic
|
||||
struct PendingRequest {
|
||||
QString method;
|
||||
QJsonObject params;
|
||||
RpcCallback callback;
|
||||
int retryCount = 0;
|
||||
QTimer* retryTimer = nullptr;
|
||||
};
|
||||
|
||||
void retryRequest(quint64 requestId);
|
||||
void cancelPendingRequests();
|
||||
[[nodiscard]] int methodToInt(const QString& method) const;
|
||||
|
||||
SettingsManager settingsManager_;
|
||||
// QPointer so a stray access after teardown is null-checked rather
|
||||
// than dangling. Qt parent-ownership still frees it.
|
||||
QPointer<QWebSocket> socket_ = nullptr;
|
||||
QString currentHost_;
|
||||
int currentPort_ = 8765;
|
||||
QString currentNickname_;
|
||||
QString authToken_;
|
||||
bool isAuthenticated_ = false;
|
||||
bool autoConnectEnabled_ = true;
|
||||
bool autoAuthEnabled_ = true;
|
||||
|
||||
// Request tracking
|
||||
std::unordered_map<quint64, PendingRequest> pendingRequests_;
|
||||
quint64 nextRequestId_ = 1;
|
||||
static constexpr int MAX_RETRIES = 3;
|
||||
static constexpr int RETRY_DELAY_MS = 500;
|
||||
|
||||
// Reconnection
|
||||
QTimer* reconnectTimer_ = nullptr;
|
||||
int reconnectAttempts_ = 0;
|
||||
static constexpr int MAX_RECONNECT_ATTEMPTS = 5;
|
||||
static constexpr int INITIAL_RECONNECT_DELAY_MS = 1000;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
+70
-67
@@ -1,84 +1,87 @@
|
||||
#include "LoginDialog.hpp"
|
||||
|
||||
namespace Echoion::Desktop {
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
LoginDialog::LoginDialog(QWidget* parent) : QDialog(parent) {
|
||||
setupUi();
|
||||
setWindowTitle("Echoion - Admin Login");
|
||||
setModal(true);
|
||||
setFixedSize(350, 200);
|
||||
}
|
||||
LoginDialog::LoginDialog(QWidget* parent) : QDialog(parent)
|
||||
{
|
||||
setupUi();
|
||||
setWindowTitle("Echoion - Admin Login");
|
||||
setModal(true);
|
||||
setFixedSize(350, 200);
|
||||
}
|
||||
|
||||
void LoginDialog::setupUi() {
|
||||
auto* mainLayout = new QVBoxLayout(this);
|
||||
void LoginDialog::setupUi()
|
||||
{
|
||||
auto* mainLayout = new QVBoxLayout(this);
|
||||
|
||||
auto* titleLabel = new QLabel("Echoion Music Bot - Admin Login", this);
|
||||
titleLabel->setAlignment(Qt::AlignCenter);
|
||||
QFont titleFont = titleLabel->font();
|
||||
titleFont.setPointSize(14);
|
||||
titleFont.setBold(true);
|
||||
titleLabel->setFont(titleFont);
|
||||
mainLayout->addWidget(titleLabel);
|
||||
auto* titleLabel = new QLabel("Echoion Music Bot - Admin Login", this);
|
||||
titleLabel->setAlignment(Qt::AlignCenter);
|
||||
QFont titleFont = titleLabel->font();
|
||||
titleFont.setPointSize(14);
|
||||
titleFont.setBold(true);
|
||||
titleLabel->setFont(titleFont);
|
||||
mainLayout->addWidget(titleLabel);
|
||||
|
||||
mainLayout->addSpacing(20);
|
||||
mainLayout->addSpacing(20);
|
||||
|
||||
auto* usernameLayout = new QHBoxLayout();
|
||||
auto* usernameLabel = new QLabel("Username:", this);
|
||||
usernameLabel->setFixedWidth(80);
|
||||
usernameEdit_ = new QLineEdit(this);
|
||||
usernameEdit_->setPlaceholderText("Enter username");
|
||||
usernameLayout->addWidget(usernameLabel);
|
||||
usernameLayout->addWidget(usernameEdit_);
|
||||
mainLayout->addLayout(usernameLayout);
|
||||
auto* usernameLayout = new QHBoxLayout();
|
||||
auto* usernameLabel = new QLabel("Username:", this);
|
||||
usernameLabel->setFixedWidth(80);
|
||||
usernameEdit_ = new QLineEdit(this);
|
||||
usernameEdit_->setPlaceholderText("Enter username");
|
||||
usernameLayout->addWidget(usernameLabel);
|
||||
usernameLayout->addWidget(usernameEdit_);
|
||||
mainLayout->addLayout(usernameLayout);
|
||||
|
||||
auto* passwordLayout = new QHBoxLayout();
|
||||
auto* passwordLabel = new QLabel("Password:", this);
|
||||
passwordLabel->setFixedWidth(80);
|
||||
passwordEdit_ = new QLineEdit(this);
|
||||
passwordEdit_->setEchoMode(QLineEdit::Password);
|
||||
passwordEdit_->setPlaceholderText("Enter password");
|
||||
passwordLayout->addWidget(passwordLabel);
|
||||
passwordLayout->addWidget(passwordEdit_);
|
||||
mainLayout->addLayout(passwordLayout);
|
||||
auto* passwordLayout = new QHBoxLayout();
|
||||
auto* passwordLabel = new QLabel("Password:", this);
|
||||
passwordLabel->setFixedWidth(80);
|
||||
passwordEdit_ = new QLineEdit(this);
|
||||
passwordEdit_->setEchoMode(QLineEdit::Password);
|
||||
passwordEdit_->setPlaceholderText("Enter password");
|
||||
passwordLayout->addWidget(passwordLabel);
|
||||
passwordLayout->addWidget(passwordEdit_);
|
||||
mainLayout->addLayout(passwordLayout);
|
||||
|
||||
mainLayout->addSpacing(20);
|
||||
mainLayout->addSpacing(20);
|
||||
|
||||
auto* buttonLayout = new QHBoxLayout();
|
||||
loginButton_ = new QPushButton("Login", this);
|
||||
loginButton_->setDefault(true);
|
||||
cancelButton_ = new QPushButton("Cancel", this);
|
||||
buttonLayout->addStretch();
|
||||
buttonLayout->addWidget(loginButton_);
|
||||
buttonLayout->addWidget(cancelButton_);
|
||||
mainLayout->addLayout(buttonLayout);
|
||||
auto* buttonLayout = new QHBoxLayout();
|
||||
loginButton_ = new QPushButton("Login", this);
|
||||
loginButton_->setDefault(true);
|
||||
cancelButton_ = new QPushButton("Cancel", this);
|
||||
buttonLayout->addStretch();
|
||||
buttonLayout->addWidget(loginButton_);
|
||||
buttonLayout->addWidget(cancelButton_);
|
||||
mainLayout->addLayout(buttonLayout);
|
||||
|
||||
connect(loginButton_, &QPushButton::clicked, this, &LoginDialog::onLoginClicked);
|
||||
connect(cancelButton_, &QPushButton::clicked, this, &LoginDialog::onCancelClicked);
|
||||
connect(passwordEdit_, &QLineEdit::returnPressed, this, &LoginDialog::onLoginClicked);
|
||||
}
|
||||
connect(loginButton_, &QPushButton::clicked, this,
|
||||
&LoginDialog::onLoginClicked);
|
||||
connect(cancelButton_, &QPushButton::clicked, this,
|
||||
&LoginDialog::onCancelClicked);
|
||||
connect(passwordEdit_, &QLineEdit::returnPressed, this,
|
||||
&LoginDialog::onLoginClicked);
|
||||
}
|
||||
|
||||
QString LoginDialog::getUsername() const {
|
||||
return usernameEdit_->text();
|
||||
}
|
||||
QString LoginDialog::getUsername() const { return usernameEdit_->text(); }
|
||||
|
||||
QString LoginDialog::getPassword() const {
|
||||
return passwordEdit_->text();
|
||||
}
|
||||
QString LoginDialog::getPassword() const { return passwordEdit_->text(); }
|
||||
|
||||
void LoginDialog::onLoginClicked() {
|
||||
if (usernameEdit_->text().isEmpty()) {
|
||||
QMessageBox::warning(this, "Login Error", "Please enter a username.");
|
||||
return;
|
||||
}
|
||||
if (passwordEdit_->text().isEmpty()) {
|
||||
QMessageBox::warning(this, "Login Error", "Please enter a password.");
|
||||
return;
|
||||
}
|
||||
accept();
|
||||
}
|
||||
void LoginDialog::onLoginClicked()
|
||||
{
|
||||
if (usernameEdit_->text().isEmpty()) {
|
||||
QMessageBox::warning(this, "Login Error",
|
||||
"Please enter a username.");
|
||||
return;
|
||||
}
|
||||
if (passwordEdit_->text().isEmpty()) {
|
||||
QMessageBox::warning(this, "Login Error",
|
||||
"Please enter a password.");
|
||||
return;
|
||||
}
|
||||
accept();
|
||||
}
|
||||
|
||||
void LoginDialog::onCancelClicked() {
|
||||
reject();
|
||||
}
|
||||
void LoginDialog::onCancelClicked() { reject(); }
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
+25
-21
@@ -1,35 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
namespace Echoion::Desktop {
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
class LoginDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
class LoginDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LoginDialog(QWidget* parent = nullptr);
|
||||
public:
|
||||
|
||||
[[nodiscard]] QString getUsername() const;
|
||||
[[nodiscard]] QString getPassword() const;
|
||||
explicit LoginDialog(QWidget* parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void onLoginClicked();
|
||||
void onCancelClicked();
|
||||
[[nodiscard]] QString getUsername() const;
|
||||
[[nodiscard]] QString getPassword() const;
|
||||
|
||||
private:
|
||||
void setupUi();
|
||||
private slots:
|
||||
void onLoginClicked();
|
||||
void onCancelClicked();
|
||||
|
||||
QLineEdit* usernameEdit_;
|
||||
QLineEdit* passwordEdit_;
|
||||
QPushButton* loginButton_;
|
||||
QPushButton* cancelButton_;
|
||||
};
|
||||
private:
|
||||
|
||||
void setupUi();
|
||||
|
||||
QLineEdit* usernameEdit_;
|
||||
QLineEdit* passwordEdit_;
|
||||
QPushButton* loginButton_;
|
||||
QPushButton* cancelButton_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
@@ -1,419 +0,0 @@
|
||||
#include "MainWindow.hpp"
|
||||
#include <format>
|
||||
|
||||
namespace Echoion::Desktop {
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
setupUi();
|
||||
setupMenuBar();
|
||||
updateButtonStates();
|
||||
|
||||
updateTimer_ = new QTimer(this);
|
||||
connect(updateTimer_, &QTimer::timeout, this, &MainWindow::updatePlaybackDisplay);
|
||||
updateTimer_->start(1000);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() = default;
|
||||
|
||||
void MainWindow::setupUi() {
|
||||
setWindowTitle("Echoion Music Bot");
|
||||
resize(900, 700);
|
||||
|
||||
centralWidget_ = new QWidget(this);
|
||||
setCentralWidget(centralWidget_);
|
||||
|
||||
mainLayout_ = new QVBoxLayout(centralWidget_);
|
||||
|
||||
setupPlaybackControls();
|
||||
setupSearchPanel();
|
||||
setupQueueView();
|
||||
setupStatusBar();
|
||||
}
|
||||
|
||||
void MainWindow::setupMenuBar() {
|
||||
auto* fileMenu = menuBar()->addMenu("&File");
|
||||
|
||||
loginAction_ = fileMenu->addAction("&Login", this, &MainWindow::onLoginLogoutClicked);
|
||||
logoutAction_ = fileMenu->addAction("Log&out", this, &MainWindow::onLoginLogoutClicked);
|
||||
logoutAction_->setEnabled(false);
|
||||
fileMenu->addSeparator();
|
||||
settingsAction_ = fileMenu->addAction("&Settings", this, &MainWindow::onSettingsClicked);
|
||||
settingsAction_->setEnabled(false);
|
||||
fileMenu->addSeparator();
|
||||
exitAction_ = fileMenu->addAction("E&xit", this, &QWidget::close, QKeySequence::Quit);
|
||||
|
||||
auto* controlMenu = menuBar()->addMenu("&Control");
|
||||
reloadAction_ = controlMenu->addAction("&Reload Core", this, &MainWindow::onReloadClicked);
|
||||
reloadAction_->setEnabled(false);
|
||||
|
||||
auto* helpMenu = menuBar()->addMenu("&Help");
|
||||
aboutAction_ = helpMenu->addAction("&About", [this]() {
|
||||
QMessageBox::about(this, "About Echoion",
|
||||
"Echoion Music Bot v1.0.0\n\n"
|
||||
"A modern music bot for TeamSpeak.");
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::setupPlaybackControls() {
|
||||
auto* playbackGroup = new QGroupBox("Now Playing", this);
|
||||
auto* playbackLayout = new QVBoxLayout(playbackGroup);
|
||||
|
||||
nowPlayingLabel_ = new QLabel("Nothing is playing", this);
|
||||
QFont titleFont = nowPlayingLabel_->font();
|
||||
titleFont.setPointSize(12);
|
||||
titleFont.setBold(true);
|
||||
nowPlayingLabel_->setFont(titleFont);
|
||||
playbackLayout->addWidget(nowPlayingLabel_);
|
||||
|
||||
artistLabel_ = new QLabel("", this);
|
||||
playbackLayout->addWidget(artistLabel_);
|
||||
|
||||
auto* progressLayout = new QHBoxLayout();
|
||||
progressBar_ = new QProgressBar(this);
|
||||
progressBar_->setRange(0, 100);
|
||||
progressBar_->setValue(0);
|
||||
progressBar_->setTextVisible(false);
|
||||
progressLayout->addWidget(progressBar_);
|
||||
|
||||
timeLabel_ = new QLabel("0:00 / 0:00", this);
|
||||
timeLabel_->setFixedWidth(100);
|
||||
progressLayout->addWidget(timeLabel_);
|
||||
playbackLayout->addLayout(progressLayout);
|
||||
|
||||
auto* controlsLayout = new QHBoxLayout();
|
||||
|
||||
playButton_ = new QPushButton("Play", this);
|
||||
pauseButton_ = new QPushButton("Pause", this);
|
||||
stopButton_ = new QPushButton("Stop", this);
|
||||
skipButton_ = new QPushButton("Skip", this);
|
||||
shuffleButton_ = new QPushButton("Shuffle: Off", this);
|
||||
repeatButton_ = new QPushButton("Repeat: Off", this);
|
||||
|
||||
controlsLayout->addWidget(playButton_);
|
||||
controlsLayout->addWidget(pauseButton_);
|
||||
controlsLayout->addWidget(stopButton_);
|
||||
controlsLayout->addWidget(skipButton_);
|
||||
controlsLayout->addStretch();
|
||||
controlsLayout->addWidget(shuffleButton_);
|
||||
controlsLayout->addWidget(repeatButton_);
|
||||
playbackLayout->addLayout(controlsLayout);
|
||||
|
||||
auto* volumeLayout = new QHBoxLayout();
|
||||
auto* volumeTextLabel = new QLabel("Volume:", this);
|
||||
volumeSlider_ = new QSlider(Qt::Horizontal, this);
|
||||
volumeSlider_->setRange(0, 100);
|
||||
volumeSlider_->setValue(50);
|
||||
volumeLabel_ = new QLabel("50%", this);
|
||||
volumeLabel_->setFixedWidth(40);
|
||||
|
||||
volumeLayout->addWidget(volumeTextLabel);
|
||||
volumeLayout->addWidget(volumeSlider_);
|
||||
volumeLayout->addWidget(volumeLabel_);
|
||||
playbackLayout->addLayout(volumeLayout);
|
||||
|
||||
mainLayout_->addWidget(playbackGroup);
|
||||
|
||||
connect(playButton_, &QPushButton::clicked, this, &MainWindow::onPlayClicked);
|
||||
connect(pauseButton_, &QPushButton::clicked, this, &MainWindow::onPauseClicked);
|
||||
connect(stopButton_, &QPushButton::clicked, this, &MainWindow::onStopClicked);
|
||||
connect(skipButton_, &QPushButton::clicked, this, &MainWindow::onSkipClicked);
|
||||
connect(shuffleButton_, &QPushButton::clicked, this, &MainWindow::onShuffleClicked);
|
||||
connect(repeatButton_, &QPushButton::clicked, this, &MainWindow::onRepeatClicked);
|
||||
connect(volumeSlider_, &QSlider::valueChanged, this, &MainWindow::onVolumeChanged);
|
||||
}
|
||||
|
||||
void MainWindow::setupSearchPanel() {
|
||||
auto* searchGroup = new QGroupBox("Search", this);
|
||||
auto* searchLayout = new QVBoxLayout(searchGroup);
|
||||
|
||||
auto* searchInputLayout = new QHBoxLayout();
|
||||
searchEdit_ = new QLineEdit(this);
|
||||
searchEdit_->setPlaceholderText("Enter search query or YouTube URL...");
|
||||
searchButton_ = new QPushButton("Search", this);
|
||||
searchInputLayout->addWidget(searchEdit_);
|
||||
searchInputLayout->addWidget(searchButton_);
|
||||
searchLayout->addLayout(searchInputLayout);
|
||||
|
||||
searchResults_ = new QListWidget(this);
|
||||
searchResults_->setMaximumHeight(150);
|
||||
searchLayout->addWidget(searchResults_);
|
||||
|
||||
mainLayout_->addWidget(searchGroup);
|
||||
|
||||
connect(searchButton_, &QPushButton::clicked, this, &MainWindow::onSearchClicked);
|
||||
connect(searchEdit_, &QLineEdit::returnPressed, this, &MainWindow::onSearchClicked);
|
||||
connect(searchResults_, &QListWidget::itemDoubleClicked, this, &MainWindow::onSearchResultDoubleClicked);
|
||||
}
|
||||
|
||||
void MainWindow::setupQueueView() {
|
||||
auto* queueGroup = new QGroupBox("Queue", this);
|
||||
auto* queueLayout = new QVBoxLayout(queueGroup);
|
||||
|
||||
queueList_ = new QListWidget(this);
|
||||
queueLayout->addWidget(queueList_);
|
||||
|
||||
auto* queueButtonsLayout = new QHBoxLayout();
|
||||
clearQueueButton_ = new QPushButton("Clear Queue", this);
|
||||
removeFromQueueButton_ = new QPushButton("Remove Selected", this);
|
||||
queueButtonsLayout->addWidget(clearQueueButton_);
|
||||
queueButtonsLayout->addWidget(removeFromQueueButton_);
|
||||
queueButtonsLayout->addStretch();
|
||||
queueLayout->addLayout(queueButtonsLayout);
|
||||
|
||||
mainLayout_->addWidget(queueGroup);
|
||||
|
||||
connect(clearQueueButton_, &QPushButton::clicked, this, &MainWindow::onClearQueueClicked);
|
||||
connect(removeFromQueueButton_, &QPushButton::clicked, this, &MainWindow::onRemoveFromQueueClicked);
|
||||
connect(queueList_, &QListWidget::itemDoubleClicked, this, &MainWindow::onQueueItemDoubleClicked);
|
||||
}
|
||||
|
||||
void MainWindow::setupStatusBar() {
|
||||
statusBar_ = new QStatusBar(this);
|
||||
setStatusBar(statusBar_);
|
||||
|
||||
connectionStatusLabel_ = new QLabel("Disconnected", this);
|
||||
statusBar_->addWidget(connectionStatusLabel_);
|
||||
|
||||
authStatusLabel_ = new QLabel("Not logged in", this);
|
||||
statusBar_->addPermanentWidget(authStatusLabel_);
|
||||
}
|
||||
|
||||
void MainWindow::setAuthToken(const QString& token) {
|
||||
authToken_ = token;
|
||||
authStatusLabel_->setText("Logged in as Admin");
|
||||
loginAction_->setEnabled(false);
|
||||
logoutAction_->setEnabled(true);
|
||||
settingsAction_->setEnabled(true);
|
||||
reloadAction_->setEnabled(true);
|
||||
updateButtonStates();
|
||||
}
|
||||
|
||||
void MainWindow::clearAuthToken() {
|
||||
authToken_.reset();
|
||||
authStatusLabel_->setText("Not logged in");
|
||||
loginAction_->setEnabled(true);
|
||||
logoutAction_->setEnabled(false);
|
||||
settingsAction_->setEnabled(false);
|
||||
reloadAction_->setEnabled(false);
|
||||
updateButtonStates();
|
||||
}
|
||||
|
||||
bool MainWindow::isAuthenticated() const {
|
||||
return authToken_.has_value();
|
||||
}
|
||||
|
||||
void MainWindow::onPlaybackStateChanged(const Api::PlaybackState& state) {
|
||||
currentState_ = state;
|
||||
updatePlaybackDisplay();
|
||||
}
|
||||
|
||||
void MainWindow::onQueueUpdated(const std::vector<Api::QueueEntry>& queue) {
|
||||
currentQueue_ = queue;
|
||||
updateQueueDisplay();
|
||||
}
|
||||
|
||||
void MainWindow::onConnectionStatusChanged(bool connected) {
|
||||
connectedToCore_ = connected;
|
||||
connectionStatusLabel_->setText(connected ? "Connected" : "Disconnected");
|
||||
if (!connected) {
|
||||
clearAuthToken();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onPlayClicked() {
|
||||
if (!isAuthenticated()) {
|
||||
QMessageBox::warning(this, "Authentication Required", "Please login first.");
|
||||
return;
|
||||
}
|
||||
|
||||
QString query = searchEdit_->text();
|
||||
if (!query.isEmpty()) {
|
||||
emit playRequested(query);
|
||||
} else if (!currentQueue_.empty()) {
|
||||
// Play first item in queue
|
||||
emit playFromQueueRequested(0);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onPauseClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
emit pauseRequested();
|
||||
}
|
||||
|
||||
void MainWindow::onStopClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
emit stopRequested();
|
||||
}
|
||||
|
||||
void MainWindow::onSkipClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
emit skipRequested();
|
||||
}
|
||||
|
||||
void MainWindow::onVolumeChanged(int value) {
|
||||
volumeLabel_->setText(QString("%1%").arg(value));
|
||||
emit volumeChangeRequested(value);
|
||||
}
|
||||
|
||||
void MainWindow::onSeekSliderMoved(int value) {
|
||||
if (!isAuthenticated()) return;
|
||||
emit seekRequested(value * 1000); // Convert to ms
|
||||
}
|
||||
|
||||
void MainWindow::onSearchClicked() {
|
||||
if (!isAuthenticated()) {
|
||||
QMessageBox::warning(this, "Authentication Required", "Please login first.");
|
||||
return;
|
||||
}
|
||||
|
||||
QString query = searchEdit_->text();
|
||||
if (query.isEmpty()) return;
|
||||
|
||||
searchResults_->clear();
|
||||
emit searchRequested(query);
|
||||
}
|
||||
|
||||
void MainWindow::onSearchResultDoubleClicked(QListWidgetItem* item) {
|
||||
if (!isAuthenticated() || !item) return;
|
||||
|
||||
int index = searchResults_->row(item);
|
||||
emit addSearchResultToQueueRequested(index);
|
||||
}
|
||||
|
||||
void MainWindow::onQueueItemDoubleClicked(QListWidgetItem* item) {
|
||||
if (!isAuthenticated() || !item) return;
|
||||
|
||||
int index = queueList_->row(item);
|
||||
emit playFromQueueRequested(index);
|
||||
}
|
||||
|
||||
void MainWindow::onShuffleClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
emit toggleShuffleRequested();
|
||||
}
|
||||
|
||||
void MainWindow::onRepeatClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
emit toggleRepeatRequested();
|
||||
}
|
||||
|
||||
void MainWindow::onClearQueueClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
|
||||
auto reply = QMessageBox::question(this, "Clear Queue",
|
||||
"Are you sure you want to clear the queue?",
|
||||
QMessageBox::Yes | QMessageBox::No);
|
||||
|
||||
if (reply == QMessageBox::Yes) {
|
||||
emit clearQueueRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onRemoveFromQueueClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
|
||||
auto* item = queueList_->currentItem();
|
||||
if (!item) return;
|
||||
|
||||
int index = queueList_->row(item);
|
||||
emit removeFromQueueRequested(index);
|
||||
}
|
||||
|
||||
void MainWindow::onSettingsClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
|
||||
// TODO: Implement settings dialog
|
||||
QMessageBox::information(this, "Settings", "Settings dialog will be implemented here.");
|
||||
}
|
||||
|
||||
void MainWindow::onReloadClicked() {
|
||||
if (!isAuthenticated()) return;
|
||||
|
||||
auto reply = QMessageBox::question(this, "Hot Reload",
|
||||
"Are you sure you want to reload the core?\n\n"
|
||||
"This will briefly interrupt playback.",
|
||||
QMessageBox::Yes | QMessageBox::No);
|
||||
|
||||
if (reply == QMessageBox::Yes) {
|
||||
emit reloadCoreRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onLoginLogoutClicked() {
|
||||
if (isAuthenticated()) {
|
||||
clearAuthToken();
|
||||
emit logoutRequested();
|
||||
} else {
|
||||
emit loginRequested();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onSearchResultsReceived(const Echoion::Api::SearchResult& results) {
|
||||
searchResults_->clear();
|
||||
for (const auto& track : results.tracks) {
|
||||
QString text = QString::fromStdString(track.title + " - " + track.artist);
|
||||
searchResults_->addItem(text);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::updatePlaybackDisplay() {
|
||||
if (currentState_.currentTrack.has_value()) {
|
||||
const auto& track = currentState_.currentTrack->track;
|
||||
nowPlayingLabel_->setText(QString::fromStdString(track.title));
|
||||
artistLabel_->setText(QString::fromStdString(track.artist));
|
||||
|
||||
if (track.duration.has_value()) {
|
||||
auto totalMs = track.duration->count();
|
||||
auto currentMs = currentState_.position.count();
|
||||
progressBar_->setMaximum(static_cast<int>(totalMs / 1000));
|
||||
progressBar_->setValue(static_cast<int>(currentMs / 1000));
|
||||
|
||||
timeLabel_->setText(QString("%1 / %2")
|
||||
.arg(formatDuration(currentState_.position))
|
||||
.arg(formatDuration(*track.duration)));
|
||||
}
|
||||
} else {
|
||||
nowPlayingLabel_->setText("Nothing is playing");
|
||||
artistLabel_->setText("");
|
||||
progressBar_->setValue(0);
|
||||
timeLabel_->setText("0:00 / 0:00");
|
||||
}
|
||||
|
||||
shuffleButton_->setText(currentState_.shuffle ? "Shuffle: On" : "Shuffle: Off");
|
||||
repeatButton_->setText(currentState_.repeat ? "Repeat: On" : "Repeat: Off");
|
||||
volumeSlider_->setValue(static_cast<int>(currentState_.volume * 100));
|
||||
}
|
||||
|
||||
void MainWindow::updateQueueDisplay() {
|
||||
queueList_->clear();
|
||||
for (size_t i = 0; i < currentQueue_.size(); ++i) {
|
||||
const auto& entry = currentQueue_[i];
|
||||
QString text = QString("%1. %2 - %3")
|
||||
.arg(i + 1)
|
||||
.arg(QString::fromStdString(entry.track.title))
|
||||
.arg(QString::fromStdString(entry.track.artist));
|
||||
queueList_->addItem(text);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::updateButtonStates() {
|
||||
bool hasAuth = isAuthenticated();
|
||||
playButton_->setEnabled(hasAuth);
|
||||
pauseButton_->setEnabled(hasAuth);
|
||||
stopButton_->setEnabled(hasAuth);
|
||||
skipButton_->setEnabled(hasAuth);
|
||||
shuffleButton_->setEnabled(hasAuth);
|
||||
repeatButton_->setEnabled(hasAuth);
|
||||
searchButton_->setEnabled(hasAuth);
|
||||
clearQueueButton_->setEnabled(hasAuth);
|
||||
removeFromQueueButton_->setEnabled(hasAuth);
|
||||
}
|
||||
|
||||
QString MainWindow::formatDuration(std::chrono::milliseconds ms) {
|
||||
auto totalSeconds = ms.count() / 1000;
|
||||
auto minutes = totalSeconds / 60;
|
||||
auto seconds = totalSeconds % 60;
|
||||
return QString("%1:%2").arg(minutes).arg(seconds, 2, 10, QChar('0'));
|
||||
}
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
+53
-135
@@ -1,155 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QPushButton>
|
||||
#include "CoreClient.hpp"
|
||||
#include <QJsonObject>
|
||||
#include <QLabel>
|
||||
#include <QSlider>
|
||||
#include <QProgressBar>
|
||||
#include <QLineEdit>
|
||||
#include <QListWidget>
|
||||
#include <QGroupBox>
|
||||
#include <QMainWindow>
|
||||
#include <QPushButton>
|
||||
#include <QStatusBar>
|
||||
#include <QMenuBar>
|
||||
#include <QAction>
|
||||
#include <QMessageBox>
|
||||
#include <QTimer>
|
||||
#include <optional>
|
||||
#include <echoion/api.hpp>
|
||||
#include <QVBoxLayout>
|
||||
#include <QWidget>
|
||||
|
||||
namespace Echoion::Desktop {
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
~MainWindow() override;
|
||||
public:
|
||||
|
||||
// Authentication
|
||||
void setAuthToken(const QString& token);
|
||||
void clearAuthToken();
|
||||
[[nodiscard]] bool isAuthenticated() const;
|
||||
explicit MainWindow(CoreClient* client, QWidget* parent = nullptr);
|
||||
~MainWindow() override;
|
||||
|
||||
// State updates
|
||||
void onPlaybackStateChanged(const Api::PlaybackState& state);
|
||||
void onQueueUpdated(const std::vector<Api::QueueEntry>& queue);
|
||||
void onConnectionStatusChanged(bool connected);
|
||||
void onSearchResultsReceived(const Api::SearchResult& results);
|
||||
private slots:
|
||||
|
||||
signals:
|
||||
// Connection signals
|
||||
void loginRequested();
|
||||
void logoutRequested();
|
||||
|
||||
// Playback control signals
|
||||
void playRequested(const QString& query);
|
||||
void playFromQueueRequested(int index);
|
||||
void pauseRequested();
|
||||
void stopRequested();
|
||||
void resumeRequested();
|
||||
void skipRequested();
|
||||
void seekRequested(int positionMs);
|
||||
void volumeChangeRequested(int volume);
|
||||
void toggleShuffleRequested();
|
||||
void toggleRepeatRequested();
|
||||
|
||||
// Queue signals
|
||||
void clearQueueRequested();
|
||||
void removeFromQueueRequested(int index);
|
||||
void moveInQueueRequested(int from, int to);
|
||||
|
||||
// Search signals
|
||||
void searchRequested(const QString& query);
|
||||
void addSearchResultToQueueRequested(int index);
|
||||
|
||||
// System signals
|
||||
void reloadCoreRequested();
|
||||
void settingsRequested();
|
||||
void onConnectClicked();
|
||||
void onAuthClicked();
|
||||
void onConnected();
|
||||
void onDisconnected();
|
||||
void onAuthTokenRequested();
|
||||
void onAuthenticated();
|
||||
void onAuthFailed(const QString& reason);
|
||||
void onEventReceived(const QString& event, const QJsonObject& data);
|
||||
void updateConnectionStatus();
|
||||
void updatePlaybackInfo(const QJsonObject& data);
|
||||
void onPlayClicked();
|
||||
void onPauseClicked();
|
||||
void onSkipClicked();
|
||||
|
||||
private slots:
|
||||
void onPlayClicked();
|
||||
void onPauseClicked();
|
||||
void onStopClicked();
|
||||
void onSkipClicked();
|
||||
void onVolumeChanged(int value);
|
||||
void onSeekSliderMoved(int value);
|
||||
void onSearchClicked();
|
||||
void onSearchResultDoubleClicked(QListWidgetItem* item);
|
||||
void onQueueItemDoubleClicked(QListWidgetItem* item);
|
||||
void onShuffleClicked();
|
||||
void onRepeatClicked();
|
||||
void onClearQueueClicked();
|
||||
void onRemoveFromQueueClicked();
|
||||
void onSettingsClicked();
|
||||
void onReloadClicked();
|
||||
void onLoginLogoutClicked();
|
||||
private:
|
||||
|
||||
private:
|
||||
void setupUi();
|
||||
void setupMenuBar();
|
||||
void setupPlaybackControls();
|
||||
void setupSearchPanel();
|
||||
void setupQueueView();
|
||||
void setupStatusBar();
|
||||
void updatePlaybackDisplay();
|
||||
void updateQueueDisplay();
|
||||
void updateButtonStates();
|
||||
|
||||
[[nodiscard]] QString formatDuration(std::chrono::milliseconds ms);
|
||||
void setupUI();
|
||||
void setupConnections();
|
||||
|
||||
// Central widget
|
||||
QWidget* centralWidget_;
|
||||
QVBoxLayout* mainLayout_;
|
||||
CoreClient* client_ = nullptr;
|
||||
|
||||
// Playback controls
|
||||
QLabel* nowPlayingLabel_;
|
||||
QLabel* artistLabel_;
|
||||
QProgressBar* progressBar_;
|
||||
QLabel* timeLabel_;
|
||||
QPushButton* playButton_;
|
||||
QPushButton* pauseButton_;
|
||||
QPushButton* stopButton_;
|
||||
QPushButton* skipButton_;
|
||||
QPushButton* shuffleButton_;
|
||||
QPushButton* repeatButton_;
|
||||
QSlider* volumeSlider_;
|
||||
QLabel* volumeLabel_;
|
||||
// Connection section
|
||||
QLineEdit* hostEdit_ = nullptr;
|
||||
QLineEdit* portEdit_ = nullptr;
|
||||
QPushButton* connectBtn_ = nullptr;
|
||||
QLabel* connectionStatus_ = nullptr;
|
||||
|
||||
// Search panel
|
||||
QLineEdit* searchEdit_;
|
||||
QPushButton* searchButton_;
|
||||
QListWidget* searchResults_;
|
||||
// Auth section
|
||||
QLineEdit* nicknameEdit_ = nullptr;
|
||||
QLineEdit* tokenEdit_ = nullptr;
|
||||
QPushButton* requestTokenBtn_ = nullptr;
|
||||
QPushButton* authBtn_ = nullptr;
|
||||
QLabel* authStatus_ = nullptr;
|
||||
|
||||
// Queue view
|
||||
QListWidget* queueList_;
|
||||
QPushButton* clearQueueButton_;
|
||||
QPushButton* removeFromQueueButton_;
|
||||
// Playback section
|
||||
QLabel* currentTrackLabel_ = nullptr;
|
||||
QLabel* artistLabel_ = nullptr;
|
||||
QLabel* statusLabel_ = nullptr;
|
||||
QPushButton* playBtn_ = nullptr;
|
||||
QPushButton* pauseBtn_ = nullptr;
|
||||
QPushButton* skipBtn_ = nullptr;
|
||||
|
||||
// Status bar
|
||||
QStatusBar* statusBar_;
|
||||
QLabel* connectionStatusLabel_;
|
||||
QLabel* authStatusLabel_;
|
||||
|
||||
// Menu actions
|
||||
QAction* loginAction_;
|
||||
QAction* logoutAction_;
|
||||
QAction* settingsAction_;
|
||||
QAction* reloadAction_;
|
||||
QAction* exitAction_;
|
||||
QAction* aboutAction_;
|
||||
|
||||
// State
|
||||
QTimer* updateTimer_;
|
||||
std::optional<QString> authToken_;
|
||||
Api::PlaybackState currentState_;
|
||||
std::vector<Api::QueueEntry> currentQueue_;
|
||||
bool connectedToCore_ = false;
|
||||
|
||||
// Buttons for login/logout
|
||||
QPushButton* loginLogoutButton_;
|
||||
};
|
||||
// Status bar
|
||||
QStatusBar* statusBar_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
+943
-60
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,294 @@
|
||||
#include "storage/SecureStorage.hpp"
|
||||
|
||||
#include <Void/Security/Security.hpp>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QCryptographicHash>
|
||||
#include <QDataStream>
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QRandomGenerator>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <algorithm>
|
||||
#include <span>
|
||||
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
const QByteArray storageMagic{"ECV2", 4};
|
||||
|
||||
[[nodiscard]] Void::Security::SecretKey
|
||||
toSecretKey(const QByteArray& value)
|
||||
{
|
||||
Void::Security::SecretKey key{};
|
||||
std::copy_n(
|
||||
reinterpret_cast<const unsigned char*>(value.constData()),
|
||||
key.size(), key.begin());
|
||||
return key;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SecureStorage::SecureStorage() = default;
|
||||
|
||||
QString SecureStorage::getStorageDir()
|
||||
{
|
||||
QString appDir = QCoreApplication::applicationDirPath();
|
||||
QString dataDir = appDir + "/data";
|
||||
|
||||
QDir dir(dataDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(".");
|
||||
}
|
||||
|
||||
return dataDir;
|
||||
}
|
||||
|
||||
QString SecureStorage::getSettingsPath()
|
||||
{
|
||||
return getStorageDir() + "/settings.dat";
|
||||
}
|
||||
|
||||
QString SecureStorage::getTokensPath()
|
||||
{
|
||||
return getStorageDir() + "/tokens.dat";
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::loadOrCreateKey() const
|
||||
{
|
||||
if (!cachedKey_.isEmpty()) {
|
||||
return cachedKey_;
|
||||
}
|
||||
|
||||
const QString keyPath = getStorageDir() + "/secrets.key";
|
||||
const QFileInfo keyInfo{keyPath};
|
||||
if (keyInfo.isSymLink()) {
|
||||
return {};
|
||||
}
|
||||
QFile keyFile{keyPath};
|
||||
if (keyFile.exists()) {
|
||||
if (!keyFile.open(QIODevice::ReadOnly)) {
|
||||
return {};
|
||||
}
|
||||
QByteArray key = keyFile.readAll();
|
||||
if (key.size() !=
|
||||
static_cast<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
cachedKey_ = key;
|
||||
return cachedKey_;
|
||||
}
|
||||
|
||||
auto generated = Void::Security::random_secret_key();
|
||||
if (!generated) {
|
||||
return {};
|
||||
}
|
||||
QByteArray key{reinterpret_cast<const char*>(generated->data()),
|
||||
static_cast<qsizetype>(generated->size())};
|
||||
QSaveFile output{keyPath};
|
||||
if (!output.open(QIODevice::WriteOnly)) {
|
||||
return {};
|
||||
}
|
||||
if (!output.setPermissions(QFileDevice::ReadOwner |
|
||||
QFileDevice::WriteOwner) ||
|
||||
output.write(key) != key.size() || !output.commit()) {
|
||||
output.cancelWriting();
|
||||
return {};
|
||||
}
|
||||
cachedKey_ = key;
|
||||
return cachedKey_;
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::legacyKey() const
|
||||
{
|
||||
QString seed =
|
||||
QCoreApplication::applicationDirPath() +
|
||||
QCoreApplication::applicationName() +
|
||||
QString::number(QDateTime::currentDateTime().date().year());
|
||||
|
||||
QByteArray hash =
|
||||
QCryptographicHash::hash(seed.toUtf8(), QCryptographicHash::Sha256);
|
||||
|
||||
// Add some randomness but keep it deterministic for this machine
|
||||
QRandomGenerator rng(qHash(seed));
|
||||
for (int i = 0; i < hash.size(); ++i) {
|
||||
hash[i] = hash[i] ^ static_cast<char>(rng.generate() & 0xFF);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::legacyXor(const QByteArray& data,
|
||||
const QByteArray& key)
|
||||
{
|
||||
if (key.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
QByteArray result = data;
|
||||
const qsizetype keyLen = key.size();
|
||||
|
||||
for (qsizetype i = 0; i < result.size(); ++i) {
|
||||
result[i] = result[i] ^ key[i % keyLen];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::encrypt(const QByteArray& data) const
|
||||
{
|
||||
const QByteArray encodedKey = loadOrCreateKey();
|
||||
if (encodedKey.size() !=
|
||||
static_cast<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
const auto* bytes =
|
||||
reinterpret_cast<const unsigned char*>(data.constData());
|
||||
auto sealed = Void::Security::seal_secret(
|
||||
std::span<const unsigned char>{
|
||||
bytes, static_cast<std::size_t>(data.size())},
|
||||
toSecretKey(encodedKey));
|
||||
if (!sealed) {
|
||||
return {};
|
||||
}
|
||||
QByteArray result = storageMagic;
|
||||
result.append(reinterpret_cast<const char*>(sealed->data()),
|
||||
static_cast<qsizetype>(sealed->size()));
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::decrypt(const QByteArray& data) const
|
||||
{
|
||||
if (data.startsWith(storageMagic)) {
|
||||
const QByteArray encodedKey = loadOrCreateKey();
|
||||
if (encodedKey.size() !=
|
||||
static_cast<qsizetype>(Void::Security::secret_key_bytes)) {
|
||||
return {};
|
||||
}
|
||||
const auto envelope = data.sliced(storageMagic.size());
|
||||
const auto* bytes =
|
||||
reinterpret_cast<const unsigned char*>(envelope.constData());
|
||||
auto opened = Void::Security::open_secret(
|
||||
std::span<const unsigned char>{
|
||||
bytes, static_cast<std::size_t>(envelope.size())},
|
||||
toSecretKey(encodedKey));
|
||||
if (!opened) {
|
||||
return {};
|
||||
}
|
||||
return QByteArray{reinterpret_cast<const char*>(opened->data()),
|
||||
static_cast<qsizetype>(opened->size())};
|
||||
}
|
||||
|
||||
// Accept the v1 format only to migrate existing installations.
|
||||
if (data.length() < 8) {
|
||||
return {};
|
||||
}
|
||||
|
||||
QDataStream headerStream(data);
|
||||
quint32 magic = 0;
|
||||
quint32 dataLen = 0;
|
||||
headerStream >> magic >> dataLen;
|
||||
if (magic != 0x4543484F ||
|
||||
dataLen != static_cast<quint32>(data.size() - 8)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return legacyXor(data.sliced(8), legacyKey());
|
||||
}
|
||||
|
||||
bool SecureStorage::saveToFile(const QString& filepath,
|
||||
const QByteArray& data) const
|
||||
{
|
||||
// Create backup of existing file
|
||||
if (QFile::exists(filepath)) {
|
||||
createBackup(filepath);
|
||||
}
|
||||
|
||||
QSaveFile output{filepath};
|
||||
if (!output.open(QIODevice::WriteOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!output.setPermissions(QFileDevice::ReadOwner |
|
||||
QFileDevice::WriteOwner) ||
|
||||
output.write(data) != data.size()) {
|
||||
output.cancelWriting();
|
||||
return false;
|
||||
}
|
||||
return output.commit();
|
||||
}
|
||||
|
||||
QByteArray SecureStorage::loadFromFile(const QString& filepath) const
|
||||
{
|
||||
QFile file(filepath);
|
||||
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
// Try to restore from backup
|
||||
if (restoreFromBackup(filepath)) {
|
||||
return loadFromFile(filepath); // Retry
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void SecureStorage::createBackup(const QString& filepath) const
|
||||
{
|
||||
QString backupPath = filepath + ".backup";
|
||||
QFile::remove(backupPath); // Remove old backup
|
||||
QFile::copy(filepath, backupPath);
|
||||
|
||||
// Cleanup old backups (keep only last 3)
|
||||
cleanupOldBackups(filepath, 3);
|
||||
}
|
||||
|
||||
bool SecureStorage::restoreFromBackup(const QString& filepath) const
|
||||
{
|
||||
QString backupPath = filepath + ".backup";
|
||||
|
||||
if (!QFile::exists(backupPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile::remove(filepath);
|
||||
return QFile::copy(backupPath, filepath);
|
||||
}
|
||||
|
||||
void SecureStorage::cleanupOldBackups(const QString& filepath,
|
||||
int maxBackups) const
|
||||
{
|
||||
QString basePath = filepath + ".backup";
|
||||
QFileInfo info(basePath);
|
||||
QDir dir = info.dir();
|
||||
QString baseName = info.fileName();
|
||||
|
||||
// List all backup files
|
||||
QStringList filters;
|
||||
filters << baseName + "*";
|
||||
|
||||
QFileInfoList backups = dir.entryInfoList(filters, QDir::Files);
|
||||
|
||||
if (backups.size() <= maxBackups) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort by modification time (oldest first)
|
||||
std::sort(backups.begin(), backups.end(),
|
||||
[](const QFileInfo& a, const QFileInfo& b) {
|
||||
return a.lastModified() < b.lastModified();
|
||||
});
|
||||
|
||||
// Remove oldest backups
|
||||
for (int i = 0; i < backups.size() - maxBackups; ++i) {
|
||||
QFile::remove(backups[i].absoluteFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
class SecureStorage
|
||||
{
|
||||
public:
|
||||
|
||||
SecureStorage();
|
||||
|
||||
// Encrypt/decrypt data with authenticated VoidLibrary primitives.
|
||||
[[nodiscard]] QByteArray encrypt(const QByteArray& data) const;
|
||||
[[nodiscard]] QByteArray decrypt(const QByteArray& data) const;
|
||||
|
||||
// File operations with backup
|
||||
[[nodiscard]] bool saveToFile(const QString& filepath,
|
||||
const QByteArray& data) const;
|
||||
|
||||
[[nodiscard]] QByteArray
|
||||
loadFromFile(const QString& filepath) const;
|
||||
|
||||
// Backup management
|
||||
void createBackup(const QString& filepath) const;
|
||||
bool restoreFromBackup(const QString& filepath) const;
|
||||
void cleanupOldBackups(const QString& filepath,
|
||||
int maxBackups = 3) const;
|
||||
|
||||
// Get storage directory (relative to executable)
|
||||
[[nodiscard]] static QString getStorageDir();
|
||||
[[nodiscard]] static QString getSettingsPath();
|
||||
[[nodiscard]] static QString getTokensPath();
|
||||
[[nodiscard]] static QString getCacheDir();
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] QByteArray loadOrCreateKey() const;
|
||||
|
||||
// Read-only migration support for the v1 deterministic XOR format.
|
||||
[[nodiscard]] QByteArray legacyKey() const;
|
||||
[[nodiscard]] static QByteArray legacyXor(const QByteArray& data,
|
||||
const QByteArray& key);
|
||||
|
||||
mutable QByteArray cachedKey_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
@@ -0,0 +1,298 @@
|
||||
#include "storage/SecureStorage.hpp"
|
||||
#include "storage/SettingsManager.hpp"
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QFile>
|
||||
#include <QIODevice>
|
||||
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
SettingsManager::SettingsManager(QObject* parent) : QObject(parent) {}
|
||||
|
||||
bool SettingsManager::load()
|
||||
{
|
||||
bool settingsOk = loadSettings();
|
||||
bool tokensOk = loadTokens();
|
||||
|
||||
if (settingsOk || tokensOk) {
|
||||
emit settingsLoaded();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SettingsManager::save()
|
||||
{
|
||||
bool settingsOk = saveSettings();
|
||||
bool tokensOk = saveTokens();
|
||||
|
||||
if (settingsOk && tokensOk) {
|
||||
emit settingsSaved();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SettingsManager::loadSettings()
|
||||
{
|
||||
SecureStorage storage;
|
||||
QString path = SecureStorage::getSettingsPath();
|
||||
|
||||
if (!QFile::exists(path)) {
|
||||
// No settings file yet, use defaults
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray encrypted = storage.loadFromFile(path);
|
||||
if (encrypted.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray decrypted = storage.decrypt(encrypted);
|
||||
if (decrypted.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream stream(decrypted);
|
||||
stream >> settings_;
|
||||
|
||||
return stream.status() == QDataStream::Ok;
|
||||
}
|
||||
|
||||
bool SettingsManager::saveSettings()
|
||||
{
|
||||
SecureStorage storage;
|
||||
|
||||
QByteArray data;
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
stream << settings_;
|
||||
|
||||
if (stream.status() != QDataStream::Ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray encrypted = storage.encrypt(data);
|
||||
return storage.saveToFile(SecureStorage::getSettingsPath(), encrypted);
|
||||
}
|
||||
|
||||
bool SettingsManager::loadTokens()
|
||||
{
|
||||
SecureStorage storage;
|
||||
QString path = SecureStorage::getTokensPath();
|
||||
|
||||
if (!QFile::exists(path)) {
|
||||
// No tokens file yet
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray encrypted = storage.loadFromFile(path);
|
||||
if (encrypted.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray decrypted = storage.decrypt(encrypted);
|
||||
if (decrypted.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream stream(decrypted);
|
||||
stream >> tokens_;
|
||||
|
||||
return stream.status() == QDataStream::Ok;
|
||||
}
|
||||
|
||||
bool SettingsManager::saveTokens()
|
||||
{
|
||||
SecureStorage storage;
|
||||
|
||||
QByteArray data;
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
stream << tokens_;
|
||||
|
||||
if (stream.status() != QDataStream::Ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray encrypted = storage.encrypt(data);
|
||||
return storage.saveToFile(SecureStorage::getTokensPath(), encrypted);
|
||||
}
|
||||
|
||||
void SettingsManager::addProfile(const ConnectionProfile& profile)
|
||||
{
|
||||
// Remove existing profile with same name
|
||||
removeProfile(profile.name);
|
||||
|
||||
settings_.profiles.append(profile);
|
||||
|
||||
// If this is the first profile or marked as default, update
|
||||
// lastProfileName
|
||||
if (settings_.profiles.size() == 1 || profile.isDefault) {
|
||||
if (profile.isDefault) {
|
||||
// Unset default on other profiles
|
||||
for (auto& p : settings_.profiles) {
|
||||
if (p.name != profile.name) {
|
||||
p.isDefault = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
settings_.lastProfileName = profile.name;
|
||||
}
|
||||
|
||||
save();
|
||||
emit profileAdded(profile.name);
|
||||
}
|
||||
|
||||
void SettingsManager::removeProfile(const QString& name)
|
||||
{
|
||||
auto it = std::remove_if(
|
||||
settings_.profiles.begin(),
|
||||
settings_.profiles.end(),
|
||||
[&name](const ConnectionProfile& p) { return p.name == name; });
|
||||
|
||||
if (it != settings_.profiles.end()) {
|
||||
settings_.profiles.erase(it, settings_.profiles.end());
|
||||
|
||||
// Update lastProfileName if needed
|
||||
if (settings_.lastProfileName == name) {
|
||||
if (!settings_.profiles.isEmpty()) {
|
||||
settings_.lastProfileName = settings_.profiles.first().name;
|
||||
} else {
|
||||
settings_.lastProfileName.clear();
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
emit profileRemoved(name);
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsManager::updateProfile(const QString& name,
|
||||
const ConnectionProfile& profile)
|
||||
{
|
||||
removeProfile(name);
|
||||
addProfile(profile);
|
||||
emit profileChanged(profile.name);
|
||||
}
|
||||
|
||||
QVector<ConnectionProfile> SettingsManager::profiles() const
|
||||
{
|
||||
return settings_.profiles;
|
||||
}
|
||||
|
||||
std::optional<ConnectionProfile>
|
||||
SettingsManager::getProfile(const QString& name) const
|
||||
{
|
||||
for (const auto& profile : settings_.profiles) {
|
||||
if (profile.name == name) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ConnectionProfile> SettingsManager::getDefaultProfile() const
|
||||
{
|
||||
// First try to find profile marked as default
|
||||
for (const auto& profile : settings_.profiles) {
|
||||
if (profile.isDefault) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
|
||||
// Then try last used profile
|
||||
if (!settings_.lastProfileName.isEmpty()) {
|
||||
auto profile = getProfile(settings_.lastProfileName);
|
||||
if (profile) {
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally return first profile
|
||||
if (!settings_.profiles.isEmpty()) {
|
||||
return settings_.profiles.first();
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void SettingsManager::setDefaultProfile(const QString& name)
|
||||
{
|
||||
for (auto& profile : settings_.profiles) {
|
||||
profile.isDefault = (profile.name == name);
|
||||
}
|
||||
|
||||
settings_.lastProfileName = name;
|
||||
save();
|
||||
}
|
||||
|
||||
void SettingsManager::saveToken(const QString& host, int port,
|
||||
const QString& nickname,
|
||||
const QString& token)
|
||||
{
|
||||
// Remove existing token for this host:port
|
||||
deleteToken(host, port);
|
||||
|
||||
CachedToken ct;
|
||||
ct.host = host;
|
||||
ct.port = port;
|
||||
ct.nickname = nickname;
|
||||
ct.token = token;
|
||||
ct.savedAt = QDateTime::currentDateTime();
|
||||
|
||||
tokens_.append(ct);
|
||||
saveTokens();
|
||||
|
||||
emit tokenSaved(host, port);
|
||||
}
|
||||
|
||||
std::optional<QString>
|
||||
SettingsManager::loadToken(const QString& host, int port,
|
||||
QString& outNickname) const
|
||||
{
|
||||
for (const auto& ct : tokens_) {
|
||||
if (ct.host == host && ct.port == port) {
|
||||
outNickname = ct.nickname;
|
||||
return ct.token;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void SettingsManager::deleteToken(const QString& host, int port)
|
||||
{
|
||||
auto it = std::remove_if(tokens_.begin(), tokens_.end(),
|
||||
[&host, port](const CachedToken& ct) {
|
||||
return ct.host == host && ct.port == port;
|
||||
});
|
||||
|
||||
if (it != tokens_.end()) {
|
||||
tokens_.erase(it, tokens_.end());
|
||||
saveTokens();
|
||||
emit tokenDeleted(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsManager::clearAllTokens()
|
||||
{
|
||||
tokens_.clear();
|
||||
saveTokens();
|
||||
}
|
||||
|
||||
bool SettingsManager::hasToken(const QString& host, int port) const
|
||||
{
|
||||
for (const auto& ct : tokens_) {
|
||||
if (ct.host == host && ct.port == port) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
AppSettings& SettingsManager::settings() { return settings_; }
|
||||
|
||||
const AppSettings& SettingsManager::settings() const { return settings_; }
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
@@ -0,0 +1,153 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QDateTime>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
|
||||
struct ConnectionProfile {
|
||||
QString name;
|
||||
QString host;
|
||||
int port = 8765;
|
||||
QString nickname;
|
||||
bool autoConnect = false;
|
||||
bool autoAuth = false;
|
||||
bool isDefault = false;
|
||||
|
||||
// Serialization
|
||||
friend QDataStream& operator<<(QDataStream& out,
|
||||
const ConnectionProfile& profile)
|
||||
{
|
||||
out << profile.name << profile.host << profile.port
|
||||
<< profile.nickname << profile.autoConnect
|
||||
<< profile.autoAuth << profile.isDefault;
|
||||
return out;
|
||||
}
|
||||
|
||||
friend QDataStream& operator>>(QDataStream& in,
|
||||
ConnectionProfile& profile)
|
||||
{
|
||||
in >> profile.name >> profile.host >> profile.port >>
|
||||
profile.nickname >> profile.autoConnect >>
|
||||
profile.autoAuth >> profile.isDefault;
|
||||
return in;
|
||||
}
|
||||
};
|
||||
|
||||
struct CachedToken {
|
||||
QString host;
|
||||
int port;
|
||||
QString nickname;
|
||||
QString token;
|
||||
QDateTime savedAt;
|
||||
|
||||
// Serialization
|
||||
friend QDataStream& operator<<(QDataStream& out,
|
||||
const CachedToken& token)
|
||||
{
|
||||
out << token.host << token.port << token.nickname << token.token
|
||||
<< token.savedAt;
|
||||
return out;
|
||||
}
|
||||
|
||||
friend QDataStream& operator>>(QDataStream& in, CachedToken& token)
|
||||
{
|
||||
in >> token.host >> token.port >> token.nickname >>
|
||||
token.token >> token.savedAt;
|
||||
return in;
|
||||
}
|
||||
};
|
||||
|
||||
struct AppSettings {
|
||||
QVector<ConnectionProfile> profiles;
|
||||
QString lastProfileName;
|
||||
bool autoConnect = true;
|
||||
bool autoAuth = true;
|
||||
bool autoReconnect = true;
|
||||
int maxReconnectAttempts = 5;
|
||||
int requestTimeoutMs = 10000;
|
||||
|
||||
// Serialization
|
||||
friend QDataStream& operator<<(QDataStream& out,
|
||||
const AppSettings& settings)
|
||||
{
|
||||
out << settings.profiles << settings.lastProfileName
|
||||
<< settings.autoConnect << settings.autoAuth
|
||||
<< settings.autoReconnect << settings.maxReconnectAttempts
|
||||
<< settings.requestTimeoutMs;
|
||||
return out;
|
||||
}
|
||||
|
||||
friend QDataStream& operator>>(QDataStream& in,
|
||||
AppSettings& settings)
|
||||
{
|
||||
in >> settings.profiles >> settings.lastProfileName >>
|
||||
settings.autoConnect >> settings.autoAuth >>
|
||||
settings.autoReconnect >> settings.maxReconnectAttempts >>
|
||||
settings.requestTimeoutMs;
|
||||
return in;
|
||||
}
|
||||
};
|
||||
|
||||
class SettingsManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit SettingsManager(QObject* parent = nullptr);
|
||||
|
||||
// Load/save all settings
|
||||
bool load();
|
||||
bool save();
|
||||
|
||||
// Connection profiles
|
||||
void addProfile(const ConnectionProfile& profile);
|
||||
void removeProfile(const QString& name);
|
||||
void updateProfile(const QString& name,
|
||||
const ConnectionProfile& profile);
|
||||
QVector<ConnectionProfile> profiles() const;
|
||||
std::optional<ConnectionProfile>
|
||||
getProfile(const QString& name) const;
|
||||
std::optional<ConnectionProfile> getDefaultProfile() const;
|
||||
void setDefaultProfile(const QString& name);
|
||||
|
||||
// Token management
|
||||
void saveToken(const QString& host, int port,
|
||||
const QString& nickname, const QString& token);
|
||||
std::optional<QString> loadToken(const QString& host, int port,
|
||||
QString& outNickname) const;
|
||||
void deleteToken(const QString& host, int port);
|
||||
void clearAllTokens();
|
||||
bool hasToken(const QString& host, int port) const;
|
||||
|
||||
// Settings accessors
|
||||
AppSettings& settings();
|
||||
const AppSettings& settings() const;
|
||||
|
||||
signals:
|
||||
|
||||
void settingsLoaded();
|
||||
void settingsSaved();
|
||||
void profileAdded(const QString& name);
|
||||
void profileRemoved(const QString& name);
|
||||
void profileChanged(const QString& name);
|
||||
void tokenSaved(const QString& host, int port);
|
||||
void tokenDeleted(const QString& host, int port);
|
||||
|
||||
private:
|
||||
|
||||
bool loadSettings();
|
||||
bool saveSettings();
|
||||
bool loadTokens();
|
||||
bool saveTokens();
|
||||
|
||||
AppSettings settings_;
|
||||
QVector<CachedToken> tokens_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Desktop
|
||||
Reference in new issue
Block a user