22
33#include < spdlog/spdlog.h>
44
5+ #include < algorithm>
6+
7+ #include " modules/sni/item.hpp"
58#include " util/scope_guard.hpp"
69
710namespace waybar ::modules::SNI {
811
9- Host::Host (const std::size_t id, const Json::Value& config, const Bar& bar,
12+ Host::Host (std::size_t id, const Json::Value& config, const Bar& bar,
1013 const std::function<void (std::unique_ptr<Item>&)>& on_add,
1114 const std::function<void (std::unique_ptr<Item>&)>& on_remove,
1215 const std::function<void ()>& on_update)
@@ -19,7 +22,18 @@ Host::Host(const std::size_t id, const Json::Value& config, const Bar& bar,
1922 bar_(bar),
2023 on_add_(on_add),
2124 on_remove_(on_remove),
22- on_update_(on_update) {}
25+ on_update_(on_update) {
26+ auto orders = config[" orders" ];
27+ if (!orders.isNull ()) {
28+ for (auto itr = orders.begin (); itr != orders.end (); ++itr) {
29+ auto key = itr.name ();
30+ auto & value = *itr;
31+ assert (value.isInt ());
32+
33+ orders_[key] = value.asInt ();
34+ }
35+ }
36+ }
2337
2438Host::~Host () {
2539 if (bus_name_id_ > 0 ) {
@@ -35,13 +49,13 @@ Host::~Host() {
3549 g_clear_object (&watcher_);
3650}
3751
38- void Host::busAcquired (const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
52+ void Host::busAcquired (const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring& name) {
3953 watcher_id_ = Gio::DBus::watch_name (conn, " org.kde.StatusNotifierWatcher" ,
4054 sigc::mem_fun (*this , &Host::nameAppeared),
4155 sigc::mem_fun (*this , &Host::nameVanished));
4256}
4357
44- void Host::nameAppeared (const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name,
58+ void Host::nameAppeared (const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring& name,
4559 const Glib::ustring& name_owner) {
4660 if (cancellable_ != nullptr ) {
4761 // TODO
@@ -52,7 +66,8 @@ void Host::nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>& conn, const G
5266 " /StatusNotifierWatcher" , cancellable_, &Host::proxyReady, this );
5367}
5468
55- void Host::nameVanished (const Glib::RefPtr<Gio::DBus::Connection>& conn, const Glib::ustring name) {
69+ void Host::nameVanished (const Glib::RefPtr<Gio::DBus::Connection>& conn,
70+ const Glib::ustring& name) {
5671 g_cancellable_cancel (cancellable_);
5772 g_clear_object (&cancellable_);
5873 g_clear_object (&watcher_);
@@ -67,11 +82,11 @@ void Host::proxyReady(GObject* src, GAsyncResult* res, gpointer data) {
6782 }
6883 });
6984 SnWatcher* watcher = sn_watcher_proxy_new_finish (res, &error);
70- if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
85+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) != 0 ) {
7186 spdlog::error (" Host: {}" , error->message );
7287 return ;
7388 }
74- auto host = static_cast <SNI::Host*>(data);
89+ auto * host = static_cast <SNI::Host*>(data);
7590 host->watcher_ = watcher;
7691 if (error != nullptr ) {
7792 spdlog::error (" Host: {}" , error->message );
@@ -89,18 +104,18 @@ void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
89104 }
90105 });
91106 sn_watcher_call_register_host_finish (SN_WATCHER (src), res, &error);
92- if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
107+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) != 0 ) {
93108 spdlog::error (" Host: {}" , error->message );
94109 return ;
95110 }
96- auto host = static_cast <SNI::Host*>(data);
111+ auto * host = static_cast <SNI::Host*>(data);
97112 if (error != nullptr ) {
98113 spdlog::error (" Host: {}" , error->message );
99114 return ;
100115 }
101116 g_signal_connect (host->watcher_ , " item-registered" , G_CALLBACK (&Host::itemRegistered), data);
102117 g_signal_connect (host->watcher_ , " item-unregistered" , G_CALLBACK (&Host::itemUnregistered), data);
103- auto items = sn_watcher_dup_registered_items (host->watcher_ );
118+ auto * items = sn_watcher_dup_registered_items (host->watcher_ );
104119 if (items != nullptr ) {
105120 for (uint32_t i = 0 ; items[i] != nullptr ; i += 1 ) {
106121 host->addRegisteredItem (items[i]);
@@ -110,13 +125,13 @@ void Host::registerHost(GObject* src, GAsyncResult* res, gpointer data) {
110125}
111126
112127void Host::itemRegistered (SnWatcher* watcher, const gchar* service, gpointer data) {
113- auto host = static_cast <SNI::Host*>(data);
128+ auto * host = static_cast <SNI::Host*>(data);
114129 host->addRegisteredItem (service);
115130}
116131
117132void Host::itemUnregistered (SnWatcher* watcher, const gchar* service, gpointer data) {
118- auto host = static_cast <SNI::Host*>(data);
119- auto [bus_name, object_path] = host-> getBusNameAndObjectPath (service);
133+ auto * host = static_cast <SNI::Host*>(data);
134+ auto [bus_name, object_path] = waybar::modules::SNI::Host:: getBusNameAndObjectPath (service);
120135 for (auto it = host->items_ .begin (); it != host->items_ .end (); ++it) {
121136 if ((*it)->bus_name == bus_name && (*it)->object_path == object_path) {
122137 host->removeItem (it);
@@ -163,7 +178,7 @@ void Host::clearItems() {
163178 }
164179}
165180
166- std::tuple<std::string, std::string> Host::getBusNameAndObjectPath (const std::string service) {
181+ std::tuple<std::string, std::string> Host::getBusNameAndObjectPath (const std::string& service) {
167182 auto it = service.find (' /' );
168183 if (it != std::string::npos) {
169184 return {service.substr (0 , it), service.substr (it)};
@@ -172,16 +187,25 @@ std::tuple<std::string, std::string> Host::getBusNameAndObjectPath(const std::st
172187}
173188
174189void Host::addRegisteredItem (const std::string& service) {
175- std::string bus_name, object_path;
190+ std::string bus_name;
191+ std::string object_path;
176192 std::tie (bus_name, object_path) = getBusNameAndObjectPath (service);
177- auto it = std::find_if (items_. begin (), items_. end () , [&bus_name, &object_path](const auto & item) {
193+ auto it = std::ranges:: find_if (items_, [&bus_name, &object_path](const auto & item) {
178194 return bus_name == item->bus_name && object_path == item->object_path ;
179195 });
180196 if (it == items_.end ()) {
181197 items_.emplace_back (new Item (
182198 bus_name, object_path, config_, bar_, [this ](Item& item) { itemReady (item); },
183- [this ](Item& item) { itemInvalidated (item); }, on_update_));
199+ [this ](Item& item) { itemInvalidated (item); }, on_update_, * this , orders_ ));
184200 }
185201}
186202
203+ void Host::reorderItems () {
204+ std::ranges::for_each (items_, on_remove_);
205+ std::ranges::sort (items_, [](std::unique_ptr<Item>& item1, std::unique_ptr<Item>& item2) {
206+ return item1->order_ < item2->order_ ;
207+ });
208+ std::ranges::for_each (items_, on_add_);
209+ }
210+
187211} // namespace waybar::modules::SNI
0 commit comments