private async void TextBoxSend_KeyDown(object o, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; if (!string.IsNullOrWhiteSpace(currentSendTextBox.Text)) { string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"); AppendPendingMessage(Username, tabControlConversations.SelectedTab.Text, timestamp, currentSendTextBox.Text); AppendChatLog(Username, tabControlConversations.SelectedTab.Text, timestamp, currentSendTextBox.Text); currentReadTextBox.AppendText(Username + "> " + currentSendTextBox.Text + "\n"); string msg = currentSendTextBox.Text; currentSendTextBox.Clear(); currentSendTextBox.Select(0, 0); if (!ServerCommunicator.Communicating) { ServerCommunicator.Communicating = true; if (await ServerCommunicator.SendMessage(Username, tabControlConversations.SelectedTab.Text, timestamp, msg)) { //Ta bort från pending_messages.xml RemovePendingMessage(); } ServerCommunicator.Communicating = false; } } } }
private async void ButtonRegister_Click(object sender, EventArgs e) { if (!ValidInput() || ServerCommunicator.Communicating) { return; } ServerCommunicator.Communicating = true; bool error = false; do { try { await ServerCommunicator.Register(textBoxUserName.Text, textBoxPassWord.Text); break; } catch (HttpRequestException ex) { error = true; DialogResult result = MessageBox.Show(ex.Message, "Fel", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); if (result == DialogResult.Cancel) { break; } } } while (error); ServerCommunicator.Communicating = false; }
private async void ChatForm_FormClosed(object sender, FormClosedEventArgs e) { Timer.Enabled = false; Timer.Stop(); await ServerCommunicator.Logout(Username); File.SetAttributes(Configurator.ChatLogPath, FileAttributes.Hidden | FileAttributes.ReadOnly); File.SetAttributes(Configurator.PendingMessagesPath, FileAttributes.Hidden | FileAttributes.ReadOnly); }
private async void LoadUsers() { XmlDocument doc = await ServerCommunicator.GetUsers(Username); if (doc != null) { XmlNodeList list = doc.SelectNodes("/users/user"); if (list == null) { return; } foreach (XmlNode node in list) { bool alreadyThere = false; foreach (ListViewItem listViewItem in listViewOthers.Items) { if (listViewItem.Text == node.InnerText) { alreadyThere = true; break; } } if (!alreadyThere) { listViewOthers.Items.Add(node.InnerText); } } for (int i = 0; i < listViewOthers.Items.Count; i++) { bool exists = false; foreach (XmlNode node in list) { if (listViewOthers.Items[i].Text == node.InnerText) { exists = true; break; } } if (!exists) { listViewOthers.Items.RemoveAt(i); i--; } } } }
private async void Tick(object s, EventArgs e) { Timer.Stop(); if (await ServerCommunicator.CheckConnection(Username)) { textBoxServerStatus.BackColor = Color.DarkOliveGreen; textBoxServerStatus.Text = "Connected"; } else { textBoxServerStatus.BackColor = Color.DarkRed; textBoxServerStatus.Text = "Disconnected"; } LoadUsers(); if (tabControlConversations.SelectedTab != null) { //Hämta timestamp string timestamp = LoadTimestamp(tabControlConversations.SelectedTab.Text); if (string.IsNullOrWhiteSpace(timestamp)) { timestamp = DateTime.UtcNow.AddSeconds(-30).ToString("yyyy-MM-dd HH:mm:ss"); } LoadChatLog(tabControlConversations.SelectedTab.Text, timestamp); } //Ta bort pending_messages if (!ServerCommunicator.Communicating) { ServerCommunicator.Communicating = true; File.SetAttributes(Configurator.PendingMessagesPath, FileAttributes.Normal); XmlDocument doc = new XmlDocument(); doc.Load(Configurator.PendingMessagesPath); XmlNode messages = doc.SelectSingleNode("/pending_messages"); XmlNode childMessage = messages.FirstChild; doc = null; if (childMessage != null && await ServerCommunicator.SendMessage(Username, childMessage.Attributes["receiver"].Value, childMessage.Attributes["timestamp"].Value, childMessage.InnerText)) { RemovePendingMessage(); } ServerCommunicator.Communicating = false; File.SetAttributes(Configurator.PendingMessagesPath, FileAttributes.Hidden | FileAttributes.ReadOnly); } Timer.Start(); }
private async void ButtonLogOut_Click(object sender, EventArgs e) { Timer.Enabled = false; Timer.Stop(); await ServerCommunicator.Logout(Username); for (int i = 0; i < listViewOthers.Items.Count; i++) { listViewOthers.Items.RemoveAt(i); i--; } for (int i = 0; i < tabControlConversations.TabPages.Count; i++) { tabControlConversations.TabPages.RemoveAt(i); i--; } Hide(); LoginForm.Show(); }
private async void ButtonLogIn_Click(object sender, EventArgs e) { if (!ValidInput() || ServerCommunicator.Communicating) { return; } ServerCommunicator.Communicating = true; bool error = false; do { try { if (await ServerCommunicator.Login(textBoxUserName.Text, textBoxPassWord.Text)) { chatForm.Username = textBoxUserName.Text; chatForm.Timer.Enabled = true; chatForm.Timer.Start(); chatForm.Show(); Hide(); } break; } catch (HttpRequestException ex) { error = true; DialogResult result = MessageBox.Show(ex.Message, "Fel", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); if (result == DialogResult.Cancel) { break; } } } while (error); ServerCommunicator.Communicating = false; }
private async void LoadChatLog(string target, string sinceTime, bool remote = true) { if (remote) { XmlDocument doc = await ServerCommunicator.GetChatLog(Username, target, sinceTime); if (doc != null) { XmlNodeList list = doc.SelectNodes("/chatlog/message"); //var querySpecificMessages = from XmlNode node in list where DateTime.Parse(node.Attributes["timestamp"].Value) > DateTime.Parse(sinceTime) select node; foreach (XmlNode node in list) { if (node.Attributes["sender"].Value == Username) { currentReadTextBox.AppendText(Username + "> " + node.InnerText + "\n"); } else { currentReadTextBox.AppendText(target + "> " + node.InnerText + "\n"); } AppendChatLog(node.Attributes["sender"].Value, target, node.Attributes["timestamp"].Value, node.InnerText); /* * XmlDocument logDoc = new XmlDocument(); * logDoc.Load(Configurator.ChatLogPath); * * XmlNode copiedNode = logDoc.CreateElement(node.Name); * foreach (XmlAttribute attribute in node.Attributes) * { * XmlAttribute copiedAttribute = logDoc.CreateAttribute(attribute.Name); * copiedAttribute.Value = attribute.Value; * copiedNode.Attributes.Append(copiedAttribute); * } * copiedNode.InnerText = node.InnerText; * * XmlNode targetNode = logDoc.SelectSingleNode("chat_log/" + target); * targetNode.AppendChild(copiedNode); * * logDoc.Save(Configurator.ChatLogPath); */ } } } else { File.SetAttributes(Configurator.ChatLogPath, FileAttributes.Normal); XmlDocument doc = new XmlDocument(); doc.Load(Configurator.ChatLogPath); XmlNodeList nodeList = doc.SelectNodes("/chat_log/" + target + "/message"); var querySpecificMessages = from XmlNode node in nodeList where DateTime.Parse(node.Attributes["timestamp"].Value) > DateTime.Parse(sinceTime) && (node.Attributes["sender"].Value == Username || node.Attributes["sender"].Value == target) select node; foreach (XmlNode node in querySpecificMessages) { if (node.Attributes["sender"].Value == Username) { currentReadTextBox.AppendText(Username + "> " + node.InnerText + "\n"); } else { currentReadTextBox.AppendText(target + "> " + node.InnerText + "\n"); } } File.SetAttributes(Configurator.ChatLogPath, FileAttributes.Hidden | FileAttributes.ReadOnly); } }