Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IsFriendQuestion.cs
1using System;
2using System.Threading.Tasks;
3using System.Windows;
4using System.Windows.Controls;
5using Waher.Events;
9
11{
13 {
14 private ProvisioningClient client;
15 private QuestionView questionView;
16 private bool response;
17 private RuleRange range;
18
19 public IsFriendQuestion()
20 : base()
21 {
22 }
23
24 public bool Response => this.response;
25
26 public override string QuestionString => "Allowed to connect?";
27
28 public override void PopulateDetailsDialog(QuestionView QuestionView, ProvisioningClient ProvisioningClient)
29 {
30 StackPanel Details = QuestionView.Details;
31 TextBlock TextBlock;
32 Button Button;
33
34 this.client = ProvisioningClient;
35 this.questionView = QuestionView;
36
37 Details.Children.Add(new TextBlock()
38 {
39 FontSize = 18,
40 FontWeight = FontWeights.Bold,
41 Text = "Allowed to connect?"
42 });
43
44 Details.Children.Add(TextBlock = new TextBlock()
45 {
46 TextWrapping = TextWrapping.Wrap,
47 Margin = new Thickness(0, 6, 0, 6)
48 });
49
50 TextBlock.Inlines.Add("Device: ");
51 this.AddJidName(this.JID, ProvisioningClient, TextBlock);
52
53 Details.Children.Add(TextBlock = new TextBlock()
54 {
55 TextWrapping = TextWrapping.Wrap,
56 Margin = new Thickness(0, 6, 0, 6)
57 });
58
59 TextBlock.Inlines.Add("Caller: ");
60 this.AddJidName(this.RemoteJID, ProvisioningClient, TextBlock);
61
62 Details.Children.Add(TextBlock = new TextBlock()
63 {
64 TextWrapping = TextWrapping.Wrap,
65 Margin = new Thickness(0, 6, 0, 6),
66 Text = "Is the caller allowed to connect to your device?"
67 });
68
69 Details.Children.Add(Button = new Button()
70 {
71 Margin = new Thickness(0, 6, 0, 6),
72 Content = "Yes"
73 });
74
75 Button.Click += this.YesButton_Click;
76
77 Details.Children.Add(Button = new Button()
78 {
79 Margin = new Thickness(0, 6, 0, 6),
80 Content = "No"
81 });
82
83 Button.Click += this.NoButton_Click;
84
85 string s = this.RemoteJID;
86 int i = s.IndexOf('@');
87 if (i >= 0)
88 {
89 s = s.Substring(i + 1);
90
91 Details.Children.Add(Button = new Button()
92 {
93 Margin = new Thickness(0, 6, 0, 6),
94 Content = "Yes, to anyone from " + s
95 });
96
97 Button.Click += this.YesDomainButton_Click;
98
99 Details.Children.Add(Button = new Button()
100 {
101 Margin = new Thickness(0, 6, 0, 6),
102 Content = "No, to no one from " + s
103 });
104
105 Button.Click += this.NoDomainButton_Click;
106 }
107
108 Details.Children.Add(Button = new Button()
109 {
110 Margin = new Thickness(0, 6, 0, 6),
111 Content = "Yes, to anyone"
112 });
113
114 Button.Click += this.YesAllButton_Click;
115
116 Details.Children.Add(Button = new Button()
117 {
118 Margin = new Thickness(0, 6, 0, 6),
119 Content = "No, to no one"
120 });
121
122 Button.Click += this.NoAllButton_Click;
123 }
124
125 internal static string GetDomain(string s)
126 {
127 int i = s.IndexOf('@');
128 if (i < 0)
129 return s;
130 else
131 return s.Substring(i + 1);
132 }
133
134 private void Process(bool Response, RuleRange Range)
135 {
136 this.response = Response;
137 this.range = Range;
138 this.client.IsFriendResponse(this.Sender, this.JID, this.RemoteJID, this.Key, Response, Range, this.RuleCallback, null);
139 }
140
141 private async Task RuleCallback(object Sender, IqResultEventArgs e)
142 {
143 try
144 {
145 if (e.Ok)
146 await this.Processed(this.questionView);
147 else
148 MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to set rule." : e.ErrorText);
149 }
150 catch (Exception ex)
151 {
152 ex = Log.UnnestException(ex);
153 MainWindow.ErrorBox(ex.Message);
154 }
155 }
156
157 private void NoAllButton_Click(object Sender, RoutedEventArgs e)
158 {
159 this.Process(false, RuleRange.All);
160 }
161
162 private void YesAllButton_Click(object Sender, RoutedEventArgs e)
163 {
164 this.Process(true, RuleRange.All);
165 }
166
167 private void NoDomainButton_Click(object Sender, RoutedEventArgs e)
168 {
169 this.Process(false, RuleRange.Domain);
170 }
171
172 private void YesDomainButton_Click(object Sender, RoutedEventArgs e)
173 {
174 this.Process(true, RuleRange.Domain);
175 }
176
177 private void NoButton_Click(object Sender, RoutedEventArgs e)
178 {
179 this.Process(false, RuleRange.Caller);
180 }
181
182 private void YesButton_Click(object Sender, RoutedEventArgs e)
183 {
184 this.Process(true, RuleRange.Caller);
185 }
186
187 public override bool IsResolvedBy(Question Question)
188 {
190 {
191 if (this.JID != IsFriendQuestion.JID)
192 return false;
193
194 switch (this.range)
195 {
196 case RuleRange.Caller:
197 return (this.RemoteJID == IsFriendQuestion.RemoteJID);
198
199 case RuleRange.Domain:
200 return (GetDomain(this.RemoteJID) == GetDomain(IsFriendQuestion.RemoteJID));
201
202 case RuleRange.All:
203 return true;
204
205 default:
206 return false;
207 }
208 }
209 else
210 return false;
211 }
212
213 }
214}
Interaction logic for QuestionView.xaml
Interaction logic for xaml
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
Event arguments for responses to IQ queries.
bool Ok
If the response is an OK result response (true), or an error response (false).
Implements an XMPP provisioning client interface.
Task IsFriendResponse(string JID, string RemoteJID, string Key, bool IsFriend, RuleRange Range, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends a response to a previous "Is Friend" question.
RuleRange
Range of a rule change
Definition: RuleRange.cs:7