Exemplo n.º 1
0
        internal void ItemNotification(ItemNotificationEventArgs e)
        {
            if (this.IsLoaded)
            {
                if (this.TryGetChild(e.ItemId, out TreeNode N) && N is PubSubItem Item)
                {
                    Item.Init(e.Item.InnerXml);
                    Item.OnUpdated();
                }
                else
                {
                    Item = new PubSubItem(this, e.Publisher, e.NodeName, e.ItemId, e.Item.InnerText, e.Publisher);

                    if (this.children is null)
                    {
                        this.children = new SortedDictionary <string, TreeNode>()
                        {
                            { Item.Key, Item }
                        }
                    }
                    ;
                    else
                    {
                        lock (this.children)
                        {
                            this.children[Item.Key] = Item;
                        }
                    }

                    MainWindow.UpdateGui(() =>
                    {
                        Service?.Account?.View?.NodeAdded(this, Item);
                        this.OnUpdated();
                    });
                }
            }
        }
Exemplo n.º 2
0
        protected override void LoadChildren()
        {
            if (!this.loadingChildren && !this.IsLoaded)
            {
                SortedDictionary <string, TreeNode> Children = new SortedDictionary <string, TreeNode>();

                Mouse.OverrideCursor = Cursors.Wait;
                this.loadingChildren = true;

                if (this.nodeType == NodeType.leaf && this.Service.SupportsLastPublished)
                {
                    this.Service.PubSubClient.GetLatestItems(this.node, 50, (sender, e) =>
                    {
                        this.loadingChildren = false;
                        MainWindow.MouseDefault();

                        if (e.Ok)
                        {
                            foreach (Networking.XMPP.PubSub.PubSubItem Item in e.Items)
                            {
                                Children[Item.ItemId] = new PubSubItem(this, this.jid, Item.Node, Item.ItemId, Item.Payload, Item.Publisher);
                            }

                            this.children = Children;

                            this.OnUpdated();
                            this.Service.NodesAdded(this.children.Values, this);

                            this.Service.PubSubClient.Subscribe(this.node, (sender2, e2) =>
                            {
                                if (!e2.Ok)
                                {
                                    MainWindow.ErrorBox("Unable to subscribe to new items: " + e.ErrorText);
                                }

                                return(Task.CompletedTask);
                            }, null);
                        }
                        else
                        {
                            MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to get latest items." : e.ErrorText);
                        }

                        return(Task.CompletedTask);
                    }, null);
                }
                else
                {
                    this.Service.Account.Client.SendServiceItemsDiscoveryRequest(this.PubSubClient.ComponentAddress, this.node, (sender, e) =>
                    {
                        this.loadingChildren = false;
                        MainWindow.MouseDefault();

                        if (e.Ok)
                        {
                            this.Service.NodesRemoved(this.children.Values, this);

                            if (this.nodeType == NodeType.leaf)
                            {
                                List <string> ItemIds = new List <string>();

                                foreach (Item Item in e.Items)
                                {
                                    ItemIds.Add(Item.Name);
                                }

                                this.Service.PubSubClient.GetItems(this.node, ItemIds.ToArray(), (sender2, e2) =>
                                {
                                    if (e2.Ok)
                                    {
                                        if (e2.Items.Length == ItemIds.Count)
                                        {
                                            foreach (Networking.XMPP.PubSub.PubSubItem Item in e2.Items)
                                            {
                                                Children[Item.ItemId] = new PubSubItem(this, this.jid, Item.Node, Item.ItemId, Item.Payload, Item.Publisher);
                                            }

                                            this.children = Children;

                                            this.OnUpdated();
                                            this.Service.NodesAdded(this.children.Values, this);
                                        }
                                        else
                                        {
                                            foreach (Item Item in e.Items)
                                            {
                                                this.Service.PubSubClient.GetItems(this.node, new string[] { Item.Name }, (sender3, e3) =>
                                                {
                                                    if (e3.Ok)
                                                    {
                                                        if (e3.Items.Length == 1)
                                                        {
                                                            Networking.XMPP.PubSub.PubSubItem Item2 = e3.Items[0];
                                                            TreeNode NewNode;

                                                            lock (Children)
                                                            {
                                                                NewNode = new PubSubItem(this, this.jid, Item2.Node, Item2.ItemId, Item2.Payload, Item2.Publisher);
                                                                Children[Item2.ItemId] = NewNode;
                                                                this.children          = new SortedDictionary <string, TreeNode>(Children);
                                                            }

                                                            this.OnUpdated();
                                                            this.Service.NodesAdded(new TreeNode[] { NewNode }, this);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get item." : e3.ErrorText);
                                                    }

                                                    return(Task.CompletedTask);
                                                }, Item);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get items." : e2.ErrorText);
                                    }

                                    return(Task.CompletedTask);
                                }, null);
                            }
                            else
                            {
                                foreach (Item Item in e.Items)
                                {
                                    this.Service.Account.Client.SendServiceDiscoveryRequest(this.PubSubClient.ComponentAddress, Item.Node, (sender2, e2) =>
                                    {
                                        if (e2.Ok)
                                        {
                                            Item Item2        = (Item)e2.State;
                                            string Jid        = Item2.JID;
                                            string Node       = Item2.Node;
                                            string Name       = Item2.Name;
                                            NodeType NodeType = NodeType.leaf;
                                            TreeNode NewNode;

                                            foreach (Identity Identity in e2.Identities)
                                            {
                                                if (Identity.Category == "pubsub")
                                                {
                                                    if (!Enum.TryParse <NodeType>(Identity.Type, out NodeType))
                                                    {
                                                        NodeType = NodeType.leaf;
                                                    }

                                                    if (!string.IsNullOrEmpty(Identity.Name))
                                                    {
                                                        Name = Identity.Name;
                                                    }
                                                }
                                            }

                                            lock (Children)
                                            {
                                                NewNode = new PubSubNode(this, Jid, Node, Name, NodeType);
                                                Children[Item2.Node] = NewNode;
                                                this.children        = new SortedDictionary <string, TreeNode>(Children);
                                            }

                                            this.OnUpdated();
                                            this.Service.NodesAdded(new TreeNode[] { NewNode }, this);
                                        }
                                        else
                                        {
                                            MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get information." : e2.ErrorText);
                                        }

                                        return(Task.CompletedTask);
                                    }, Item);
                                }
                            }
                        }
                        else
                        {
                            MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to get information." : e.ErrorText);
                        }

                        return(Task.CompletedTask);
                    }, null);
                }
            }

            base.LoadChildren();
        }