Exemplo n.º 1
0
        /// <summary>
        /// The function sort a wanted range by Users nickname and timestemp
        /// </summary>
        /// <param name="i">start index of the range</param>
        /// <param name="range">length of the range (include index i)</param>
        /// <param name="msgs">list to sort</param>
        public void sortRange(int i, int range, List <GuiMessage> msgs)
        {
            // sort the list by users nickname
            msgs.Sort(i, range, comperator);
            // sort the list by Time
            int count = 1;
            int size  = i + range;

            if (msgs.Count > 0)
            {
                // find the range of the current User in the list, so we can sort that range by time
                string     tempGroup = msgs[i].UserName;
                SortByTime sbn       = new SortByTime();
                while (i + count <= size && i + count < msgs.Count)
                {
                    if (tempGroup.Equals(msgs[i + count].UserName))
                    {
                        count++;
                    }
                    else
                    {
                        // send to SortByName to sort the wanted range
                        if (i + count < msgs.Count && count > 1)
                        {
                            sbn.sortRange(i, count, msgs);
                        }
                        tempGroup = msgs[i + count].UserName;
                        i         = i + count;
                        count     = 1;
                    }
                }
                // sbn.sortRange(i, count, msgs);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// the function retrieve messages from the data base
        /// </summary>
        public void retrieveMessages()
        {
            // check for the last message retrieved from the data base
            SortByTime s = new SortByTime();

            s.action(presMsgs);
            DateTime time;

            if (presMsgs.Count > 0)
            {
                time = presMsgs[presMsgs.Count - 1].DateTime.ToUniversalTime();

                // retrives last messages from the server
                List <Message> msgToUpdate = allMessages.retrieve(time);

                // check which messages we should update in our files
                for (int i = 0; i < msgToUpdate.Count; i++)
                {
                    if (msgToUpdate[i].User == null)
                    {
                        throw new Exception("an error with getUserByID");
                    }
                    // creates a GuiMessage for the curr message
                    presMsgs.Add(new GuiMessage(msgToUpdate[i].Body, msgToUpdate[i].DateTime.ToLocalTime(), msgToUpdate[i].Id, msgToUpdate[i].User.Nickname, msgToUpdate[i].User.G_id));
                }
                // update messages list
                if (msgToUpdate.Count > 0)
                {
                    allMessages.List.AddRange(msgToUpdate);
                    if (presMsgs.Count > 200)
                    {
                        presMsgs.RemoveRange(0, presMsgs.Count - 200);
                        allMessages.List.RemoveRange(0, allMessages.List.Count - 200);
                    }
                }
                sort.action(presMsgs);
            }
            else
            {
                allMessages.filterByNone();
            }
        }