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