1using Gee;
2
3namespace Swayfloatingswitcher {
4    public class AppNode : Object {
5        public int id { get; set; }
6        public bool focused { get; set; default = false; }
7        public bool visible { get; set; default = false; }
8        public int pid { get; set; }
9
10        public string ? name { get; set; }
11        public string ? app_id { get; set; }
12    }
13
14    public class WorkspaceNode : Object, Json.Serializable {
15        public int id { get; set; }
16        public bool focused { get; set; default = false; }
17        public bool visible { get; set; default = false; }
18        public string ? name { get; set; }
19        public bool floating_nodes { get; set; default = false; }
20        public ArrayList<AppNode> nodes {
21            get;
22            private set;
23            default = new ArrayList<AppNode>();
24        }
25
26        /**
27         * Called when `Json.gobject_deserialize` is called
28         */
29        public bool deserialize_property (string property_name,
30                                          out GLib.Value value,
31                                          GLib.ParamSpec pspec,
32                                          Json.Node property_node) {
33            switch (property_name) {
34                case "floating-nodes":
35                    // To manually deserialize all of the floating nodes
36                    bool result = false;
37                    if (property_node.get_node_type () == Json.NodeType.ARRAY) {
38                        result = true;
39                        if (property_node.get_node_type () == Json.NodeType.ARRAY) {
40                            var array = property_node.get_array ();
41                            get_app_nodes (array);
42                        }
43                    }
44                    value = result;
45                    return result;
46                default:
47                    return default_deserialize_property (
48                        property_name, out @value, pspec, property_node);
49            }
50        }
51
52        void get_app_nodes (Json.Array ? array) {
53            foreach (var item in array.get_elements ()) {
54                var obj = item.get_object ();
55                int64 pid = obj.get_int_member_with_default (
56                    "pid", -1);
57
58                if (pid <= 0) {
59                    if (!obj.has_member ("nodes")) break;
60                    get_app_nodes (obj.get_array_member ("nodes"));
61                } else {
62                    AppNode n = Json.gobject_deserialize (
63                        typeof (AppNode), item) as AppNode;
64                    if (n != null) nodes.insert (0, n);
65                }
66            }
67        }
68    }
69}
70