예제 #1
0
        private async void ChatItemClicked(object sender, EventArgs e)
        {
            ChatItem chatItem = (ChatItem)sender;

            foreach (Control control in this.Controls)
            {
                try
                {
                    Chat chat = (Chat)control;
                    if (chat.ChatID == chatItem.ChatID)
                    {
                        chat.BringToFront();
                        return;
                    }
                }
                catch (Exception ex)
                {
                }
            }
            NetworkStream networkStream = tcpClient.GetStream();
            StreamWriter  streamWriter  = new StreamWriter(networkStream);

            streamWriter.AutoFlush = true;
            await streamWriter.WriteLineAsync("GetChat&" + chatItem.ChatID.ToString());
        }
예제 #2
0
        void GetProfile(string[] Queries)
        {
            int ind = 1;

            lblProfileName.Text = Queries[ind++];
            this.Text           = lblProfileName.Text;
            ChatItems.Clear();
            int ChatID = int.Parse(Queries[ind++]);

            while (ChatID != -1)
            {
                string chatName    = Queries[ind++];
                bool   type        = Queries[ind++] == "True";
                string lastMessage = Queries[ind++];
                lastMessage += " : ";
                lastMessage += Queries[ind++];
                string   dateLastMessage = Queries[ind++];
                ChatItem chatItem        = new ChatItem(ChatID, chatName, lastMessage, dateLastMessage, false, type);
                chatItem.GetClicked += ChatItemClicked;
                ChatItems.Add(chatItem);
                ChatID = int.Parse(Queries[ind++]);
            }
            UpdateChats();
            MyFriends.Clear();
            string username = Queries[ind++];

            while (username != "-1")
            {
                string     Status     = Queries[ind++];
                FriendItem friendItem = new FriendItem(username, Status);
                friendItem.GetClicked += FriendClick;
                MyFriends.Add(friendItem);
                username = Queries[ind++];
            }
            UpdateFriends();
            Clients.Clear();
            username = Queries[ind++];
            while (username != "-1")
            {
                string     requestSended = Queries[ind++];
                ClientItem clientItem;
                if (requestSended == "True")
                {
                    clientItem = new ClientItem(username, true);
                }
                else
                {
                    clientItem = new ClientItem(username, false);
                }
                clientItem.SendingRequest += FriendRequestToClient;
                Clients.Add(clientItem);
                username = Queries[ind++];
            }
            UpdateClients();
            tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls.Clear();
            username = Queries[ind++];
            while (username != "-1")
            {
                FriendRequestItem friendRequestItem = new FriendRequestItem(username);
                friendRequestItem.GetAccepted += FriendRequestAccept;
                friendRequestItem.GetRejected += FriendRequestReject;
                tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls.Add(friendRequestItem);
                username = Queries[ind++];
            }
        }
예제 #3
0
        async void ReadNotifications()
        {
            NetworkStream networkStream = tcpClient.GetStream();
            StreamReader  streamReader  = new StreamReader(networkStream);

            while (true)
            {
                string query = await streamReader.ReadLineAsync();

                string[] Queries = query.Split('&');
                if (Queries[0] == "GetMyInformation")
                {
                    foreach (Control control in this.Controls)
                    {
                        try
                        {
                            MyProfile myProfile = (MyProfile)control;
                            myProfile.Username  = Queries[1];
                            myProfile.Password  = myProfile.ConfirmPassword = Queries[2];
                            myProfile.Birthdate = DateTime.Parse(Queries[3]);
                            myProfile.Gender    = Queries[4];
                        }
                        catch
                        {
                        }
                    }
                }
                if (Queries[0] == "Get Profile")
                {
                    GetProfile(Queries);
                }
                if (Queries[0] == "ClientUpdated")
                {
                    string type        = Queries[1];
                    string OldUsername = Queries[2];
                    string NewUsername = Queries[3];
                    if (type == "Me")
                    {
                        lblProfileName.Text = NewUsername;
                        foreach (ChatItem chatItem in ChatItems)
                        {
                            if (chatItem.LastMessage.StartsWith(OldUsername))
                            {
                                chatItem.LastMessage = NewUsername + chatItem.LastMessage.Substring(OldUsername.Length);
                            }
                        }
                        UpdateChats();
                        foreach (Control control in this.Controls)
                        {
                            try
                            {
                                Chat chat = (Chat)control;
                                foreach (Control control1 in chat.Controls.Find("flbChat", false)[0].Controls)
                                {
                                    TextMessage textMessage = (TextMessage)control1;
                                    if (textMessage.Username == OldUsername)
                                    {
                                        textMessage.Username = NewUsername;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        foreach (Control control in this.Controls)
                        {
                            try
                            {
                                MyProfile myProfile = (MyProfile)control;
                                this.Controls.Remove(myProfile);
                            }
                            catch
                            {
                            }
                        }
                    }
                    if (type == "Friend")
                    {
                        string birthdate = Queries[4];
                        string gender    = Queries[5];
                        foreach (ChatItem chatItem in ChatItems)
                        {
                            if (chatItem.Type && chatItem.ChatName == OldUsername)
                            {
                                chatItem.ChatName = NewUsername;
                            }
                            if (chatItem.LastMessage.StartsWith(OldUsername))
                            {
                                chatItem.LastMessage = NewUsername + chatItem.LastMessage.Substring(OldUsername.Length);
                            }
                        }
                        UpdateChats();
                        foreach (FriendItem friendItem in MyFriends)
                        {
                            if (friendItem.FriendName == OldUsername)
                            {
                                friendItem.FriendName = NewUsername;
                            }
                        }
                        UpdateFriends();
                        foreach (Control control in this.Controls)
                        {
                            try
                            {
                                Chat chat = (Chat)control;
                                if (chat.Type && chat.ChatName == OldUsername)
                                {
                                    chat.ChatName = NewUsername;
                                }
                                foreach (Control control1 in chat.Controls.Find("flbChat", false)[0].Controls)
                                {
                                    TextMessage textMessage = (TextMessage)control1;
                                    if (textMessage.Username == OldUsername)
                                    {
                                        textMessage.Username = NewUsername;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        foreach (Control control in this.Controls)
                        {
                            try
                            {
                                FriendProfile friendProfile = (FriendProfile)control;
                                if (friendProfile.Username == OldUsername)
                                {
                                    friendProfile.Username  = NewUsername;
                                    friendProfile.Birthdate = birthdate;
                                    friendProfile.Gender    = gender;
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    if (type == "Client")
                    {
                        foreach (Control control in this.Controls)
                        {
                            try
                            {
                                Chat chat = (Chat)control;
                                foreach (Control control1 in chat.Controls.Find("flbChat", false)[0].Controls)
                                {
                                    TextMessage textMessage = (TextMessage)control1;
                                    if (textMessage.Username == OldUsername)
                                    {
                                        textMessage.Username = NewUsername;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        foreach (ClientItem clientItem in Clients)
                        {
                            if (clientItem.Username == OldUsername)
                            {
                                clientItem.Username = NewUsername;
                            }
                        }
                        UpdateClients();
                        foreach (Control control2 in tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls)
                        {
                            FriendRequestItem friendRequestItem = (FriendRequestItem)control2;
                            if (friendRequestItem.Username == OldUsername)
                            {
                                friendRequestItem.Username = NewUsername;
                            }
                        }
                    }
                }
                if (Queries[0] == "NotUpdated")
                {
                    MessageBox.Show("Username is used !!");
                }
                if (Queries[0] == "Friend Login")
                {
                    string username = Queries[1];
                    foreach (Control control in MyFriends)
                    {
                        FriendItem friendItem = (FriendItem)control;
                        if (friendItem.FriendName == username)
                        {
                            friendItem.Status = true;
                        }
                    }
                    UpdateFriends();
                }
                if (Queries[0] == "Friend Logout")
                {
                    string username = Queries[1];
                    foreach (Control control in MyFriends)
                    {
                        FriendItem friendItem = (FriendItem)control;
                        if (friendItem.FriendName == username)
                        {
                            friendItem.Status = false;
                        }
                    }
                    UpdateFriends();
                }
                if (Queries[0] == "Client Registered")
                {
                    string     username   = Queries[1];
                    ClientItem clientItem = new ClientItem(username, false);
                    clientItem.SendingRequest += FriendRequestToClient;
                    Clients.Add(clientItem);
                    UpdateClients();
                }
                if (Queries[0] == "Request Accepted")
                {
                    string     username   = Queries[1];
                    FriendItem friendItem = new FriendItem(username, "Online");
                    friendItem.GetClicked += FriendClick;
                    MyFriends.Add(friendItem);
                    ClientItem clientItem1 = null;
                    foreach (Control control in Clients)
                    {
                        ClientItem clientItem = (ClientItem)control;
                        if (clientItem.Username == username)
                        {
                            clientItem1 = clientItem;
                            break;
                        }
                    }
                    Clients.Remove(clientItem1);
                    UpdateClients();
                    UpdateFriends();
                }
                if (Queries[0] == "Request Rejected")
                {
                    string username = Queries[1];
                    foreach (Control control in Clients)
                    {
                        ClientItem clientItem = (ClientItem)control;
                        if (clientItem.Username == username)
                        {
                            clientItem.Send = false;
                            break;
                        }
                    }
                    UpdateClients();
                }
                if (Queries[0] == "Cancel Request")
                {
                    string  username = Queries[1];
                    Control control1 = null;
                    foreach (Control control in tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls)
                    {
                        FriendRequestItem friendRequestItem = (FriendRequestItem)control;
                        if (friendRequestItem.Username == username)
                        {
                            control1 = control;
                            break;
                        }
                    }
                    tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls.Remove(control1);
                }
                if (Queries[0] == "Friend Request")
                {
                    string            username          = Queries[1];
                    FriendRequestItem friendRequestItem = new FriendRequestItem(username);
                    friendRequestItem.GetAccepted += FriendRequestAccept;
                    friendRequestItem.GetRejected += FriendRequestReject;
                    tabFriends.TabPages[2].Controls.Find("flbRequests", false)[0].Controls.Add(friendRequestItem);
                }
                if (Queries[0] == "Status")
                {
                    string username = Queries[1];
                    string status   = Queries[2];
                    foreach (Control control in MyFriends)
                    {
                        FriendItem friendItem = (FriendItem)control;
                        if (friendItem.FriendName == username)
                        {
                            if (status == "Online")
                            {
                                friendItem.Status = true;
                            }
                            else
                            {
                                friendItem.Status = false;
                            }
                        }
                    }
                    UpdateFriends();
                }
                if (Queries[0] == "Message Recieved")
                {
                    string chatName = Queries[1];
                    string username = Queries[2];
                    int    chatId   = int.Parse(Queries[3]);
                    string message  = Queries[4];
                    string date     = Queries[5];
                    string type     = Queries[6];
                    bool   exist    = false;
                    foreach (Control control in this.Controls)
                    {
                        try
                        {
                            Chat chat = (Chat)control;
                            if (chat.ChatID == chatId)
                            {
                                exist = true;
                                chat.Controls.Find("flbChat", false)[0].Controls.Add(new TextMessage(username, message, date));
                                control.BringToFront();
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    if (!exist && lblProfileName.Text == username)
                    {
                        GetChat(chatId);
                    }
                    bool ChatExist = false;
                    foreach (ChatItem chatItem in ChatItems)
                    {
                        if (chatItem.ChatID == chatId)
                        {
                            ChatExist         = true;
                            chatItem.ChatName = chatName;
                            string lMesseage = username + ":" + message;
                            chatItem.LastMessage     = lMesseage;
                            chatItem.DateLastMessage = date;
                            chatItem.Unread          = !exist;
                            break;
                        }
                    }
                    if (!ChatExist)
                    {
                        string   lMesseage = username + ":" + message;
                        ChatItem chatItem1 = new ChatItem(chatId, chatName, lMesseage, date, true, type == "True");
                        chatItem1.GetClicked += ChatItemClicked;
                        ChatItems.Add(chatItem1);
                    }
                    UpdateChats();
                }
                if (Queries[0] == "GetChat")
                {
                    int    chatId   = int.Parse(Queries[1]);
                    string chatName = Queries[2];
                    bool   type     = Queries[3] == "True";
                    Chat   chat     = new Chat(chatId, chatName, type);
                    this.Controls.Add(chat);
                    chat.Location   = panel2.Location;
                    chat.Size       = panel2.Size;
                    chat.GetClosed += ChatClosed;
                    chat.GetSended += ChatSendMessage;
                    chat.Show();
                    chat.BringToFront();
                    for (int i = 4; i < Queries.Length; i += 3)
                    {
                        TextMessage textMessage = new TextMessage(Queries[i], Queries[i + 1], Queries[i + 2]);
                        chat.Controls.Find("flbChat", false)[0].Controls.Add(textMessage);
                    }
                }
                if (Queries[0] == "GetFriendProfile")
                {
                    string username = Queries[1];
                    foreach (Control control in this.Controls)
                    {
                        try
                        {
                            FriendProfile friendProfile = (FriendProfile)control;
                            if (friendProfile.Username == username)
                            {
                                friendProfile.Birthdate = Queries[2];
                                friendProfile.Gender    = Queries[3];
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
        }