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