· 8 years ago · Mar 03, 2018, 07:38 PM
1#region Usings
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading;
7using agsXMPP;
8using agsXMPP.protocol.Base;
9using agsXMPP.protocol.client;
10
11#endregion
12
13namespace WebtvImplementatie.BU.FrontEnd
14{
15 public class GTalk
16 {
17 private readonly Sessie _sessie;
18 public bool EersteKeer = true;
19
20 public GTalk()
21 {
22 _sessie = new Sessie();
23 }
24
25 public XmppClientConnection Xmpp
26 {
27 get { return _sessie.Xmpp; }
28 }
29
30 public bool LogIn(string gebruiker, string wachtwoord)
31 {
32 return _sessie.LogIn(gebruiker, wachtwoord);
33 }
34
35 public void LogUit()
36 {
37 _sessie.LogUit();
38 }
39
40 public void VerstuurBericht(string chatContact, string bericht)
41 {
42 _sessie.Xmpp.Send(new Message(chatContact, MessageType.chat, bericht));
43 }
44
45 public List<List<string>> HaalBerichtenOp(string chatContact)
46 {
47 return _sessie.HaalBerichtenOp(chatContact).Select(bericht => new List<string>
48 {
49 bericht.BerichtInhoud,
50 bericht.Tijd.ToString(),
51 bericht.Verstuurder
52 }).ToList();
53 }
54
55 public List<List<string>> HaalContactenOp()
56 {
57 return _sessie.HaalContactenOp().Select(contact => new List<string>
58 {
59 contact.ContactNaam,
60 contact.ContactStatus
61 }).ToList();
62 }
63
64 public void VoegBerichtToe(string chatContact, string bericht)
65 {
66 _sessie.VoegBerichtToe(chatContact, bericht);
67 }
68
69 public void WijzigContactStatus(string chatContact, ShowType statusType)
70 {
71 _sessie.WijzigContactStatus(chatContact, statusType);
72 }
73
74 #region Nested type: Bericht
75
76 internal class Bericht
77 {
78 public Bericht(string verstuurder, string bericht, DateTime tijd)
79 {
80 Verstuurder = verstuurder;
81 BerichtInhoud = bericht;
82 Tijd = tijd;
83 }
84
85 public string Verstuurder { get; private set; }
86
87 public string BerichtInhoud { get; private set; }
88
89 public DateTime Tijd { get; private set; }
90 }
91
92 #endregion
93
94 #region Nested type: Contact
95
96 internal class Contact
97 {
98 private readonly List<Bericht> _berichten;
99 private readonly string _contactNaam;
100
101 public Contact(string contactNaam)
102 {
103 _contactNaam = contactNaam;
104 ContactStatus = " - Offline";
105 _berichten = new List<Bericht>();
106 }
107
108 public string ContactNaam
109 {
110 get { return _contactNaam; }
111 }
112
113 public string ContactStatus { get; set; }
114
115 public IEnumerable<Bericht> Berichten
116 {
117 get { return _berichten; }
118 }
119
120 public void VoegBerichtToeAanLijst(Bericht bericht)
121 {
122 _berichten.Add(bericht);
123 }
124
125 public void LijstLegen()
126 {
127 _berichten.Clear();
128 }
129 }
130
131 #endregion
132
133 #region Nested type: Gebruiker
134
135 internal class Sessie
136 {
137 private readonly List<Contact> _contacten;
138
139 private readonly Presence _status;
140 private readonly XmppClientConnection _xmpp;
141
142 public Sessie()
143 {
144 _xmpp = new XmppClientConnection();
145 _status = new Presence();
146 _contacten = new List<Contact>();
147 WijzigServer();
148 BindAanEvents();
149 }
150
151 public XmppClientConnection Xmpp
152 {
153 get { return _xmpp; }
154 }
155
156 public List<Contact> Contacten
157 {
158 get { return _contacten; }
159 }
160
161 private void WijzigServer()
162 {
163 _xmpp.Server = "gmail.com";
164 _xmpp.ConnectServer = "talk.google.com";
165 }
166
167 public bool LogIn(string gebruiker, string wachtwoord)
168 {
169 _xmpp.Username = gebruiker;
170 _xmpp.Password = wachtwoord;
171 _xmpp.Open();
172 Thread.Sleep(2000);
173
174 return _xmpp.Authenticated;
175 }
176
177 public void LogUit()
178 {
179 _xmpp.Close();
180 }
181
182 private void BindAanEvents()
183 {
184 _xmpp.OnRosterItem += xmpp_OnRosterItem;
185 _xmpp.OnMessage += Xmpp_OnMessage;
186 _xmpp.OnPresence += Xmpp_OnPresence;
187 }
188
189 private void VoegContactToeAanLijst(string chatContact)
190 {
191 _contacten.Add(new Contact(chatContact));
192 }
193
194 public List<Contact> HaalContactenOp()
195 {
196 return _contacten;
197 }
198
199 public void WijzigContactStatus(string chatContact, ShowType statusType)
200 {
201 foreach (Contact c in _contacten)
202 {
203 if (chatContact.ToLower() == c.ContactNaam.ToLower())
204 {
205 switch (statusType)
206 {
207 case ShowType.NONE:
208 c.ContactStatus = _status.Type == PresenceType.available ? " - Online " : " - Offline";
209 break;
210
211 case ShowType.dnd:
212 c.ContactStatus = " - Bezig ";
213 break;
214
215 case ShowType.away:
216 c.ContactStatus = " - Afwezig ";
217 break;
218
219 case ShowType.xa:
220 c.ContactStatus = " - Afwezig ";
221 break;
222
223 default:
224 c.ContactStatus = " - Offline";
225 break;
226 }
227 }
228 }
229 }
230
231 public void VoegBerichtToe(string chatContact, string bericht)
232 {
233 foreach (Contact c in _contacten)
234 {
235 if (c.ContactNaam.ToLower() == chatContact.ToLower())
236 {
237 c.VoegBerichtToeAanLijst(new Bericht(chatContact, bericht, DateTime.Now));
238 }
239 }
240 }
241
242 public List<Bericht> HaalBerichtenOp(string chatContact)
243 {
244 var tempList = new List<Bericht>();
245 foreach (Contact c in _contacten)
246 {
247 if (chatContact == c.ContactNaam)
248 {
249 tempList.AddRange(c.Berichten);
250 c.LijstLegen();
251 }
252 }
253 return tempList;
254 }
255
256 private void xmpp_OnRosterItem(object sender, RosterItem item)
257 {
258 VoegContactToeAanLijst(item.Jid.Bare);
259 }
260
261 private void Xmpp_OnMessage(object sender, Message msg)
262 {
263 VoegBerichtToe(msg.From.Bare, msg.Body);
264 }
265
266 private void Xmpp_OnPresence(object sender, Presence pres)
267 {
268 WijzigContactStatus(pres.From.Bare.ToLower(), pres.Show);
269 }
270 }
271
272 #endregion
273 }
274}