Esempio n. 1
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();
        }
Esempio n. 2
0
        public override void Edit()
        {
            Mouse.OverrideCursor = Cursors.Wait;

            this.Service.PubSubClient.GetItems(this.node, new string[] { this.itemId }, (sender, e) =>
            {
                MainWindow.MouseDefault();

                if (e.Ok)
                {
                    if (e.Items.Length == 1)
                    {
                        Networking.XMPP.PubSub.PubSubItem Item = e.Items[0];
                        DataForm Form          = null;
                        ParameterDialog Dialog = null;

                        Form = new DataForm(this.Service.PubSubClient.Client,
                                            (sender2, e2) =>
                        {
                            string Payload = Form["Payload"].ValueString;

                            try
                            {
                                XmlDocument Xml = new XmlDocument();
                                Xml.LoadXml(Payload);
                            }
                            catch (Exception ex)
                            {
                                Form["Payload"].Error = ex.Message;

                                MainWindow.currentInstance.Dispatcher.BeginInvoke(new ThreadStart(() =>
                                {
                                    Dialog = new ParameterDialog(Form);
                                    Dialog.ShowDialog();
                                }));

                                return;
                            }

                            Mouse.OverrideCursor = Cursors.Wait;

                            this.Service.PubSubClient.Publish(this.node, this.itemId, Payload, (sender3, e3) =>
                            {
                                MainWindow.MouseDefault();

                                if (e3.Ok)
                                {
                                    this.Init(Payload);
                                    this.OnUpdated();
                                }
                                else
                                {
                                    MainWindow.ErrorBox("Unable to update item: " + e3.ErrorText);
                                }
                            }, null);
                        },
                                            (sender2, e2) =>
                        {
                            // Do nothing.
                        }, e.From, e.To,
                                            new JidSingleField(null, "Publisher", "Publisher:", false, new string[] { Item.Publisher }, null, "JID of publisher.",
                                                               null, null, string.Empty, false, true, false),
                                            new TextMultiField(null, "Payload", "XML:", false, new string[] { Item.Payload }, null, "XML payload of item.",
                                                               StringDataType.Instance, null, string.Empty, false, false, false));

                        Dialog = new ParameterDialog(Form);

                        MainWindow.currentInstance.Dispatcher.BeginInvoke(new ThreadStart(() =>
                        {
                            Dialog.ShowDialog();
                        }));
                    }
                }
                else
                {
                    MainWindow.ErrorBox("Unable to get item from server: " + e.ErrorText);
                }
            }, null);
        }