Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OccupantNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Windows;
5using System.Windows.Controls;
6using System.Windows.Media;
7using System.Xml;
13
15{
19 public class OccupantNode : TreeNode
20 {
21 private readonly string roomId;
22 private readonly string domain;
23 private readonly string nickName;
24 private string jid;
25 private Affiliation? affiliation;
26 private Role? role;
27 private Availability? availability;
28
29 public OccupantNode(TreeNode Parent, string RoomId, string Domain, string NickName, Affiliation? Affiliation, Role? Role, string Jid)
30 : base(Parent)
31 {
32 this.roomId = RoomId;
33 this.domain = Domain;
34 this.nickName = NickName;
35 this.jid = Jid;
36 this.affiliation = Affiliation;
37 this.role = Role;
38 this.availability = null;
39
40 this.SetParameters();
41 }
42
43 public string RoomId => this.roomId;
44 public string Domain => this.domain;
45 public string NickName => this.nickName;
46
47 public string Jid
48 {
49 get => this.jid;
50 set => this.jid = value;
51 }
52
54 {
55 get => this.affiliation;
56 set => this.affiliation = value;
57 }
58
59 public Role? Role
60 {
61 get => this.role;
62 set => this.role = value;
63 }
64
66 {
67 get => this.availability;
68 set => this.availability = value;
69 }
70
71 public override void OnUpdated()
72 {
73 this.SetParameters();
74 base.OnUpdated();
75 }
76
77 private void SetParameters()
78 {
79 List<Parameter> Parameters = new List<Parameter>()
80 {
81 new StringParameter("RoomID", "Room ID", this.roomId),
82 new StringParameter("Domain", "Domain", this.domain),
83 new StringParameter("NickName", "Nick-Name", this.nickName)
84 };
85
86 if (!string.IsNullOrEmpty(this.jid))
87 Parameters.Add(new StringParameter("JID", "JID", this.jid));
88
89 if (this.affiliation.HasValue)
90 Parameters.Add(new StringParameter("Affiliation", "Affiliation", this.Affiliation.ToString()));
91
92 if (this.role.HasValue)
93 Parameters.Add(new StringParameter("Role", "Role", this.Role.ToString()));
94
95 if (this.availability.HasValue)
96 Parameters.Add(new StringParameter("Availability", "Availability", this.Availability.ToString()));
97
98 this.parameters = new DisplayableParameters(Parameters.ToArray());
99 }
100
101 public override string ToolTip => this.Jid;
102 public override bool CanRecycle => false;
103
104 public override string TypeName
105 {
106 get
107 {
108 return "Occupant";
109 }
110 }
111
112 public override ImageSource ImageResource
113 {
114 get
115 {
116 if (!this.availability.HasValue)
117 return XmppAccountNode.offline;
118
119 switch (this.availability.Value)
120 {
121 case Networking.XMPP.Availability.Away:
122 return XmppAccountNode.away;
123
124 case Networking.XMPP.Availability.Chat:
125 return XmppAccountNode.chat;
126
127 case Networking.XMPP.Availability.DoNotDisturb:
128 return XmppAccountNode.busy;
129
130 case Networking.XMPP.Availability.ExtendedAway:
131 return XmppAccountNode.away;
132
133
134 case Networking.XMPP.Availability.Online:
135 return XmppAccountNode.online;
136
137 case Networking.XMPP.Availability.Offline:
138 default:
139 return XmppAccountNode.offline;
140 }
141 }
142 }
143
144 public override void Write(XmlWriter Output)
145 {
146 // Don't output.
147 }
148
149 public MucService Service
150 {
151 get
152 {
153 TreeNode Loop = this.Parent;
154
155 while (!(Loop is null))
156 {
157 if (Loop is MucService MucService)
158 return MucService;
159
160 Loop = Loop.Parent;
161 }
162
163 return null;
164 }
165 }
166
167 public MultiUserChatClient MucClient
168 {
169 get
170 {
171 return this.Service?.MucClient;
172 }
173 }
174
175 public override string Key => this.nickName;
176 public override string Header => this.nickName;
177 public override bool CanAddChildren => false;
178 public override bool CanDelete => true;
179 public override bool CanEdit => true;
180 public override bool CanChat => true;
181
182 public override async Task Delete(TreeNode Parent, EventHandler OnDeleted)
183 {
184 if (!(this.MucClient is null))
185 await this.MucClient.Kick(this.RoomId, this.domain, this.nickName, null, null);
186
187 await base.Delete(Parent, OnDeleted);
188 }
189
190 public override void Edit()
191 {
193
194 Form.Affiliation.SelectedIndex = (int)this.affiliation;
195 Form.Role.SelectedIndex = (int)this.role;
196
197 if (string.IsNullOrEmpty(this.jid))
198 Form.Affiliation.IsEnabled = false;
199
200 bool? b = Form.ShowDialog();
201 if (b.HasValue && b.Value)
202 {
203 Affiliation NewAffiliation = (Affiliation)Form.Affiliation.SelectedIndex;
204 Role NewRole = (Role)Form.Role.SelectedIndex;
205 string Reason = Form.Reason.Text;
206
207 Task.Run(() => this.Config(NewAffiliation, NewRole, Reason));
208 }
209 }
210
211 private async Task Config(Affiliation Affiliation, Role Role, string Reason)
212 {
213 try
214 {
215 if (this.affiliation != Affiliation)
216 {
217 await this.MucClient.ConfigureOccupantAsync(this.roomId, this.domain, XmppClient.GetBareJID(this.jid), Affiliation, Reason);
218 this.affiliation = Affiliation;
219 this.OnUpdated();
220 }
221
222 if (this.role != Role)
223 {
224 await this.MucClient.ConfigureOccupantAsync(this.roomId, this.domain, this.nickName, Role, Reason);
225 this.role = Role;
226 this.OnUpdated();
227 }
228 }
229 catch (Exception ex)
230 {
231 MainWindow.ErrorBox(ex.Message);
232 }
233 }
234
235 public override async Task SendChatMessage(string Message, string ThreadId, MarkdownDocument Markdown)
236 {
237 if (Markdown is null)
238 await this.MucClient.SendPrivateMessage(this.roomId, this.domain, this.nickName, Message, string.Empty, ThreadId);
239 else
240 {
241 await this.MucClient.SendCustomPrivateMessage(this.roomId, this.domain, this.nickName,
242 await XmppContact.MultiFormatMessage(Message, Markdown), string.Empty, ThreadId);
243 }
244 }
245
246 public override void AddContexMenuItems(ref string CurrentGroup, ContextMenu Menu)
247 {
248 base.AddContexMenuItems(ref CurrentGroup, Menu);
249
250 MenuItem Item;
251 MenuItem Affiliation;
252 MenuItem Role;
253
254 this.GroupSeparator(ref CurrentGroup, "MUC", Menu);
255
256 Menu.Items.Add(Affiliation = new MenuItem()
257 {
258 Header = "_Affiliation",
259 IsEnabled = true,
260 });
261
262 Affiliation.Items.Add(Item = new MenuItem()
263 {
264 Header = "Owner",
265 IsEnabled = true,
266 IsCheckable = true,
267 IsChecked = this.affiliation == Networking.XMPP.MUC.Affiliation.Owner
268 });
269
270 Item.Click += this.SetAffiliationOwner_Click;
271
272 Affiliation.Items.Add(Item = new MenuItem()
273 {
274 Header = "Administrator",
275 IsEnabled = true,
276 IsCheckable = true,
277 IsChecked = this.affiliation == Networking.XMPP.MUC.Affiliation.Admin
278 });
279
280 Item.Click += this.SetAffiliationAdministrator_Click;
281
282 Affiliation.Items.Add(Item = new MenuItem()
283 {
284 Header = "Member",
285 IsEnabled = true,
286 IsCheckable = true,
287 IsChecked = this.affiliation == Networking.XMPP.MUC.Affiliation.Member
288 });
289
290 Item.Click += this.SetAffiliationMember_Click;
291
292 Affiliation.Items.Add(Item = new MenuItem()
293 {
294 Header = "None",
295 IsEnabled = true,
296 IsCheckable = true,
297 IsChecked = this.affiliation == Networking.XMPP.MUC.Affiliation.None
298 });
299
300 Item.Click += this.SetAffiliationNone_Click;
301
302 Affiliation.Items.Add(Item = new MenuItem()
303 {
304 Header = "Outcast",
305 IsEnabled = true,
306 IsCheckable = true,
307 IsChecked = this.affiliation == Networking.XMPP.MUC.Affiliation.Outcast
308 });
309
310 Item.Click += this.SetAffiliationOutcast_Click;
311
312 Menu.Items.Add(Role = new MenuItem()
313 {
314 Header = "_Role",
315 IsEnabled = true,
316 });
317
318 Role.Items.Add(Item = new MenuItem()
319 {
320 Header = "Moderator",
321 IsEnabled = true,
322 IsCheckable = true,
323 IsChecked = this.role == Networking.XMPP.MUC.Role.Moderator
324 });
325
326 Item.Click += this.SetRoleModerator_Click;
327
328 Role.Items.Add(Item = new MenuItem()
329 {
330 Header = "Participant",
331 IsEnabled = true,
332 IsCheckable = true,
333 IsChecked = this.role == Networking.XMPP.MUC.Role.Participant
334 });
335
336 Item.Click += this.SetRoleParticipant_Click;
337
338 Role.Items.Add(Item = new MenuItem()
339 {
340 Header = "Visitor",
341 IsEnabled = true,
342 IsCheckable = true,
343 IsChecked = this.role == Networking.XMPP.MUC.Role.Visitor
344 });
345
346 Item.Click += this.SetRoleVisitor_Click;
347
348 Role.Items.Add(Item = new MenuItem()
349 {
350 Header = "None",
351 IsEnabled = true,
352 IsCheckable = true,
353 IsChecked = this.role == Networking.XMPP.MUC.Role.None
354 });
355
356 Item.Click += this.SetRoleNone_Click;
357
358 Menu.Items.Add(Item = new MenuItem()
359 {
360 Header = "_Ban...",
361 IsEnabled = true,
362 });
363
364 Item.Click += this.Ban_Click;
365 }
366
367 private void Ban_Click(object Sender, RoutedEventArgs e)
368 {
369 BanOccupantForm Form = new BanOccupantForm(this.nickName);
370 bool? b = Form.ShowDialog();
371
372 if (b.HasValue && b.Value)
373 {
374 this.MucClient.Ban(this.roomId, this.domain, this.jid, Form.Reason.Text, async (sender2, e2) =>
375 {
376 if (e2.Ok)
377 await this.Delete(this.Parent, null);
378 else
379 MainWindow.ErrorBox(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to ban the occupant." : e2.ErrorText);
380 }, null);
381 }
382 }
383
384 private void SetAffiliationOwner_Click(object Sender, RoutedEventArgs e)
385 {
386 this.SetAffiliation(Networking.XMPP.MUC.Affiliation.Owner);
387 }
388
389 private void SetAffiliationAdministrator_Click(object Sender, RoutedEventArgs e)
390 {
391 this.SetAffiliation(Networking.XMPP.MUC.Affiliation.Admin);
392 }
393
394 private void SetAffiliationMember_Click(object Sender, RoutedEventArgs e)
395 {
396 this.SetAffiliation(Networking.XMPP.MUC.Affiliation.Member);
397 }
398
399 private void SetAffiliationNone_Click(object Sender, RoutedEventArgs e)
400 {
401 this.SetAffiliation(Networking.XMPP.MUC.Affiliation.None);
402 }
403
404 private void SetAffiliationOutcast_Click(object Sender, RoutedEventArgs e)
405 {
406 this.SetAffiliation(Networking.XMPP.MUC.Affiliation.Outcast);
407 }
408
409 private void SetAffiliation(Affiliation Affiliation)
410 {
411 this.MucClient.ConfigureOccupant(this.roomId, this.domain, XmppClient.GetBareJID(this.jid), Affiliation, string.Empty, (Sender, e) =>
412 {
413 if (e.Ok)
414 {
415 this.affiliation = Affiliation;
416 this.OnUpdated();
417 }
418 else
419 MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to change affiliation." : e.ErrorText);
420
421 return Task.CompletedTask;
422 }, null);
423 }
424
425 private void SetRoleModerator_Click(object Sender, RoutedEventArgs e)
426 {
427 this.SetRole(Networking.XMPP.MUC.Role.Moderator);
428 }
429
430 private void SetRoleParticipant_Click(object Sender, RoutedEventArgs e)
431 {
432 this.SetRole(Networking.XMPP.MUC.Role.Participant);
433 }
434
435 private void SetRoleVisitor_Click(object Sender, RoutedEventArgs e)
436 {
437 this.SetRole(Networking.XMPP.MUC.Role.Visitor);
438 }
439
440 private void SetRoleNone_Click(object Sender, RoutedEventArgs e)
441 {
442 this.SetRole(Networking.XMPP.MUC.Role.None);
443 }
444
445 private void SetRole(Role Role)
446 {
447 this.MucClient.ConfigureOccupant(this.roomId, this.domain, this.nickName, Role, string.Empty, (Sender, e) =>
448 {
449 if (e.Ok)
450 {
451 this.role = Role;
452 this.OnUpdated();
453 }
454 else
455 MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to change role." : e.ErrorText);
456
457 return Task.CompletedTask;
458 }, null);
459 }
460
461 }
462}
Interaction logic for BanOccupantForm.xaml
Interaction logic for OccupantPrivilegesForm.xaml
Interaction logic for xaml
Represents an occupant in a room hosted by a Multi-User Chat service.
Definition: OccupantNode.cs:20
override async Task SendChatMessage(string Message, string ThreadId, MarkdownDocument Markdown)
Sends a chat message.
override void OnUpdated()
Raises the Updated event.
Definition: OccupantNode.cs:71
override void AddContexMenuItems(ref string CurrentGroup, ContextMenu Menu)
Adds context sensitive menu items to a context menu.
override void Edit()
Is called when the user wants to edit a node.
override async Task Delete(TreeNode Parent, EventHandler OnDeleted)
Method called when a node is to be deleted.
override void Write(XmlWriter Output)
Saves the object to a file.
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
virtual DisplayableParameters DisplayableParameters
Gets available displayable parameters.
Definition: TreeNode.cs:214
TreeNode Parent
Parent node. May be null if a root node.
Definition: TreeNode.cs:107
Represents an XMPP contact whose capabilities have not been measured.
Definition: XmppContact.cs:24
Contains a markdown document. This markdown document class supports original markdown,...
Client managing communication with a Multi-User-Chat service. https://xmpp.org/extensions/xep-0045....
Task Ban(string RoomId, string Domain, string OccupantBareJid, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Bans an occupant out of a room.
Task SendPrivateMessage(string RoomId, string Domain, string NickName, string Message)
Sends a simple private message to a chat room.
Task< IqResultEventArgs > ConfigureOccupantAsync(string RoomId, string Domain, string OccupantNickName, Role Role, string Reason)
Configures the role of an occupant.
Task Kick(string RoomId, string Domain, string OccupantNickName, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Kicks an occupant out of a room.
Task SendCustomPrivateMessage(string RoomId, string Domain, string NickName, string Xml)
Sends a simple private message to a chat room.
Task ConfigureOccupant(string RoomId, string Domain, string OccupantNickName, Role Role, string Reason, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Configures the role of an occupant.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static string GetBareJID(string JID)
Gets the Bare JID from a JID, which may be a Full JID.
Definition: XmppClient.cs:6901
Contains information about a message logged on a node.
Definition: Message.cs:32
Affiliation
Affiliation enumeration
Definition: Enumerations.cs:11
Role
Role enumeration
Definition: Enumerations.cs:42
Availability
Resource availability.
Definition: Availability.cs:7