Replace config HashTable with OrderedHashTable (#258)

This commit is contained in:
Erik Reider
2023-05-14 10:16:06 +02:00
committed by GitHub
parent 9b3c147e8d
commit b8ef618132
5 changed files with 71 additions and 28 deletions

View File

@@ -479,26 +479,28 @@ namespace SwayNotificationCenter {
} }
/** Categories settings */ /** Categories settings */
public HashTable<string, Category> categories_settings { public OrderedHashTable<Category> categories_settings {
get; get;
set; set;
default = new HashTable<string, Category> (str_hash, str_equal); default = new OrderedHashTable<Category> ();
} }
/** Notification Status */ /** Notification Status */
public HashTable<string, NotificationVisibility> notification_visibility { public OrderedHashTable<NotificationVisibility> notification_visibility {
get; get;
set; set;
default = new HashTable<string, NotificationVisibility> (str_hash, str_equal); default = new OrderedHashTable<NotificationVisibility> ();
} }
#if WANT_SCRIPTING #if WANT_SCRIPTING
/** Scripts */ /** Scripts */
public HashTable<string, Script> scripts { public OrderedHashTable<Script> scripts {
get; get;
set; set;
default = new HashTable<string, Script> (str_hash, str_equal); default = new OrderedHashTable<Script> ();
} }
/** Show notification if script fails */ /** Show notification if script fails */
public bool script_fail_notify { get; set; default = true; } public bool script_fail_notify { get; set; default = true; }
#endif #endif
@@ -597,12 +599,13 @@ namespace SwayNotificationCenter {
} }
/** Widgets to show in ControlCenter */ /** Widgets to show in ControlCenter */
public HashTable<string, Json.Object> widget_config { public OrderedHashTable<Json.Object> widget_config {
get; get;
set; set;
default = new HashTable<string, Json.Object> (str_hash, str_equal); default = new OrderedHashTable<Json.Object> ();
} }
/* Methods */ /* Methods */
/** /**
@@ -617,7 +620,7 @@ namespace SwayNotificationCenter {
switch (property_name) { switch (property_name) {
case "categories-settings" : case "categories-settings" :
bool status; bool status;
HashTable<string, Category> result = OrderedHashTable<Category> result =
extract_hashtable<Category> ( extract_hashtable<Category> (
property_name, property_name,
property_node, property_node,
@@ -626,7 +629,7 @@ namespace SwayNotificationCenter {
return status; return status;
case "notification-visibility": case "notification-visibility":
bool status; bool status;
HashTable<string, NotificationVisibility> result = OrderedHashTable<NotificationVisibility> result =
extract_hashtable<NotificationVisibility> ( extract_hashtable<NotificationVisibility> (
property_name, property_name,
property_node, property_node,
@@ -636,7 +639,7 @@ namespace SwayNotificationCenter {
#if WANT_SCRIPTING #if WANT_SCRIPTING
case "scripts": case "scripts":
bool status; bool status;
HashTable<string, Script> result = OrderedHashTable<Script> result =
extract_hashtable<Script> ( extract_hashtable<Script> (
property_name, property_name,
property_node, property_node,
@@ -653,8 +656,8 @@ namespace SwayNotificationCenter {
value = result; value = result;
return status; return status;
case "widget-config": case "widget-config":
HashTable<string, Json.Object> result OrderedHashTable<Json.Object> result
= new HashTable<string, Json.Object> (str_hash, str_equal); = new OrderedHashTable<Json.Object> ();
if (property_node.get_value_type ().name () != "JsonObject") { if (property_node.get_value_type ().name () != "JsonObject") {
value = result; value = result;
return true; return true;
@@ -668,7 +671,7 @@ namespace SwayNotificationCenter {
Json.Node ? node = obj.get_member (key); Json.Node ? node = obj.get_member (key);
if (node.get_node_type () != Json.NodeType.OBJECT) continue; if (node.get_node_type () != Json.NodeType.OBJECT) continue;
Json.Object ? o = node.get_object (); Json.Object ? o = node.get_object ();
if (o != null) result.set (key, o); if (o != null) result.insert (key, o);
} }
value = result; value = result;
return true; return true;
@@ -701,29 +704,29 @@ namespace SwayNotificationCenter {
switch (property_name) { switch (property_name) {
case "categories-settings" : case "categories-settings" :
node = new Json.Node (Json.NodeType.OBJECT); node = new Json.Node (Json.NodeType.OBJECT);
var table = (HashTable<string, Category>) value.get_boxed (); var table = (OrderedHashTable<Category>) value;
node.set_object (serialize_hashtable<Category> (table)); node.set_object (serialize_hashtable<Category> (table));
break; break;
case "notification-visibility": case "notification-visibility":
node = new Json.Node (Json.NodeType.OBJECT); node = new Json.Node (Json.NodeType.OBJECT);
var table = (HashTable<string, NotificationVisibility>) value.get_boxed (); var table = (OrderedHashTable<NotificationVisibility>) value;
node.set_object (serialize_hashtable<NotificationVisibility> (table)); node.set_object (serialize_hashtable<NotificationVisibility> (table));
break; break;
#if WANT_SCRIPTING #if WANT_SCRIPTING
case "scripts": case "scripts":
node = new Json.Node (Json.NodeType.OBJECT); node = new Json.Node (Json.NodeType.OBJECT);
var table = (HashTable<string, Script>) value.get_boxed (); var table = (OrderedHashTable<Script>) value;
node.set_object (serialize_hashtable<Script> (table)); node.set_object (serialize_hashtable<Script> (table));
break; break;
#endif #endif
case "widgets": case "widgets":
node = new Json.Node (Json.NodeType.ARRAY); node = new Json.Node (Json.NodeType.ARRAY);
var table = (GenericArray<string>) value.get_boxed (); var table = (GenericArray<string>) value;
node.set_array (serialize_array<string> (table)); node.set_array (serialize_array<string> (table));
break; break;
case "widget-config": case "widget-config":
node = new Json.Node (Json.NodeType.OBJECT); node = new Json.Node (Json.NodeType.OBJECT);
var table = (HashTable<string, Json.Object>) value.get_boxed (); var table = (OrderedHashTable<Json.Object>) value;
node.set_object (serialize_hashtable<Json.Object> (table)); node.set_object (serialize_hashtable<Json.Object> (table));
break; break;
default: default:
@@ -739,7 +742,7 @@ namespace SwayNotificationCenter {
} }
/** /**
* Extracts and returns a HashTable<string, GLib.Object> * Extracts and returns a OrderedHashTable<GLib.Object>
* from a nested JSON Object. * from a nested JSON Object.
* *
* Can only accept these types: * Can only accept these types:
@@ -748,11 +751,11 @@ namespace SwayNotificationCenter {
* - int64 * - int64
* - GLib.Object * - GLib.Object
*/ */
private HashTable<string, T> extract_hashtable<T> (string property_name, private OrderedHashTable<T> extract_hashtable<T> (string property_name,
Json.Node node, Json.Node node,
out bool status) { out bool status) {
status = false; status = false;
var tmp_table = new HashTable<string, T> (str_hash, str_equal); var tmp_table = new OrderedHashTable<T> ();
if (node.get_node_type () != Json.NodeType.OBJECT) { if (node.get_node_type () != Json.NodeType.OBJECT) {
stderr.printf ("Node %s is not a json object!...\n", stderr.printf ("Node %s is not a json object!...\n",
@@ -833,12 +836,12 @@ namespace SwayNotificationCenter {
return tmp_table; return tmp_table;
} }
private Json.Object serialize_hashtable<T> (HashTable<string, T> table) { private Json.Object serialize_hashtable<T> (OrderedHashTable<T> table) {
var json_object = new Json.Object (); var json_object = new Json.Object ();
if (table == null) return json_object; if (table == null) return json_object;
foreach (string * key in table.get_keys ()) { foreach (string key in table.get_keys ()) {
unowned T item = table.get (key); unowned T item = table.get (key);
if (item == null) continue; if (item == null) continue;

View File

@@ -27,7 +27,7 @@ namespace SwayNotificationCenter.Widgets {
} }
protected Json.Object ? get_config (Gtk.Widget widget) { protected Json.Object ? get_config (Gtk.Widget widget) {
unowned HashTable<string, Json.Object> config unowned OrderedHashTable<Json.Object> config
= ConfigModel.instance.widget_config; = ConfigModel.instance.widget_config;
string ? orig_key = null; string ? orig_key = null;
Json.Object ? props = null; Json.Object ? props = null;

View File

@@ -55,6 +55,7 @@ widget_sources = [
app_sources = [ app_sources = [
'main.vala', 'main.vala',
'orderedHashTable/orderedHashTable.vala',
'configModel/configModel.vala', 'configModel/configModel.vala',
'swayncDaemon/swayncDaemon.vala', 'swayncDaemon/swayncDaemon.vala',
'notiDaemon/notiDaemon.vala', 'notiDaemon/notiDaemon.vala',

View File

@@ -154,7 +154,8 @@ namespace SwayNotificationCenter {
// The notification visibility state // The notification visibility state
NotificationStatusEnum state = NotificationStatusEnum.ENABLED; NotificationStatusEnum state = NotificationStatusEnum.ENABLED;
var visibilities = ConfigModel.instance.notification_visibility; unowned OrderedHashTable<NotificationVisibility> visibilities =
ConfigModel.instance.notification_visibility;
foreach (string key in visibilities.get_keys ()) { foreach (string key in visibilities.get_keys ()) {
unowned NotificationVisibility vis = visibilities[key]; unowned NotificationVisibility vis = visibilities[key];
if (!vis.matches_notification (param)) continue; if (!vis.matches_notification (param)) continue;
@@ -211,7 +212,7 @@ namespace SwayNotificationCenter {
return id; return id;
} }
// Run the first script if notification meets requirements // Run the first script if notification meets requirements
HashTable<string, Script> scripts = ConfigModel.instance.scripts; OrderedHashTable<Script> scripts = ConfigModel.instance.scripts;
if (scripts.length == 0) return id; if (scripts.length == 0) return id;
this.run_scripts (param, ScriptRunOnType.RECEIVE); this.run_scripts (param, ScriptRunOnType.RECEIVE);
#endif #endif
@@ -229,7 +230,7 @@ namespace SwayNotificationCenter {
return; return;
} }
// Run the first script if notification meets requirements // Run the first script if notification meets requirements
HashTable<string, Script> scripts = ConfigModel.instance.scripts; OrderedHashTable<Script> scripts = ConfigModel.instance.scripts;
if (scripts.length == 0) return; if (scripts.length == 0) return;
foreach (string key in scripts.get_keys ()) { foreach (string key in scripts.get_keys ()) {
unowned Script script = scripts[key]; unowned Script script = scripts[key];

View File

@@ -0,0 +1,38 @@
namespace SwayNotificationCenter {
/** A regular GLib HashTable but preserves the order of inserted keys and values */
public class OrderedHashTable<T> {
private HashTable<string, T> hash_table;
private List<string> order;
public uint length {
get {
return hash_table.length;
}
}
public OrderedHashTable () {
hash_table = new HashTable<string, T> (str_hash, str_equal);
order = new List<string> ();
}
public unowned T @get (string key) {
return hash_table.get (key);
}
public void insert (owned string key, owned T value) {
if (!hash_table.contains (key)) {
order.append (key);
}
hash_table.insert (key, value);
}
public List<weak string> get_keys () {
return order.copy ();
}
public bool lookup_extended (string lookup_key, out unowned string orig_key, out unowned T value) {
return hash_table.lookup_extended (lookup_key, out orig_key, out value);
}
}
}