mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-04-18 07:07:08 +02:00
Port NotificationId conversion to cppgir
This commit is contained in:
parent
f2a54e3cbb
commit
59e53c1edf
3 changed files with 80 additions and 62 deletions
Telegram/SourceFiles
|
@ -19,7 +19,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
#include <QtCore/QAbstractEventDispatcher>
|
||||
#include <qpa/qwindowsysteminterface.h>
|
||||
|
||||
#include <glibmm.h>
|
||||
#include <gio/gio.hpp>
|
||||
#include <xdpinhibit/xdpinhibit.hpp>
|
||||
|
||||
|
@ -28,6 +27,32 @@ namespace {
|
|||
|
||||
using namespace gi::repository;
|
||||
|
||||
std::vector<std::any> AnyVectorFromVariant(GLib::Variant value) {
|
||||
std::vector<std::any> result;
|
||||
|
||||
auto iter = gi::wrap(
|
||||
g_variant_iter_new(value.gobj_()),
|
||||
gi::transfer_full);
|
||||
|
||||
const auto uint64Type = GLib::VariantType::new_("t");
|
||||
const auto int64Type = GLib::VariantType::new_("x");
|
||||
|
||||
while (auto value = iter.next_value()) {
|
||||
value = value.get_variant();
|
||||
if (value.is_of_type(uint64Type)) {
|
||||
result.push_back(std::make_any<uint64>(value.get_uint64()));
|
||||
} else if (value.is_of_type(int64Type)) {
|
||||
result.push_back(std::make_any<int64>(value.get_int64()));
|
||||
} else if (value.is_container()) {
|
||||
result.push_back(
|
||||
std::make_any<std::vector<std::any>>(
|
||||
AnyVectorFromVariant(value)));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
class Application : public Gio::impl::ApplicationImpl {
|
||||
public:
|
||||
Application();
|
||||
|
@ -102,23 +127,8 @@ Application::Application()
|
|||
|
||||
using Window::Notifications::Manager;
|
||||
using NotificationId = Manager::NotificationId;
|
||||
using NotificationIdTuple = std::invoke_result_t<
|
||||
decltype(&NotificationId::toTuple),
|
||||
NotificationId*
|
||||
>;
|
||||
|
||||
const auto notificationIdVariantType = [] {
|
||||
try {
|
||||
return gi::wrap(
|
||||
Glib::create_variant(
|
||||
NotificationId().toTuple()
|
||||
).get_type().gobj_copy(),
|
||||
gi::transfer_full
|
||||
);
|
||||
} catch (...) {
|
||||
return GLib::VariantType();
|
||||
}
|
||||
}();
|
||||
const auto notificationIdVariantType = GLib::VariantType::new_("av");
|
||||
|
||||
auto notificationActivateAction = Gio::SimpleAction::new_(
|
||||
"notification-activate",
|
||||
|
@ -128,17 +138,9 @@ Application::Application()
|
|||
Gio::SimpleAction,
|
||||
GLib::Variant parameter) {
|
||||
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
||||
try {
|
||||
const auto &app = Core::App();
|
||||
app.notifications().manager().notificationActivated(
|
||||
NotificationId::FromTuple(
|
||||
Glib::wrap(
|
||||
parameter.gobj_copy_()
|
||||
).get_dynamic<NotificationIdTuple>()
|
||||
)
|
||||
);
|
||||
} catch (...) {
|
||||
}
|
||||
Core::App().notifications().manager().notificationActivated(
|
||||
NotificationId::FromAnyVector(
|
||||
AnyVectorFromVariant(parameter)));
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -152,18 +154,10 @@ Application::Application()
|
|||
Gio::SimpleAction,
|
||||
GLib::Variant parameter) {
|
||||
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
||||
try {
|
||||
const auto &app = Core::App();
|
||||
app.notifications().manager().notificationReplied(
|
||||
NotificationId::FromTuple(
|
||||
Glib::wrap(
|
||||
parameter.gobj_copy_()
|
||||
).get_dynamic<NotificationIdTuple>()
|
||||
),
|
||||
{}
|
||||
);
|
||||
} catch (...) {
|
||||
}
|
||||
Core::App().notifications().manager().notificationReplied(
|
||||
NotificationId::FromAnyVector(
|
||||
AnyVectorFromVariant(parameter)),
|
||||
{});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -28,7 +28,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|||
#include <QtCore/QVersionNumber>
|
||||
#include <QtGui/QGuiApplication>
|
||||
|
||||
#include <glibmm.h>
|
||||
#include <xdgnotifications/xdgnotifications.hpp>
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
@ -139,6 +138,32 @@ bool UseGNotification() {
|
|||
return KSandbox::isFlatpak() && !ServiceRegistered;
|
||||
}
|
||||
|
||||
GLib::Variant AnyVectorToVariant(const std::vector<std::any> &value) {
|
||||
return GLib::Variant::new_array(
|
||||
value | ranges::views::transform([](const std::any &value) {
|
||||
try {
|
||||
return GLib::Variant::new_variant(
|
||||
GLib::Variant::new_uint64(std::any_cast<uint64>(value)));
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
try {
|
||||
return GLib::Variant::new_variant(
|
||||
GLib::Variant::new_int64(std::any_cast<int64>(value)));
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
try {
|
||||
return GLib::Variant::new_variant(
|
||||
AnyVectorToVariant(
|
||||
std::any_cast<std::vector<std::any>>(value)));
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
return GLib::Variant(nullptr);
|
||||
}) | ranges::to_vector);
|
||||
}
|
||||
|
||||
class NotificationData final : public base::has_weak_ptr {
|
||||
public:
|
||||
using NotificationId = Window::Notifications::Manager::NotificationId;
|
||||
|
@ -239,9 +264,7 @@ bool NotificationData::init(
|
|||
set_category(_notification.gobj_(), "im.received");
|
||||
}
|
||||
|
||||
const auto idVariant = gi::wrap(
|
||||
Glib::create_variant(_id.toTuple()).gobj_copy(),
|
||||
gi::transfer_full);
|
||||
const auto idVariant = AnyVectorToVariant(_id.toAnyVector());
|
||||
|
||||
_notification.set_default_action_and_target(
|
||||
"app.notification-activate",
|
||||
|
|
|
@ -233,19 +233,19 @@ public:
|
|||
const ContextId&,
|
||||
const ContextId&) = default;
|
||||
|
||||
[[nodiscard]] auto toTuple() const {
|
||||
return std::make_tuple(
|
||||
sessionId,
|
||||
peerId.value,
|
||||
topicRootId.bare);
|
||||
[[nodiscard]] auto toAnyVector() const {
|
||||
return std::vector<std::any>{
|
||||
std::make_any<uint64>(sessionId),
|
||||
std::make_any<uint64>(peerId.value),
|
||||
std::make_any<int64>(topicRootId.bare),
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] static auto FromTuple(const T &tuple) {
|
||||
[[nodiscard]] static auto FromAnyVector(const auto &vector) {
|
||||
return ContextId{
|
||||
std::get<0>(tuple),
|
||||
PeerIdHelper(std::get<1>(tuple)),
|
||||
std::get<2>(tuple),
|
||||
std::any_cast<uint64>(vector[0]),
|
||||
PeerIdHelper(std::any_cast<uint64>(vector[1])),
|
||||
std::any_cast<int64>(vector[2]),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -257,17 +257,18 @@ public:
|
|||
const NotificationId&,
|
||||
const NotificationId&) = default;
|
||||
|
||||
[[nodiscard]] auto toTuple() const {
|
||||
return std::make_tuple(
|
||||
contextId.toTuple(),
|
||||
msgId.bare);
|
||||
[[nodiscard]] auto toAnyVector() const {
|
||||
return std::vector<std::any>{
|
||||
std::make_any<std::vector<std::any>>(contextId.toAnyVector()),
|
||||
std::make_any<int64>(msgId.bare),
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] static auto FromTuple(const T &tuple) {
|
||||
[[nodiscard]] static auto FromAnyVector(const auto &vector) {
|
||||
return NotificationId{
|
||||
ContextId::FromTuple(std::get<0>(tuple)),
|
||||
std::get<1>(tuple),
|
||||
ContextId::FromAnyVector(
|
||||
std::any_cast<std::vector<std::any>>(vector[0])),
|
||||
std::any_cast<int64>(vector[1]),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue