예제 #1
0
        //If the message applies to us this routine:
        //Adds the given message to the message collection.
        //Adds the message to the textbox.
        public void AddMessageToHistory(MessagePacket msg)
        {
            //this probably means a different type of message (like a "composing" event)
            //'but for the sake of this sample we don't need to handle it
            if (msg.BodiesByCulture.ContainsKey(String.Empty) && msg.Body.Length == 0) return;

            //*** Determine which languages this message has in it
            System.Globalization.CultureInfo defaultCulture = _SessionManager.Session.StreamCulture;

            foreach (string localizedLanguage in msg.BodiesByCulture.Keys)
            {
                string actualLanguage = localizedLanguage;

                string localizedBody = msg.BodiesByCulture[actualLanguage];
                if (actualLanguage.Equals(String.Empty))
                {
                    if (msg.HasXMLLangAttribute() == true)
                    {
                        actualLanguage = msg.Culture.Name;
                    }
                    else
                    {
                        //*** The message body didn't have a language tag on it. The
                        //	Packet doesn't have a language tag on it, so we have
                        //  no idea what language this is in - assume it to be the
                        //  stream native language
                        actualLanguage = defaultCulture.Name;
                    }
                }

                if (localizedBody.EndsWith("\r\n"))
                {
                    localizedBody = localizedBody.Substring(0, localizedBody.Length - "\r\n".Length);
                }

                RichTextBox rtb = GetTextBoxForLanguage(new System.Globalization.CultureInfo(actualLanguage));

                System.Text.StringBuilder messageBuilder = new System.Text.StringBuilder();
                messageBuilder.Append(msg.From.UserName.ToString());
                if (msg.From.IsFullJID())
                {
                    messageBuilder.Append("/");
                    messageBuilder.Append(msg.From.Resource);
                }
                messageBuilder.Append(":  ");

                if (rtb.Text.Length > 0)
                {
                    messageBuilder.Insert(0, "\r\n");
                }

                rtb.Text = messageBuilder.Insert(0, rtb.Text).Append(localizedBody).ToString();

                //make sure the current text stays visible
                rtb.Select(rtb.TextLength - 1, 1);
                rtb.ScrollToCaret();
            }
        }