vibed a cached downloader for images
This commit is contained in:
136
include/services/dbus/mpris.hpp
Normal file
136
include/services/dbus/mpris.hpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <gtkmm.h>
|
||||
#include <giomm.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "services/notificationController.hpp"
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusproxy.h"
|
||||
|
||||
class MprisController {
|
||||
public:
|
||||
MprisController() {
|
||||
// 1. Connect to the Session Bus
|
||||
Gio::DBus::Connection::get(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
sigc::mem_fun(*this, &MprisController::on_bus_connected)
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
Glib::RefPtr<Gio::DBus::Connection> m_connection;
|
||||
Glib::RefPtr<Gio::DBus::Proxy> m_proxy;
|
||||
|
||||
void on_bus_connected(const Glib::RefPtr<Gio::AsyncResult>& result) {
|
||||
if (!result) {
|
||||
std::cerr << "DBus Connection Error: null async result" << std::endl;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
m_connection = Gio::DBus::Connection::get_finish(result);
|
||||
|
||||
// 2. Create a Proxy to the media player
|
||||
// NOTE: In a real app, you must find the name dynamically (see Step 2 below).
|
||||
// For now, ensure a player like Spotify or VLC is running.
|
||||
// Try "org.mpris.MediaPlayer2.spotify" or "org.mpris.MediaPlayer2.vlc"
|
||||
std::string player_bus_name = "org.mpris.MediaPlayer2.spotify";
|
||||
|
||||
m_proxy = Gio::DBus::Proxy::create_sync(
|
||||
m_connection,
|
||||
player_bus_name, // The Bus Name
|
||||
"/org/mpris/MediaPlayer2", // The Object Path
|
||||
"org.mpris.MediaPlayer2.Player" // The Interface
|
||||
);
|
||||
|
||||
if (m_proxy) {
|
||||
std::cout << "Connected to: " << player_bus_name << std::endl;
|
||||
|
||||
// Get initial state
|
||||
print_metadata();
|
||||
|
||||
// 3. Listen for changes (Song change, Pause/Play)
|
||||
m_proxy->signal_properties_changed().connect(
|
||||
sigc::mem_fun(*this, &MprisController::on_properties_changed)
|
||||
);
|
||||
}
|
||||
|
||||
} catch (const Glib::Error& ex) {
|
||||
std::cerr << "DBus Connection Error: " << ex.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void print_metadata() {
|
||||
// Retrieve the cached property "Metadata"
|
||||
Glib::VariantBase metadata_var;
|
||||
m_proxy->get_cached_property(metadata_var, "Metadata");
|
||||
|
||||
if (!metadata_var) {
|
||||
std::cout << "No metadata available." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. Unpack Metadata (Type: a{sv})
|
||||
// This is a dictionary of string -> variant
|
||||
using MetadataMap = std::map<Glib::ustring, Glib::VariantBase>;
|
||||
MetadataMap metadata_map;
|
||||
|
||||
// Cast the variant to a container and iterate
|
||||
Glib::Variant<MetadataMap> variant_dict =
|
||||
Glib::VariantBase::cast_dynamic<Glib::Variant<MetadataMap>>(metadata_var);
|
||||
|
||||
metadata_map = variant_dict.get();
|
||||
|
||||
std::string title, artist, artwork_url;
|
||||
|
||||
if (metadata_map.count("xesam:title")) {
|
||||
auto title_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["xesam:title"]);
|
||||
title = title_var.get();
|
||||
}
|
||||
|
||||
if (metadata_map.count("xesam:artist")) {
|
||||
auto artist_var = metadata_map["xesam:artist"];
|
||||
|
||||
if (artist_var.is_of_type(Glib::VariantType("as"))) {
|
||||
auto artists = Glib::VariantBase::cast_dynamic<Glib::Variant<std::vector<Glib::ustring>>>(artist_var).get();
|
||||
if (!artists.empty()) {
|
||||
artist = artists[0]; // Take the first artist
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata_map.count("mpris:artUrl")) {
|
||||
auto art_var = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(metadata_map["mpris:artUrl"]);
|
||||
artwork_url = art_var.get();
|
||||
}
|
||||
|
||||
auto notifactionController = NotificationController::getInstance();
|
||||
notifactionController->showSpotifyNotification(title, artist, artwork_url);
|
||||
|
||||
}
|
||||
|
||||
// Called when the song changes
|
||||
void on_properties_changed(const Gio::DBus::Proxy::MapChangedProperties& changed_properties,
|
||||
const std::vector<Glib::ustring>& invalidated_properties) {
|
||||
(void)invalidated_properties;
|
||||
|
||||
// Only refresh when Metadata is updated
|
||||
if (changed_properties.find("Metadata") != changed_properties.end()) {
|
||||
// You could parse 'changed_properties' directly, but it's easier to just pull the new cache
|
||||
print_metadata();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
// Call this to toggle play/pause
|
||||
void toggle_play() {
|
||||
if(m_proxy) {
|
||||
m_proxy->call("PlayPause");
|
||||
}
|
||||
}
|
||||
|
||||
// Call this to skip
|
||||
void next_song() {
|
||||
if(m_proxy) {
|
||||
m_proxy->call("Next");
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusownname.h"
|
||||
@@ -47,7 +46,6 @@ class NotificationService {
|
||||
|
||||
public:
|
||||
NotificationService() : notificationIdCounter(1) {
|
||||
|
||||
Gio::DBus::own_name(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
"org.freedesktop.Notifications",
|
||||
@@ -56,6 +54,7 @@ class NotificationService {
|
||||
{}, // Name lost slot (optional)
|
||||
Gio::DBus::BusNameOwnerFlags::REPLACE);
|
||||
}
|
||||
|
||||
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
|
||||
|
||||
private:
|
||||
23
include/services/notificationController.hpp
Normal file
23
include/services/notificationController.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
class NotificationController {
|
||||
static std::shared_ptr<NotificationController> instance;
|
||||
|
||||
public:
|
||||
static std::shared_ptr<NotificationController> getInstance() {
|
||||
if (!NotificationController::instance) {
|
||||
NotificationController::instance = std::shared_ptr<NotificationController>(new NotificationController());
|
||||
}
|
||||
return NotificationController::instance;
|
||||
}
|
||||
|
||||
void showSpotifyNotification(const std::string &title, const std::string &message, const std::string &artwork_url);
|
||||
void showNotificationOnAllMonitors(const std::string &title, const std::string &message);
|
||||
private:
|
||||
NotificationController();
|
||||
std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors;
|
||||
};
|
||||
19
include/services/textureCache.hpp
Normal file
19
include/services/textureCache.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "gdkmm/texture.h"
|
||||
#include "glibmm/refptr.h"
|
||||
|
||||
class TextureCacheService {
|
||||
public:
|
||||
static TextureCacheService *getInstance();
|
||||
|
||||
Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url);
|
||||
|
||||
private:
|
||||
TextureCacheService() = default;
|
||||
|
||||
std::unordered_map<std::string, Glib::RefPtr<Gdk::Texture>> cache;
|
||||
};
|
||||
Reference in New Issue
Block a user