refactor: simpler sni naming

This commit is contained in:
Alexis 2018-10-26 10:05:54 +02:00
parent 3f269ff463
commit f20441fa92
8 changed files with 55 additions and 60 deletions

View File

@ -19,10 +19,8 @@ class Host {
static void nameVanished(GDBusConnection*, const gchar*, gpointer); static void nameVanished(GDBusConnection*, const gchar*, gpointer);
static void proxyReady(GObject*, GAsyncResult*, gpointer); static void proxyReady(GObject*, GAsyncResult*, gpointer);
static void registerHost(GObject*, GAsyncResult*, gpointer); static void registerHost(GObject*, GAsyncResult*, gpointer);
static void itemRegistered(SnOrgKdeStatusNotifierWatcher*, const gchar*, static void itemRegistered(SnWatcher*, const gchar*, gpointer);
gpointer); static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
static void itemUnregistered(SnOrgKdeStatusNotifierWatcher*, const gchar*,
gpointer);
std::tuple<std::string, std::string> getBusNameAndObjectPath(const gchar*); std::tuple<std::string, std::string> getBusNameAndObjectPath(const gchar*);
void addRegisteredItem(const gchar* service); void addRegisteredItem(const gchar* service);
@ -33,7 +31,7 @@ class Host {
std::string object_path_; std::string object_path_;
Glib::Dispatcher* dp_; Glib::Dispatcher* dp_;
GCancellable* cancellable_ = nullptr; GCancellable* cancellable_ = nullptr;
SnOrgKdeStatusNotifierWatcher* watcher_ = nullptr; SnWatcher* watcher_ = nullptr;
}; };
} }

View File

@ -45,7 +45,7 @@ private:
Glib::Dispatcher *dp_; Glib::Dispatcher *dp_;
GCancellable *cancellable_ = nullptr; GCancellable *cancellable_ = nullptr;
SnOrgKdeStatusNotifierItem *proxy_ = nullptr; SnItem *proxy_ = nullptr;
}; };
} // namespace waybar::modules::SNI } // namespace waybar::modules::SNI

View File

@ -34,13 +34,13 @@ private:
static void nameVanished(GDBusConnection *connection, const char *name, static void nameVanished(GDBusConnection *connection, const char *name,
gpointer data); gpointer data);
void updateRegisteredItems(SnOrgKdeStatusNotifierWatcher *obj); void updateRegisteredItems(SnWatcher *obj);
uint32_t bus_name_id_; uint32_t bus_name_id_;
uint32_t watcher_id_; uint32_t watcher_id_;
GSList *hosts_ = nullptr; GSList *hosts_ = nullptr;
GSList *items_ = nullptr; GSList *items_ = nullptr;
SnOrgKdeStatusNotifierWatcher *watcher_ = nullptr; SnWatcher *watcher_ = nullptr;
}; };
} // namespace waybar::modules::SNI } // namespace waybar::modules::SNI

View File

@ -2,6 +2,7 @@
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node> <node>
<interface name="org.kde.StatusNotifierItem"> <interface name="org.kde.StatusNotifierItem">
<annotation name="org.gtk.GDBus.C.Name" value="Item" />
<property name="Category" type="s" access="read"/> <property name="Category" type="s" access="read"/>
<property name="Id" type="s" access="read"/> <property name="Id" type="s" access="read"/>
<property name="Title" type="s" access="read"/> <property name="Title" type="s" access="read"/>

View File

@ -1,13 +1,16 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node> <node>
<interface name="org.kde.StatusNotifierWatcher"> <interface name="org.kde.StatusNotifierWatcher">
<annotation name="org.gtk.GDBus.C.Name" value="Watcher" />
<!-- methods --> <!-- methods -->
<method name="RegisterStatusNotifierItem"> <method name="RegisterStatusNotifierItem">
<annotation name="org.gtk.GDBus.C.Name" value="RegisterItem" />
<arg name="service" type="s" direction="in"/> <arg name="service" type="s" direction="in"/>
</method> </method>
<method name="RegisterStatusNotifierHost"> <method name="RegisterStatusNotifierHost">
<annotation name="org.gtk.GDBus.C.Name" value="RegisterHost" />
<arg name="service" type="s" direction="in"/> <arg name="service" type="s" direction="in"/>
</method> </method>
@ -15,10 +18,13 @@
<!-- properties --> <!-- properties -->
<property name="RegisteredStatusNotifierItems" type="as" access="read"> <property name="RegisteredStatusNotifierItems" type="as" access="read">
<annotation name="org.gtk.GDBus.C.Name" value="RegisteredItems" />
<annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QStringList"/> <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QStringList"/>
</property> </property>
<property name="IsStatusNotifierHostRegistered" type="b" access="read"/> <property name="IsStatusNotifierHostRegistered" type="b" access="read">
<annotation name="org.gtk.GDBus.C.Name" value="IsHostRegistered" />
</property>
<property name="ProtocolVersion" type="i" access="read"/> <property name="ProtocolVersion" type="i" access="read"/>
@ -26,17 +32,21 @@
<!-- signals --> <!-- signals -->
<signal name="StatusNotifierItemRegistered"> <signal name="StatusNotifierItemRegistered">
<arg type="s"/> <annotation name="org.gtk.GDBus.C.Name" value="ItemRegistered" />
<arg type="s" direction="out" name="service" />
</signal> </signal>
<signal name="StatusNotifierItemUnregistered"> <signal name="StatusNotifierItemUnregistered">
<arg type="s"/> <annotation name="org.gtk.GDBus.C.Name" value="ItemUnregistered" />
<arg type="s" direction="out" name="service" />
</signal> </signal>
<signal name="StatusNotifierHostRegistered"> <signal name="StatusNotifierHostRegistered">
<annotation name="org.gtk.GDBus.C.Name" value="HostRegistered" />
</signal> </signal>
<signal name="StatusNotifierHostUnregistered"> <signal name="StatusNotifierHostUnregistered">
<annotation name="org.gtk.GDBus.C.Name" value="HostUnregistered" />
</signal> </signal>
</interface> </interface>
</node> </node>

View File

@ -35,7 +35,7 @@ void Host::nameAppeared(GDBusConnection* connection,
// TODO // TODO
} }
host->cancellable_ = g_cancellable_new(); host->cancellable_ = g_cancellable_new();
sn_org_kde_status_notifier_watcher_proxy_new( sn_watcher_proxy_new(
connection, connection,
G_DBUS_PROXY_FLAGS_NONE, G_DBUS_PROXY_FLAGS_NONE,
"org.kde.StatusNotifierWatcher", "org.kde.StatusNotifierWatcher",
@ -57,8 +57,7 @@ void Host::proxyReady(GObject* src, GAsyncResult* res,
gpointer data) gpointer data)
{ {
GError* error = nullptr; GError* error = nullptr;
SnOrgKdeStatusNotifierWatcher* watcher = SnWatcher* watcher = sn_watcher_proxy_new_finish(res, &error);
sn_org_kde_status_notifier_watcher_proxy_new_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
std::cerr << error->message << std::endl; std::cerr << error->message << std::endl;
g_error_free(error); g_error_free(error);
@ -71,7 +70,7 @@ void Host::proxyReady(GObject* src, GAsyncResult* res,
g_error_free(error); g_error_free(error);
return; return;
} }
sn_org_kde_status_notifier_watcher_call_register_status_notifier_host( sn_watcher_call_register_host(
host->watcher_, host->object_path_.c_str(), host->cancellable_, host->watcher_, host->object_path_.c_str(), host->cancellable_,
&Host::registerHost, data); &Host::registerHost, data);
} }
@ -80,8 +79,7 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
gpointer data) gpointer data)
{ {
GError* error = nullptr; GError* error = nullptr;
sn_org_kde_status_notifier_watcher_call_register_status_notifier_host_finish( sn_watcher_call_register_host_finish(SN_WATCHER(src), res, &error);
SN_ORG_KDE_STATUS_NOTIFIER_WATCHER(src), res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
std::cerr << error->message << std::endl; std::cerr << error->message << std::endl;
g_error_free(error); g_error_free(error);
@ -93,12 +91,11 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
g_error_free(error); g_error_free(error);
return; return;
} }
g_signal_connect(host->watcher_, "status-notifier-item-registered", g_signal_connect(host->watcher_, "item-registered",
G_CALLBACK(&Host::itemRegistered), data); G_CALLBACK(&Host::itemRegistered), data);
g_signal_connect(host->watcher_, "status-notifier-item-unregistered", g_signal_connect(host->watcher_, "item-unregistered",
G_CALLBACK(&Host::itemUnregistered), data); G_CALLBACK(&Host::itemUnregistered), data);
auto items = auto items = sn_watcher_dup_registered_items(host->watcher_);
sn_org_kde_status_notifier_watcher_dup_registered_status_notifier_items(host->watcher_);
if (items) { if (items) {
for (uint32_t i = 0; items[i] != nullptr; i += 1) { for (uint32_t i = 0; items[i] != nullptr; i += 1) {
host->addRegisteredItem(items[i]); host->addRegisteredItem(items[i]);
@ -108,14 +105,14 @@ void Host::registerHost(GObject* src, GAsyncResult* res,
} }
void Host::itemRegistered( void Host::itemRegistered(
SnOrgKdeStatusNotifierWatcher* watcher, const gchar* service, gpointer data) SnWatcher* watcher, const gchar* service, gpointer data)
{ {
auto host = static_cast<SNI::Host *>(data); auto host = static_cast<SNI::Host *>(data);
host->addRegisteredItem(service); host->addRegisteredItem(service);
} }
void Host::itemUnregistered( void Host::itemUnregistered(
SnOrgKdeStatusNotifierWatcher* watcher, const gchar* service, gpointer data) SnWatcher* watcher, const gchar* service, gpointer data)
{ {
auto host = static_cast<SNI::Host *>(data); auto host = static_cast<SNI::Host *>(data);
auto [bus_name, object_path] = host->getBusNameAndObjectPath(service); auto [bus_name, object_path] = host->getBusNameAndObjectPath(service);

View File

@ -12,7 +12,7 @@ waybar::modules::SNI::Item::Item(std::string bn, std::string op,
event_box.signal_button_press_event().connect( event_box.signal_button_press_event().connect(
sigc::mem_fun(*this, &Item::handleClick)); sigc::mem_fun(*this, &Item::handleClick));
cancellable_ = g_cancellable_new(); cancellable_ = g_cancellable_new();
sn_org_kde_status_notifier_item_proxy_new_for_bus( sn_item_proxy_new_for_bus(
G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, bus_name.c_str(), G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, bus_name.c_str(),
object_path.c_str(), cancellable_, &Item::proxyReady, this); object_path.c_str(), cancellable_, &Item::proxyReady, this);
} }
@ -20,8 +20,8 @@ waybar::modules::SNI::Item::Item(std::string bn, std::string op,
void waybar::modules::SNI::Item::proxyReady(GObject *obj, GAsyncResult *res, void waybar::modules::SNI::Item::proxyReady(GObject *obj, GAsyncResult *res,
gpointer data) { gpointer data) {
GError *error = nullptr; GError *error = nullptr;
SnOrgKdeStatusNotifierItem *proxy = SnItem *proxy =
sn_org_kde_status_notifier_item_proxy_new_for_bus_finish(res, &error); sn_item_proxy_new_for_bus_finish(res, &error);
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
g_error_free(error); g_error_free(error);
return; return;
@ -227,7 +227,7 @@ waybar::modules::SNI::Item::getIconByName(std::string name, int request_size) {
void waybar::modules::SNI::Item::handleActivate(GObject *src, GAsyncResult *res, void waybar::modules::SNI::Item::handleActivate(GObject *src, GAsyncResult *res,
gpointer data) { gpointer data) {
auto item = static_cast<SNI::Item *>(data); auto item = static_cast<SNI::Item *>(data);
sn_org_kde_status_notifier_item_call_activate_finish(item->proxy_, res, sn_item_call_activate_finish(item->proxy_, res,
nullptr); nullptr);
} }
@ -235,7 +235,7 @@ void waybar::modules::SNI::Item::handleSecondaryActivate(GObject *src,
GAsyncResult *res, GAsyncResult *res,
gpointer data) { gpointer data) {
auto item = static_cast<SNI::Item *>(data); auto item = static_cast<SNI::Item *>(data);
sn_org_kde_status_notifier_item_call_secondary_activate_finish(item->proxy_, sn_item_call_secondary_activate_finish(item->proxy_,
res, nullptr); res, nullptr);
} }
@ -247,11 +247,11 @@ bool waybar::modules::SNI::Item::handleClick(GdkEventButton *const &ev) {
} }
gtk_menu->popup(ev->button, ev->time); gtk_menu->popup(ev->button, ev->time);
} else { } else {
sn_org_kde_status_notifier_item_call_activate( sn_item_call_activate(
proxy_, ev->x, ev->y, nullptr, &Item::handleActivate, this); proxy_, ev->x, ev->y, nullptr, &Item::handleActivate, this);
} }
} else if (ev->type == GDK_2BUTTON_PRESS) { } else if (ev->type == GDK_2BUTTON_PRESS) {
sn_org_kde_status_notifier_item_call_secondary_activate( sn_item_call_secondary_activate(
proxy_, ev->x, ev->y, nullptr, &Item::handleSecondaryActivate, this); proxy_, ev->x, ev->y, nullptr, &Item::handleSecondaryActivate, this);
} else { } else {
return false; return false;

View File

@ -12,7 +12,7 @@ Watcher::Watcher()
bus_name_id_ = g_bus_own_name(G_BUS_TYPE_SESSION, bus_name_id_ = g_bus_own_name(G_BUS_TYPE_SESSION,
"org.kde.StatusNotifierWatcher", flags, "org.kde.StatusNotifierWatcher", flags,
&Watcher::busAcquired, nullptr, nullptr, this, nullptr); &Watcher::busAcquired, nullptr, nullptr, this, nullptr);
watcher_ = sn_org_kde_status_notifier_watcher_skeleton_new(); watcher_ = sn_watcher_skeleton_new();
} }
Watcher::~Watcher() Watcher::~Watcher()
@ -32,14 +32,13 @@ void Watcher::busAcquired(GDBusConnection* connection, const gchar* name,
return; return;
} }
g_signal_connect_swapped(host->watcher_, g_signal_connect_swapped(host->watcher_,
"handle-register-status-notifier-item", "handle-register-item",
G_CALLBACK(&Watcher::handleRegisterItem), data); G_CALLBACK(&Watcher::handleRegisterItem), data);
g_signal_connect_swapped(host->watcher_, g_signal_connect_swapped(host->watcher_,
"handle-register-status-notifier-host", "handle-register-host",
G_CALLBACK(&Watcher::handleRegisterHost), data); G_CALLBACK(&Watcher::handleRegisterHost), data);
sn_org_kde_status_notifier_watcher_set_protocol_version(host->watcher_, 0); sn_watcher_set_protocol_version(host->watcher_, 0);
sn_org_kde_status_notifier_watcher_set_is_status_notifier_host_registered( sn_watcher_set_is_host_registered(host->watcher_, TRUE);
host->watcher_, TRUE);
} }
gboolean Watcher::handleRegisterHost(Watcher* obj, gboolean Watcher::handleRegisterHost(Watcher* obj,
@ -66,14 +65,11 @@ gboolean Watcher::handleRegisterHost(Watcher* obj,
} }
watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj); watch = gfWatchNew(GF_WATCH_TYPE_HOST, service, bus_name, object_path, obj);
obj->hosts_ = g_slist_prepend(obj->hosts_, watch); obj->hosts_ = g_slist_prepend(obj->hosts_, watch);
sn_org_kde_status_notifier_watcher_set_is_status_notifier_host_registered( sn_watcher_set_is_host_registered(obj->watcher_, TRUE);
obj->watcher_, TRUE);
if (g_slist_length(obj->hosts_)) { if (g_slist_length(obj->hosts_)) {
sn_org_kde_status_notifier_watcher_emit_status_notifier_host_registered( sn_watcher_emit_host_registered(obj->watcher_);
obj->watcher_);
} }
sn_org_kde_status_notifier_watcher_complete_register_status_notifier_host( sn_watcher_complete_register_host(obj->watcher_, invocation);
obj->watcher_, invocation);
return TRUE; return TRUE;
} }
@ -96,19 +92,16 @@ gboolean Watcher::handleRegisterItem(Watcher* obj,
if (watch != nullptr) { if (watch != nullptr) {
g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered", g_warning("Status Notifier Item with bus name '%s' and object path '%s' is already registered",
bus_name, object_path); bus_name, object_path);
sn_org_kde_status_notifier_watcher_complete_register_status_notifier_item( sn_watcher_complete_register_item(obj->watcher_, invocation);
obj->watcher_, invocation);
return TRUE; return TRUE;
} }
watch = gfWatchNew(GF_WATCH_TYPE_ITEM, service, bus_name, object_path, obj); watch = gfWatchNew(GF_WATCH_TYPE_ITEM, service, bus_name, object_path, obj);
obj->items_ = g_slist_prepend(obj->items_, watch); obj->items_ = g_slist_prepend(obj->items_, watch);
obj->updateRegisteredItems(obj->watcher_); obj->updateRegisteredItems(obj->watcher_);
gchar* tmp = g_strdup_printf("%s%s", bus_name, object_path); gchar* tmp = g_strdup_printf("%s%s", bus_name, object_path);
sn_org_kde_status_notifier_watcher_emit_status_notifier_item_registered( sn_watcher_emit_item_registered(obj->watcher_, tmp);
obj->watcher_, tmp);
g_free(tmp); g_free(tmp);
sn_org_kde_status_notifier_watcher_complete_register_status_notifier_item( sn_watcher_complete_register_item(obj->watcher_, invocation);
obj->watcher_, invocation);
return TRUE; return TRUE;
} }
@ -147,22 +140,19 @@ void Watcher::nameVanished(GDBusConnection* connection, const char* name,
if (watch->type == GF_WATCH_TYPE_HOST) { if (watch->type == GF_WATCH_TYPE_HOST) {
watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch); watch->watcher->hosts_ = g_slist_remove(watch->watcher->hosts_, watch);
if (watch->watcher->hosts_ == nullptr) { if (watch->watcher->hosts_ == nullptr) {
sn_org_kde_status_notifier_watcher_set_is_status_notifier_host_registered( sn_watcher_set_is_host_registered(watch->watcher->watcher_, FALSE);
watch->watcher->watcher_, FALSE); sn_watcher_emit_host_registered(watch->watcher->watcher_);
sn_org_kde_status_notifier_watcher_emit_status_notifier_host_registered(
watch->watcher->watcher_);
} }
} else if (watch->type == GF_WATCH_TYPE_ITEM) { } else if (watch->type == GF_WATCH_TYPE_ITEM) {
watch->watcher->items_ = g_slist_remove(watch->watcher->items_, watch); watch->watcher->items_ = g_slist_remove(watch->watcher->items_, watch);
watch->watcher->updateRegisteredItems(watch->watcher->watcher_); watch->watcher->updateRegisteredItems(watch->watcher->watcher_);
gchar* tmp = g_strdup_printf("%s%s", watch->bus_name, watch->object_path); gchar* tmp = g_strdup_printf("%s%s", watch->bus_name, watch->object_path);
sn_org_kde_status_notifier_watcher_emit_status_notifier_item_unregistered( sn_watcher_emit_item_unregistered(watch->watcher->watcher_, tmp);
watch->watcher->watcher_, tmp);
g_free(tmp); g_free(tmp);
} }
} }
void Watcher::updateRegisteredItems(SnOrgKdeStatusNotifierWatcher* obj) void Watcher::updateRegisteredItems(SnWatcher* obj)
{ {
GVariantBuilder builder; GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
@ -174,8 +164,7 @@ void Watcher::updateRegisteredItems(SnOrgKdeStatusNotifierWatcher* obj)
} }
GVariant* variant = g_variant_builder_end(&builder); GVariant* variant = g_variant_builder_end(&builder);
const gchar** items = g_variant_get_strv(variant, nullptr); const gchar** items = g_variant_get_strv(variant, nullptr);
sn_org_kde_status_notifier_watcher_set_registered_status_notifier_items( sn_watcher_set_registered_items(obj, items);
obj, items);
g_variant_unref(variant); g_variant_unref(variant);
g_free(items); g_free(items);
} }